Skip to main content
JCuna.1
Senior
December 19, 2021
Question

Acces and modify a constant in flash memory

  • December 19, 2021
  • 7 replies
  • 3386 views

I have the following trouble:

I am using a radio which needs a address and channel parameters, this is given in production phase. So I am planning to flash every board with this parameters modified each time before the board is flashed.

This parameters are loaded to the radio only the first time. Which means, after a reset the parameters should not be ​loaded.

So my idea is create 3 constant in code:

const uint8_t first_time = 0xff;

const uint8_t address = 0;

const uint8_t channel = 0;

My code at the beginning will ask for first time in order to recognize the 0xff value.

If(first_time == 0xff)

Then read parameters address and channel and load to radio.

Modify using half flash program the const first_time with 0 value, so the next time this process wont be execute.

The parameters address and channel will be changed by modifying the binary.​

Is this viable? ​

This topic has been closed for replies.

7 replies

MM..1
Chief III
December 19, 2021

This isnt good idea, but doable.

Compiler use your constants directly in code and when is used on more as one place then modifying binary will not work.

More better is use EEPROM emulation in free page on flash. Read appnote

And how you plan read parameters address and channel and load ...???

JCuna.1
JCuna.1Author
Senior
December 20, 2021

EEPROM emulation need at least two sector because this needs perform a erase operation. It is only available for sectors. So in stm32f411 is not viable ​

MM..1
Chief III
December 20, 2021

No work ok too with only one sector

Tesla DeLorean
Guru
December 19, 2021

Not really viable.

The Flash sectors are typically large and need erasing to rewrite content. Typically one would journal things across the larger page, and find the most recent entry.

For configuration, perhaps a larger structure, where defaults are configured on first usage. Or pushed in via final production test, etc.

Partition the Flash memory to free up the smaller sectors for this type of usage. Establish a location for the configuration structure.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
TDK
Super User
December 19, 2021

On most families this is viable. On some families flash can only be written once, even if it's 0xFFs.

"If you feel a post has answered your question, please click ""Accept as Solution""."
VAlar.1
Associate
December 21, 2021

I think I am having this problem with the STM32H723 MCU.

I am writing a flashword (32bytes) on the flash with some bytes to 0xFF. After a time, I want to change the value of the 0xFF bytes but I get a ECC double detection error.

But with a STM32F4 MCU I can do it.

So is it not possible on H7 family to write twice the same flashword without erasing it? Either to write with 0's??

Thank you for your help

TDK
Super User
December 21, 2021

> So is it not possible on H7 family to write twice the same flashword without erasing it? Either to write with 0's??

Correct.

https://www.st.com/resource/en/application_note/dm00623136-error-correction-code-ecc-management-for-internal-memories-protection-on-stm32h7-series-stmicroelectronics.pdf

"If you feel a post has answered your question, please click ""Accept as Solution""."
KnarfB
Super User
December 20, 2021

> This parameters are loaded to the radio only the first time. Which means, after a reset the parameters should not be ​loaded.

​You could find one bit in the option bytes and (ab)use that as an indicator for the first boot and reprogram it afterwards.

hth

KnarfB

JCuna.1
JCuna.1Author
Senior
December 20, 2021

Stm32f411 in option bytes for user just have a bit which is called not used. I am not sure if I can use it for my purpose.​

Pavel A.
Super User
December 20, 2021

As TDK wrote, "On some families flash can only be written once, even if it's 0xFFs"

I confirmed this on STM32H743. Defined a big enough array in flash data initialized with FFs, indeed rewriting it does not work.

No error is returned, the data just is not written.

So, your "patch" should be placed outside of the flash image.

For example, you can define in the linker script a NOLOAD section at very end of the flash data. Then add the following to your code:

__attribute__((section("flash_patch"), align(32)))
struct {
 char init_done;
 .............
 .........
} config;
 
 
bool write_patch_once()
{
 if (config.init_done == 0xFF) {
 // program your data into the config struct
 }
}

JCuna.1
JCuna.1Author
Senior
December 20, 2021

This is a good idea. Probably i will try this.

waclawek.jan
Super User
December 20, 2021

> On some families flash can only be written once, even if it's 0xFFs.

ECC? But then just leave the given area erased and unprogrammed (this boils down to ability to control content of .hex and any utility used for programming). And it also implies a write granularity. IMO it's documented in RM for every family where relevant. My copy of RM0433 says, for example,

This mechanism uses 10 ECC bits per 256-bit Flash word

so you'd need to set aside well-aligned 32 bytes for every one-time-programmable item (I assume here both address and channel could be written at once, so that's only one such area).

In other words, it does not mean it's not viable, just potentially cumbersome.

Btw. 'F0 and 'F3 have two halfwords available for the user in Option bytes (confusingly NOT the field called USER, but Data0 and Data1).

JW

JCuna.1
JCuna.1Author
Senior
December 20, 2021

I am using stm32f411. There is one opt byte with name not used. I am not sure if I can use it for my purpose.

Piranha
Principal III
December 20, 2021

Can you read those address and channel parameters from the radio? If yes, you can just read them at every boot and, if they are not at desired values, then reprogram.

Another bonus of this approach is that, if at some point the device is repaired and the radio is replaced, then the MCU will reprogram the new radio automatically.