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.

Compiler/CC2642R: Volatile DATA_SECTION Being Optimized Away

Part Number: CC2642R


Tool/software: TI C/C++ Compiler

We have a workspace that combines a CC2642R with an MSP430.  The CC2642R has a custom bootloader that can rewrite the application code.  The application code then can reprogram the MSP430 via Spy-Bi-Wire.  All three code projects (MSP, CC2642 boot, CC2642 app) are built together and a single image is created with the bootloader as the master, allowing us to program a single file to the device; the application code is just an object placed in flash, and it programs the MSP with another object manually placed in flash.  Both of these flash objects have sections declared in the linker command file:

SECTIONS
{
  .intvecs        :   >  BOOTLOAD_BASE

  /* Allocate Sections for Flash Space */
  .text           :   >> FLASH_LAST_PAGE
  .const          :   >> FLASH_LAST_PAGE
  .bootloader     :   >> FLASH_LAST_PAGE

  /* Allocate Sections for CC2642 Update Image Space */
  .update_image   :   >> UPDATE_IMAGE

  /* Allocate Sections for MSP430 Update Image Space */
  .msp_image      :   >> MSP_IMAGE

...

And both images are included in header files in the bootloader project:

// Main Application Image
#pragma DATA_SECTION(update, ".update_image") // Force this into specific section of memory
const uint8_t update[] =
{
	// App Contents
    0x00, 0x3C, 0x01, 0x20, 0xF5, 0x73, 0x01, 0x00, 
...

// MSP430 Image
#pragma DATA_SECTION(msp_image, ".msp_image") // Force this into specific section of memory
const volatile uint16_t msp_image[] =
{
    // @f000
    0x40B2, 0x5A80, 0x0120, 0x93F2, 0x10FF, 0x2408, 0x43C2, 0x0056, 
...

The CC2642R application image works fine, as the bootloader is repsonsible for it and references it in the code.  The MSP430 image, however, is getting optimized away since the application code actually handles it, and the bootloader code never makes direct reference to it.  The .msp_image section is empty in the Memory Allocation view.  If we add a throwaway line of code that references msp_image[0] in an IF statement, the image is properly placed in memory, but this is kludgy and shouldn't be necessary.  Is there a way to tell the compiler/linker that this data needs to be there even if the code doesn't reference it?  I would have thought declaring it as volatile would do this, but that didn't help.

  • Wait, I may have just figured it out.

    #pragma RETAIN(msp_image)
    #pragma DATA_SECTION(msp_image, ".msp_image") // Force this into specific section of memory
    extern const volatile uint16_t msp_image[] =
    {
        // @f000
        0x40B2, 0x5A80, 0x0120, 0x93F2, 0x10FF, 0x2408, 0x43C2, 0x0056, 
    ...

    Adding the RETAIN pragma seems to do the trick.  Is there a way to add that directive directly to the linker command file for the .msp_image section?

  • Chris Hunt said:
    Is there a way to add that directive directly to the linker command file for the .msp_image section?

    Yes.  Please search the MSP430 assembly tools manual for the sub-chapter titled Retain Discarded Sections.

    Thanks and regards,

    -George

  • I think you may have misread.  This question is about Part Number CC2642R.  The object being placed in memory is a pre-built MSP430 image, but it's being placed into a CC2642R that will program the image onto the other chip via Spy-Bi-Wire.  The linker in question is for the CC2642R when building its bootloader project.  I actually did consult the Arm Optimizing C/C++ Compiler User's Guide, which is where I learned the #pragma RETAIN.

    I believe I've figured this out now as well, and it ended up actually being similar to how the MSP does it, though there are some slight differences.  This should have been included in the User's Guide for the ARM.  For anyone else who may be curious, there are a few different ways, using my msp_image array object placed in the .msp_image memory section as an example:

    1. Use #pragma RETAIN(symbol_name) in C code when declaring the symbol, as above.  Used to retain each object individually no matter where it's placed.
    2. In the project's Properties, go to Build -> ARM Linker -> Advanced Options -> Symbol Management, and under "Specify symbols/sections to be retained by linker (--retain)", add the name of the symbol; e.g., "msp_image" without the quotes.  This also retains the object no matter where it's placed.
    3. In that same menu, add "*(.msp_image)" without the quotes.  This retains every object placed in the .msp_image section without having to list them individually, which includes my msp_image array.
    4. In the linker command file, add the line "--retain=*(.msp_image)" without the quotes.  This can be done before or after the section allocation list apparently; just needs to be in the file somewhere.  This also retains every object placed in the .msp_image memory section without having to list them individually.