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.

Accessing & defining variable in RAM at particular addresss

Other Parts Discussed in Thread: MSP430G2553

Hi,

I want to define particular RAM address in MSP430G2553, lets say 0x200h.

How to store -any variable at this point & access it again.

Is it by:

data = (unsigned int *)(0x220);                 //0x220 is RAM address


  • Hello Aamir,

    yes this is correct, you are not defining a Variable with this, you just directly access RAM
    data has to be a pointer of type unsigned int in this case:

    unsigned int
    *data = (unsigned int*)0x220;

    Then you can modify the memory with a sizeof(unsigned int) by dereferencing the data pointer:

    *data=123;

    following should also work, without defining a pointer:

    *((unsigned int*)0x220)=123; // write 123 to address 0x220

    But I would not recommend to use this pointer feature of C for RAM,
    as you can get easily into hot water with this.

    E.g. if you do not tell the compiler/linker not to use this RAM,
    it may catch exactly this address for own vars.

    Just for interest: What are you trying to do?


    Regards Marco
  • Hi Aamir,

    If you want to put a variable in a specific location in RAM so that you can always know exactly where it is for some reason, it is a little different depending on if you are using CCS or IAR.

    CCS:

    1.  First, you need to modify your linker command file lnk_msp430g2553.cmd. (I usually make a copy of the normal command file, exclude that from building in my project, then rename the one that I'm modifying to be something like lnk_msp430g2553_modified.cmd so that I won't get them mixed up).

    a. You need to set aside enough room in your memory map for your variable in RAM. First find the RAM in the memory map at the top of the linker file and comment it out. You're going to make a new area in RAM, you can name it whatever you like. For example:

    said:

    //RAM : origin = 0x0200, length = 0x0200

    MY_RAM: origin = 0x0200, length = 0x0002 //area of RAM for one unsigned int variable

    RAM: origin = 0x0202, length = 0x01FE //rest of RAM, modified starting location and length to allow for my section

    If you want to store something bigger, like an array, just make sure to adjust the numbers to provide the right amount of room for your data.

    b. You need to specify what's going to be allocated for that area in memory. Scroll down to the "SECTIONS" area of the linker file. Above the .infoA etc. allocations, add this (again, you can call your section whatever name you want):

    said:

    .MY_MEM : {} > MY_RAM //place section MY_MEM in area MY_RAM

    2. In your main application code .c file where you declare this global variable data at the top of the code, do this to place your variable in the section you just put in RAM.

    said:

    #pragma DATA_SECTION(data, ".MY_MEM")

    unsigned int data;

    3. Make sure that when you build ccs is using your modified linker file. Project > Properties > General, the Linker command file listed should be your modified one, if it's not, change it to use that one.

    IAR:

    1. First, you need to modify your linker command file lnk_msp430g2553.xcl. (I usually make a copy of the normal command file, exclude that from building in my project, then rename the one that I'm modifying to be something like lnk_msp430g2553_modified.xcl so that I won't get them mixed up). All you need to do is add a line under placement directives to specify where in RAM you want to put your data.

    said:

    // ---------------------------------------------------------
    // Placement directives
    //

    -Z(DATA)MY_MEM=0200-0202 //Area in RAM for the data

     

    If you want to store something bigger, like an array, just make sure to adjust the numbers to provide the right amount of room for your data.

    2. in your main application code .c file where you declare this global variable data at the top of the code, do this to place your variable in the section you just put in RAM.

    said:

    #pragma segment="MY_MEM"

    #pragma location="MY_MEM"

    __no_init unsigned int data;

    3. Make sure IAR is using your modified linker file. Project > options > Linker, under Linker configuration file check the "override default" box and then browse to your modified file.

    Regards,

    Katie

**Attention** This is a public forum