AnsweredAssumed Answered

I2C EEPROM M24C64 sequential read

Question asked by Ck-befra on Jun 3, 2014
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):
001.// ---------------- While the bus is busy ------------------------------------- 
002. u32_Timeout = I2CEE_LONG_TIMEOUT;
003. while(I2C_GetFlagStatus(I2CEE_I2C, I2C_FLAG_BUSY))
004. {
005.   if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_WHILE);       
006. }
007.  
008.    
009. // ---------------- Send Start condition --------------------------------------
010. I2C_GenerateSTART(I2CEE_I2C, ENABLE);
011.  
012. /* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
013. u32_Timeout = I2CEE_FLAG_TIMEOUT;
014. while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
015. {
016.   if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SC);
017. 
018. 
019.   // ---------------- Send chip address for Write ------------------------------- 
020. I2C_Send7bitAddress(I2CEE_I2C, U8_i2cee_chip_addr, I2C_Direction_Transmitter);
021.    
022.   /* Test on EV6 and clear it */
023. u32_Timeout = I2CEE_LONG_TIMEOUT;
024. while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
025. {
026.   if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SAW);
027. }
028.  
029.   // ---------------- Send address you wanna read from --------------------------    
030. /* Send the EEPROM's internal address to read from: MSB of the address first */
031. I2C_SendData(I2CEE_I2C, (uint8_t)((u16_ReadAddr & 0xFF00) >> 8));   
032. 
033. /* Test on EV8_2 and clear it */
034. u32_Timeout = I2CEE_LONG_TIMEOUT;
035.   //while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
036. while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
037. {
038.   if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_MSB);
039. }
040. 
041. /* Send the EEPROM's internal address to read from: LSB of the address */
042. I2C_SendData(I2CEE_I2C, (uint8_t)(u16_ReadAddr & 0x00FF));   
043.    
044. /* Test on EV8? and clear it */
045.   u32_Timeout = I2CEE_LONG_TIMEOUT;
046.   //while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
047. while(I2C_GetFlagStatus(I2CEE_I2C, I2C_FLAG_BTF) == RESET)
048. {
049.   if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_LSB);
050. }
051.    
052.   /* Clear AF flag if arised */
053. //I2C1->SR1 |= (uint16_t)0x0400;
054.     
055. // ---------------- Send Start condition --------------------------------------
056. I2C_GenerateSTART(I2CEE_I2C, ENABLE);
057.  
058. /* Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
059. u32_Timeout = I2CEE_LONG_TIMEOUT;
060. while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
061. {
062.   if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SC2);
063. }
064.    
065. // ---------------- Send chip address for Read --------------------------------   
066. I2C_Send7bitAddress(I2CEE_I2C, U8_i2cee_chip_addr, I2C_Direction_Receiver); 
067.    
068.   /* Test on EV6 and clear it */
069.   u32_Timeout = I2CEE_LONG_TIMEOUT;
070.   //while(I2C_GetFlagStatus(I2CEE_I2C, I2C_FLAG_ADDR) == RESET)
071. while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
072. {
073.   if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(I2C_ER_RB_SAR);
074. }
075.    
076.   // ---------------- Read all data from eeprom to Buffer -----------------------        
077.   while(u16_DataReadCnt > 0){
078.       // If only 1 byte (or last byte) has to be read
079.       if(u16_NumByteToRead < 2){
080.            
081.           /* Prepare an NACK for the next data received */
082.           I2C_AcknowledgeConfig(I2CEE_I2C, DISABLE);
083.            
084.           /* Have to be generated before reading DR register */
085.           I2C_GenerateSTOP(I2CEE_I2C, ENABLE);
086.            
087.           /* Test on EV7 and clear it */
088.           u32_Timeout = I2CEE_LONG_TIMEOUT;
089.           while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
090.           {
091.               if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(8);
092.           }
093.                        
094.            
095.           /* Read data and decrement counter */
096.           *pu8_Buffer = I2C_ReceiveData(I2CEE_I2C);
097.           u16_DataReadCnt--;
098.       }
099.       /* If more then 1 byte has to be read */
100.       else{
101.           /* Test on EV7 and clear it */
102.           u32_Timeout = I2CEE_LONG_TIMEOUT;
103.           while(!I2C_CheckEvent(I2CEE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
104.           {
105.               if((u32_Timeout--) == 0) return I2CEE_TIMEOUT_UserCallback(9);
106.           }
107.            
108.           /* Read data and decrement counter */
109.           *pu8_Buffer++ = I2C_ReceiveData(I2CEE_I2C);
110.           u16_DataReadCnt--;     
111.       }
112.   } // end while
113.    
114. I2C_AcknowledgeConfig(I2CEE_I2C, ENABLE);

Outcomes