cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 I2C EEPROM oddity

jprinster
Associate II
Posted on June 17, 2013 at 00:39

I am trying to use the I2C to read and write to an external 24LC024 EEPROM.  I am using a polling method since my DMAs are being used for USART2.  I remapped I2C1 to PB8/PB9. 

When I read the EEPROM it works fine and I see the correct data.  However, when I write it sometimes fails.  If I single step through my code (IAR EWARM) it will always write correctly with no errors.  However when I let it run freely it will fail waiting for a response from the EEPROM usually when I am sending the data address I want to write to.  I have used the settings as suggested in the examples from IAR.  What else should I look at?

Also, when I read or write I am doing the entire process for each byte.  Does anyone have an example of a polling method that is more efficient?  For example, setting the eeprom to auto-increment through a page of bytes.

Thanks.

#stm32f103 #stm32-i2c-eeprom
2 REPLIES 2
firatparlak
Associate II
Posted on June 17, 2013 at 09:01

Hello,

I wrote for another mcu in the past. Perhaps it would be useful for you. In the code, the pins are defined for a RTC, but this is not important for you. In conclusion both your eeprom and the rtc are using I2C protocol. I hope this helps you.

/***********************************************************************/
/*
Function : I2C_WriteBit()
Author : Fırat Parlak
Description : Writes one bit on i2c bus.
Bit_Value : The value to write on i2c bus.
Date Created : 2010
Modifications :
*/
/***********************************************************************/
void I2C_WriteBit(unsigned char Bit_Value)
{
RTC_SDA_dir = 1;
RTC_SCL = 0;
if(Bit_Value)
{
RTC_SDA = 1;
}
else
{
RTC_SDA = 0;
}
delay_five_us; //delay 5 us
RTC_SCL = 1;
delay_two_us; //delay 4 us
delay_two_us;
}
/***********************************************************************/
/*
Function : I2C_WriteByte()
Author : Fırat Parlak
Description : Writes one byte on i2c bus.
Tx_Value : The data to write on i2c bus.
Date Created : 2010
Modifications :
*/
/***********************************************************************/
void I2C_WriteByte(unsigned char Tx_Value)
{
unsigned char Bit_cnt;
Bit_cnt = 8;
while(Bit_cnt--)
{
if(Tx_Value&0x80)
{
I2C_WriteBit(1);
}
else
{
I2C_WriteBit(0);
}
Tx_Value <<= 1;
}
I2C_Error = eI2C_Error_None;
if( I2C_ReadBit() )
{
I2C_Error = eI2C_Error_Ack;
}
}

joe
Associate II
Posted on June 17, 2013 at 16:34

Because it always works while stepping through code, it sounds like not enough time has passed after issuing the write command until the write is complete while running in normal mode.

Usually you would wait in a while loop for a flag to be set to indicate that the write has completed before you continue in your code. Is you code doing this?

If a flag isn't being checked then you could test by adding a relative long delay after the write is issued to make sure that this is what is happening?

Best is to find out what flag gets set after EEPROM write is complete and wait in your code for this flag. You could also have an incrementing count which would indicate a timeout if reaches a certain value just in case the write never completes.