Showing results for 
Search instead for 
Did you mean: 

volatile const

Ddu P
Associate III

I need to declare a 'volatile const', meaning that the value is placed in FLASH and that it is re-read every time it is referenced (as the FLASH is written to elsewhere), but am finding it very difficult to accomplish this as the compiler either places this in RAM or optimises it so that the FLASH is not read every time.

volatile const uint8_t mcau8VT_SP_Backup[] __attribute__((aligned(4))) = {0xFF, 0xFF, 0xFF, 0xFF};

The compiler gives this a 'Run Address (VMA)' of 0x20000000 (i.e. RAM) and a 'Load Address (LMA)' in FLASH.  

const uint8_t mcau8VT_SP_Backup[] __attribute__((aligned(4))) = {0xFF, 0xFF, 0xFF, 0xFF};

In this case the 'Run Address (VMA)' is in FLASH but the compiler "optimises" things so that the value is not read from FLASH every time and does not update when the FLASH is written to.

I have tried every combination of suggestions which I can find online including adding an attribute trying to specify which section the const should go in (.rodata), adding additional const keywords so that the "pointer" is const as well (and not just the "data"), and placing the consts in an external file.

How can I get this to be placed in FLASH and re-read every time, so that it sees updates made to the FLASH elsewhere?


Surely there are better approaches to this?

Like using memcpy() or a single function to access and return content?

How often is content actually changing?

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

I'm open to anything.

I basically need to get the compiler to reserve some memory in FLASH and make sure that it is all 0xFFs so that it can be written to. I then need to know the address of that so that I can read/write it.

It only changes once.

Ddu P
Associate III

I ended up solving this by creating a linker section for the volatile consts. This is certainly not easier / cleaner / "better" - compiler which I ported this code from handled it correctly - but it's working / a solution for now. Would be great if the compiler could just be made to treat a volatile const as just that :-).