Memory management - How storing constants in PRAM space?

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
droll
Posts: 48
Joined: 20 Dec 2013 01:37

Memory management - How storing constants in PRAM space?

Post by droll »

How can I store constants (mainly big tables) into the program memory space to keep the 2kB data RAM available for the stack and variables?

Background information about my question:
I use in my application a large constant lookup table (a font table) that is placed by the compiler into the variable space (reduced to 5 bytes for the example here):

Code: Select all

unsigned char LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
I tried to declare as constant and or static constant. However this doesn't change anything; the compiler continues to place them into the variable space:

Code: Select all

const char LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
static const unsigned char LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
So I tried using some known 8051 compiler directives to force the compiler using the program memory space, but all the following declarations are generating compilation errors:

Code: Select all

static const char CODE LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
CODE static const char LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
static const char code LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
code static const char LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
__code unsigned char LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
unsigned char __code LuTable[] = {0x10, 0x21, 0x31, 0x41, 0x50};
Is there another compilation directive or a trick that can be used to select the program memory space?
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Memory management - How storing constants in PRAM space?

Post by PoltoS »

Please check this: https://github.com/Z-Wave-Me/Z-Uno-Core ... /main.c#L4
__code is what you search for. But it DOES NOT work in Aruidno code - it will be ignored.

You can do it directly in main.c, but this is a bit urgly.

You can also try to place a .c file in the sketch folder. And include .h with the same name as .c (create empty .h)

Or even define it via string (using escape chars) - compiler will place it in code space.
droll
Posts: 48
Joined: 20 Dec 2013 01:37

Re: Memory management - How storing constants in PRAM space?

Post by droll »

Adding the data into main.c is indeed ugly, and I encountered some troubles to add it into a .c file and to make the data accessible then from the Arduino main program. But your last suggestion to define it as string seems to work, if the definition is well done in the following way:

Code: Select all

const char *LuTable = "\x10\x21\x31\x41\x50";
However I encounter already the next problem related to the Z-Uno C compiler: Adjacent string literals should be concatenated by C compilers, which allows splitting long strings nicely over multiple lines:

Code: Select all

const char *LuTable = 
   "\x01\x02\x03\x04\x05"
   "\x06\x07\x08\x09\x0A"
   "\x0B\x0C\x0D\x0E\x0F";
The Z-Uno C compilers accepts this syntax, but the captured character sequence is different from the following sequence that should be equivalent to the previous one:

Code: Select all

const char *LuTable = "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F";
This is really painful - It takes so much time to figure out these Compiler limitations or bugs!
Post Reply