stm32f1xx-hal icon indicating copy to clipboard operation
stm32f1xx-hal copied to clipboard

I2c Example

Open andrewmruddy opened this issue 5 years ago • 10 comments

New to Rust - Would it be possible to have an example for I2c? After looking through the documentation, The Rust Book, and other examples, I am still having trouble figuring out proper implementation.

andrewmruddy avatar Feb 17 '19 21:02 andrewmruddy

Writing an example without using a specific device to interract with is difficult.

I will try to write some documentation for the i2c device though, is there anything in particular you're having trouble with?

TheZoq2 avatar Feb 18 '19 19:02 TheZoq2

Thank you for your quick reply! I have the following for setting up the i2c peripheral:

    let peripherals = stm32f103::Peripherals::take().unwrap();
    let mut gpiob = peripherals.GPIOB.split(&mut rcc.apb2);
    let sda = gpiob.pb7.into_alternate_open_drain(&mut gpiob.crl);
    let scl = gpiob.pb6.into_alternate_open_drain(&mut gpiob.crl);

    let i2c_bus = I2c::i2c1(
        peripherals.I2C1,
        (scl,sda),
        &mut afio.mapr,
        hal::i2c::I2c::Mode::Standard, // < i am having problems with the enum "Mode"
        clocks,
        &mut rcc.apb1,
    );

I am using the MMA8451 accelerometer (adafruit breakout board) for my proof-of-concept.

andrewmruddy avatar Feb 18 '19 22:02 andrewmruddy

@andrewmruddy There're some examples for I2C use with stm32f1xx-hal in the ssd1306 crate, e.g. https://github.com/jamwaffles/ssd1306/blob/master/examples/graphics_i2c.rs

therealprof avatar Feb 18 '19 22:02 therealprof

Thank you so much!

andrewmruddy avatar Feb 18 '19 23:02 andrewmruddy

If you have a working example, you could post it here. This would surely help others (like me, I'm planning to do something similar :-D )

jounathaen avatar Feb 19 '19 19:02 jounathaen

@jounathaen I wouldn't want to add external dependencies for examples and unfortunately I2C without connected devices is not too impressive/useful.

therealprof avatar Feb 19 '19 19:02 therealprof

@therealprof I can understand that dependencies are undesired, but first I think that you can have I2C examples without external dependencies, like reading generic registers, and second I think that having an outdated example is still better than no up-to-date example. I was not necessarily thinking about full blown working examples, but code snippets for the documentation would be helpful. (Of course just my humble opinion)

jounathaen avatar Feb 19 '19 22:02 jounathaen

@jounathaen Oh sure, I definitely agree we should have proper code documentation with logical code snippets.

therealprof avatar Feb 19 '19 23:02 therealprof

The i2c scanner example in the stm32f3xx-hal may be a good reference for including a practical i2c example that doesn't depend on any connected devices

jaxter184 avatar Jan 13 '21 03:01 jaxter184

@jounathaen Looking at the I2C examples on this repository I could not find one simple enough for me to follow - super new to rust as well - so after reading some documentation and looking at example implementations on device libraries I came up with this example i2c scanner for stm32f1xx, and tried to make code simple and README detailed, so anyone can follow it.

I wish I did a better job googling in the first place, since I see now listed the i2c scanner example in the stm32f3xx-hal mentioned here, that should be quite similar and I assume better than what my poorly polished rust skills allowed me to write.

JoaquinEduardoArreguez avatar Mar 04 '23 00:03 JoaquinEduardoArreguez