简体   繁体   English

在Arm Cortex M4 NRF52的局部Flash中重写变量的初始化值

[英]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. 我知道像int test_data = 3这样的变量存储在闪存的.data节中,然后在运行时复制到RAM中。 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. 我想在运行时为变量test_data修改存储在闪存中的值,以便在下次重新启动时将加载不同的默认初始化值。

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. 我知道可以在运行时编写CPU上的闪存,但是我不知道如何在闪存中找到test_variable的地址。 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. 您必须将整个擦除块复制到RAM,修改副本中的值,擦除该块,然后从修改后的RAM副本中写入整个块。 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. 这可能甚至不可能,因为您的RAM小于擦除块的大小。 Moreover if the SoC executes code from Flash, the block erase is likely to be erasing the code that is executing. 此外,如果SoC从闪存执行代码,则块擦除可能会擦除正在执行的代码。

Check your part's documentation but the nRF52840 has relatively small 4Kb pages. 请检查您零件的文档,但nRF52840的页面相对较小,为4Kb。 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. nRF52840的页面擦除时间最长为85ms。 If that is an issue, then you are better off perhaps using an external EEPROM. 如果这是一个问题,那么最好使用外部EEPROM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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