-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Return error in class deepSleep() when max sleeptime is exceeded #4936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Proposed codechange returns 0 when max sleeptime is exceeded.
cores/esp8266/Esp.cpp
Outdated
#endif | ||
return 0; // error: max sleeptime exceeded | ||
} | ||
else |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove else and braces.
Removed else and braces.
Removed return 1;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies, I missed this detail in my previous review.
cores/esp8266/Esp.cpp
Outdated
#ifdef DEBUG_SERIAL | ||
DEBUG_SERIAL.println("Error: max sleeptime exceeded"); | ||
#endif | ||
return 0; // error: max sleeptime exceeded |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type is bool, but returned value is int => return false here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right.
Strange though that this is not detected by git code check.
cores/esp8266/Esp.cpp
Outdated
system_deep_sleep_set_option(static_cast<int>(mode)); | ||
system_deep_sleep(time_us); | ||
esp_yield(); | ||
return 1; // never gets called |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return type is bool, but returned value is int => return true here
I have a question. I've noticed that the deepSleepMax() can vary. For instance deep sleep an hour, take an action and then go back to sleep. I print the deepSleepMax() value and it changes. Does this value only change during a sleep or could it very between calling deepSleepMax() and then calling deepSleepMax()? Asking because that could create a race condition even with the check being added here. |
I think it can only vary within the sys context, i.e.: between loop()s, or between a yield() and return. That means that it shouldn't vary between the call to deepSleepMax() and the actual sleep. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a user of the ESP in powerdown modes, and this patch is technically correct, so I'm fine approving this patch.
However, since you are a user, do please think about what you and others would expect it to do if passed in a value that's greater than the (potentially variable) deepSleepMax()
time.
cores/esp8266/Esp.cpp
Outdated
{ | ||
if (time_us > deepSleepMax()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not be more useful (and help fix peoples code which is broken already) if, instead of possibly printing an error and return false;
, you just printed a warning and adjusted the time down to deepSleepMax()
?
Existing code will not expect to ever get a return value and potentially go off into lala-land, since this was a void
.
"Be conservative in what you do, be liberal in what you accept from others."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@earlephilhower Point taken. I'll make some changes to the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good with just the floating point math issue noted below.
cores/esp8266/Esp.cpp
Outdated
@@ -109,6 +109,13 @@ extern "C" void esp_yield(); | |||
|
|||
void EspClass::deepSleep(uint64_t time_us, WakeMode mode) | |||
{ | |||
if (time_us > deepSleepMax()) // we need to prevent the esp8266 from not waking up from deepsleep | |||
{ | |||
time_us = (deepSleepMax() - (round(deepSleepMax() * 5 / 100))); // 5% correction because of inaccurate timekeeping by the esp8266 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for round() here. You're doing integer division already, so this takes the integer division result, promotes it to a double, then promotes deepSleepMax() to a double, subtracts them as doubles, then truncates it all back to an integer. A shorter way which won't cause a program to include the whole floating point libs (if not being included already):
time_us = (deepSleepMax() * 95L) / 100L;
or if time_us can be > 2^24 (so the *95 but could overflow)
time_us = deepSleepMax() - (deepSleepMax() / 20);
I bring this up because there was much howling of pain when we included floating point support in printf with the latest release, as some folks binaries ended up being just a hair too big for OTA...
@marcvtew did you actually encounter a case where the deepsleep would get stuck with a value close to the max? |
@devyte Made some code to read and display maxSleeptime(). The esp8266 on my Lolin board has a max sleeptime of 13840678905 microseconds. I'll set my sleeptime to 13840678904 microseconds and see if it wakes up. Actually there's a difference of more than 35 seconds between two different calculations of maxSleeptime(). So i'll settle for a sleeptime of 13800000000 microseconds (230 minutes). |
@devyte I made some code to measure the value of maxDeepsleep() every two seconds, remembering the absolute max value and the absolute min value measured. I let the code run for about 15 minutes to get a stable result: So, in my case the shortest calculated maxsleeptime differs more than 11 minutes (within max around 3,8 to around 4 hours deepsleep) from the longest calculated maxsleeptime. It looks to me that it's not sufficient to only test (and set) deepsleeptime once (i.e. before going into sleep) because during deepsleep the calculated maxsleeptime may become shorter (worst case: more than 11 minutes) resulting in a timer overflow (esp8266 never restarts allthough we passed if (time_us > (deepSleepMax())). Safe deepsleep time for me seems to be13704364025 microseconds. With this sleeptime the esp8266 should always wake up. I'll test this tomorrow. |
True. datasheet tells 2^31-1.
But (datasheet):
So we need either interpret this decimal part (datasheet gives the way to interpret it, it gives a maximum of 1000s / 16mn40s - not 100% sure about this)), or remove it:
My board gives 10240s with this formula (2h50mn). I remember having seen it fluctuating a bit. |
for me this is purely academic, I cannot see any use in ESP.deepSleep(ESP.deepSleepMax()); So the decent thing to do would be to either just for the unlikely case that someone really exeeds tmax for ESP.deepSleep(); regarding 2^31 vs. 2^32 see here #4969 (comment) (or 3.3.9 vs 3.3.50) |
@5chufti for me, knowing the deepSleepMax() is about to save battery on
some applications. For now, I wake ESP once a hour with modem sleeping,
check the sleep cycles left and put it to deepSleep next cycle. But anyway,
it’s ~30mA consumption while I’m checking these cycles.
So it’s not academic, it’s purely practical question.
…On Fri, Aug 3, 2018 at 10:52 AM 5chufti ***@***.***> wrote:
for me this is purely academic, I cannot see any use in
ESP.deepSleep(ESP.deepSleepMax());
For a start I would have expected the API call to return false if the
max_deep_sleep_time is exceeded, but I forgot we are dealing with espressif
...
So the decent thing to do would be to either
a) replace the passed value with tmax if exceeded
(I think this is what API does instead of returning false)
b) return with "false" if tmax exceeded
just for the unlikely case that someone really exeeds tmax for
ESP.deepSleep();
They should beforhand check on the use and probable range in documentation
and/or div forum posts. I don't even see need of deepSleepMax().
regarding 2^31 vs. 2^32 see here #4969 (comment)
<#4969 (comment)>
(or 3.3.9 vs 3.3.50)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#4936 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGV_3Xpm3pqTIzByLlpWCyTVYmIL2hRPks5uNA8qgaJpZM4VUMs8>
.
|
How are you doing that ? |
@artua @marcvtew @d-a-v |
do we really want/need two deepSleepMax versions? |
I'm using my esp8266's for IoT purposes, mostly battery powered. For me not knowing the safe max sleeptime makes the esp8266 totally useless. |
thats why I argue for only one function, returning a safe value. |
@5chufti I agree that it's best to have only one meaningful function. However ths might mean that we will be deviating from the api specification of deepSleepMax(). |
Since we are still speaking in uS, and bits 0..11 are BCD, and(?) we don't wish to deal with them, we could use: |
@5chufti I'm ok with that. But how can I know the max safe value? If the
deepSleepMax() will return any safe constant - it would be great.
Let it be deepSleepSafe() or anything like that.
…On Fri, Aug 3, 2018 at 2:56 PM, david gauchard ***@***.***> wrote:
Since we are still speaking in uS, and bits 0..11 are BCD and(?) we don't
wish to deal with them, we could use:
time_in_us < ((((uint64_t)system_rtc_clock_cali_proc()) >> 12) << (12+19))
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4936 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGV_3fM2ZUZvPgopaWyxs70FUsL2L0xeks5uNDqEgaJpZM4VUMs8>
.
|
@d-a-v @marcvtew edit: former test result was not correct, have to repeat |
@marcvtew |
@5chufti some timers don't continue to count past overflow, or have undefined behavior if they overflow. Another explanation is that some interrupt is triggered on overflow, but no ISR is assigned, or a garbage ISR is assigned.
The key point here is that neither of the above cases is really addressed in the current Espressif SDK API or docs.
|
breaking news:
behaves much better as it at least restarts if tmax is exceeded. @devyte |
I now personally have settled for this approach
now I can easily select the functionality I want with
Edit: inserted probable workaround for #3408 |
I'm currently testing wether this code always wakes up the esp8266.
The current implementation uses system_deep_sleep(), which internally shuts down the wifi core "safely" (whatever that means), and then goes to sleep.
|
The past 30 hours i've tested wether the esp8266 wakes up with deepSleepMax() = ((uint64_t)system_rtc_clock_cali_proc()/2*(0xF4240ULL)-1). My esp8266 restarted 11 times so using deepSleepMax() = ((uint64_t)system_rtc_clock_cali_proc()/2*(0xF4240ULL)-1) seems to always wake up the esp8266. In one of the api docs i remember to have read that system_deep_sleep_instant() function is void. Is this function still being supported by Espressif? I did some testing shutting down wifi before going into system_deep_sleep(). Made no difference: esp8266 did not wake up with time_us = deepSleepMax(). Maybe someone else is willing to test this as well? |
Proposed codechange returns 0 when max sleeptime is exceeded.