Closed daniel-dona closed 3 months ago
Seems to be more than one clock source available to be configured for sleep modes, not sure if all of them are available for deep sleep but I think at least the two based on 32kHz crystal
I think the problem is on this part:
uint64_t duration_math = 32768 * sleep_duration;
Looks like the variable is not capable (?) of holding a 64 bit unsigned int, not sure how to probe this in a better way but I tried testing with this piece of code:
void setup() {
Serial.println("Boot");
for(int s = 0; s < 300; s++){
uint32_t sleep_duration = s*1000; //ms
uint32_t sleep_time; // From manual_ps_pub.h as UINT32
uint64_t duration_math = 32768 * sleep_duration;
if (duration_math / 1000 > 0xFFFFFFFF) {
// Sleep forever
sleep_time = 0xFFFFFFFF;
} else {
sleep_time = (duration_math / 1000) & 0xFFFFFFFF;
}
Serial.print("Seconds: ");
Serial.print(s);
Serial.print(" duration_math: ");
Serial.print(duration_math);
Serial.print(" sleep_time: ");
Serial.println(sleep_time);
}
}
And at about 131 seconds the variable for sleep_time overflow:
[1704116401] (2024-01-01 14:40:01): Seconds: 124 duration_math: 4063232000 sleep_time: 4063232
[1704116401] (2024-01-01 14:40:01): Seconds: 125 duration_math: 4096000000 sleep_time: 4096000
[1704116401] (2024-01-01 14:40:01): Seconds: 126 duration_math: 4128768000 sleep_time: 4128768
[1704116401] (2024-01-01 14:40:01): Seconds: 127 duration_math: 4161536000 sleep_time: 4161536
[1704116401] (2024-01-01 14:40:01): Seconds: 128 duration_math: 4194304000 sleep_time: 4194304
[1704116401] (2024-01-01 14:40:01): Seconds: 129 duration_math: 4227072000 sleep_time: 4227072
[1704116401] (2024-01-01 14:40:01): Seconds: 130 duration_math: 4259840000 sleep_time: 4259840
[1704116401] (2024-01-01 14:40:01): Seconds: 131 duration_math: 4292608000 sleep_time: 4292608
[1704116401] (2024-01-01 14:40:01): Seconds: 132 duration_math: 30408704 sleep_time: 30408
[1704116401] (2024-01-01 14:40:01): Seconds: 133 duration_math: 63176704 sleep_time: 63176
[1704116401] (2024-01-01 14:40:01): Seconds: 134 duration_math: 95944704 sleep_time: 95944
[1704116401] (2024-01-01 14:40:01): Seconds: 135 duration_math: 128712704 sleep_time: 128712
[1704116401] (2024-01-01 14:40:01): Seconds: 136 duration_math: 161480704 sleep_time: 161480
[1704116401] (2024-01-01 14:40:01): Seconds: 137 duration_math: 194248704 sleep_time: 194248
[1704116401] (2024-01-01 14:40:01): Seconds: 138 duration_math: 227016704 sleep_time: 227016
[1704116401] (2024-01-01 14:40:01): Seconds: 139 duration_math: 259784704 sleep_time: 259784
[1704116401] (2024-01-01 14:40:01): Seconds: 140 duration_math: 292552704 sleep_time: 292552
[1704116401] (2024-01-01 14:40:01): Seconds: 141 duration_math: 325320704 sleep_time: 325320
Using seconds and not milliseconds works fine, at least tested up to 15 minutes, but some precision is lost... Working on a PR
Seems to work fine now with the change:
Since sleep_duration
is an uint32_t, the line uint64_t duration_math = 32768 * sleep_duration;
is probably using 32-bit math, and it's cast to 64-bit later. That probably causes the overflow.
Hello,
How did you fix this?
Do you use any external components or how?
Thanks
hello,
I use bk72xx boards in esphome home assistant integration. how I can add deep sleep to my bk72xx boards.
could you provide ,yaml example? or maybe something must be added before?
Ed
Fixed in #253.
Looks like setting the wake up timer for deep sleep only works as expected up to about 120 seconds (~2 min). Setting a longer sleep time results on sleeping only for about 15 ~ 20 seconds.
With 150 seconds
With 120 seconds
Is in deep sleep in both cases, tested checking power consumption using a lab PSU.
Code: