本帖最后由 121322620 于 2019-9-9 11:42 编辑
小弟最近在玩一块新唐M058S的片子,实验的dataflash功能。程序是读数据,再++,写进去。本意是每次上电或者复位从上次数值开始,可是发现,每次断电,第一次读出的数据都是从FFFF。很奇怪这样?求大神指点
#define DATA_FLASH_TEST_BASE (FMC->DFBADR)
#define DATA_FLASH_TEST_END (FMC->DFBADR + 0x1000)
int32_t VerifyData(uint32_t u32StartAddr, uint32_t u32EndAddr, uint32_t u32Pattern)
{
uint32_t u32Addr;
uint32_t u32Data;
for(u32Addr = u32StartAddr; u32Addr < u32EndAddr; u32Addr += 4)
{
u32Data = FMC_Read(u32Addr);
if(u32Data != u32Pattern)
{
return -1;
}
}
return 0;
}
int32_t WriteToFlash(uint32_t u32StartAddr, uint32_t u32Length, uint32_t *u32data)
{
uint32_t u32Addr,i;
u32Addr = u32StartAddr;
for(u32Addr = u32StartAddr; u32Addr < (u32StartAddr + u32Length*4); u32Addr += FMC_FLASH_PAGE_SIZE)
{
FMC_Erase(u32Addr);
if(VerifyData(u32Addr, u32Addr + FMC_FLASH_PAGE_SIZE, 0xFFFFFFFF) < 0)
{
return -1;
}
}
u32Addr = u32StartAddr;
for(i =0; i < u32Length; i++)
{
FMC_Write(u32Addr, u32data);
u32Addr += 4;
}
return 0;
}
int32_t ReadFromFlash(uint32_t u32StartAddr, uint32_t u32Length, uint32_t *u32data)
{
uint32_t u32Addr,i;
u32Addr = u32StartAddr;
for(i =0; i < u32Length; i++)
{
u32data = FMC_Read(u32Addr);
u32Addr += 4;
}
return 0;
}
uint32_t DateFlash[4];
void Flash_Test(void)
{
//uint32_t recorddata[4]={1,2,3,4};
SYS_UnlockReg();
FMC_Open();
ReadFromFlash(DATA_FLASH_TEST_BASE,4,DateFlash);
DateFlash[0]+=2;
DateFlash[1]+=2;
DateFlash[2]+=2;
DateFlash[3]+=2;
WriteToFlash(DATA_FLASH_TEST_BASE,4,DateFlash);
ReadFromFlash(DATA_FLASH_TEST_BASE,4,DateFlash);
FMC_Close();
SYS_LockReg();
}
int main(void)
{
/* Unlock protected registers */
SYS_UnlockReg();
/* Init System, peripheral clock and multi-function I/O */
SYS_Init();
/* Lock protected registers */
SYS_LockReg();
Inter_Init();
Led_init();
Timer3_Init();
UART_Open(UART0, 115200);
//PWM_OUT_Init();
printf(" uart--- \r\r\n");
while(1)
{
Flash_Test();
}
}
|