为了增加Flash使用寿命,在一个扇区内采用标志的方法,写满一页后再擦除,想知道不擦除写会不会减少Flash使用寿命
void UserClock_SpecialSave(void)
{
WORD buff[4] = {0};
buff[0] = 0xAA55;
buff[1] = clockDay;
buff[2] = clockHour;
buff[3] = clockMin;
GD_FlashWriteNoErase(buff,sizeof(buff)/2);
}
//不写满一个扇区,不擦除写Flash测试,延长Flash使用寿命
//写入数据最大为1KB
//return 0操作正确
const WORD markStr = 0xAA55;
u8 GD_FlashWriteNoErase(u16 *p_buffer,u16 numToWrite)
{
s8 i = 0;
s8 a = 0;
fmc_unlock(); //解锁
GD_FlashRead(GD32_CLOCK_TIME_ADDR, GDFLASH_BUF,sizeof(GDFLASH_BUF)/2);
for(i=127; i>=0; i--) //如果在127*8处读到0xAA55,说明整页已经写满
{
a = memcmp(&GDFLASH_BUF[4*i], &markStr, sizeof(markStr));
if(a == 0)
{
if(i != 127)
{
GD_FlashWriteNoCheck(GD32_CLOCK_TIME_ADDR+8*(i+1),p_buffer,numToWrite);
break;
}
else
{
fmc_page_erase(GD32_CLOCK_TIME_ADDR);//擦除这个扇区
GD_FlashWriteNoCheck(GD32_CLOCK_TIME_ADDR,p_buffer,numToWrite);
break;
}
}
}
if(i < 0)
{
GD_FlashWriteNoCheck(GD32_CLOCK_TIME_ADDR,p_buffer,numToWrite);
}
fmc_lock();//上锁
return 0;
}
u16 flashReadBuff[512] = {0};
extern WORD markStr;
void UserClock_SpecialRead(void)
{
s8 i = 0;
WORD buff[3] = {0};
GD_FlashRead(GD32_CLOCK_TIME_ADDR,flashReadBuff,sizeof(flashReadBuff)/2);
for(i=127; i>=0; i--)
{
if(memcmp(&flashReadBuff[4*i], &markStr, sizeof(markStr)) == 0)
{
memcpy(buff, &flashReadBuff[4*i+1], sizeof(buff));
break;
}
}
if(i < 0)
{
clockDay = 0;
clockHour = 0;
clockMin = 0;
}
else
{
clockDay = buff[0];
clockHour = buff[1];
clockMin = buff[2];
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>