Reserving space in Code Segment for Data

I am porting an application that is heavily dependent on being able to store data in address range 0x101000 to 0x1010ff and also in range 0x10D800 to 0x10FDFF.

I made the following declarations in my Main.c

#pragma location = 0x101000

const unsigned char cProvDataTable[256];

#pragma location = 0x10D800

const unsigned char cIndexTable[1024];

These spaces are linked at the appropriate addresses and I can read and write to the address range via my FLASH routines. The old app references the addresses directly (I know it sucks but it is not my app, I am just hired to port it).

The problem becomes if I enable Optimization, the linker detects that I am not really using these and optimizes them away and links code into these address ranges.

thoughts?

It depends heavily on your compiler (#pragma’s are compiler-specific anyway) so have a real good look in the compiler documentation.

What compiler are you using anyway?

For GCC, I think you can specify your own section in the link file and then use an attribute in the source files to put a specific variable in that section. Something like:

const unsigned char cProvDataTable[256] __attribute__ ((section (".provdatatable")));

I have no ready-made example for the linker file.

Here’s an example of an project that modifies the linker file (but for different reasons): http://www.siwawi.arubi.uni-kl.de/avr_p … cc_ramfunc

Try

volatile __no_init const unsigned char cProvDataTable[256];

and make a dummy use of cProvDataTable in your code. There should be some read references to this data area somwhere in your code anyway ?

Yeah, indeed that would be much easier. Simply referencing the data should stop the compiler from optimising it away. So where the data is used, simply try something like

unsigned char *p = cProvDataTable;

instead of

unsigned char *p = (unsigned char *)0x101000;

(Just guessing what your other code looks like)

Thanks I will try some of these on Monday.

I was considering using Segment definitions but there are three or four of these tables and I didn’t want to make things too complicated in the linker file.

I am using IAR’s compiler with ATMEL’s at91sam7s as the target chip.

Amazingly, even with some dummy assignments in the code, IAR’s optimizer eliminates the cProvTable. I will try the “volatile” attribute. I was hoping to avoid writing a bunch of code to make the cProvTable look like it is used.

And as I said, I am hired to do a simple port, so rewriting the code to eliminate the references to 0x101000 and make it look like code written by a software professional is not what my client desires.

GCC has a attribute((unused)) (or something like that) specifically to deal with this situation; if volatile doesnt work then you could try looking for an IAR equivalent.