EEPROM模拟IIC时序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#define CHIP_IC_1_BYTE (uint32_t)0x8000 //256k bits=0x8000bytes
#define CHIP_IC_2_BYTE (uint32_t)0 //0 bits=0x8000bytes
#define CHIP_IC_3_BYTE (uint32_t)0 //0 bits=0x8000bytes
#define EPPROM_LAST_ADDR (uint32_t)(CHIP_IC_1_BYTE+CHIP_IC_2_BYTE+CHIP_IC_3_BYTE-1)
#define E2_REOPERATE_CNT_MAX 3
//--用于级联,自动识别(前提:定义好CHIP_IC_1_BYTE等 大小)
#define CHIP1 0xa0
#define CHIP2 0xa2
#define CHIP3 0xa4
void start24(){
BOUT(E2sclGpio,E2sclGpio_bit); //--设置SCL口味输出
BCLR(E2sclGpio,E2sclGpio_bit);
nop5();
BOUT(E2sdaGpio,E2sdaGpio_bit);
BUP(E2sdaGpio,E2sdaGpio_bit);
BSET(E2sdaGpio,E2sdaGpio_bit);
nop5();
BSET(E2sclGpio,E2sclGpio_bit);
nop10();
BCLR(E2sdaGpio,E2sdaGpio_bit);
nop10();
}
void stop24(){
BCLR(E2sclGpio,E2sclGpio_bit);
nop5();
BOUT(E2sdaGpio,E2sdaGpio_bit);
BCLR(E2sdaGpio,E2sdaGpio_bit);
nop10();
BSET(E2sclGpio,E2sclGpio_bit);
nop10();
BSET(E2sdaGpio,E2sdaGpio_bit);
nop10();
}
uint8_t writex(uint8_t temp){
uint8_t i;
BOUT(E2sdaGpio,E2sdaGpio_bit);
for (i=0;i<8;i++){
BCLR(E2sclGpio,E2sclGpio_bit);
nop5();
if((temp&0x80)==0){
BCLR(E2sdaGpio,E2sdaGpio_bit);
}
else BSET(E2sdaGpio,E2sdaGpio_bit);
nop5();
BSET(E2sclGpio,E2sclGpio_bit);
nop10();
temp=temp<<1;
}
BCLR(E2sclGpio,E2sclGpio_bit);
BIN(E2sdaGpio, E2sdaGpio_bit);
nop10();
BSET(E2sclGpio,E2sclGpio_bit);
nop10();
wait_us(2);
return (BGET(E2sdaGpio, E2sdaGpio_bit));
}
uint8_t readx(void){
uint8_t i,temp=0;
BCLR(E2sclGpio,E2sclGpio_bit); //---clr short high_pluse
BIN(E2sdaGpio, E2sdaGpio_bit);
for(i=0;i<8;i++){
temp=temp<<1;
BCLR(E2sclGpio,E2sclGpio_bit);
nop10();
BSET(E2sclGpio,E2sclGpio_bit);
nop10();
wait_us(2);
temp |= (uint8_t)BGET(E2sdaGpio, E2sdaGpio_bit);
}
return(temp);
}
void send_ack(){
BCLR(E2sclGpio,E2sclGpio_bit); //---ack
BOUT(E2sdaGpio,E2sdaGpio_bit);
nop5();
BCLR(E2sdaGpio,E2sdaGpio_bit);
nop5();
BSET(E2sclGpio,E2sclGpio_bit);
nop10();
}
void send_noack(){
BCLR(E2sclGpio,E2sclGpio_bit); //---no ack
BOUT(E2sdaGpio,E2sdaGpio_bit);
nop5();
BSET(E2sdaGpio,E2sdaGpio_bit);
nop5();
BSET(E2sclGpio,E2sclGpio_bit);
nop10();
}
/*----------------------------------
input: shbaddr //shebei dizhi
output: 1 --busy
0 --ready
-----------------------------------*/
uint8_t checkbusy(uint8_t shbaddr)
{
uint8_t i=10;
if(getE2PowerStatus()==FALSE) //--检测设备是否供电
{
i2cMemoryDevice_init();//--初始化E2口状态和开启设备电源
wait_ms(1);
}
if(shbaddr==CHIP1)CLRMERR(Merr_e2device1Err);
else if(shbaddr==CHIP2)CLRMERR(Merr_e2device2Err);
else if(shbaddr==CHIP3)CLRMERR(Merr_e2device3Err);
stop24();
Api_FeedDog(); //--喂狗
while(i--){
wait_ms(1);
start24();
if(writex(shbaddr)==0){
start24();
stop24();
nop10();
start24();
if(writex(shbaddr)==0)
return(TRUE);
}
}
stop24();
if(shbaddr==CHIP1)SETMERR(Merr_e2device1Err);
else if(shbaddr==CHIP2)SETMERR(Merr_e2device2Err);
else if(shbaddr==CHIP3)SETMERR(Merr_e2device3Err);
return (FALSE);
}
void r24nb(void* dst,uint32_t eaddr,uint16_t ecount)
{
uint8_t i,temp,*copy_data=(uint8_t*)dst;
uint32_t copy_eaddr=eaddr;
uint16_t copy_ecount=ecount;
for(i=0;(ecount)&&(i
eaddr=copy_eaddr;
dst=copy_data;
ecount=copy_ecount;
for(;ecount;)
{
if(eaddr
else if(eaddr<(CHIP_IC_1_BYTE+CHIP_IC_2_BYTE+CHIP_IC_3_BYTE))temp=CHIP3;
else
{//--存储器地址访问出错
return;
}
if(checkbusy(temp)==FALSE)break;
if(writex((uint8_t)(eaddr>>8)))break;
if(writex((uint8_t)eaddr))break;
start24();
if(writex(temp|bit0))break;
for(;ecount;)
{
ecount -= 1;
*(uint8_t*)dst=readx();
dst=(uint8_t*)dst+1;
eaddr++;
if(ecount==0)
{
send_noack();
}
else if((eaddr&0x0000ffff)==0)
{ //--更换存储器,需要重新写设备地址
send_noack();
stop24();
break;
}
else
{
send_ack();
}
}
}
}
stop24();
}
void w24nb_page(void* dst,uint32_t eaddr,uint16_t ecount)
{
uint8_t operateFlag,i,temp=CHIP1,*copy_data=(uint8_t*)dst;
uint32_t copy_eaddr=eaddr;
uint16_t copy_ecount=ecount;
if(!ecount)return;
for(i=0;(ecount)&&(i
eaddr=copy_eaddr;
dst=copy_data;
ecount=copy_ecount;
operateFlag=TRUE;
portOperate(E2ProtectPort, clr_P);
for(;ecount && (operateFlag==TRUE);)
{
if(eaddr
else if(eaddr<(CHIP_IC_1_BYTE+CHIP_IC_2_BYTE+CHIP_IC_3_BYTE))temp=CHIP3;
else
{//--存储器地址访问出错
return;
}
if(checkbusy(temp)==FALSE)break;
if(writex((uint8_t)(eaddr>>8)))break;
if(writex((uint8_t)eaddr))break;
for(;ecount;)
{
ecount -= 1;
if(writex(*(uint8_t*)dst))
{
//--写数据出错,需要重置传参,需要跳出上一次层for循环,需用operateFlag=FALSE实现
operateFlag=FALSE;
break;
}
dst=(uint8_t*)dst+1;
eaddr++;
//if(((eaddr&0x7f)==0)&&ecount){ //512k,128bytes
if(((eaddr&0x3f)==0)&&ecount){ //256k,64bytes
stop24();
break;
}
}
}
}
stop24();
portOperate(E2ProtectPort,set_P);
}
void set24nb(uint8_t data,uint32_t eaddr,uint16_t ecount)
{
uint8_t operateFlag,i,temp=CHIP1;
uint32_t copy_eaddr=eaddr;
uint16_t copy_ecount=ecount;
if(!ecount)return;
for(i=0;(ecount)&&(i
eaddr=copy_eaddr;
ecount=copy_ecount;
operateFlag=TRUE;
portOperate(E2ProtectPort, clr_P);
for(;ecount && (operateFlag==TRUE);)
{
if(eaddr
HIP_IC_1_BYTE+CHIP_IC_2_BYTE))temp=CHIP2;
else if(eaddr<(CHIP_IC_1_BYTE+CHIP_IC_2_BYTE+CHIP_IC_3_BYTE))temp=CHIP3;
else
{//--存储器地址访问出错
return;
}
if(checkbusy(temp)==FALSE)break;
if(writex((uint8_t)(eaddr>>8)))break;
if(writex((uint8_t)eaddr))break;
for(;ecount;)
{
ecount -= 1;
if(writex(data))
{
//--写数据出错,需要重置传参,需要跳出上一次层for循环,需用operateFlag=FALSE实现
operateFlag=FALSE;
break;
}
eaddr++;
//if(((eaddr&0x7f)==0)&&ecount){ //512k,128bytes
if(((eaddr&0x3f)==0)&&ecount){ //256k,64bytes
stop24();
break;
}
}
}
}
stop24();
portOperate(E2ProtectPort,set_P);
}