[SOLVED] Non Volatile RAM that survives resets

markwj
Posts: 90
Joined: Tue Mar 08, 2016 5:03 am

[SOLVED] Non Volatile RAM that survives resets

Postby markwj » Wed Mar 07, 2018 11:40 am

My requirement is for a non volatile RAM to store state between resets of the CPU. I would prefer it go to zero (or whatever) on a power cycle, but other than (deep sleep, software resets, watchdog timer resets, crash resets, etc) I need to retain contents.

I'm aware of RTC_DATA_ATTR, but the components/esp32/cpu_start.c does this:

Code: Select all

    /* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
    if (rst_reas[0] != DEEPSLEEP_RESET) {
        memset(&_rtc_bss_start, 0, (&_rtc_bss_end - &_rtc_bss_start) * sizeof(_rtc_bss_start));
so wipes that RAM on anything other than a DEEPSLEEP_RESET.

Flash/NVS would work except for (a) write count limitations, and (b) it doesn't meet the requirement of zeroing the value on a power cycle.

Anybody have any ideas for where I can store stuff that would behave like that? I only need a few bytes. It seems to be that storing it in RTC RAM, but outside the rtc_bss segment, might work?
Last edited by markwj on Thu Mar 08, 2018 3:53 am, edited 1 time in total.

markwj
Posts: 90
Joined: Tue Mar 08, 2016 5:03 am

Re: Non Volatile RAM that survives resets

Postby markwj » Wed Mar 07, 2018 1:20 pm

Answering my own question, I found a 'hacky' way of doing this in GCC:

https://community.nxp.com/docs/DOC-328171

But this means modifying:

components//esp32/ld/esp32.common.ld

to add:

Code: Select all

.rtc.noload (NOLOAD) :
  {
    . = ALIGN(4);
    *(.rtc.noload) /* .rtc.noload section */
    . = ALIGN(4);
  } > rtc_slow_seg
and then the data can be marked appropriately:

Code: Select all

int __attribute__((section(".rtc.noload"))) boot_count;
The data is then uninitialised, so garbage at first boot, but that can be picked up with special handling of rtc_get_reset_reason(0)==POWERON_RESET.

Apart from the modification of components//esp32/ld/esp32.common.ld that seems an acceptable solution to me. Just need to work out how I can add that custom section type without having to modify the framework.

markwj
Posts: 90
Joined: Tue Mar 08, 2016 5:03 am

Re: Non Volatile RAM that survives resets

Postby markwj » Thu Mar 08, 2018 3:52 am

Final solution that worked for me (documented here for posterity):

A .ld file in the main project component:

Code: Select all

SECTIONS
{
  .rtc.noload (NOLOAD) :
  {
    . = ALIGN(4);
    *(.rtc.noload) /* .rtc.noload section */
    . = ALIGN(4);
  } > rtc_slow_seg
}
Add "-T <my-ld-file>" to COMPONENT_ADD_LDFLAGS in the main component.mk.

A structure to store my non-volatile data, tagged with __attribute__((section(".rtc.noload"))) to put it in that special section.

A boot-time check if rtc_get_reset_reason(0) == POWERON_RESET then zero the storage structure as this is a power on boot.

Doing it that way required no changes to the IDF, and seems to work well. It is zeroed on first power on, but survives soft reboots (watchdogs, deep sleeps, aborts, panics, stack overflows, etc).

User avatar
hassan789
Posts: 156
Joined: Thu Jun 29, 2017 2:15 am

Re: [SOLVED] Non Volatile RAM that survives resets

Postby hassan789 » Sat May 19, 2018 7:04 pm

this was very helpful

themadsens
Posts: 17
Joined: Wed Sep 05, 2018 6:49 pm

Re: [SOLVED] Non Volatile RAM that survives resets

Postby themadsens » Mon Mar 25, 2019 7:16 pm

Thanks a million! This saved my day!!

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

Re: [SOLVED] Non Volatile RAM that survives resets

Postby ESP_igrr » Mon Mar 25, 2019 11:36 pm

Note that this is now possible to do without linker script modifications, using RTC_NOINIT_ATTR (available in v3.2 branch and later versions).

sebosfato
Posts: 2
Joined: Tue Jun 23, 2020 10:25 am

Re: [SOLVED] Non Volatile RAM that survives resets

Postby sebosfato » Tue Jun 23, 2020 10:29 am

Hello, Is there a way to accomplish it with arduino ide?

Im doing a Pulmonary Ventilator with the esp32 and it would be a must to use this technique to keep the values after a possible reset.

any help please? i found a library for the esp8266 but none for the esp32...

thanks in advance

ESP_Sprite
Posts: 9014
Joined: Thu Nov 26, 2015 4:08 am

Re: [SOLVED] Non Volatile RAM that survives resets

Postby ESP_Sprite » Tue Jun 23, 2020 12:23 pm

sebosfato wrote:
Tue Jun 23, 2020 10:29 am
Hello, Is there a way to accomplish it with arduino ide?

Im doing a Pulmonary Ventilator with the esp32 and it would be a must to use this technique to keep the values after a possible reset.

any help please? i found a library for the esp8266 but none for the esp32...

thanks in advance
Okay, taking off my Espressif hat and speaking on a purely personal basis here: seriously, a medical device, on a chip with WiFi/BL and as such a huge attack surface on it, with Arduino as a base, and you ask here how you can retain potentially-corrupt data in RAM after a reset because of who-knows-what reason? Please, please, please tell me that this is only for practice and that you never ever intend to use this on a living human being ever...

sebosfato
Posts: 2
Joined: Tue Jun 23, 2020 10:25 am

Re: [SOLVED] Non Volatile RAM that survives resets

Postby sebosfato » Sat Jun 27, 2020 8:57 am

im pretty confident with it indeed... because wifi and bluetooth will not be used and it have 2 cores.. no joke at all ... im very new to it but so far i think the way i have found to have pretty much stable devices is to first have a very stable power supply and second to never use for or while loops in the code.. we are having a big crisis with this corona virus and i just wanted to do something or at least start with an alternative approach.. actually the esp controls only servos and is connected to a nextion screen and some sensors.. no patient yet... however i found that in case of a required reboot for whatever reason it should maintain its state to get a fast cal of some values.. only few variables are needed the constants will be saved eeprom..

i used RTC_NOINIT_ATTR uint32_t on some 8 variables and it make it survive software reset..

so far surviving a software reset is already great because it only need to be powered well... but i wonder if would be possible to get the two resets safe.. just in case. and if it would retain values with other types of resets..

any solution for surviving all kinds of resets with arduino ide? well i know you may use a professional software different ide but perhaps you u can guide me there..

after all if it is not approved will end up as a very good exercise... all files will be on my google drive free to all with pics 3d drawings, and open source code for the screen and the controler so it can be used anywhere..

theres only few variables.. and as the chip restart so fast the idea is that in case of corruption or problem with calibration it can fast reset between breathing cycles thats why would be nice to maintain some few variables... it takes only250ms to software restart... but one thing im going to do is a external watchdog to restart it in case of panic so would be great if we could stand this kind of poweron reset...
hope you have an answer...
=D thanks a lot

Who is online

Users browsing this forum: No registered users and 94 guests