avr eeprom保护方法
时间:11-23
来源:互联网
点击:
在项目中复制出来的程序,使用时可能有些地方需要修改。
编译环境:WinAVR-20060421 + AVR Studio 4.12.498 Service Pack 4
基本思路:每份写到EEPRM的数据,都做三个备份,每个备份的数据都做CRC16校验,只要系统运行中出错,错误地修改了EEPROM数据,
那么根据校验字节就知道哪个备份的数据被修改了,然后用正确的备份覆盖出错的备份,达到数据恢复的目的。
EEPROMSave.h 文件:
#define EepromPageSize 64 //页容量定义
#define EepromPage0Addr 0x0000 //各个页的其始地址定义
#define EepromPage1Addr (EepromPage0Addr + EepromPageSize)
#define EepromPage2Addr (EepromPage1Addr + EepromPageSize)
#define EepromPage3Addr (EepromPage2Addr + EepromPageSize)
#define EepromPage4Addr (EepromPage3Addr + EepromPageSize)
#define EepromPage5Addr (EepromPage4Addr + EepromPageSize)
#define EepromPage6Addr (EepromPage5Addr + EepromPageSize)
#define EepromPage7Addr (EepromPage6Addr + EepromPageSize)
#define VALID 0x01
#define INVALID 0x00
EEPROMSave.c 文件:
unsigned char EepromReadByte(unsigned char *address)
{
unsigned char data;
data = 0;
eeprom_busy_wait();
data = eeprom_read_byte(address);
return data;
}
uint16_t EepromReadWord(uint16_t *address)
{
uint16_t data;
data = 0;
eeprom_busy_wait();
data = eeprom_read_word(address);
return data;
}
void EepromWriteByte(unsigned char *address,unsigned char data)
{
eeprom_busy_wait();
eeprom_write_byte(address,data);
}
void EepromWriteWord(unsigned int *address,unsigned int data)
{
eeprom_busy_wait();
eeprom_write_word(address,data);
}
void EepromWriteBlock(unsigned char *buff,unsigned char *address,unsigned char n)
{
unsigned char i;
for (i = 0; i n; i++)
{
EepromWriteByte((unsigned char *)(address + i),*buff);
buff++;
}
}
unsigned char EepromCheck(unsigned char *pdata,unsigned char packsize)
{
unsigned char i,j;
unsigned int crc,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)
{
return VALID;
}
else
{
return INVALID;
}
}
unsigned char CheckWriteCRC(unsigned char *pdata,unsigned char packsize)
{
unsigned char i,j;
unsigned int crc;
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))
{
return VALID;
}
else
{
return INVALID;
}
}
uint8_t CheckAllPage(void)
{
if ((EepromCheck((unsigned char *)EepromPage1Add,EepromPageSize) == VALID)
&&(EepromCheck((unsigned char *)EepromPage2Add,EepromPageSize) == VALID)
&&(EepromCheck((unsigned char *)EepromPage3Add,EepromPageSize) == VALID))
{
return VALID;
}
return INVALID;
}
uint8_t DataRecover(void)
{
unsigned char i;
unsigned char temp;
unsigned char page;
unsigned int invalidpage[3];
unsigned int validpage;
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
编译环境:WinAVR-20060421
基本思路:每份写到EEPRM的数据,都做三个备份,每个备份的数据都做CRC16校验,只要系统运行中出错,错误地修改了EEPROM数据,
EEPROMSave.h
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
EEPROMSave.c
unsigned
{
unsigned
data
eeprom_busy_wait();
data
return
}
uint16_t
{
uint16_t
data
eeprom_busy_wait();
data
return
}
void
{
eeprom_busy_wait();
eeprom_write_byte(address,data);
}
void
{
eeprom_busy_wait();
eeprom_write_word(address,data);
}
void
{
unsigned
for
{
EepromWriteByte((unsigned
buff++;
}
}
unsigned
{
unsigned
unsigned
crc
ref_crc
for
{
crc
for
{
if
{
crc
}
else
{
crc
}
}
pdata
}
ref_crc
ref_crc
pdata
ref_crc
if
{
return
}
else
{
return
}
}
unsigned
{
unsigned
unsigned
crc
for
{
crc
for
{
if
{
crc
}
else
{
crc
}
}
pdata
}
EepromWriteByte(pdata,(uint8_t)
pdata
EepromWriteByte(pdata,(uint8_t)
pdata
if
{
return
}
else
{
return
}
}
uint8_t
{
if
{
return
}
return
}
uint8_t
{
unsigned
unsigned
unsigned
unsigned
unsigned
invalidpage[0]
invalidpage[1]
invalidpage[2]
validpage
temp
page
if
{
validpage
}
else
{
invalidpage
avreeprom保护方 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)