嵌入式 > 嵌入式开发 > 详情

avr eeprom保护方法

发布时间:2024-03-26 发布时间:
|
在项目中复制出来的程序,使用时可能有些地方需要修改。
编译环境:WinAVR-20060421+AVRStudio4.12.498ServicePack4


基本思路:每份写到EEPRM的数据,都做三个备份,每个备份的数据都做CRC16校验,只要系统运行中出错,错误地修改了EEPROM数据,
那么根据校验字节就知道哪个备份的数据被修改了,然后用正确的备份覆盖出错的备份,达到数据恢复的目的。


EEPROMSave.h文件:



#defineEepromPageSize 64 //页容量定义

#defineEepromPage0Addr 0x0000 //各个页的其始地址定义
#defineEepromPage1Addr (EepromPage0Addr+EepromPageSize)
#defineEepromPage2Addr (EepromPage1Addr+EepromPageSize)
#defineEepromPage3Addr (EepromPage2Addr+EepromPageSize)
#defineEepromPage4Addr (EepromPage3Addr+EepromPageSize)
#defineEepromPage5Addr (EepromPage4Addr+EepromPageSize)
#defineEepromPage6Addr (EepromPage5Addr+EepromPageSize)
#defineEepromPage7Addr (EepromPage6Addr+EepromPageSize)



#defineVALID 0x01
#defineINVALID 0x00



EEPROMSave.c文件:


unsignedcharEepromReadByte(unsignedchar*address)
{
unsignedchardata;

data=0;

eeprom_busy_wait();
data=eeprom_read_byte(address);

returndata;
}


uint16_tEepromReadWord(uint16_t*address)
{
uint16_tdata;

data=0;

eeprom_busy_wait();
data=eeprom_read_word(address);

returndata;
}


voidEepromWriteByte(unsignedchar*address,unsignedchardata)
{
eeprom_busy_wait();
eeprom_write_byte(address,data);
}


voidEepromWriteWord(unsignedint*address,unsignedintdata)
{
eeprom_busy_wait();
eeprom_write_word(address,data);
}


voidEepromWriteBlock(unsignedchar*buff,unsignedchar*address,unsignedcharn)
{
unsignedchari;

for(i=0;i<n;i++)
{
EepromWriteByte((unsignedchar*)(address+i),*buff);

buff++;
}
}


unsignedcharEepromCheck(unsignedchar*pdata,unsignedcharpacksize)
{
unsignedchari,j;
unsignedintcrc,ref_crc;

crc=0;
ref_crc=0;

for(i=0;i<(packsize-2);i++)
{
crc=crc^((uint16_t)EepromReadByte(pdata)<<8);

for(j=0;j<8;j++)
{
if(crc&0x8000)
{
crc=(crc<<1)^0x1021;
}
else
{
crc=crc<<1;
}
}

pdata++;
}

ref_crc=(uint16_t)EepromReadByte(pdata);
ref_crc=ref_crc<<8;
pdata++;
ref_crc|=(uint16_t)EepromReadByte(pdata);

if(crc==ref_crc)
{
returnVALID;
}
else
{
returnINVALID;
}
}


unsignedcharCheckWriteCRC(unsignedchar*pdata,unsignedcharpacksize)
{
unsignedchari,j;
unsignedintcrc;

crc=0;

for(i=0;i<(packsize-2);i++)
{
crc=crc^((uint16_t)EepromReadByte(pdata)<<8);

for(j=0;j<8;j++)
{
if(crc&0x8000)
{
crc=(crc<<1)^0x1021;
}
else
{
crc=crc<<1;
}
}

pdata++;
}

EepromWriteByte(pdata,(uint8_t)(crc>>8));
pdata++;
EepromWriteByte(pdata,(uint8_t)crc);
pdata++;

if(EepromCheck((pdata-packsize),packsize))
{
returnVALID;
}
else
{
returnINVALID;
}
}


uint8_tCheckAllPage(void)
{
if((EepromCheck((unsignedchar*)EepromPage1Add,EepromPageSize)==VALID)
&&(EepromCheck((unsignedchar*)EepromPage2Add,EepromPageSize)==VALID)
&&(EepromCheck((unsignedchar*)EepromPage3Add,EepromPageSize)==VALID))
{
returnVALID;
}

returnINVALID;
}


uint8_tDataRecover(void)
{
unsignedchari;
unsignedchartemp;
unsignedcharpage;
unsignedintinvalidpage[3];
unsignedintvalidpage;

invalidpage[0]=0;
invalidpage[1]=0;
invalidpage[2]=0;
validpage=0;
temp=0;
page=0;

if(EepromCheck((uint8_t*)EepromPage1Add,EepromPageSize)==VALID)
{
validpage=EepromPage1Add;
}
else
{
invalidpage[page]=EepromPage1Add;
page++;
}

if(EepromCheck((uint8_t*)EepromPage2Add,EepromPageSize)==VALID)
{
validpage=EepromPage2Add;
}
else
{
invalidpage[page]=EepromPage2Add;
page++;
}

if(EepromCheck((uint8_t*)EepromPage3Add,EepromPageSize)==VALID)
{
validpage=EepromPage3Add;
}
else
{
invalidpage[page]=EepromPage3Add;
page++;
}

if(page==3) //三个备份都被破坏了
{
returnINVALID; //数据完全无效了
}

while((page--)>0) //数据恢复
{
for(i=0;i<EepromPageSize;i++)
{
temp=EepromReadByte((uint8_t*)(validpage+i));
EepromWriteByte((uint8_t*)(invalidpage[page]+i),temp);
}
}

if(CheckAllPage()==VALID)
{
returnVALID;
}

returnINVALID;
}



使用方法(三个备份):
1、定义一个数组:EEPROMData[EepromPageSize-2],数组定义为EepromPageSize-2是为了给每个备份留2个字节的校验
2、要保存数据时,先把数据放到数组中,然后调用EepromWriteBlock()函数,把这个数组的数据写进EEPROM,三个备份要写三次。
3、写完了之后,调用CheckWriteCRC()函数,该函数会计算出当前备份的CRC16检验数据并写到EEPROM备份的尾部,有多少个备份就要调用多少次。
4、至此,数据的备份工作已经完成。

5、校验数据(一般在复位后运行),执行CheckAllPage()函数,若通过了,则EEPROM数据没有问题,否则要运行DataRecover()函数,对损坏的备份进行修复






------------------修改原因:修改变量的定义形式


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机串口通信仿真实现16 16点阵显示程序