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: Constants Manually Placed in Flash Are Not Initialized

Other Parts Discussed in Thread: MSP430F6733, MSP430F5438A

This feels like a very basic issue, but I've not been able to find the solution through random experimentation nor searches of this forum. I'm developing an application for the MSP430F6733 using the Code Composer 5.5, and I'm trying to place certain constants in Info Flash using the following trivial code:

#include <stdint.h>

#pragma DATA_SECTION(".infoD")
const uint16_t test = 0x1234;

int main()
{
	while (1) ;
}

When I start the debugger and break at the beginning of main(), infoD does not contain 0x1234. I also tried using #pragma location=0x1800 instead of DATA_SECTION, but that didn't work either. I believe that the constant is getting placed correctly, because I was able to manually change the value of that memory in the debugger and saw the new value reflected in a local variable. However, it's clearly not getting programmed correctly initially.

The Debugger in Code Composer is set to "Erase main, information and protected information memory", so I don't believe this issue is due to a restriction on the debugger. Could somebody point me in the right direction on getting the constant to be initialized correctly in memory?

  • Since there is no reference to "test" in the code, the linker is removing that section because it deems it unused, hence unnecessary. You can use the #pragma RETAIN to indicate that the section that contains that data symbol is not to be removed.

    #pragma DATA_SECTION(".infoD")
    #pragma RETAIN(test)
    const uint16_t test = 0x1234;

    Alternately if there is a reference to the variable in the code, then it will be retained even without the pragma RETAIN. Let us know if this resolves the issue.

  • Sorry, I should have mentioned that I tried both adding a reference and adding "#pragma RETAIN" already. Here is the updated code:

    #include <stdint.h>
    
    #pragma DATA_SECTION(".infoD")
    #pragma RETAIN
    uint16_t test = 0x1234;
    
    int main()
    {
    	while (1)
    	{
    		uint16_t dummy = test;
    		dummy++;
    	}
    }
    

    Notice that I had to remove the "const" keyword, otherwise the compiler would remove the access to the memory location and insert 0x1234 directly into the assembly. While debugging I can change the value at memory location .infoD (0x1800) and see it reflected in dummy; however memory location 0x1800 has a value of 0xFFFF when the firmware is first flashed and the debugger breaks at main().

    My overall goal is to be able to initialize particular flash addresses to constant values; however I can't find a combination of keywords which will accomplish that correctly.

  • I just realized you must be working with a C++ soource file, based on the syntax you are using for the pragmas. Is that correct?

    If so, try using an "extern const" as explained in this post:

    #pragma DATA_SECTION(".infoD")
    #pragma RETAIN
    extern const uint16_t test = 0x1234;

    With a C source file, I could get it working with this:

    #pragma DATA_SECTION(test, ".infoD")
    #pragma RETAIN(test)
    const uint16_t test = 0x1234;

    I tested on a MSP430F5438A target board (different device than the one you are using, but don't think that matters) with both the above C and C++ source files and the value 0x1234 was there at 0x1800 when firmware was flashed and debugger was halted at main.

  • This worked - thanks for the help AartiG!