3

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;
}
ozturkib
  • 1,493
  • 16
  • 28
  • Did you check that the addresses between `BANK1_START_ADDR` and `BANK1_END_ADDR` are not getting overwritten by whatever you use to program/debug the stm32? Why don't you use the back-up registers if you just want to store something as small as a device-ID? – Étienne Mar 25 '14 at 20:37
  • The amount of stored data will be increased too much so BKP is not a good option for me. However, I have solved it and post it to my blog with solution. http://www.ozturkibrahim.com/TR/eeprom-emulation-on-stm32/ – ozturkib Apr 04 '14 at 16:38

1 Answers1

1

Fortunately, I have solved my issue as explained here: After initialization of peripherals inside the main function, I was calling EEPROM_Write() function based on some conditions. Once I have restarted the entire system, the flash writing operation was broken by external switch on-off demand. Therefore, I have found a couple of different solution instead of calling EEPROM_Write() at the beginning such as I am calling this function when low power interrupt comes. More details are posted on my blog : http://www.ozturkibrahim.com/TR/eeprom-emulation-on-stm32/ Thanks

ozturkib
  • 1,493
  • 16
  • 28
  • 3
    Can you write a summary of your solution here on stackoverflow? Your blog-post is not in english and if the link tp your blog gets invalid with time the information will at least be available on stackoverflow. – Étienne Apr 05 '14 at 11:28
  • That is right. I need to prepare quick post for one of the mcu blog so I prepared it in a different language however I will translate it to English asap. However, graphs, code and figures are english although the body of the post is not so you can try those if you urgently need. Sorry about that again – ozturkib Apr 21 '14 at 23:21