写函数,写一个长度为bytesize,到eeprom
uint16_t EERPOM_Writeup4byte(uint32_t adr,uint8_t *pData,uint32_t length)
{
uint8_t buf[512];
uint8_t *pbuf;
uint32_t e_adr;
uint32_t e_sec;
uint32_t e_offset;
uint32_t a;
uint32_t err = EEPROM_ERR_SUCCESS;
#ifdef IAR
if(adr & 0x03)
{
err = EEPROM_ERR_INVALID_PARAM;
return (err);
}
#endif
if((adr + length )>1024)
{
err=EEPROM_ADR_OverFlow;
return(err);
}
e_adr=adr+EEPROM_START_ADR;
e_sec=e_adr & EEPROM_SECTOR_MASK;
e_offset=e_adr & 0x1ff;
while (length>0){
//如果起始地址不等于0,或者长度小于512 都进入这个循环
if (e_offset||(length<512)){
pbuf=buf;
a=512-e_offset;
a=(length>a?a:length);
memcpy(buf,(uint8_t*)e_sec,512);
memcpy(&buf[e_offset],pData,a);
pData+=a;
length-=a;
e_offset=0;
}else{ //如果起始地址等于0且长度大于512
pbuf=pData;
pData+=512;
length-=512;
}
err=Flash_EraseSector(e_sec);
err=Flash_Program(e_sec,(uint8_t*)pbuf,512);//然后写入flash
e_sec+=0x200;
}
return err;
}
|