Store constant variables in Flash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-05-31 11:54 AM
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 #ramSolved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-05-31 12:38 PM
static const unsigned char foo[] = { 0x01, 0x02, 0x03, 0x04 };
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-05-31 12:38 PM
static const unsigned char foo[] = { 0x01, 0x02, 0x03, 0x04 };
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-05-31 03:16 PM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-05-31 03:24 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-05-31 06:01 PM
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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-06-01 02:28 PM
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
:6.2.2 Linkages of identifiers
If the declaration of afile scope
identifier for an object or a function contains the storage classspecifier static, the identifier hasinternal linkage
.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 theentire
execution of the program
and its stored value is initialized only once, prior to programstartup.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-06-01 06:44 PM
>>
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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-06-02 12:18 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-06-07 09:16 AM
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))) = {