Page 1 of 1

[SOLVED] RTC Memory Corruption after Deep Sleep

Posted: Mon Feb 25, 2019 9:46 pm
by chrismerck
We've observed a strange behavior on 2 units of ESP32 dev kits where the RTC memory becomes corrupted after deep sleep.

We are still working to isolate the problem, and hope to provide an example project that shows the issue, but for now I just want to share what we are seeing:

Code: Select all

/* application writes to RTC memory */
*((uint32_t*)(0x50001FF0))=0x1badbabe
ESP_LOGI(TAG, "before: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));
ESP_LOGI(TAG, "before: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));
ESP_LOGI(TAG, "before: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));
ESP_LOGI(TAG, "before: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));

/* deep sleep reset */
esp_deep_sleep(1000 /* 1 ms */);

/* now, from the bootloader */
ESP_LOGI(TAG, "after: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));
ESP_LOGI(TAG, "after: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));
ESP_LOGI(TAG, "after: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));
ESP_LOGI(TAG, "after: 0x%" PRIx32, *((uint32_t*)(0x50001FF0)));
We get 0x1badbabe printed "before", as expected. But "after" is sometimes corrupted as 0x1bad3abe or similar.

The 4x "after" prints all print the same value. So it seems that the memory is getting corrupted through the reset, rather than being unreliably read. -- But there may be some caching involved?

We use RTC memory to signal a change in bootloader behavior to jump to the OTA image rather than the factory image --- so the corruption causes a bootloop!

Re: RTC Memory Corruption after Deep Sleep

Posted: Tue Feb 26, 2019 2:47 am
by ESP_igrr
Are you calling esp_sleep_pd_config to keep RTC memory powered on in deep sleep? https://docs.espressif.com/projects/esp ... d_option_t

This is not required if you place some data into RTC memory using the linker. However if you are accessing RTC memory by absolute address, deep sleep API can not recognize this and automatically enable RTC memory. If you set log level to debug, esp_deep_sleep function should report which power domains are left in which state.

Re: RTC Memory Corruption after Deep Sleep

Posted: Wed Feb 27, 2019 7:39 pm
by chrismerck
:facepalm: Thanks Igrr. Your quick response saved us a lot of headache!

Increasing the deep sleep time made the situation worse (as expected), and adding the following before deep_sleep totally fixed the issue:

Code: Select all

esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);