This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How to write to program memory at runtime

I am using few keys that are 20 byte arrays, these are unique for each chip. Right now they are hardcoded.  I don't want to recompile for each device. I want to write to these variables via ble. What is the way of finding their addresses? and How can i write to these addresses on flash. (if possible). Thanks a lot in advance. 

  • You can find their addresses in the linker memory map after you compile, or debug the code and find them that way. You can easily write the flash by following the steps in the manual for writing to flash, unlock the nvmc controller, write the data, lock it up again. Remember to leave the default as all 1s (0xffffff...) as flash can only be written FROM 1 TO 0 without erasing the whole page. You're probably easier doing this via a programmer (JLink) after you flash the image than doing it via BLE, if it's a one-time thing which you set when you build the module. 

    Usually small one-time data like that is written to the UICR as part of the programming process and then copied out at runtime, it's not quite so usual to change the written image, but you can. 

Related