using a block of flash as eeprom - cortex-M3

Discussion in 'Embedded Systems and Microcontrollers' started by aamirali, Sep 8, 2014.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    I want to use block of flash(16KB) as eeprom.
    I am using STM32F205RBT6 & keil 4.72.

    What I have to do:
    1. Store 5Kb of user data & retrieve it on startup.
    2. Flash endurance of 10000 cycles is good for my project & I have fixed 16Kb block for it.

    What I have done is :

    1. OPtion for target-> target-> (check)IROM1 ; Start 0x8000000 ; size 0x4000 OPtion for target-> target-> (check)IROM2 ; Start 0x8008000 ; size 0x18000

    i.e fixing flash block 0x80004000 fix for eeprom whose size is 16Kb

    2. Flah write code for 5Kb data:

    Code (Text):
    1.  
    2. void flash_write(void)
    3. {
    4. uint32_t address = 0x08004000;
    5. uint32_t data;
    6. uint32_t cnt = 0;
    7.  
    8. /* read entire sector */
    9. while(address < 0x08008000)
    10. {
    11. data = *(__IO uint32_t*)address;
    12.  
    13. flah_union.data_u32[cnt++] = data;
    14.  
    15. address = address + 4;
    16.  
    17. }
    18.  
    19. /* fill the array */
    20. for( cnt = 0 ; cnt < 5120 ; cnt++ )
    21. {
    22. flah_union.data_u8[cnt] = g_ft232_arr_data[cnt];
    23. }
    24.  
    25. /* unlock flash */
    26. FLASH_Unlock();
    27.  
    28. /* Clear pending flags (if any) */
    29. FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR|FLASH_FLAG_PGSERR);
    30.  
    31. /* erase the entire sector */
    32. if (FLASH_EraseSector(FLASH_Sector_1, VoltageRange_3) != FLASH_COMPLETE)
    33. {
    34. /* Error occurred while sector erase.
    35. User can add here some code to deal with this error */
    36. while (1)
    37. {
    38. }
    39. }
    40.  
    41. /* write back array */
    42. address = 0x08004000;
    43. cnt = 0;
    44.  
    45. while(address < 0x08008000)
    46. {
    47. if (FLASH_ProgramWord(address, flah_union.data_u32[cnt++]) == FLASH_COMPLETE)
    48. {
    49. address = address + 4;
    50. }
    51. else
    52. {
    53. while (1)
    54. {
    55. }
    56. }
    57.  
    58. }
    59.  
    60. FLASH_Lock();
    61.  
    62. }
    63.  
    3. Flash read code for 5Kb data:

    Code (Text):
    1.  
    2. void flash_read(void)
    3. {
    4. uint32_t address = 0x08004000;
    5. uint32_t data;
    6. uint32_t cnt = 0;
    7.  
    8.  
    9. /* read entire sector */
    10. while(address < 0x08008000)
    11. {
    12. data = *(__IO uint32_t*)address;
    13.  
    14.  
    15. flah_union.data_u32[cnt++] = data;
    16.  
    17.  
    18. address = address + 4;
    19.  
    20.  
    21. }
    22.  
    23.  
    24. /* fill the array */
    25. for( cnt = 0 ; cnt < 5120 ; cnt++ )
    26. {
    27. g_ft232_arr_data[cnt] = flah_union.data_u8[cnt];
    28. }
    29.  
    30.  
    31.  
    32. }
    33.  
    34.  
    My queries:
    1. Do I need to disable interrupts before reading or writing to flash even if I had defines separate block for eeprom, as I said earlier.

    2. Since I am writing to flash & also while write to flash, code is getting executing from flash i.e code for reading/writing flash. Do I need to move the code to Ram?
    If yes how
     
    Last edited by a moderator: Sep 8, 2014
  2. aamirali

    Thread Starter Member

    Feb 2, 2012
    415
    1
    Edit: All the flash function inside write/read functions are called from standard peripheral library provided by ST.
     
  3. Xkyve

    Member

    Nov 30, 2012
    32
    2
    1. For reading you don't need to disable interrupts if nobody else writes to that block. For writing you may have to disable interrupts if there are potential routines which write to any block of Flash (if you get an interrupt while writing to flash and that interrupt service routine locks your flash again, then it might be a problem).

    2. You don't need to move your code to RAM if the block you are writing to is not used for storing instructions.

    I hope I understood correctly: you are trying to write a 5k block to the same Flash address? (0x08004000)
    If the datasheet of the controller specifies that the Flash has a life of at least 10000 write cycles, then it's ok.
     
Loading...