微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > avr eeprom保护方法

avr eeprom保护方法

时间:11-23 来源:互联网 点击:
在项目中复制出来的程序,使用时可能有些地方需要修改。
编译环境: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;in;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;j8;j++)
{
if(crc&0x8000)
{
crc=(crc1)^0x1021;
}
else
{
crc=crc1;
}
}

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;j8;j++)
{
if(crc&0x8000)
{
crc=(crc1)^0x1021;
}
else
{
crc=crc1;
}
}

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

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top