From cf938983e9c584b0c41a909723dc71e9161cc494 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 11 Dec 2020 08:10:26 -0500 Subject: [PATCH] SleepMemory + set alarm.wake_alarm --- main.c | 12 ++++++++---- ports/esp32s2/common-hal/alarm/time/TimeAlarm.c | 1 + shared-bindings/alarm/__init__.c | 7 ++++++- shared-bindings/alarm/__init__.h | 5 ++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index da04ada466..378c506579 100755 --- a/main.c +++ b/main.c @@ -112,9 +112,6 @@ STATIC void start_mp(supervisor_allocation* heap) { reset_status_led(); autoreload_stop(); supervisor_workflow_reset(); -#if CIRCUITPY_ALARM - alarm_reset(); -#endif // Stack limit should be less than real stack size, so we have a chance // to recover from limit hit. (Limit is measured in bytes.) @@ -158,6 +155,13 @@ STATIC void start_mp(supervisor_allocation* heap) { mp_obj_list_init(mp_sys_argv, 0); + #if CIRCUITPY_ALARM + // Record which alarm woke us up, if any. An object may be created so the heap must be functional. + alarm_save_wakeup_alarm(); + // Reset alarm module only after we retrieved the wakeup alarm. + alarm_reset(); + #endif + #if CIRCUITPY_NETWORK network_module_init(); #endif @@ -285,6 +289,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { filesystem_flush(); supervisor_allocation* heap = allocate_remaining_memory(); start_mp(heap); + found_main = maybe_run_list(supported_filenames, &result); #if CIRCUITPY_FULL_BUILD if (!found_main){ @@ -354,7 +359,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { serial_write_compressed(translate("Woken up by alarm.\n")); board_init(); supervisor_set_run_reason(RUN_REASON_STARTUP); - // TODO: Reset any volatile memory the user may have access to. return true; } #endif diff --git a/ports/esp32s2/common-hal/alarm/time/TimeAlarm.c b/ports/esp32s2/common-hal/alarm/time/TimeAlarm.c index d5e896c015..4d0bba2387 100644 --- a/ports/esp32s2/common-hal/alarm/time/TimeAlarm.c +++ b/ports/esp32s2/common-hal/alarm/time/TimeAlarm.c @@ -52,6 +52,7 @@ mp_obj_t alarm_time_timealarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t * alarm_time_time_alarm_obj_t *timer = m_new_obj(alarm_time_time_alarm_obj_t); timer->base.type = &alarm_time_time_alarm_type; // TODO: Set monotonic_time based on the RTC state. + timer->monotonic_time = 0.0f; return timer; } diff --git a/shared-bindings/alarm/__init__.c b/shared-bindings/alarm/__init__.c index 28e34ad6aa..5ef548ef27 100644 --- a/shared-bindings/alarm/__init__.c +++ b/shared-bindings/alarm/__init__.c @@ -197,7 +197,7 @@ STATIC mp_map_elem_t alarm_module_globals_table[] = { }; STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table); -void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) { +STATIC void alarm_set_wake_alarm(mp_obj_t alarm) { // Equivalent of: // alarm.wake_alarm = alarm mp_map_elem_t *elem = @@ -207,6 +207,11 @@ void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) { } } +// Initialize .wake_alarm value. +void alarm_save_wakeup_alarm(void) { + alarm_set_wake_alarm(common_hal_alarm_get_wake_alarm()); +} + const mp_obj_module_t alarm_module = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t*)&alarm_module_globals, diff --git a/shared-bindings/alarm/__init__.h b/shared-bindings/alarm/__init__.h index 5a8c613d9d..8c4b6cad96 100644 --- a/shared-bindings/alarm/__init__.h +++ b/shared-bindings/alarm/__init__.h @@ -43,8 +43,11 @@ extern void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj // Deep sleep is entered outside of the VM so we omit the `common_hal_` prefix. extern NORETURN void alarm_enter_deep_sleep(void); +extern mp_obj_t common_hal_alarm_get_wake_alarm(void); + // Used by wake-up code. -extern void common_hal_alarm_set_wake_alarm(mp_obj_t alarm); +void alarm_save_wakeup_alarm(void); + // True if an alarm is alerting. This is most useful for pretend deep sleep. extern bool alarm_woken_from_sleep(void);