const variables to ROM

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

const variables to ROM

5,443 Views
Anders
Contributor I
Hi,
I'm new to this forum, but I have been reading quite a few threads in the hope to find some clues as to how I should make const declared variables end up in ROM instead of RAM. I haven't found the answer yet, so I was hoping to get some help here...
 
I'm using CW5.5 and the target is the GC16 (1kB RAM) microcontroller (I'm new to this too...).
 
What I have is something like this:
 
in the .prm file:
SEGMENTS
   RAM = READ_WRITE 0x0C00 TO 0x0FFF; //9S12GC16
   /* unbanked FLASH ROM */
   ROM_C000 = READ_ONLY  0xC000 TO 0xFEFF; //9S12GC16
END
PLACEMENT
   _PRESTART, STARTUP,
   ROM_VAR, STRINGS,
   VIRTUAL_TABLE_SEGMENT,
   DEFAULT_ROM, NON_BANKED  ,
   OTHER_ROM  , COPY            INTO  ROM_C000;  //9S12GC16
   DEFAULT_RAM                  INTO  RAM;
END
STACKSIZE 0x100
 
in my c-file.
static const unsigned char a[ ] = {1, 5};
static const unsigned char b[ ] = {2, 6};
static const unsigned char c[ ] = {3, 7};
static const unsigned char d[ ] = {4, 8};
 
static const unsigned char* p_misc_0[ ] = {a, b};
static const unsigned char* p_misc_1[ ] = {c, d};
 
static const unsigned char** p_misc[ ] = {p_misc_0, p_misc_1};
 
 
In the .map file I then find the following:
  • a,b,c, and d all end up in .rodata (ROM as expected)
  • p_misc_0, p_misc_1, and p_misc all end up in .data (RAM!)
  • All "non const" declared variables end up in .bss (RAM)
I believe that this is in the ELF object file format, and therefore #pragma INTO_ROM cannot be used. (Using this directive will fool the .mcp window to not count the variables as data, but the linker is not buying it!) I've been browsing the CW help for hours, but it just gets me confused, what with all the pragma directives and compiler options available...
 
I'd like to think that there's an easy way to put the const vars in ROM. Any help is much appreciated.
 
Thanks,
Anders.
 
Labels (1)
0 Kudos
Reply
3 Replies

1,188 Views
UK_CF_FAE
NXP Employee
NXP Employee
The declaration CONST should be treated to mean "READ ONLY". It has nothing to do with RAM or ROM, as you found.
0 Kudos
Reply

1,188 Views
KrisRutecki
Contributor III
Hi,
Years ago I had a similiar problem with a 'Cosmic' C compiler.
It turnes out that a declaration:
const char *pcA;
declares a pointer to constant char, what you want is a contant pointer to character which, I think looks like this:
char (const *) pcA;
But, as I mentioned, this was more than 10 years ago... so I can't say for sure if the syntax is right.
The idea is that there is a difference between a pointer to constant and a constant pointer. Please try one of those declarations and let me know which ones work, can't find my old code at the moment...
char (const *) pcA;
const char const *pcA;
char const *pcA;
char (const*) pcA;
char *(const) pcA;
0 Kudos
Reply

1,188 Views
Anders
Contributor I
Thanks!
Your reply solved it. I was actually thinking along these lines earlier today, but eventually gave up. I felt a bit stupid trying to insert a "const" at random places in the declaration. Your post gave me new hope, so I Googled a bit and came up with the following: (Note. The below syntax is not covered by any of your suggestions, which I hope shows that you do not nesessarily have to be stupid not to get it right the first time;-)
 
static const unsigned char a[ ] = {1, 5};
static const unsigned char b[ ] = {2, 6};
static const unsigned char c[ ] = {3, 7};
static const unsigned char d[ ] = {4, 8};
 
static const unsigned char* const p_misc_0[ ] = {a, b};
static const unsigned char* const p_misc_1[ ] = {c, d};
 
static const unsigned char* const * const p_misc[ ] = {p_misc_0, p_misc_1};
 
I think I like that last declaration the best....
 
Again, thanks.
Anders.
0 Kudos
Reply