cancel
Showing results for 
Search instead for 
Did you mean: 

Question regarding storing data in flash

JayDev
Senior II

Hello! I was looking at finding a way to store/read data from flash. I understand it's slow but, for my application, I mainly want to be able to have some data written to part of the flash (in byte form) that I can pull from flash during my application and decode/manipulate at will. This has left me with a few questions:

1) How do I write this data to a desired location in flash? I assume I wouldn't just be editing a hex/elf file and amending it as desired and, instead, there might be a way in the IDE to write the desired bytes or at least to set aside the desired write location?

2) Is there a way to update the project with the desired start location for the program so I don't accidentally write over the stored data in flash?

3) What's the best way to learn to read the data from flash? It looks like there are a number of projects setup to use DMA to read flash to memory and I assume I'd likely want to find some chip that's all setup and do something similar with mine, maybe start with a "DMA_FlashToRAM" project (I assume this is what I'd want to do?).

If you guys could help point me in the right direction, that would be awesome! There might even be answers to this in the forum but I haven't had any luck when searching (maybe I'm searching for the wrong thing?).

Thanks for your help!

2 REPLIES 2

Which STM32?

Reading Flash reads like any other memory, use the address, use pointers, etc. It lives within the 4GB address space.

Writing Flash uses the HAL_FLASH_??? library functions to erase and write memory. See the library, read the notes/comments in there, and look at the examples.

Most of the flash memory is not really byte writable, ie the flash lines are much wider, and have error correction bits. Consider using a structure.

Journal the writes, ie keep moving forward in memory to save updated values, and have the read code skip through the list to find the last.

Modify the linker script to tell it that it has less memory, shrink it by one flash sector, and have it at the end.

For example if you had 64KB, and the sector is 4KB, tell the linker script you have 60KB at 0x08000000, and then use a pointer to access the "hidden" memory at 0x0800E000

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

I'm using an STM32L412 dev board. Thanks for the recommendations, this makes sense (and I found the flash.ID file to change to flash size/location, so thanks for that!).

So, in theory, if I wanted to store data in a desired location (say the last 4 kb of flash, I could create a program, use the flash library to write to those locations, then create a new file with amended flash size (124 kb instead of 128 kb so it's omitting the last 4 kb), and then I could read back the data stored at the desired location without having to worry about it accidentally being overwritten?

Rereading the second part of your code, it sounds like just simply writing bytes won't be as easy as I'd like and I think I get the logic of what you're saying (to essentially find out where/how the data is being stored. I imagine it will make a lot more sense when I'm reading back the data and seeing how it's being stored.

Thanks for the information and sorry for the delay in responding! If it sounds like I missed an important point, let me know. Thanks again!