From 7fd4648cd5ce467520920d58dd7518e7beace08d Mon Sep 17 00:00:00 2001 From: jun2sak Date: Fri, 26 Feb 2021 09:06:11 +0900 Subject: [PATCH] rase error if Alarm time >= 512 sec. --- locale/circuitpython.pot | 2 +- ports/nrf/common-hal/alarm/time/TimeAlarm.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index dc0ec9fb7e..39bc4d74e1 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -308,7 +308,7 @@ msgid "Address type out of range" msgstr "" #: ports/nrf/common-hal/alarm/time/TimeAlarm.c -msgid "Alarm time is too far." +msgid "Alarm time must be < 512 seconds." msgstr "" #: ports/esp32s2/common-hal/canio/CAN.c diff --git a/ports/nrf/common-hal/alarm/time/TimeAlarm.c b/ports/nrf/common-hal/alarm/time/TimeAlarm.c index 2a9a0fcd33..e9c536414b 100644 --- a/ports/nrf/common-hal/alarm/time/TimeAlarm.c +++ b/ports/nrf/common-hal/alarm/time/TimeAlarm.c @@ -86,12 +86,13 @@ void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_ mp_float_t now_secs = uint64_to_float(common_hal_time_monotonic_ms()) / 1000.0f; mp_float_t wakeup_in_secs = MAX(0.0f, timealarm->monotonic_time - now_secs); int wsecs = (int)(wakeup_in_secs); - if (wsecs > 510) { //XXX - mp_raise_ValueError(translate("Alarm time is too far.")); + // timealarm is implemented by RTC, which is a 24bit counter + // running at 32768Hz. So, 2^24 / 32768 = 512sec is an upper limit. + if (wsecs >= 512) { + mp_raise_ValueError(translate("Alarm time must be < 512 seconds.")); } uint32_t wakeup_in_ticks = (uint32_t)(wakeup_in_secs * 1024.0f); - //printf("alarm_time_timealarm_set_alarms() %d secs 0x%08X ticks\r\n", wsecs, (int)wakeup_in_ticks); port_interrupt_after_ticks_ch(1, wakeup_in_ticks); rtc_woke_up_counter = 0; }