This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Is there a way to place variable into a specific ram location?

Hello,

I am using IAR embedded workbench for MSP430. I want to place a global array of ints with 10 elements, at certain RAM loacation. I read about #pragma location directive and @ operator, but they are used for only non-volatile memory spaces. Thank you in advance!

  • Hi Radoslav,

    Is there a reason for placing the array at specific absolute addresses? Maybe they are accessed by another program?

    Looking in the IAR compiler guide, you can use the location pragma or @, but it has to be either const or a __no_init variable, meaning that it will not be automatically initialized at first startup. This is intended for cases where maybe you are accessing something that was set by some other program/project, so there needs to be no init for the variable in the C-startup code in this project because it would overwrite the value. You would do this like this:

    #define ARRAY_SIZE 10
    #pragma location=0x202
    __no_init unsigned char count_array[ARRAY_SIZE];

    Or

    #define ARRAY_SIZE 10
    __no_init unsigned char count_array[ARRAY_SIZE] @ 0x202;

    If for some reason you want to specify a starting value for your array (e.g. you want it to be zero-initialized), you would have to add code to zero it out at startup.

    Regards,

    Katie

  • " you would have to add code to zero it out at startup"
    Could you please show me an example of such initialization code?
  • It can just be a loop to set all array values to 0 (or to whatever constant default starting values you want them to have). For example, you could even do this in __low_level_init so that it is initialized before you get to main:

    #include <intrinsics.h>
    
    int __low_level_init(void)
    {
        WDTCTL = WDTPW | WDTHOLD;
        unsigned char i;
        for(i = 0; i < ARRAY_SIZE; i++)
            count_array[i] = 0;
        return 1;
    }

**Attention** This is a public forum