记录AT24C128 eeprom的多字节读写
#define _EEPROM_DEBUG_LEVEL 0
#if _EEPROM_DEBUG_LEVEL == 1
#defineEEPROM_DEBUG(fmt,args...) do{ \
printf(fmt,##args);\
}while(0)
#elif _EEPROM_DEBUG_LEVEL == 2
#defineEEPROM_DEBUG(fmt,args...) do{ \
printf("%s:line:%d->",__FUNCTION__,__LINE__); \
printf(fmt,##args);\
}while(0)
#elif _EEPROM_DEBUG_LEVEL == 0
#defineEEPROM_DEBUG(fmt,args...)
#else
#defineEEPROM_DEBUG(fmt,args...)
#endif
[cpp] view plain copy
#define AT24C128_PAGE_SIZE 64
#define AT24C128_PAGE_COUNT 256
#define AT24C128_BYTE_SIZE 16*1024
#define AT24C128_WADDR 0xA0
#define AT24C128_RADDR 0xA1
写时序:
///
//// wrtie eeprom by page
////
int writeE2promByPage(u32 startaddr,void* data,u32 len){
int retry=0,pageCnt =0,i=0,j=0,k =0;
u32 temp1=0,temp2=0,temp3=0,start=0,startPage;
u8 * pBuf;
pBuf = (u8*)data;
temp1 = startaddr % AT24C128_PAGE_SIZE;
temp2 = len % AT24C128_PAGE_SIZE;
temp3 = len / AT24C128_PAGE_SIZE;
startPage = startaddr / AT24C128_PAGE_SIZE;
if(temp1 ==0){
if(temp2 !=0) pageCnt = temp3 + 2;
else pageCnt = temp3 + 1;
}
else
{
if(temp2 !=0) pageCnt = temp3 + 3;
else pageCnt = temp3 + 2;
}
if(pageCnt > AT24C128_PAGE_COUNT) return -1;
EEPROM_DEBUG("pageCnt %d \r\n",pageCnt);
for(i=0;i
//start signal !!!
while(SCCB_Start() ==0){
if(retry > 100) {
EEPROM_DEBUG("start fail \r\n");
return -1;
}
retry++;
}
if(i == 0){
start = startaddr;
temp1 = AT24C128_PAGE_SIZE - startaddr;
}
else{
start = (startPage + i) * AT24C128_PAGE_SIZE;
temp1 = AT24C128_PAGE_SIZE;
}
EEPROM_DEBUG("start:%d \r\n",start);
EEPROM_DEBUG("temp1:%d \r\n",temp1);
//write slave addr
SCCB_writeByte(AT24C128_WADDR);
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write addr fail \r\n");
SCCB_Stop();
goto _end;
}
//write page high addr
SCCB_writeByte(start / 256 );
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write high page addr fail \r\n");
SCCB_Stop();
goto _end;
}
//write page low addr
SCCB_writeByte(start % 256 );
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write low page addr fail \r\n");
SCCB_Stop();
goto _end;
}
//write date
for(j = 0; j
SCCB_writeByte(pBuf[k]);
//write operation need slave response ack
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write data fail \r\n");
SCCB_Stop();
goto _end;
}
else{
k++;
}
if( k == len){
SCCB_Stop();
goto _end;
}
}
EEPROM_DEBUG("page %d write success \r\n",i);
//stop
SCCB_Stop();
SCCB_Delay(SCL_TIME_US);
}
_end:
return k;
}
读取时序参考:
注意:Sequential read的时序图并不是完整的,根据文档描述Sequential read时序需要加上random的头!
即start -> slave waddr ->high addr -> low addr ->startagain ->slave raddr -> read n byte -> stop
注意写时候是wait ack ,需要eeprom回复!而读取的时候master只需要发送ack即可,读取的最后一个字节要发送的是noack。
/*
*SEQUENTIAL READ,can read multi byte!!!
*
*/
int readE2promBySeq(u32 startaddr,void* data,u32 len){
int retry=0,j=0;
u8 * pBuf;
pBuf = (u8*)data;
//startc signal
SCCB_Delay(SCL_TIME_US);
while(SCCB_Start() ==0){
if(retry > 100){
EEPROM_DEBUG("start fail \r\n");
return -1;
}
retry++;
}
//write slave w addr
SCCB_writeByte(AT24C128_WADDR);
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write slave w addr fail \r\n");
SCCB_Stop();
goto _end;
}
//write high addr
SCCB_writeByte(startaddr / 256 );
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write h addr fail \r\n");
SCCB_Stop();
goto _end;
}
//write low addr
SCCB_writeByte(startaddr % 256 );
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write l addr fail \r\n");
SCCB_Stop();
goto _end;
}
//start again
retry = 0;
while(SCCB_Start() ==0){
if(retry > 100){
EEPROM_DEBUG("start fail \r\n");
return -1;
}
retry++;
}
//write slave read addr
SCCB_writeByte(AT24C128_RADDR);
if(SCCB_Wait_Ack() == 0){
EEPROM_DEBUG("write r addr fail \r\n");
SCCB_Stop();
goto _end;
}
//read data
for(j = 0; j
pBuf[j] = SCCB_readByte();
if(j
//read operation ,master just send ack ,no response
SCCB_Ack();
}
}
SCCB_noAck();
SCCB_Stop();
_end:
return j;
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』