Page 1 of 2

keep variable after restart

Posted: Wed Sep 05, 2018 5:39 am
by dhs2017
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?

Re: keep variable after restart

Posted: Wed Sep 05, 2018 8:06 am
by WiFive
Software restart? RTC_NOINIT_ATTR

Re: keep variable after restart

Posted: Wed Sep 05, 2018 9:08 am
by dhs2017
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();
}

Re: keep variable after restart

Posted: Thu Sep 06, 2018 8:57 am
by dhs2017
I found that the RTC_NOINIT_ATTR variable will be reset if it's wake up from deep sleep.

Re: keep variable after restart

Posted: Thu Sep 06, 2018 9:14 am
by WiFive
You have to check reset reason and init to 0 yourself.

You have to make sure RTC_SLOW_MEM is powered during deepsleep.

Re: keep variable after restart

Posted: Thu Sep 06, 2018 9:29 am
by dhs2017
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);
}

Re: keep variable after restart

Posted: Thu Sep 06, 2018 10:23 am
by ESP_igrr
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.

Re: keep variable after restart

Posted: Fri Sep 07, 2018 12:20 am
by dhs2017
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?

Re: keep variable after restart

Posted: Fri Sep 07, 2018 2:04 am
by ESP_igrr
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.

Re: keep variable after restart

Posted: Fri Sep 07, 2018 2:09 am
by dhs2017
i see,, thx ivan