cancel
Showing results for 
Search instead for 
Did you mean: 

How do I save application data on my MCU in NVM?

DJC
Senior

I'm using the STMF1 series and the HAL drivers. I already use a boot-loader to write into application space. However, now the the application to take some input data and save it permanently to a non-volatile-memory space.

I assume I can use the HAL_FLASH generically driver to save data permanently on the chip. By following this document: https://www.st.com/content/ccc/resource/technical/document/user_manual/72/52/cc/53/05/e3/4c/98/DM00154093.pdf/files/DM00154093.pdf/jcr:content/translations/en.DM00154093.pdf

However, isn't this also where my program is stored? How do i declare a user-space so that i can safely write to a location in flash? Is it just to declare a constant object and then write to its location, or some sort of block/page object?

Perhaps there is a good application example document for this?

6 REPLIES 6

Usually one tells the linker about less memory, and then use pointers/structures to access memory beyond the scope given to the linker.​

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Evangelist III

> Perhaps there is a good application example document for this?

Just a bit of common sense. Define the area for data in your linker file (.ld) or its equivalent for Keil etc.

Make sure it starts on a flash page boundary. Number and size of pages varies and can be found in the data sheet of your specific MCU model.

Keep in mind that running your code may be suspended while flash erase or write is going (again, the times are in the data sheet)

If this is a problem, some sophistication will be needed,

-- pa

DJC
Senior

Ah. ok. Thanks!. I'll give that a try. 

(document: stm RM0008 is the one I needed to get a clear understanding of how the memory is allocated)

Just one sanity check here:

Based on Table 7. Flash module organization (connectivity line devices)

Page 127 0x0803 F800 - 0x0803 FFFF 2 Kbytes

That tells me that at each address there is one 8bit-byte. 

However the HAL module states 1/2 word(16), full word(32) or double(64).

So the minumum grandualirty is to write a 16bit half-word to every 2nd address 0x0000, 0x0002 etc.

so if i write a 16bit-half-word to 0x0803F800 (start of memory-page 127)

then the low bit will be at 0x0803F804 and the high-bit will be at 0x0803F801 ?

and then i can safely write the next half word to 0x0803F802.

Pavel A.
Evangelist III

It is little endian, In each 16-bit, the low byte goes to even address and high byte to odd address.

If you write a 'half word' to 0x0803F800 the low byte goes to 0x0803F800 and the high byte to 0x0803F801. The next half word goes to 0x0803F802-0x0803F803, and so on,

-- pa

The STM32F1 is a bit antiquated for my tastes, seem to recall doing word or halfword writes.

The sector size depends on the specific model, and is the minimum erase size. You can only write a value once. You can stack multiple structures to journal them across the sector to reduce the erase cycles, and increase the robustness.

For configuration work, I tend to make a larger structure, with headers and checksums, multiple variables and pull the values from the structure into a RAM based one. In situations where the FLASH structure is blank or corrupt, it will get updated with a set of default workable values.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
DJC
Senior

great. Thanks so much to both of you.