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.

How to assign an address in ROM to a variable?

Hi,

 

I'm using CCS 5.2 and I need to store a variable at a specific address. I read the forums and the pragma directives in the compiler guide. I followed the instructions but things are not working. This is what I'm doing:

                  In the linker cmd file:

                  MEMORY
                  {
                      ...
                      MYFLASH : origin=0x00000020 length=0x00000020
                  }
                  SECTIONS
                  {
                      ...
                      .mySection : {} > MYFLASH
                  }
                  In a source file:
                  #pragma DATA_SECTION(var ,".mySection")
                  int   var  =  0xBB;

 Please, let me know if you see something wrong or have another way of assigning a variable to a specific address.

Thanks,

  • Scott,

    "int var = 0xBB" defines a variable with initial values. In the code generated by TI compiler, CPU writes initial value to the memory location after booting up. Your code will work if the variable is located in RAM. From your post, it seems that you want to save a constant value to a known Flash location. In order to do so, you need to define the variable as a constant: "const int var = 0xBB". Then you should be able to see the value in Flash after programming your code.

    Thanks and regards,

    Zhaohong

  • Zhaohong,
     
    I've tried your suggestion to save a constant value to a defined Flash section with this code:
    #pragma DATA_SECTION(FlashConstant, "META_DATA");
    const uint32_t FlashConstant = 0xDEADBEEF;
     
    The linker command file looks like this:
    MEMORY
    {
       .
       .
       .
       FLASH_META_DATA (RX) : origin=0x00010020 length=0x00000020
       .
       .
       .
    }
     
    SECTIONS
    {
       .
       .
       .
       META_DATA: {} > FLASH_META_DATA
       .
       .
       .
    }
     
    But it didn't work. Neither I can find the constant value 0xDEADBEEF in Flash at address 0x10020 nor in .out file.
    Is there anything else I'm missing?
     
    Best Regards
    Christian
  • Hi,
     
    I've found the Problem. I must use the defined constant somewhere in code, otherwise the Compiler will remove it.
     
    Can I tell the compiler not to remove the constant without using it in code?
     
    Thanks
    BR
    Christian
  • Hi Christian,

    To tell the compiler not to remove the unused constants go to:

    project -> show build settings -> ccs build -> ARM linker -> advanced options -> link time optimization -> eliminate sections... and set to: Off

    Regards,
    Elena

  • Hi Elena,
    now it works without my workaround.
    Thanks for your answer.
    Regards,
    Christian