Page 1 of 1

Save array of bytes in the RTC Fast Memory

Posted: Fri Jun 03, 2022 8:20 am
by andreaz98
Hello

I'm developing an application that must save some buffers in the RTC Fast Memory in order to persist data between deep sleeps.

My code is something like

Code: Select all

RTC_DATA_ATTR static uint8_t * buffer;
RTC_DATA_ATTR static int flag = 0;

void app_main(void)
{ 
	/*compute the dimension of the buffer if and only if this cycle is not a wake up from deep sleep*/
	if(flag == 0){
		buffer = (uint8_t *)malloc(computed_dimension);
	}
	else {
		/*read the content of the buffer for the application logic*/
		/*main application logic*/
	}
	
	/*reads and writes on the buffer*/
	
	esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
        esp_sleep_pd_config(ESP_PD_DOMAIN_VDDSDIO, ESP_PD_OPTION_OFF);
        
        esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
        
        flag = 1;
        
        esp_deep_sleep_start();
}
I expected that the above code worked but I get a LoadStoreError when the code tries to access the buffer, that means the content of buffer remains in the RAM losing its meaning after the deep sleep. How can I tell the ESP32 to save the content from the RAM pointed by buffer to the RTC Fast RAM?

Details of the working environment:
Ubuntu 20.04.4 LTS
Platformio 6.0.2
Framework ESP-IDF 4.3.2
Board az-delivery-devkit-v4 ESP32

Re: Save array of bytes in the RTC Fast Memory

Posted: Fri Jun 03, 2022 9:51 am
by ESP_Sprite
In your code, only the pointer to 'buffer' is in RTC memory, not the buffer data itself. I don't think the heap allocator is capable of allocating persistent RTC memory, so you'd have to statically allocate memory; e.g.

Code: Select all

RTC_DATA_ATTR static uint8_t buffer[128];

Re: Save array of bytes in the RTC Fast Memory

Posted: Fri Jun 03, 2022 10:53 am
by andreaz98
Ok, I understand. I must set a static size.

Thank you for the clear answer ESP_Sprite