简体   繁体   中英

Rewriting the initialised value of a variable in the local flash of a arm cortex m4 nrf52

I would like to have a variable in flash memory that will retain the value I want after reset. I know that a variable like this int test_data = 3 is stored in the .data section of the flash memory and then copied to RAM at run time. I would like, at run time, to modify the value stored in flash memory for variable test_data so that at the next reboot it will load a different default initialised value.

I know it is possible to write the on CPU flash at run time but I don't know how to find the address of the test_variable in flash memory. Can you give me some hints in this direction ?

Thank you.

What you are proposing is unlikely to be practical. Flash memory is word-write, block-erase. Rewriting a single word is not possible; you have to copy the entire erase-block to RAM, modify the value in the copy, erase the block, then write the entire block from the modified RAM copy. Hoping that the power is not removed during the process.

This may not even be possible is your RAM is smaller that the erase-block size. Moreover if the SoC executes code from Flash, the block erase is likely to be erasing the code that is executing.

Check your part's documentation but the nRF52840 has relatively small 4Kb pages. A better solution therefore, is to reserve a page for your configuration/initialisation data and read that data in your code and assign it to the appropriate variables on start-up rather than try and rewrite the linker generated code/data. Better yet, to protect yourself from power-fail use two pages with a sequence number and validation that you write last ; then on start-up the page with the largest valid sequence number is the one in use. When you modify the data, you overwrite the older data - that way if it fails before writing the sequence number and validation, you will not have lost all your data. If the data page is blank, then you use the linker generated initialisation.

This is only an outline of what you need to do - the level of sophistication is up to you.

If your application has hard real-time constraints; you may need to check that the memory bus is not blocked during a page erase/write - this could halt executing of code including interrupt handlers during erase/write and cause you to miss deadlines. Page erase time is up-to is 85ms for nRF52840. If that is an issue, then you are better off perhaps using an external EEPROM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM