keep variable after restart

dhs2017
Posts: 40
Joined: Thu Mar 30, 2017 8:56 am

keep variable after restart

Postby dhs2017 » Wed Sep 05, 2018 5:39 am

There is a RTC_DATA_ATTR which can keep the variable after deepsleep wake up, is there any method to keep the variable after restart?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: keep variable after restart

Postby WiFive » Wed Sep 05, 2018 8:06 am

Software restart? RTC_NOINIT_ATTR

dhs2017
Posts: 40
Joined: Thu Mar 30, 2017 8:56 am

Re: keep variable after restart

Postby dhs2017 » Wed Sep 05, 2018 9:08 am

Hi WiFive,

Yes, not power restart,but just software restart or restart cause by system crash.
I have tried the following way, but it seems the value cannot be init to zero. It started with some value. I expected it start with 0 and then increment after each esp_restart(). Any example on how to use this ATTR?

Code: Select all

static RTC_NOINIT_ATTR  int test=0;
void app_main(){
test++;
esp_restart();
}

dhs2017
Posts: 40
Joined: Thu Mar 30, 2017 8:56 am

Re: keep variable after restart

Postby dhs2017 » Thu Sep 06, 2018 8:57 am

I found that the RTC_NOINIT_ATTR variable will be reset if it's wake up from deep sleep.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: keep variable after restart

Postby WiFive » Thu Sep 06, 2018 9:14 am

You have to check reset reason and init to 0 yourself.

You have to make sure RTC_SLOW_MEM is powered during deepsleep.

dhs2017
Posts: 40
Joined: Thu Mar 30, 2017 8:56 am

Re: keep variable after restart

Postby dhs2017 » Thu Sep 06, 2018 9:29 am

yes, I did.

below is my simple code.
If I use part A, variable test will increment every 4 second.
However, if I comment out PART A, and use the esp_deep_sleep, the test variable become a strange value and won't increment. that means deep sleep wake up cleared the test variable?

Code: Select all

static RTC_NOINIT_ATTR int test;

void app_main() {
esp_reset_reason_t reason = esp_reset_reason();
if ((reason != ESP_RST_DEEPSLEEP) && (reason != ESP_RST_SW)) {
	test = 0;
}
test++;
printf("test %d",test);
/**********PART A*********/
delay_ms(4000)
esp_restart();
/****************************/
//esp_deep_sleep(1000000LL*4);
}

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

Re: keep variable after restart

Postby ESP_igrr » Thu Sep 06, 2018 10:23 am

The fact that RTC_NOINIT variable is lost after deep sleep is a bug, thank you for reporting this.

You can work around this by adding some RTC_DATA variable into your program or by calling esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON); before entering deep sleep.

dhs2017
Posts: 40
Joined: Thu Mar 30, 2017 8:56 am

Re: keep variable after restart

Postby dhs2017 » Fri Sep 07, 2018 12:20 am

Hi Ivan,

it seems RTC_DATA_ATTR will lose after software reset.

In fact, I am working on this to resolve the problem of wifi turn on/off and then RTC IO case deep sleep current increase which I raised in github issue, is there any update on this issue?

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

Re: keep variable after restart

Postby ESP_igrr » Fri Sep 07, 2018 2:04 am

What I meant was to use RTC_NOINIT_ATTR for your variable, and then force RTC_SLOW_MEM to be powered on in deep sleep — currently this does not happen automatically (which is a bug) hence RTC_NOINIT variables are getting lost.

Regarding wifi/RTC IOs, will follow up on GitHub.

dhs2017
Posts: 40
Joined: Thu Mar 30, 2017 8:56 am

Re: keep variable after restart

Postby dhs2017 » Fri Sep 07, 2018 2:09 am

i see,, thx ivan

Who is online

Users browsing this forum: Bing [Bot] and 91 guests