I want to store Device ID inside the flash permanently. So I am using internal Flash of STM32. I can write&read to Flash in STM32. After writing the value to the flash, and switch off and then I can read it without any problem. But the problem is coming a couple of different scenario such as : When I am resetting MCU immidieatly after first reset, Data inside flash is becoming 0. The other scenario is could be the data is disappearing after a while (not tested and can not get entire conditions) Any idea about this flash problem ?
IDE : Keil MDK uVision 4
Write Function:
void EEPROM_Write(uint32_t Data)
{
/* Porgram FLASH Bank1 ********************************************************/
FLASH_UnlockBank1(); /* Unlock the Flash Bank1 Program Erase controller */
NbrOfPage = (BANK1_WRITE_END_ADDR - BANK1_WRITE_START_ADDR) / FLASH_PAGE_SIZE; /* Define the number of page to be erased */
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Clear All pending flags */
for(EraseCounter = 0; (EraseCounter < NbrOfPage) && (FLASHStatus == FLASH_COMPLETE); EraseCounter++) /* Erase the FLASH pages */
{
FLASHStatus = FLASH_ErasePage(BANK1_WRITE_START_ADDR + (FLASH_PAGE_SIZE * EraseCounter));
}
Address = BANK1_WRITE_START_ADDR; /* Program Flash Bank1 */
while((Address < BANK1_WRITE_END_ADDR) && (FLASHStatus == FLASH_COMPLETE))
{
FLASHStatus = FLASH_ProgramWord(Address, Data);
Address = Address + 4;
}
FLASH_LockBank1();
Address = BANK1_WRITE_START_ADDR; /* Check the corectness of written data */
while((Address < BANK1_WRITE_END_ADDR) && (MemoryProgramStatus != FAILED))
{
if((*(__IO uint32_t*) Address) != Data)
{
MemoryProgramStatus = FAILED;
}
Address += 4;
}
}
Read Function
uint32_t EEPROM_Read(void)
{
uint32_t readValue;
FLASH_UnlockBank1();
Address = BANK1_WRITE_START_ADDR;
readValue = (*(__IO uint32_t*) Address);
FLASH_LockBank1();
return readValue;
}