STM32 I2C1 Start bit not set on SR1 register
Question
I am trying to program the stm32 to talk to my i2c EEprom, but it seems like everytime I say:
I2C_GenerateSTART(I2C1, ENABLE);
while( !(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) ); the code hangs here
I went through with the debugger and I found that the SR1 bit 0 flag (which is the start bit generated flag) is not set which is why the code hangs. I can see in the oscilloscope that the start bit was generated and this works sometimes. it usually breaks when I tried to do several writes in a row. I checked the HW is everything looks fine I checked the frequency that I am running on the i2c bus it is 100Khz well within the 24lc1025 eeprom.
any ideas,
thanks
Solution
Since you say that this usually breaks when you're doing several writes in a row, you might want to ensure that you're not violating this note in the STM32 datasheet for the STOP
bit in the I2C_CR1 control register:
Note: When the
STOP
,START
orPEC
bit is set, the software must not perform any write access toI2C_CR1
before this bit is cleared by hardware. Otherwise there is a risk of setting a secondSTOP
,START
orPEC
request.
Once you set the STOP
bit you need to make sure that the hardware has cleared it before you write the next START
bit.
OTHER TIPS
Check the Errata for your device (get it from the ST website). The STM32 has a lot of bugs in the I2C implementation. The errata gives software workarounds for them.
Also, when using the driver source files typically provided with STM32 platforms, don't forget to read through the headers! They can be very helpful, especially for novices (like me) who aren't sure where to start.
In this case, you would be looking at stm32f4xx_i2c.c