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.

CCS/MSP430F5438A: Why is a const array located in RAM?

Part Number: MSP430F5438A


Tool/software: Code Composer Studio

Hello all,

I have just created the default example project at the Code Composer Studio (Version: 9.3.0.00012), and added a array Variable like this:

#include <msp430.h>
const unsigned long array[5000]; /* why is this in RAM ??? */

/**
* main.c
*/
int main(void)
{
int var=0;
static unsigned long a;
WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

for (; var < 500; ++var) {
a = a + array[var];
}

return (int)a;
}

Now I am kind of wondering why the variable is put to the RAM, because it is actually a constant. The error message is clear:

"../lnk_msp430f5438a.cmd", line 139: error #10099-D: program will not fit into available memory. run placement with alignment fails for section ".bss" size 0x4e24 . Available memory ranges:
RAM size: 0x4000 unused: 0x3f60 max hole: 0x3f60

My compiler flags are this:

-vmspx --code_model=large --data_model=large -Ooff --opt_for_speed=1 --use_hw_mpy=F5 --include_path="C:/tools/ti/ccs/ccs_base/msp430/include" --include_path="C:/projects/workspace_v9/MSP430F5_Test" --include_path="C:/tools/ti/ccs/tools/compiler/ti-cgt-msp430_18.12.4.LTS/include" --advice:power="all" --define=__MSP430F5438A__ -g --c99 --printf_support=minimal --diag_warning=225 --diag_wrap=off --display_error_number --silicon_errata=CPU21 --silicon_errata=CPU22 --silicon_errata=CPU23 --silicon_errata=CPU40 --asm_listing --verbose

And of course the Linker flags:

-vmspx --code_model=large --data_model=large -Ooff --opt_for_speed=1 --use_hw_mpy=F5 --advice:power="all" --define=__MSP430F5438A__ -g --c99 --printf_support=minimal --diag_warning=225 --diag_wrap=off --display_error_number --silicon_errata=CPU21 --silicon_errata=CPU22 --silicon_errata=CPU23 --silicon_errata=CPU40 --asm_listing -z -m"MSP430F5_Test.map" --heap_size=160 --stack_size=160 --cinit_hold_wdt=on -i"C:/tools/ti/ccs/ccs_base/msp430/include" -i"C:/tools/ti/ccs/ccs_base/msp430/lib/5xx_6xx_FRxx" -i"C:/tools/ti/ccs/tools/compiler/ti-cgt-msp430_18.12.4.LTS/lib" -i"C:/tools/ti/ccs/tools/compiler/ti-cgt-msp430_18.12.4.LTS/include" --reread_libs --diag_wrap=off --display_error_number --verbose_diagnostics --warn_sections --xml_link_info="MSP430F5_Test_linkInfo.xml" --use_hw_mpy=F5 --rom_model

What is the reason to put constants to RAM? I haven't seen that before, and don't want to create ROM sections to put my variables in all the time. Is there a generic linker switch/flag to put const variables automatically into ROM? 

Thanks for your help!

Marco

  • Initialize even one element of the array, like this ...

    const unsigned long array[5000] = { 0 };

    ... and the compiler will put the array in the section .const.  The .const section is allocated to flash memory.

    Thanks and regards,

    -George

  • Marco Steinborn said:

    I have just created the default example project at the Code Composer Studio (Version: 9.3.0.00012), and added a array Variable like this:

    #include <msp430.h>
    const unsigned long array[5000]; /* why is this in RAM ??? */

    To follow on to what George wrote, your array isn't initialized, so it's not of much use.

  • Also, the linker is slave to what you tell it to do by means of the .cmd command file.

    Tell it to allocate .const to a RAM segment and it will put it there regardless of the type of memory.

    lnk_msp430f5438a.cmd said:

        .const      : {} > FLASH                /* Constant data                     */

    --Cheers

  • The linker file lnk_msp430f5438a.cmd has those sections where I use the large data model:

    #ifndef __LARGE_DATA_MODEL__
    .const : {} > FLASH /* Constant data */
    #else
    .const : {} >> FLASH | FLASH2 /* Constant data */
    #endif

    Even though the linker is not doing what I tell. :-) 

    "../lnk_msp430f5438a.cmd", line 139: error #10099-D: program will not fit into available memory. run placement with alignment fails for section ".bss" size 0x4e24 . Available memory ranges:
    RAM size: 0x4000 unused: 0x3f60 max hole: 0x3f60

    const unsigned long array[5000];

    Same result when I use the gcc linker

    makefile:144: recipe for target 'MSP430F5_Test.out' failed
    c:/tools/ti/ccs/tools/compiler/msp430-gcc-8.3.1.25_win64/bin/../lib/gcc/msp430-elf/8.3.1/../../../../msp430-elf/bin/ld.exe: MSP430F5_Test.out section `.bss' will not fit in region `RAM'

    For me it does not make sense to put a constant into RAM,  does not matter if it is initialized or not. I still don't understand the logic behind.

  • Uninitialized global data is implicitly initialized to 0, so must be placed in .bss so that the startup code can zero its contents.

    "const" is merely a guideline for the compiler and does not indicate that the variable must be placed in read-only memory.

    The compiler is free to optimize out non-volatile variables where it knows they are either unused, or its values at the points it is used are known, so you should not think of variables used in your programs as being tied to any particular memory area.