Author Topic: Saving variables to non volatile memory in STM32F429  (Read 884 times)

0 Members and 1 Guest are viewing this topic.

Offline Luis

  • Contributor
  • Posts: 6
  • Country: mx
Saving variables to non volatile memory in STM32F429
« on: October 20, 2017, 06:34:48 pm »
I have been reading so many post related to saving data to non volatile memory in STM32F4, but until now I haven't had success. I wonder if you can help me by doing this? Modifying my scatter file I have:

LR_IROM1 0x08000000 0x00080000  {    ; load region size_region

ER_IROM1 0x08000000 0x00080000  {  ; load address = execution address
      *.o (RESET, +First)
      *(InRoot$$Sections)
      .ANY (+RO)


   }
   A 0x20000000 UNINIT 0x00000100  { ;no init section
      *(.noinit)

   }
   RW_IRAM2 0x20000100 0x0000FFF0  {                ;all other rw data
      .ANY(+RW +ZI)

   }

   ER_IROM2 0x08008000 UNINIT 0x00000FFF  { 
   *(nom_base)     
   }

}
The section nom_base supposes to be in flash memory, im sure the address is right but it doesn't do anything. The section no_init is working but it only survives the resets, if I power off the board, then the value is lost. I only want to save 4 variables status, or one or two. Can you help me telling me what Im doing wrong?
 

Offline ajb

  • Super Contributor
  • ***
  • Posts: 1770
  • Country: us
Re: Saving variables to non volatile memory in STM32F429
« Reply #1 on: October 20, 2017, 07:28:12 pm »
Do you mean you want to be able to have a variable reside in flash, while being able to alter its value during runtime just like a normal variable in RAM?  Flash doesn't work like that.  Writing to the microcontroller's flash requires a certain sequence of operations, usually involving unlocking the flash, erasing some portion of it, and then executing an explicit write command to the section you just erased.  The exact steps vary from MCU to MCU, and the minimum size chunk that you can erase at a time will vary depending on the memory layout of the MCU.  This will all be detailed in the datasheet.

Note that you don't necessarily have to erase every time you want to write to flash, but a write can only clear bits, not set them.  So if you have b11101100, you could write this to b11100000, but not to b11100011 without performing an erase first.  However, sometimes you can only do a limited number of writes to a section before it must be erased and rewritten.  This should also be indicated int he datasheet.

Depending on where in flash you are storing your values and what the memory layout of your MCU is, you might need to erase 32k or more at a time in order to change a single value.  So if you need to store n sectors worth of nonvolatile data you need to allocate at least n+1 sectors so that you can write the new values to one while erasing the other.  I know Atmel has an EEPROM emulation driver for their SAM D series that handles all of that, I don't know if anything similar exists for STM32F4.  But if you can consolidate all of your non-volatile data into a single data structure that fits into a single sector it's not that hard to implement a simple ping-pong system for this.  Handling multiple pages worth of data isn't much harder, although at least on some of the larger memory STM32s you might have only a handful of 32k sectors, with the rest being 128-256k.

EDIT: since you only want to store a few variables, you can also divide a sector up into multiple slots, each sized for the total data you need to save.  Initially you save to the first slot, then each subsequent save you would use the next slot in line.  At startup, your program would need to scan through the slots and find the last one that is not erased; this will be the latest data, which it should load.  Once you fill up a whole sector, erase it, and start over in the same sector (consumes less flash, but risks data loss if power is lost before the erase and rewrite is complete) or in another sector (consumes more flash, but safer).  This will also be subject to the number of writes you can do before erasing, of course, but can drastically cut down on the required erase cycles, which can extend the life of the flash.
« Last Edit: October 20, 2017, 07:33:16 pm by ajb »
 

Offline Luis

  • Contributor
  • Posts: 6
  • Country: mx
Re: Saving variables to non volatile memory in STM32F429
« Reply #2 on: October 24, 2017, 05:34:43 pm »
Thanks for you response, I was really lost in the topic, reading comments like you helped me to understand what I've got to do. Thanks a lot!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf