cancel
Showing results for 
Search instead for 
Did you mean: 

I2C EEPROM M24C64 sequential read

bogusz
Associate II
Posted on June 03, 2014 at 12:55

Hello,

I'm looking for source code example of i2c eeprom library for STM32F2xx which is not using DMA. Especially I'm looking for sequential read function. Please if you know any links, post it. I found here at forum this (void I2C_EE_BufferRead(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)): [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/I2C%20Busy%20flag%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1848]stm32 forum post And here is my function (but as you can see there are some commented while(event) according to STM32F2xx reference manual - and it's different as in example above so I'm not sure for correct way):


// ---------------- While the bus is busy ------------------------------------- 

u32_Timeout = I2CEE_LONG_TIMEOUT;

while(I2C_GetFlagStatus(I2CEE_I2C, I2C_FLAG_BUSY))

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_WHILE); 

}



// ---------------- Send Start condition --------------------------------------

I2C_GenerateSTART(I2CEE_I2C, ENABLE);


/* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */

u32_Timeout = I2CEE_FLAG_TIMEOUT;

while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SC);

} 


// ---------------- Send chip address for Write ------------------------------- 

I2C_Send7bitAddress(I2CEE_I2C, U8_i2cee_chip_addr, I2C_Direction_Transmitter);


/* Test on EV6 and clear it */

u32_Timeout = I2CEE_LONG_TIMEOUT;

while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SAW);

} 


// ---------------- Send address you wanna read from -------------------------- 

/* Send the EEPROM's internal address to read from: MSB of the address first */

I2C_SendData(I2CEE_I2C, (uint8_t)((u16_ReadAddr & 0xFF00) >> 8)); 


/* Test on EV8_2 and clear it */

u32_Timeout = I2CEE_LONG_TIMEOUT;

//while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))

while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING)) 

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_MSB);

}


/* Send the EEPROM's internal address to read from: LSB of the address */

I2C_SendData(I2CEE_I2C, (uint8_t)(u16_ReadAddr & 0x00FF)); 


/* Test on EV8? and clear it */

u32_Timeout = I2CEE_LONG_TIMEOUT;

//while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))

while(I2C_GetFlagStatus(I2CEE_I2C, I2C_FLAG_BTF) == RESET)

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_LSB);

}


/* Clear AF flag if arised */

//I2C1->SR1 |= (uint16_t)0x0400;


// ---------------- Send Start condition --------------------------------------

I2C_GenerateSTART(I2CEE_I2C, ENABLE);


/* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */

u32_Timeout = I2CEE_LONG_TIMEOUT;

while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SC2);

} 


// ---------------- Send chip address for Read -------------------------------- 

I2C_Send7bitAddress(I2CEE_I2C, U8_i2cee_chip_addr, I2C_Direction_Receiver); 


/* Test on EV6 and clear it */

u32_Timeout = I2CEE_LONG_TIMEOUT;

//while(I2C_GetFlagStatus(I2CEE_I2C, I2C_FLAG_ADDR) == RESET)

while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SAR);

} 


// ---------------- Read all data from eeprom to Buffer ----------------------- 

while(u16_DataReadCnt > 0){

// If only 1 byte (or last byte) has to be read

if(u16_NumByteToRead < 2){


/* Prepare an NACK for the next data received */

I2C_AcknowledgeConfig(I2CEE_I2C, DISABLE);


/* Have to be generated before reading DR register */

I2C_GenerateSTOP(I2CEE_I2C, ENABLE);


/* Test on EV7 and clear it */

u32_Timeout = I2CEE_LONG_TIMEOUT;

while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(8);

} 



/* Read data and decrement counter */

*pu8_Buffer = I2C_ReceiveData(I2CEE_I2C);

u16_DataReadCnt--;

}

/* If more then 1 byte has to be read */

else{

/* Test on EV7 and clear it */

u32_Timeout = I2CEE_LONG_TIMEOUT;

while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))

{

if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(9);

} 


/* Read data and decrement counter */

*pu8_Buffer++ = I2C_ReceiveData(I2CEE_I2C);

u16_DataReadCnt--; 

}

} // end while


I2C_AcknowledgeConfig(I2CEE_I2C, ENABLE);

0 REPLIES 0