sparkfun / Arduino_Apollo3

Arduino core to support the Apollo3 microcontroller from Ambiq Micro
83 stars 37 forks source link

More I2C / Wire issues #289

Closed paulvha closed 3 years ago

paulvha commented 3 years ago

Trying to get my SPS30 going on Apollo3 2.0.1, I run into the following issues: I2C speed. From Wire.begin() master the mbed::I2C() is called, part of mbed-os/drivers/source/I2C.cpp. In I2C::I2C(PinName sda, PinName scl) it is stated that " // The init function also set the frequency to 100000". This is incorrect as function I2C_init(), in TARGET_ambiq_micro/TARGET_Apollo3/Device/i2c_api.c, will use the DEFAULT_CLK_FREQ. This is defined in line 30 in the same file as AM_HAL_IOM_400KHZ and thus different than assumed. This should be corrected for both Master-calls as well as I2C slave-calls else the different driver files are out of sync and the user (me) confused why the SPS30 did not work. I just called frequency(_hz) at the end of I2C::I2C, but it's better to set the DEFAULT_CLK_FREQ to AM_HAL_IOM_100KHZ.

RX buffer clear In Wire.requestFrom() an rxBuffer.clear() should be performed BEFORE adding new content with rxBuffer.store_char. Now old content from a previous requestFrom call, which has not been read completely by user-level, is returned as if it is the result of the new requestFrom(). Any Wire.cpp on other boards perform this clear first as well. I have tested and added rxBuffer.clear() and that worked as expected.

Intialize The variables master and slave should initialized as NULL when the Wire constructor is created. Now it is assumed they a NULL and checked in begin().

I2C examples The current I2C example is corect BUT very complicated and there should be a better explanation of how to use the IOM modules to add a new I2C channel. (see https://forum.sparkfun.com/viewtopic.php?f=169&t=53961)

regards, Paul

Wenn0101 commented 3 years ago

I2C Speed is changed, master and slave are initialized to NULL.

I have done my best simplifying the example a bit, but I may still add a better one in the future.

The RX buffer clear, I ended up not doing this change. I felt as if the data from a previous requestFrom should still be available, even if another has been called. Since each requestFrom returns a length, the amount of data each request has returned should be available.