cancel
Showing results for 
Search instead for 
Did you mean: 

Writing data to internal flash

Todd Barrett
Associate II

I have a custom board using a STM32F071VTB processor. I would like to store a small amount of configuration data in flash. The configuration can change during operation so that I need to be able to write to the flash from the executing application running on the processor. The application is written in C/C++ and is being developed on the STM32CubeIDE supplied by ST. I see some documentation discussing how to use the HAL drivers to write to flash but not any real documentation of how to make sure that I don't write over the code/etc stored in the flash.

I see the STM32F071BTX_FLASH.ld file but it says that it was auto generated by the IDE and changing it so that it doesn't regenerate and overwrite it.

Also documentation of how to use the .ld file seems hard to find. I've used similar type files on Analog BlackFin processors but some of the syntax etc of the .ld file is unfamiliar to me.

I only need a small amount of Flash probably 128 bytes would be more than enough. Is there a quick explanation (or even a longer explanation) of how I might accomplish my goal?

2 REPLIES 2

The minimum size tends to be dictated by the size of the sector/page.

One would typically place the configuration data at the end of memory in a device that has uniform sector/page sizes, as I recall the F0 is one of these, the F2/F4/F7 are not.

Putting it at the end allows you to debug/recompile your firmware, and not have it touch your data.

Typically I'd check the integrity of the configuration data, and have default data/settings to use if it were corrupt/blank, and update the data there where necessary.

Shrink the size the .LD file sets for the FLASH size by the amount you want to use. This isn't critical, but will cause the linker to throw an error if the rest of your code crashes into it.

The linker script is a GNU/GCC thing, not super well documented, but common in theme to Scatter Files, and linker scripts from earlier tools.

Basically defines the area of memory in the device you want to make visible to the linker, and then associating them with content, and where to stage data that will need to be copied from non-volatile (FLASH) in to volatile (RAM), ie things like statics, BSS, constructors, etc.

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

First check the Flash memory characteristics and endurance tables in the datasheet. Erase time is up to 40 milliseconds. During this time the MCU core is halted, no code runs. Endurance is 10k cycles, meaning that it isn't guaranteed to hold data reliably after 10000 erase/write cycles. If these parameters are not acceptable, forget the internal flash, and get an $0.03 external one.

Look up the Flash memory organization chart for your MCU in the reference manual. The page size is critical, it's the smallest amount of memory that can be erased. You need at least 2 pages (4 kbytes), otherwise all your data will be lost if the power goes away while rewriting.

There is extensive documentation of the .ld linker script file format, but you won't really need all that. You know how much flash your MCU has, the line beginning with FLASH should cover it all, decrease the LENGTH and it's done. Don't touch the rest of the file. I don't think it will be regenerated, but save a copy just in case.