tags:

views:

385

answers:

1

Hi guys,

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

+1  A: 

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.

Michael Burr
Thank you. I will check this on monday I know for sure that I am not checking if the STOP bit has been cleared so this could be the root of all my problems. I dont know how I missed that in the datasheet.
jramirez
Well, it's kind of non-obvious since it's not in a status register. But then again, there's almost always some non-obvious stuff in hardware registers - that's why drivers are hard.
Michael Burr
Quick update I tried doing what you said, and it did fix the problem for the start bit. Now there is something else, after I send the slave address the device does not respond. this is when I try to to two writes in a row when I try one it works fine. I check in the scope and the signals look fine. the EEprom is just not acking after its address is sent. I checked make sure the address does not change between writes and it doesnt also if I put a delay of 5ms in between writes then it works with consecutive writes. I am running at 100khz bus this EEprom the 24lc1025 can run up to 1Mhz any ideas
jramirez
@jramirez: The EEPROM is working to spec there - it can take up to 5ms for a write command to complete its cycle (Parameter 17 - TWC - in the datasheet's "AC Characteristics" table). You can either delay between write requests or use the "Acknowledge Polling" technique described in section 7.0 and Figure 7-1 of the datasheet.
Michael Burr
haha yes I did find that on Friday I meant to post it, but I forgot. Thank you so much for the help. I gotta remember to read the whole datasheet instead of thinking "I2C no problem let's code it" then wonder why it doesnt work as intended that will come with experience I hope :)
jramirez