Search code examples
cembeddedi2cstm32

STM32 I2C1 Start bit not set on SR1 register


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 or PEC bit is set, the software must not perform any write access to I2C_CR1 before this bit is cleared by hardware. Otherwise there is a risk of setting a second STOP, START or PEC 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.