cancel
Showing results for 
Search instead for 
Did you mean: 

Store constant variables in Flash

Mohammad A
Senior
Posted on May 31, 2017 at 20:54

I am trying to figure out how to store a constant variable (a constant string of chars) on flash, so it does not occupy valuable blocks of RAM to store a const. In AVR there was something called PROGMEM, but here I can't find any similar method to do so.

How can I store a constant array of chars in the Flash memory?

#variable #flash #ram
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on May 31, 2017 at 21:38

static const unsigned char foo[] = { 0x01, 0x02, 0x03, 0x04 };

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

View solution in original post

8 REPLIES 8
Posted on May 31, 2017 at 21:38

static const unsigned char foo[] = { 0x01, 0x02, 0x03, 0x04 };

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 31, 2017 at 22:16

shouldn't I only use const without static keyword? because when I use const without static, I can find my variable inside map file in .rodata and stored inside FLASH section

 .rodata 0x08003be4 0x8 Src/main.o
 0x08003be4 constvar�?�?

but when I add static keyword, I can't find constvar in the map file.

Actually what does static do here if only adding const makes the variable to be stored in the flash section?

Posted on May 31, 2017 at 22:24

static (in that context) just means that the data isn't visible outside that file. I guess as a consequence it won't show in the map file.

Posted on June 01, 2017 at 01:01

I tend to use static, especially when used inside functions, because it limits scope and doesn't copy.

Scope outside the file can often let the linker make decisions about placement if some other files has some looser definition of the data's attributes.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 01, 2017 at 21:28

Unfortunately you are causing some confusion with the static keyword here. Let me clear things up.

The static keyword in C has two meanings as

storage-class specifier

:
  1. 6.2.2 Linkages of identifiers

    If the declaration of a

    file scope

    identifier for an object or a function contains the storage class

    specifier static, the identifier has

    internal linkage

    .
  2. 6.2.4 Storage durations of objects.

    An object whose identifier is declared with external or internal linkage, or with the

    storage-class specifier static has static storage duration. Its lifetime is the 

    entire

    execution of the program

    and its stored value is initialized only once, prior to program

    startup.

One

means that identifier declared at the top of the file is only available in that specific file, internal linkage. The counterpart is extern, which declares that the original identifier is somewhere else. Which will throw you a warning when used on an static identifier.

Eg: You use this to protect variables in foo.c that are not to be used outside foo.c.

Two

means that an identifier inside a block (any code within { }) with static will only be available in that block, but will be persistent for the entire lifetime of the application.

Eg: You use this in

https://stackoverflow.com/questions/717725/understanding-recursion

functions to limit the depth of recursion. (and thus stack size) 

These are described in ISO/IEC 9899. Maybe my version not up-to-date, but these core functions of C will not change.

Posted on June 02, 2017 at 01:44

>>

Unfortunately you are causing some confusion

...

Perhaps, but I'm not confused.

My use case is born out of specific compile/linker failure cases (yes stuff where the standards are at odds with the facts on the ground). The code snippet provided has an exceedingly high chance of placing the data in ROM/FLASH over a very wide range of tools, despite bugs and quirks they might otherwise have. I've got at least one example where constant tables in a function are copied onto the stack at each invocation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 02, 2017 at 07:18

Perhaps, but I'm not confused.

Let's hope not no!

The advantage of using the static keyword on const is that it explicitly states: '

Its lifetime is the 

entire

execution of the program'

, so that the identifier is there, even if not used.

Viktor POHORELY
ST Employee
Posted on June 07, 2017 at 18:16

One example from Keil project to force array at given address. You could force it similary to generic address as well.

variable_t volatile array_storage[NUMBER_OF_RECORDS] __attribute__((at(FLASH_ADDRESS))) = {