AnsweredAssumed Answered

Correct to use _RODATA(Flash2) to define "variable" data in flash?

Question asked by M I on Aug 9, 2019
Latest reply on Aug 18, 2019 by Jing Pan

*MCU = MKL28Z512VLL7  (has 512KB flash memory built-in)

*IDE = MCUXpresso

*Debugger = PEMicro Multilink

*Custom target board




I'm starting new & separate question from previous one about writing data to non-volatile memory, specifically MKL28Z's flash memory.


(Sorry I had to take a break from this operation last 1.5 weeks because I got pulled-back to the Touch Sensing operation of the project to deal with "materials" dielectric constants surrounding our touch electrode which cause unwanted self-capacitive touch triggers, besides just finger tip touch...)


Inside MCUXpresso, this image below is my partitioned Memory details, as shown on image.   In MKL28Z, for its built-in 512KB Flash, I keep 508KB for code & just assign 4K for data variables that need to be kept in non-volatile memory for safe-keeping in case of power failure, we don't want to lose latest measurements data.


Technical support mentioned about using a macro called,  _RODATA(Flash2), to define a variable in that section.

This is not self-explanatory to me given an example post about this as also stated on MCUXpresso IDE User Guide.  It is not clear because if I read their statements literally which mention "constant", then that says to me that "RO" means "read-only".  (see



1) Does the "RO" in RODATA mean "read-only"?   I need my variables to be changeable to different values that can be updated whenever statistical measurements of the system are gathered periodically.


2) The posted discussion did not provide the "memory map" as I showed on image above.  And they say some other or additional flash memory block added from other external flash or block.   I got 2 flash sections by simply splitting up what used to be all 512KB  "PROGRAM_FLASH_512" & Aliased "Flash" and changing the address & size range accordingly.   Is their discussion applicable & similar anyway to my situation?


3)  Having no memory map shown like my image above, I'm not sure if the Alias term, "Flash2" is coincidental as used in _RODATA(Flash2).   If "Flash2" is exactly whatever alias or name was assigned for the "other or new" flash memory area to be used for data (or function as posted), then I guess I should also use "Flash2" as in _RODATA(Flash2) ...?


4) The post stated to include as:

#include <cr_section_macros.h>


Include where?  In my own header .h or source .c file?


5)  If so, with _RODATA(), there is no need for me to hardcode or program any #definition like starting & ending address of my Flash2 data section in flash?


#define  DATA_FLASH_4_START_ADDR   0x0007F000U

#define  DATA_FLASH_4_ENDING_ADDR   0x0007FFFFU   (correct?  or less 4-byte alignment must be 0x0007FFB ?)


6) Without the need for hardcoding direct address definitions of start & ending address for that section in flash, does defining a string will automatically put that string definition inside Flash2 section without worries?

 _RODATA(Flash2) char str[] = "hello world\r\n";


7) Where does flash memory "protection" and "secured" settings come into play in all these?     During flash (code) programming, I don't want my stored data in Flash2 to be erased or overwritten.   Should I be setting up FPROT registers for that area in memory to prevent my data area from being deleted or corrupted?  Where in code should I set FPROT registers to protect my data flash area from flash erase & programming (inside MCUXpresso or external flash IC programmer)?


8) MKL28Z flash memory is 2KB size sectors.    To write values to the flash data area, a whole 2KB section first needs to be erased & verified as erased.    However, I may already have some previously saved data values within that sector even though I'm only changing one of the variables now, which so happen to have a size of 1 byte only.  The best practice for this situation would be during program initialization when data variables are placed in RAM space (.data or .bss, initialized or zeroed out).    How do I make "a copy" of my data (2K+2K=4K) from Flash2 be copied in RAM space for volatile fast access?  And so when it's time to save an update to one of the bytes or word variables to non-volatile flash, AFTER ERASE of 2KB sector, I can RESTORE all the full 2KB values from RAM & write them to flash.  Is that correct method to do this?


That's all for now.


Thank you very much for your help.