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.

MSPM0C1104: Memory map location (to prevent self-write of flash from overwriting program memory)

Part Number: MSPM0C1104

Tool/software:

I'm using the MSPM0C1104 chip, and I'm needing to have the program write to it's own flash. 

I found the examples in the New Project window of CCS that show how to write using the DL_FlashCTL_{...} functions.

The information that I can't find is: How do I manage the memory map to prevent the flash writes from accidentally overwriting program memory?

Ideally, I'd like to:

  1. Designate a block of flash that the build tools won't write to
  2. Raise an error in case my compiled program won't fit into the smaller flash space now that the block has been designated

Thanks,
Seth

  • Hi Seth,

    Sorry for the delay in my response.

    I will have an answer for you shortly.

  • Hi Seth,


    First, to protect a range of FLASH memory from rouge self erase/programming by the application, consider using the Dynamic Write Protect method described in section 5.4 in the TRM. It will allow the application to write protect one or more sectors from accidental erase/programming operations by the CPU.  When the application needs to modify the write protected sectors, disable write protect (modify R/W bits in CMDWEPROTA register) and modify the sectors. At the end of the programming/erase cycle the write protection is automatically re-enabled. This is how it is used in EEPROM emulation.

    BTW, There is an example of this in the MSPM0 SDK.  You can find it in the folder:
    C:\ti\mspm0_sdk_2_04_00_06\examples\nortos\LP_MSPM0C1104\driverlib\flashctl_dynamic_memory_protection


    Next, to protect the sector from external programming/erase (XDS100), locate your sector(s) that you want to preserve as the last sectors in FLASH. For example, in the mspm0c1104.cmd linker script file, FLASH is defined at address = 0x00000000 with length = 0x00004000 and we want to protect the last sector in FLASH memory.  So borrow one sector from the main FLASH memory region and call it EEPROM and locate it at 0x00003C00 and length = 0x00000400.  BTW, 1 sector size is 0x00000400. Since you will be modifying the linker file,  the EEPROM must be assigned to a "memory section" so create one and name it .calib (calibration) for example.  Now this symbol can be used by the compiler/linker to represent the start address of the EEPROM memory.  See below how to make the changes.

    Then, you will want to set the XDS110 debugger Erase configuration to limit the programmer to erase only the area of FLASH from 0x00000000 to 0x00003C00 as seen here, leaving 0x00003C00 through 0x00004000 untouched.

    Last, in your code you can access this memory section by declaring your variable as follows and passing the address of this variable, cast as an uint32_t to the DL_FlashCTL functions.

    /*
     * Assign the eepromCalibrationTable to the memory section ".calibration"
     * that was created in the linker .cmd file.
     */
    __attribute__((section(".calibration")))
    uint32_t eepromCalibrationTable;
    
    int main(void)
    {
        SYSCFG_DL_init();
        ...
        ...
        ...
            /*
             * Ensure proper flash command execution by clearing the STATCMD
             * register before executing a flash operation
             */
            DL_FlashCTL_executeClearStatus(FLASHCTL);
            DL_FlashCTL_unprotectSector(
                FLASHCTL, (uint32_t) &eepromCalibrationTable, DL_FLASHCTL_REGION_SELECT_MAIN);
    
            /* Program to flash in main memory */
            gCmdStatus = DL_FlashCTL_programMemoryFromRAM32(
                FLASHCTL, (uint32_t)&eepromCalibrationTable, &gData32);
                ...
                ...

    Example project attached.

    flash_dynamic_memory_protection_modifed_linker_MSPM0C1104.zip

  • Hi Dennis,

    Thanks for the thorough response.  The project you attached works great. 

    For my own education, could you let me know what steps I need to perform to use a linker file that is outside of the Debug directory?  I tried comparing the various project files that reference device_linker.cmd, but they are all in the auto-generated Debug directory.  I also tried moving the linker file out of the Debug directory, and when I build the project, it generates a new linker file which results in an error. 

    FYIs for anyone reading this later:

    • The attached project has a range of 0->0x3BFF, but the range in Dennis' screenshots works as well (where the length and the origin of the next line are the same value).  Thankfully, the linker is smart enough to throw errors if ranges overlap and such. 
    • The Erase Configuration is found in Project Properties -> Debug -> Category = MSPM0 Flash Settings. (It wasn't obvious where to find it initially.)
  • Hi Dennis,

    One minor correction to the post:

    The Erase Configuration should be 0x0 - 0x00003BFF.  When I include 0x00003C00, it erases the data stored in that section.  

    Thanks,
    Seth