我用UCOS2 FLASH模拟EEPROM,往里写数据时有时会死机,请教大侠帮忙分析原因
void EE_Program(void)
{
u32 EraseCounter = 0x00;
u32 NbrOfPage = 0x00;
FLASH_Status FLASHStatus;
u32 Address = EE_StartAddr;
u32 *pData;
u16 i;
u32 Data;
// OS_EXIT_CRITICAL();
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开关预取指缓存
FLASH_SetLatency(FLASH_Latency_2); //设置代码延时2周期
FLASH_Unlock(); //解锁
NbrOfPage = (EE_EndAddr - EE_StartAddr) / FLASH_PAGE_SIZE;
//清除FLASH待处理标志位
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
//摖除页面
FLASHStatus = FLASH_COMPLETE;
for ( EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++ ) {
FLASHStatus = FLASH_ErasePage(EE_StartAddr + (FLASH_PAGE_SIZE * EraseCounter));
}
Address = EE_StartAddr;
pData = (u32 *)&Ux[0][1];
FLASHStatus = FLASH_ProgramWord(Address, *pData);
Address+=4;
pData = (u32 *)&Ux[1][1];
FLASHStatus = FLASH_ProgramWord(Address, *pData);
Address+=4;
pData = (u32 *)&Ux[2][1];
FLASHStatus = FLASH_ProgramWord(Address, *pData);
Address+=4;
pData = (u32 *)&Ix[0][1];
FLASHStatus = FLASH_ProgramWord(Address, *pData);
Address+=4;
pData = (u32 *)&Ix[1][1];
FLASHStatus = FLASH_ProgramWord(Address, *pData);
Address+=4;
pData = (u32 *)&Ix[2][1];
FLASHStatus = FLASH_ProgramWord(Address, *pData);
Address+=4;
pData = (u32 *)&Ix[3][1];
FLASHStatus = FLASH_ProgramWord(Address, *pData);
Address+=4;
Data = Sys.LdAct;
Data <<=8;
Data |= Sys.LdActT;
Data <<=8;
Data |= Sys.LdVar;
FLASHStatus = FLASH_ProgramWord(Address, Data);
Address+=4;
Data = Sys.In;
Data <<=8;
Data |= Sys.Bps;
Data <<=8;
Data |= Sys.InT;
FLASHStatus = FLASH_ProgramWord(Address, Data);
Address+=4;
Data = Sys.Un;
Data <<=8;
Data |= Sys.UlT;
Data <<=8;
Data |= Sys.ChT;
Data <<=8;
Data |= Sys.ChT_H;
FLASHStatus = FLASH_ProgramWord(Address, Data);
Address+=4;
Data = Sys.Addr[0];
Data <<=16;
Data |= Sys.Addr[1];
FLASHStatus = FLASH_ProgramWord(Address, Data);
Address+=4;
Data = Sys.Uh;
Data <<=16;
Data |= Sys.Ul;
FLASHStatus = FLASH_ProgramWord(Address,Data);
Address+=4;
Data = Sys.Uh;
Data <<=16;
Data |= Sys.Ul;
FLASHStatus = FLASH_ProgramWord(Address,Data);
for(i=0;i<100;i++) {
Address+=4;
Data = ErrData[i].ErrTime.Sec;
Data <<=8;
Data |= ErrData[i].ErrTime.Min;
Data <<=8;
Data |= ErrData[i].ErrTime.Hour;
Data <<=8;
Data |= ErrData[i].ErrTime.Day;
FLASHStatus = FLASH_ProgramWord(Address,Data);
Address+=4;
Data = ErrData[i].ErrTime.Week;
Data <<=8;
Data |= ErrData[i].ErrTime.Month;
Data <<=8;
Data |= ErrData[i].ErrTime.Year;
Data <<=8;
Data = ErrData[i].ErrType;
FLASHStatus = FLASH_ProgramWord(Address,Data);
Address+=4;
Data = ErrData[i].ErrVar;
FLASHStatus = FLASH_ProgramWord(Address,Data);
}
FLASH_Lock();
// OS_ENTER_CRITICAL();
} |