Description
Reference discussion in Arduino core issue tracker here.
CC @marcvtew
CC @5chufti
Hi,
Per NONOS SDK api reference doc section 3.3.9:
The theoretical maximum value of time_in_us can be calculated by formula:
(time_in_us / cali) << 12 = 2^31 - 1
- cali = system_rtc_clock_cali_proc(), the cali is the RTC clock period (in
us); bit11 ~ bit0 are decimal. For more details about the cali, please see the
API: system_rtc_clock_cali_proc
From the previous follows this implementation for calculating the max theoretical sleep time::
uint64_t deepSleepMax()
{
//cali*(2^31-1)/(2^12)
return (uint64_t)system_rtc_clock_cali_proc()*(0x80000000-1)/(0x1000);
}
However, calling this:
system_deep_sleep(deepSleepMax());
seems to have odd behavior. Sometimes there is an error message in the serial output saying sleep time too long, sometimes the ESP sleeps, and sometimes the ESP doesn't wake up.
The NONOS SDK api reference doc says in section 3.3.9, in Note, 3rd bullet:
After configuration, the chip will not enter Deep-sleep mode immediately, but will wait for a while till the Wi-Fi core is closed safely.
I suspect that:
- there is a race condition, because it is possible that the max theoretical value for sleep changes between the time the user calls system_deep_sleep() and the actual sleep begins
- If system_deep_sleep() is called with a value larger than the max theoretical value, the ESP could never wake up again
- If there is a check on the SDK code side that checks against some max value, that it suffers from the same race condition above, which means, which means that it is still possible for a value that is too big to reach the sleep code, and make the ESP never wake up.