Page 1 of 1

Allocated memory in RTC

Posted: Mon Mar 27, 2017 8:37 am
by Dav_FR
After read some post and documentation I think the response is no, but a sentence in the documentation let me an open doubt, so I have to ask.

It's possible allocate memory which will remain unchanged after deep sleep state?

At http://esp-idf.readthedocs.io/en/latest ... -stub.html is specified that strings must be declared array and RTC_RODATA_ATTR, but later, a sentence about the use of "rtc_wake_stub" as file prefix makes me doubt: "The second way is a better option if you need to use strings, or write other more complex code"

I checked with this small example to confirm the behaviour, but now I'm not sure if this is due to a bug when using "rtc_wake_stub" file prefix.

Code: Select all

char * test1 = NULL;
int wake_count = 0;

void RTC_IRAM_ATTR esp_wake_deep_sleep(void) {
    esp_default_wake_deep_sleep();
    wake_count++;
}

void app_main(void)
{

	if (wake_count==0) {

		test1 = (char *) pvPortMallocCaps(32, MALLOC_CAP_8BIT);
		memset(test1,0,32);
		memcpy(test1,"esp32",5);


		ESP_LOGI(tagg,"First time! %d - %s",wake_count,test1);
	} else {
		ESP_LOGI(tagg,"Not First time! %d",wake_count);
		ESP_LOGI(tagg,"String val: %s",test1);
	}

	ESP_LOGI(tagg,"Going to sleep");
	esp_deep_sleep(5*1000000);
}
Btw, there is other option which I missed?

Best regards and sorry for the incovenience,
Dav

Re: Allocated memory in RTC

Posted: Mon Mar 27, 2017 9:10 am
by ESP_igrr
It is not possible to do dynamic allocation in RTC memory, but you can do static (compile-time) allocation.

If you define a global variable with RTC_DATA_ATTR attribute, the variable will be placed into RTC_SLOW_MEM memory. You can see how this is done in sntp example:

https://github.com/espressif/esp-idf/bl ... main.c#L49

Re: Allocated memory in RTC

Posted: Mon Mar 27, 2017 9:22 am
by Dav_FR
Hi Igrr,

Thanks your for the fast response, my idea is avoid the loss of complex structures (which contains allocated pointers, lists of allocated structs, ...) between deep sleeps.

So, I guess there are two solutions for me:
- Create a fixed structure declared as RTC_DATA_ATTR and "copy" the dynamic memory to this structure before deep sleep to recover this into dynamic memory after wake up.
- Use non volatile memory, which is my last option.

I guess RTC_DATA_ATTR is applied to structs without problems.

Thanks you again,
Dav

Re: Allocated memory in RTC

Posted: Mon Mar 27, 2017 2:26 pm
by ESP_igrr
Yes, you should be able to use RTC_DATA_ATTR with struct instances.

Re: Allocated memory in RTC

Posted: Mon Mar 27, 2017 5:06 pm
by martinayotte
which contains allocated pointers
Be aware that your structure can't hold pointers, but you need to have those arrays directly into the structures.

Re: Allocated memory in RTC

Posted: Tue Mar 28, 2017 10:31 am
by Dav_FR
Sure, the problem I'm facing now is the lack of space in the RTC memory :(