若按字节写的话,写EEPROM每个字节耗时约6ms。块操作的方式将一个基本页(64字节)写入的理论时间也是6ms。但是在操作过程中也出现了写死机的情况。int16 EepRomWrite(void *pWriteData, uint16 nWriteLen, SaveType nType)
{
uint8 i;
uint8 *p;
if (pWriteData==NULL || nWriteLen==0 || nWriteLen>=EEPROM_SIZE)
{
return -1;
}
SetGpioLevel(PA,1,0);
if (EepRomUnLock() == 0)
{
//UART1_SendStr("eeprom fail unlock", 19);
return -1;
}
// 以下两行代码导致死机
FLASH_CR2 |=0x01;
FLASH_NCR2 &= ~(0x01);
SetGpioLevel(PA,1,1);
p = (uint8 *)(EEPROM_STARTADDR+nType*64);
for(i=0; i<nWriteLen+(EEPROM_BLOCK_SIZE-nWriteLen%EEPROM_BLOCK_SIZE); i++, p++) // !!!!!
{
*p = ((uint8 *)pWriteData)[i];
if (i>=nWriteLen)
{
*p = 0x00;
}
if ((i+1)%EEPROM_BLOCK_SIZE == 0)
{
SetGpioLevel(PA,1,0);
while((FLASH_IAPSR & 0x40) ==0);
SetGpioLevel(PA,1,1);
while((FLASH_IAPSR & 0x04) ==1);
SetGpioLevel(PA,1,0);
}
}
FLASH_CR2 &= ~(0x01);
EepRomLock();
SetGpioLevel(PA,1,1);
return 0;
}
|