Skip to content

Commit 2a8d7ee

Browse files
committed
stmhal: Fix RTC.wakeup so it correctly calculates WUT for large periods.
Thanks to Peter Hinch. Addresses issue adafruit#1488.
1 parent fd38799 commit 2a8d7ee

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

stmhal/rtc.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,17 +419,23 @@ mp_obj_t pyb_rtc_wakeup(mp_uint_t n_args, const mp_obj_t *args) {
419419
if (div <= 16) {
420420
wut = 32768 / div * ms / 1000;
421421
} else {
422+
// use 1Hz clock
422423
wucksel = 4;
423424
wut = ms / 1000;
424-
if (ms > 0x10000) {
425-
wucksel = 5;
426-
ms -= 0x10000;
427-
if (ms > 0x10000) {
425+
if (wut > 0x10000) {
426+
// wut too large for 16-bit register, try to offset by 0x10000
427+
wucksel = 6;
428+
wut -= 0x10000;
429+
if (wut > 0x10000) {
430+
// wut still too large
428431
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "wakeup value too large"));
429432
}
430433
}
431434
}
432-
wut -= 1;
435+
// wut register should be 1 less than desired value, but guard against wut=0
436+
if (wut > 0) {
437+
wut -= 1;
438+
}
433439
enable = true;
434440
}
435441
if (n_args == 3) {

0 commit comments

Comments
 (0)