diff --git a/main.c b/main.c index 06ca1ba93b..10fdc65224 100644 --- a/main.c +++ b/main.c @@ -132,7 +132,7 @@ static void reset_devices(void) { #endif } -STATIC void start_mp(supervisor_allocation *heap, bool first_run) { +STATIC void start_mp(supervisor_allocation *heap) { supervisor_workflow_reset(); // Stack limit should be less than real stack size, so we have a chance @@ -176,14 +176,6 @@ STATIC void start_mp(supervisor_allocation *heap, bool first_run) { mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib)); mp_obj_list_init((mp_obj_list_t *)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. - // There is no alarm if this is not the first time code.py or the REPL has been run. - shared_alarm_save_wake_alarm(first_run ? common_hal_alarm_create_wake_alarm() : mp_const_none); - // Reset alarm module only after we retrieved the wakeup alarm. - alarm_reset(); - #endif } STATIC void stop_mp(void) { @@ -373,7 +365,7 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) { } } -STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_reset) { +STATIC bool run_code_py(safe_mode_t safe_mode, bool *simulate_reset) { bool serial_connected_at_start = serial_connected(); bool printed_safe_mode_message = false; #if CIRCUITPY_AUTORELOAD_DELAY_MS > 0 @@ -409,8 +401,8 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re supervisor_allocation *heap = allocate_remaining_memory(); - // Prepare the VM state. Includes an alarm check/reset for sleep. - start_mp(heap, first_run); + // Prepare the VM state. + start_mp(heap); #if CIRCUITPY_USB usb_setup_with_vm(); @@ -755,8 +747,7 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) { supervisor_allocation *heap = allocate_remaining_memory(); - // true means this is the first set of VM's after a hard reset. - start_mp(heap, true); + start_mp(heap); #if CIRCUITPY_USB // Set up default USB values after boot.py VM starts but before running boot.py. @@ -853,12 +844,12 @@ STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) { #endif } -STATIC int run_repl(bool first_run) { +STATIC int run_repl(void) { int exit_code = PYEXEC_FORCED_EXIT; stack_resize(); filesystem_flush(); supervisor_allocation *heap = allocate_remaining_memory(); - start_mp(heap, first_run); + start_mp(heap); #if CIRCUITPY_USB usb_setup_with_vm(); @@ -968,6 +959,15 @@ int __attribute__((used)) main(void) { safe_mode = NO_CIRCUITPY; } + #if CIRCUITPY_ALARM + // Record which alarm woke us up, if any. + // common_hal_alarm_record_wake_alarm() should return a static, non-heap object + shared_alarm_save_wake_alarm(common_hal_alarm_record_wake_alarm()); + // Then reset the alarm system. It's not reset in reset_port(), because that's also called + // on VM teardown, which would clear any alarm setup. + alarm_reset(); + #endif + // Reset everything and prep MicroPython to run boot.py. reset_port(); // Port-independent devices, like CIRCUITPY_BLEIO_HCI. @@ -1001,20 +1001,21 @@ int __attribute__((used)) main(void) { // Boot script is finished, so now go into REPL or run code.py. int exit_code = PYEXEC_FORCED_EXIT; bool skip_repl = true; - bool first_run = true; - bool simulate_reset; + bool simulate_reset = true; for (;;) { - simulate_reset = false; if (!skip_repl) { - exit_code = run_repl(first_run); + exit_code = run_repl(); supervisor_set_run_reason(RUN_REASON_REPL_RELOAD); } if (exit_code == PYEXEC_FORCED_EXIT) { - if (!first_run) { + if (!simulate_reset) { serial_write_compressed(translate("soft reboot\n")); } if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { - skip_repl = run_code_py(safe_mode, first_run, &simulate_reset); + // If code.py did a fake deep sleep, pretend that we + // are running code.py for the first time after a hard + // reset. This will preserve any alarm information. + skip_repl = run_code_py(safe_mode, &simulate_reset); } else { skip_repl = false; } @@ -1022,10 +1023,10 @@ int __attribute__((used)) main(void) { break; } - // Either the REPL or code.py has run and finished. - // If code.py did a fake deep sleep, pretend that we are running code.py for - // the first time after a hard reset. This will preserve any alarm information. - first_run = simulate_reset; + #if CIRCUITPY_ALARM + shared_alarm_save_wake_alarm(simulate_reset ? common_hal_alarm_record_wake_alarm() : mp_const_none); + alarm_reset(); + #endif } mp_deinit(); return 0; diff --git a/ports/atmel-samd/common-hal/alarm/SleepMemory.h b/ports/atmel-samd/common-hal/alarm/SleepMemory.h index 5fad5946e2..14848cd5a0 100644 --- a/ports/atmel-samd/common-hal/alarm/SleepMemory.h +++ b/ports/atmel-samd/common-hal/alarm/SleepMemory.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_SLEEPMEMORY_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_SLEEPMEMORY_H +#pragma once #include "py/obj.h" @@ -34,5 +33,3 @@ typedef struct { } alarm_sleep_memory_obj_t; extern void alarm_sleep_memory_reset(void); - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_SLEEPMEMORY_H diff --git a/ports/atmel-samd/common-hal/alarm/__init__.c b/ports/atmel-samd/common-hal/alarm/__init__.c index 8f66b3a90c..96d6d9694a 100644 --- a/ports/atmel-samd/common-hal/alarm/__init__.c +++ b/ports/atmel-samd/common-hal/alarm/__init__.c @@ -48,6 +48,10 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; +// Non-heap alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// This object lives across VM instantiations, so none of these objects can contain references to the heap. +alarm_wake_alarm_union_t alarm_wake_alarm; + void alarm_reset(void) { // Reset the alarm flag alarm_pin_pinalarm_reset(); @@ -57,7 +61,7 @@ void alarm_reset(void) { void alarm_get_wakeup_cause(void) { // Called from rtc_init, just before SWRST of RTC. It is called // at an early stage of main(), to save TAMPID from SWRST. Later, - // common_hal_alarm_create_wake_alarm is called to make a wakeup + // common_hal_alarm_record_wake_alarm is called to make a wakeup // alarm from the deep sleep. TAMPID = RTC->MODE0.TAMPID.reg; @@ -67,7 +71,7 @@ bool common_hal_alarm_woken_from_sleep(void) { return alarm_pin_pinalarm_woke_this_cycle() || alarm_time_timealarm_woke_this_cycle(); } -mp_obj_t common_hal_alarm_create_wake_alarm(void) { +mp_obj_t common_hal_alarm_record_wake_alarm(void) { // Called from main.c on the first start up, just before alarm_reset. // Return a copy of wakeup alarm from deep sleep / fake deep sleep. // In case of fake sleep, status should be left in TimeAlarm/PinAlarm. @@ -76,13 +80,13 @@ mp_obj_t common_hal_alarm_create_wake_alarm(void) { if (alarm_pin_pinalarm_woke_this_cycle()) { TAMPID = RTC->MODE0.TAMPID.reg; RTC->MODE0.TAMPID.reg = TAMPID; // clear register - return alarm_pin_pinalarm_create_wakeup_alarm(TAMPID); + return alarm_pin_pinalarm_record_wake_alarm(TAMPID); } if (alarm_time_timealarm_woke_this_cycle() || (true_deep && TAMPID == 0)) { - return alarm_time_timealarm_create_wakeup_alarm(); + return alarm_time_timealarm_record_wake_alarm(); } if (true_deep) { - return alarm_pin_pinalarm_create_wakeup_alarm(TAMPID); + return alarm_pin_pinalarm_record_wake_alarm(TAMPID); } return mp_const_none; } diff --git a/ports/atmel-samd/common-hal/alarm/__init__.h b/ports/atmel-samd/common-hal/alarm/__init__.h index 061775e568..3094811c27 100644 --- a/ports/atmel-samd/common-hal/alarm/__init__.h +++ b/ports/atmel-samd/common-hal/alarm/__init__.h @@ -24,10 +24,11 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM__INIT__H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM__INIT__H +#pragma once #include "common-hal/alarm/SleepMemory.h" +#include "common-hal/alarm/pin/PinAlarm.h" +#include "common-hal/alarm/time/TimeAlarm.h" extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; @@ -53,8 +54,13 @@ typedef enum { SAMD_WAKEUP_RTC } samd_sleep_source_t; -extern void alarm_set_wakeup_reason(samd_sleep_source_t reason); -void alarm_get_wakeup_cause(void); -extern void alarm_reset(void); +typedef union { + alarm_pin_pinalarm_obj_t pin_alarm; + alarm_time_timealarm_obj_t time_alarm; +} alarm_wake_alarm_union_t; -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM__INIT__H +extern alarm_wake_alarm_union_t alarm_wake_alarm; + +extern void alarm_set_wakeup_reason(samd_sleep_source_t reason); +extern void alarm_get_wakeup_cause(void); +extern void alarm_reset(void); diff --git a/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.c b/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.c index 9289a66a87..dd2509b8c4 100644 --- a/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.c +++ b/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.c @@ -31,9 +31,9 @@ #include "hal/include/hal_gpio.h" // #include +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/pin/PinAlarm.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/microcontroller/Pin.h" #include "common-hal/alarm/__init__.h" // This variable stores whether a PinAlarm woke in light sleep or fake deep sleep @@ -128,12 +128,11 @@ mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t return mp_const_none; } -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(uint32_t TAMPID) { - // Create tamper alarm that caused wakeup from deep sleep +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(uint32_t TAMPID) { + alarm_pin_pinalarm_obj_t *const alarm = &alarm_wake_alarm.pin_alarm; for (samd_tamper_pin_t *t = TAMPER_PINS; t->n >= 0; t++) { if (TAMPID & (1 << t->n)) { - alarm_pin_pinalarm_obj_t *alarm = m_new_obj(alarm_pin_pinalarm_obj_t); alarm->base.type = &alarm_pin_pinalarm_type; alarm->pin = t->pin; return alarm; diff --git a/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.h b/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.h index 57dc7d6c65..ee9ea07b71 100644 --- a/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.h +++ b/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.h @@ -24,12 +24,13 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H +#pragma once #include "py/obj.h" #include "py/objtuple.h" +#include "shared-bindings/microcontroller/Pin.h" + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -39,7 +40,7 @@ typedef struct { } alarm_pin_pinalarm_obj_t; mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(uint32_t TAMPID); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(uint32_t TAMPID); void pin_alarm_callback(uint8_t num); void alarm_pin_pinalarm_reset(void); @@ -47,5 +48,3 @@ void alarm_pin_pinalarm_deinit_alarms(size_t n_alarms, const mp_obj_t *alarms); void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); void alarm_pin_pinalarm_prepare_for_deep_sleep(void); bool alarm_pin_pinalarm_woke_this_cycle(void); - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H diff --git a/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.c b/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.c index 3f116f07e3..2ead87428b 100644 --- a/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.c +++ b/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.c @@ -27,9 +27,9 @@ #include "py/runtime.h" #include "hpl/pm/hpl_pm_base.h" +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/time/TimeAlarm.h" #include "shared-bindings/time/__init__.h" -#include "common-hal/alarm/__init__.h" #include "supervisor/port.h" STATIC volatile bool woke_up = false; @@ -58,13 +58,14 @@ mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj return mp_const_none; } -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void) { - alarm_time_timealarm_obj_t *timer = m_new_obj(alarm_time_timealarm_obj_t); - timer->base.type = &alarm_time_timealarm_type; +mp_obj_t alarm_time_timealarm_record_wake_alarm(void) { + alarm_time_timealarm_obj_t *const alarm = &alarm_wake_alarm.time_alarm; + + alarm->base.type = &alarm_time_timealarm_type; // TODO: Set monotonic_time based on the RTC state. // Or don't, most of the other ports don't have this either. - timer->monotonic_time = 0.0f; - return timer; + alarm->monotonic_time = 0.0f; + return alarm; } void time_alarm_callback(void) { diff --git a/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.h b/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.h index a6102dee6b..ee517e0236 100644 --- a/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.h +++ b/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H +#pragma once #include "py/obj.h" @@ -35,12 +34,10 @@ typedef struct { } alarm_time_timealarm_obj_t; mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void); +mp_obj_t alarm_time_timealarm_record_wake_alarm(void); void time_alarm_callback(void); bool alarm_time_timealarm_woke_this_cycle(void); void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); void alarm_time_timealarm_reset(void); void alarm_time_timealarm_prepare_for_deep_sleep(void); - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H diff --git a/ports/atmel-samd/common-hal/alarm/touch/TouchAlarm.h b/ports/atmel-samd/common-hal/alarm/touch/TouchAlarm.h index d7f0f8cf1d..59f202c69d 100644 --- a/ports/atmel-samd/common-hal/alarm/touch/TouchAlarm.h +++ b/ports/atmel-samd/common-hal/alarm/touch/TouchAlarm.h @@ -24,12 +24,9 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TOUCHALARM_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TOUCHALARM_H +#pragma once typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; } alarm_touch_touchalarm_obj_t; - -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TOUCHALARM_H diff --git a/ports/espressif/common-hal/alarm/SleepMemory.h b/ports/espressif/common-hal/alarm/SleepMemory.h index 37d6cd371f..626f748a67 100644 --- a/ports/espressif/common-hal/alarm/SleepMemory.h +++ b/ports/espressif/common-hal/alarm/SleepMemory.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_ALARM_SLEEPMEMORY_H -#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_ALARM_SLEEPMEMORY_H +#pragma once #include "py/obj.h" @@ -49,5 +48,3 @@ typedef struct { } alarm_sleep_memory_obj_t; extern void alarm_sleep_memory_reset(void); - -#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_ALARM_SLEEPMEMORY_H diff --git a/ports/espressif/common-hal/alarm/__init__.c b/ports/espressif/common-hal/alarm/__init__.c index 4183b7e61b..02ce3e1082 100644 --- a/ports/espressif/common-hal/alarm/__init__.c +++ b/ports/espressif/common-hal/alarm/__init__.c @@ -58,6 +58,10 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; +// Non-heap alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// This object lives across VM instantiations, so none of these objects can contain references to the heap. +alarm_wake_alarm_union_t alarm_wake_alarm; + void alarm_reset(void) { alarm_sleep_memory_reset(); alarm_pin_pinalarm_reset(); @@ -90,27 +94,27 @@ bool common_hal_alarm_woken_from_sleep(void) { return _get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED; } -mp_obj_t common_hal_alarm_create_wake_alarm(void) { +mp_obj_t common_hal_alarm_record_wake_alarm(void) { // If woken from deep sleep, create a copy alarm similar to what would have // been passed in originally. Otherwise, just return none esp_sleep_wakeup_cause_t cause = _get_wakeup_cause(); switch (cause) { case ESP_SLEEP_WAKEUP_TIMER: { - return alarm_time_timealarm_create_wakeup_alarm(); + return alarm_time_timealarm_record_wake_alarm(); } case ESP_SLEEP_WAKEUP_GPIO: case ESP_SLEEP_WAKEUP_EXT0: case ESP_SLEEP_WAKEUP_EXT1: { - return alarm_pin_pinalarm_create_wakeup_alarm(); + return alarm_pin_pinalarm_record_wake_alarm(); } case ESP_SLEEP_WAKEUP_TOUCHPAD: { - return alarm_touch_touchalarm_create_wakeup_alarm(); + return alarm_touch_touchalarm_record_wake_alarm(); } case ESP_SLEEP_WAKEUP_ULP: { - return alarm_coproc_coprocalarm_create_wakeup_alarm(); + return alarm_coproc_coprocalarm_record_wake_alarm(); } case ESP_SLEEP_WAKEUP_UNDEFINED: diff --git a/ports/espressif/common-hal/alarm/__init__.h b/ports/espressif/common-hal/alarm/__init__.h index ae7286db76..9f762f9c00 100644 --- a/ports/espressif/common-hal/alarm/__init__.h +++ b/ports/espressif/common-hal/alarm/__init__.h @@ -24,13 +24,22 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_ALARM__INIT__H -#define MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_ALARM__INIT__H +#pragma once #include "common-hal/alarm/SleepMemory.h" +#include "common-hal/alarm/coproc/CoprocAlarm.h" +#include "common-hal/alarm/pin/PinAlarm.h" +#include "common-hal/alarm/time/TimeAlarm.h" +#include "common-hal/alarm/touch/TouchAlarm.h" -const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; +typedef union { + alarm_coproc_coprocalarm_obj_t coproc_alarm; + alarm_pin_pinalarm_obj_t pin_alarm; + alarm_time_timealarm_obj_t time_alarm; + alarm_touch_touchalarm_obj_t touch_alarm; +} alarm_wake_alarm_union_t; + +extern alarm_wake_alarm_union_t alarm_wake_alarm; +extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; extern void alarm_reset(void); - -#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_ALARM__INIT__H diff --git a/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c b/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c index aa49e13a2a..9ee187d27c 100644 --- a/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c +++ b/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/coproc/CoprocAlarm.h" #include "shared-bindings/coproc/__init__.h" @@ -47,9 +48,9 @@ mp_obj_t alarm_coproc_coprocalarm_find_triggered_alarm(const size_t n_alarms, co return mp_const_none; } -mp_obj_t alarm_coproc_coprocalarm_create_wakeup_alarm(void) { - // Create CoprocAlarm object. - alarm_coproc_coprocalarm_obj_t *alarm = m_new_obj(alarm_coproc_coprocalarm_obj_t); +mp_obj_t alarm_coproc_coprocalarm_record_wake_alarm(void) { + alarm_coproc_coprocalarm_obj_t *const alarm = &alarm_wake_alarm.coproc_alarm; + alarm->base.type = &alarm_coproc_coprocalarm_type; return alarm; } @@ -111,7 +112,7 @@ mp_obj_t alarm_coproc_coprocalarm_find_triggered_alarm(const size_t n_alarms, co return mp_const_none; } -mp_obj_t alarm_coproc_coprocalarm_create_wakeup_alarm(void) { +mp_obj_t alarm_coproc_coprocalarm_record_wake_alarm(void) { return mp_const_none; } diff --git a/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.h b/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.h index d70e90265d..57a9bcdbff 100644 --- a/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.h +++ b/ports/espressif/common-hal/alarm/coproc/CoprocAlarm.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_ALARM_COPROC_COPROCALARM_H -#define MICROPY_INCLUDED_COMMON_HAL_ALARM_COPROC_COPROCALARM_H +#pragma once #include "py/obj.h" #include "py/runtime.h" @@ -38,11 +37,9 @@ typedef struct { } alarm_coproc_coprocalarm_obj_t; mp_obj_t alarm_coproc_coprocalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_coproc_coprocalarm_create_wakeup_alarm(void); +mp_obj_t alarm_coproc_coprocalarm_record_wake_alarm(void); void alarm_coproc_coprocalarm_prepare_for_deep_sleep(void); void alarm_coproc_coprocalarm_reset(void); void alarm_coproc_coprocalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms); bool alarm_coproc_coprocalarm_woke_this_cycle(void); - -#endif // MICROPY_INCLUDED_COMMON_HAL_ALARM_COPROC_COPROCALARM_H diff --git a/ports/espressif/common-hal/alarm/pin/PinAlarm.c b/ports/espressif/common-hal/alarm/pin/PinAlarm.c index bc89cc9ee7..6db184a4dd 100644 --- a/ports/espressif/common-hal/alarm/pin/PinAlarm.c +++ b/ports/espressif/common-hal/alarm/pin/PinAlarm.c @@ -26,10 +26,10 @@ */ #include "py/runtime.h" - #include "supervisor/port.h" + +#include "common-hal/alarm/__init__.h" #include "shared-bindings/alarm/pin/PinAlarm.h" -#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/__init__.h" #include "esp_sleep.h" @@ -111,7 +111,7 @@ mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t return mp_const_none; } -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void) { +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void) { esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause(); // Pin status will persist into a fake deep sleep @@ -135,7 +135,8 @@ mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void) { } } - alarm_pin_pinalarm_obj_t *alarm = m_new_obj(alarm_pin_pinalarm_obj_t); + alarm_pin_pinalarm_obj_t *const alarm = &alarm_wake_alarm.pin_alarm; + alarm->base.type = &alarm_pin_pinalarm_type; alarm->pin = NULL; // Map the pin number back to a pin object. diff --git a/ports/espressif/common-hal/alarm/pin/PinAlarm.h b/ports/espressif/common-hal/alarm/pin/PinAlarm.h index cbc20b996c..309b27c2e9 100644 --- a/ports/espressif/common-hal/alarm/pin/PinAlarm.h +++ b/ports/espressif/common-hal/alarm/pin/PinAlarm.h @@ -24,9 +24,13 @@ * THE SOFTWARE. */ +#pragma once + #include "py/obj.h" #include "py/objtuple.h" +#include "shared-bindings/microcontroller/Pin.h" + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -35,7 +39,7 @@ typedef struct { } alarm_pin_pinalarm_obj_t; mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void); void alarm_pin_pinalarm_prepare_for_deep_sleep(void); void alarm_pin_pinalarm_reset(void); diff --git a/ports/espressif/common-hal/alarm/time/TimeAlarm.c b/ports/espressif/common-hal/alarm/time/TimeAlarm.c index 973da33c67..758e3c8f58 100644 --- a/ports/espressif/common-hal/alarm/time/TimeAlarm.c +++ b/ports/espressif/common-hal/alarm/time/TimeAlarm.c @@ -31,6 +31,7 @@ #include "components/esp_timer/include/esp_timer.h" +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/time/TimeAlarm.h" #include "shared-bindings/time/__init__.h" @@ -51,12 +52,13 @@ mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj return mp_const_none; } -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void) { - alarm_time_timealarm_obj_t *timer = m_new_obj(alarm_time_timealarm_obj_t); - timer->base.type = &alarm_time_timealarm_type; +mp_obj_t alarm_time_timealarm_record_wake_alarm(void) { + alarm_time_timealarm_obj_t *const alarm = &alarm_wake_alarm.time_alarm; + + alarm->base.type = &alarm_time_timealarm_type; // TODO: Set monotonic_time based on the RTC state. - timer->monotonic_time = 0.0f; - return timer; + alarm->monotonic_time = 0.0f; + return alarm; } esp_timer_handle_t pretend_sleep_timer; diff --git a/ports/espressif/common-hal/alarm/time/TimeAlarm.h b/ports/espressif/common-hal/alarm/time/TimeAlarm.h index 36986e06b2..c679c3cd9f 100644 --- a/ports/espressif/common-hal/alarm/time/TimeAlarm.h +++ b/ports/espressif/common-hal/alarm/time/TimeAlarm.h @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#pragma once #include "py/obj.h" @@ -33,7 +34,7 @@ typedef struct { } alarm_time_timealarm_obj_t; mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void); +mp_obj_t alarm_time_timealarm_record_wake_alarm(void); void alarm_time_timealarm_reset(void); void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); diff --git a/ports/espressif/common-hal/alarm/touch/TouchAlarm.c b/ports/espressif/common-hal/alarm/touch/TouchAlarm.c index ad55e1e056..232614ce32 100644 --- a/ports/espressif/common-hal/alarm/touch/TouchAlarm.c +++ b/ports/espressif/common-hal/alarm/touch/TouchAlarm.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/touch/TouchAlarm.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Pin.h" @@ -52,9 +53,9 @@ mp_obj_t alarm_touch_touchalarm_find_triggered_alarm(const size_t n_alarms, cons return mp_const_none; } -mp_obj_t alarm_touch_touchalarm_create_wakeup_alarm(void) { - // Create TouchAlarm object. - alarm_touch_touchalarm_obj_t *alarm = m_new_obj(alarm_touch_touchalarm_obj_t); +mp_obj_t alarm_touch_touchalarm_record_wake_alarm(void) { + alarm_touch_touchalarm_obj_t *const alarm = &alarm_wake_alarm.touch_alarm; + alarm->base.type = &alarm_touch_touchalarm_type; alarm->pin = NULL; diff --git a/ports/espressif/common-hal/alarm/touch/TouchAlarm.h b/ports/espressif/common-hal/alarm/touch/TouchAlarm.h index df2521c12a..0f35063ee4 100644 --- a/ports/espressif/common-hal/alarm/touch/TouchAlarm.h +++ b/ports/espressif/common-hal/alarm/touch/TouchAlarm.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H -#define MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H +#pragma once #include "py/obj.h" #include "common-hal/microcontroller/Pin.h" @@ -36,11 +35,9 @@ typedef struct { } alarm_touch_touchalarm_obj_t; mp_obj_t alarm_touch_touchalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_touch_touchalarm_create_wakeup_alarm(void); +mp_obj_t alarm_touch_touchalarm_record_wake_alarm(void); void alarm_touch_touchalarm_prepare_for_deep_sleep(void); void alarm_touch_touchalarm_reset(void); void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms); bool alarm_touch_touchalarm_woke_this_cycle(void); - -#endif // MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H diff --git a/ports/nrf/common-hal/alarm/SleepMemory.h b/ports/nrf/common-hal/alarm/SleepMemory.h index 8fd702ea83..bb50c36e89 100644 --- a/ports/nrf/common-hal/alarm/SleepMemory.h +++ b/ports/nrf/common-hal/alarm/SleepMemory.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H +#pragma once #include "py/obj.h" @@ -37,5 +36,3 @@ typedef struct { extern void set_memory_retention(void); extern void alarm_sleep_memory_reset(void); - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H diff --git a/ports/nrf/common-hal/alarm/__init__.c b/ports/nrf/common-hal/alarm/__init__.c index 75f616c805..b3f9979ae0 100644 --- a/ports/nrf/common-hal/alarm/__init__.c +++ b/ports/nrf/common-hal/alarm/__init__.c @@ -56,6 +56,10 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; +// Non-heap alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// This object lives across VM instantiations, so none of these objects can contain references to the heap. +alarm_wake_alarm_union_t alarm_wake_alarm; + void alarm_reset(void) { alarm_sleep_memory_reset(); alarm_pin_pinalarm_reset(); @@ -115,19 +119,19 @@ bool common_hal_alarm_woken_from_sleep(void) { || cause == NRF_SLEEP_WAKEUP_TOUCHPAD; } -mp_obj_t common_hal_alarm_create_wake_alarm(void) { +mp_obj_t common_hal_alarm_record_wake_alarm(void) { // If woken from deep sleep, create a copy alarm similar to what would have // been passed in originally. Otherwise, just return none nrf_sleep_source_t cause = _get_wakeup_cause(); switch (cause) { case NRF_SLEEP_WAKEUP_TIMER: { - return alarm_time_timealarm_create_wakeup_alarm(); + return alarm_time_timealarm_record_wake_alarm(); } case NRF_SLEEP_WAKEUP_TOUCHPAD: { - return alarm_touch_touchalarm_create_wakeup_alarm(); + return alarm_touch_touchalarm_record_wake_alarm(); } case NRF_SLEEP_WAKEUP_GPIO: { - return alarm_pin_pinalarm_create_wakeup_alarm(); + return alarm_pin_pinalarm_record_wake_alarm(); } default: break; diff --git a/ports/nrf/common-hal/alarm/__init__.h b/ports/nrf/common-hal/alarm/__init__.h index 47051dbd72..7c4634610d 100644 --- a/ports/nrf/common-hal/alarm/__init__.h +++ b/ports/nrf/common-hal/alarm/__init__.h @@ -25,10 +25,12 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM__INIT__H -#define MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM__INIT__H +#pragma once #include "common-hal/alarm/SleepMemory.h" +#include "common-hal/alarm/pin/PinAlarm.h" +#include "common-hal/alarm/time/TimeAlarm.h" +#include "common-hal/alarm/touch/TouchAlarm.h" typedef enum { NRF_SLEEP_WAKEUP_UNDEFINED, @@ -40,6 +42,13 @@ typedef enum { NRF_SLEEP_WAKEUP_ZZZ } nrf_sleep_source_t; +typedef union { + alarm_pin_pinalarm_obj_t pin_alarm; + alarm_time_timealarm_obj_t time_alarm; + alarm_touch_touchalarm_obj_t touch_alarm; +} alarm_wake_alarm_union_t; + +extern alarm_wake_alarm_union_t alarm_wake_alarm; extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; enum { @@ -52,5 +61,3 @@ extern uint8_t sleepmem_wakeup_event; extern uint8_t sleepmem_wakeup_pin; extern void alarm_reset(void); - -#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM__INIT__H diff --git a/ports/nrf/common-hal/alarm/pin/PinAlarm.c b/ports/nrf/common-hal/alarm/pin/PinAlarm.c index 9d0f8f4b88..5bef48773e 100644 --- a/ports/nrf/common-hal/alarm/pin/PinAlarm.c +++ b/ports/nrf/common-hal/alarm/pin/PinAlarm.c @@ -30,10 +30,9 @@ #include #include +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/pin/PinAlarm.h" #include "shared-bindings/microcontroller/__init__.h" -#include "shared-bindings/microcontroller/Pin.h" -#include "common-hal/alarm/__init__.h" #include "nrfx.h" #include "nrf_gpio.h" @@ -101,8 +100,9 @@ mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t return mp_const_none; } -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void) { - alarm_pin_pinalarm_obj_t *alarm = m_new_obj(alarm_pin_pinalarm_obj_t); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void) { + alarm_pin_pinalarm_obj_t *const alarm = &alarm_wake_alarm.pin_alarm; + alarm->base.type = &alarm_pin_pinalarm_type; alarm->pin = NULL; // Map the pin number back to a pin object. diff --git a/ports/nrf/common-hal/alarm/pin/PinAlarm.h b/ports/nrf/common-hal/alarm/pin/PinAlarm.h index 87b7b9833c..9181cb6094 100644 --- a/ports/nrf/common-hal/alarm/pin/PinAlarm.h +++ b/ports/nrf/common-hal/alarm/pin/PinAlarm.h @@ -24,9 +24,13 @@ * THE SOFTWARE. */ +#pragma once + #include "py/obj.h" #include "py/objtuple.h" +#include "shared-bindings/microcontroller/Pin.h" + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -35,7 +39,7 @@ typedef struct { } alarm_pin_pinalarm_obj_t; mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void); void alarm_pin_pinalarm_reset(void); void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); diff --git a/ports/nrf/common-hal/alarm/time/TimeAlarm.c b/ports/nrf/common-hal/alarm/time/TimeAlarm.c index 03b91068f9..a69e299bc2 100644 --- a/ports/nrf/common-hal/alarm/time/TimeAlarm.c +++ b/ports/nrf/common-hal/alarm/time/TimeAlarm.c @@ -28,7 +28,7 @@ #include "py/runtime.h" #include -#include "common-hal/alarm/__init__.h" +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/time/TimeAlarm.h" #include "shared-bindings/time/__init__.h" @@ -49,12 +49,13 @@ mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj return mp_const_none; } -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void) { - alarm_time_timealarm_obj_t *timer = m_new_obj(alarm_time_timealarm_obj_t); - timer->base.type = &alarm_time_timealarm_type; +mp_obj_t alarm_time_timealarm_record_wake_alarm(void) { + alarm_time_timealarm_obj_t *const alarm = &alarm_wake_alarm.time_alarm; + + alarm->base.type = &alarm_time_timealarm_type; // TODO: Set monotonic_time based on the RTC state. - timer->monotonic_time = 0.0f; - return timer; + alarm->monotonic_time = 0.0f; + return alarm; } bool alarm_time_timealarm_woke_this_cycle(void) { diff --git a/ports/nrf/common-hal/alarm/time/TimeAlarm.h b/ports/nrf/common-hal/alarm/time/TimeAlarm.h index 734feb1c87..c5f76c8003 100644 --- a/ports/nrf/common-hal/alarm/time/TimeAlarm.h +++ b/ports/nrf/common-hal/alarm/time/TimeAlarm.h @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#pragma once #include "py/obj.h" @@ -37,7 +38,7 @@ extern void port_disable_interrupt_after_ticks_ch(uint32_t channel); extern void port_interrupt_after_ticks_ch(uint32_t channel, uint32_t ticks); mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void); +mp_obj_t alarm_time_timealarm_record_wake_alarm(void); bool alarm_time_timealarm_woke_this_cycle(void); void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); diff --git a/ports/nrf/common-hal/alarm/touch/TouchAlarm.c b/ports/nrf/common-hal/alarm/touch/TouchAlarm.c index f8daf50f54..5c1d489262 100644 --- a/ports/nrf/common-hal/alarm/touch/TouchAlarm.c +++ b/ports/nrf/common-hal/alarm/touch/TouchAlarm.c @@ -25,6 +25,7 @@ */ #include "py/runtime.h" + #include "shared-bindings/alarm/touch/TouchAlarm.h" #include "shared-bindings/microcontroller/__init__.h" @@ -39,7 +40,7 @@ mp_obj_t alarm_touch_touchalarm_find_triggered_alarm(const size_t n_alarms, cons return mp_const_none; } -mp_obj_t alarm_touch_touchalarm_create_wakeup_alarm(void) { +mp_obj_t alarm_touch_touchalarm_record_wake_alarm(void) { return mp_const_none; } diff --git a/ports/nrf/common-hal/alarm/touch/TouchAlarm.h b/ports/nrf/common-hal/alarm/touch/TouchAlarm.h index 58ad8c20fe..31aa30117a 100644 --- a/ports/nrf/common-hal/alarm/touch/TouchAlarm.h +++ b/ports/nrf/common-hal/alarm/touch/TouchAlarm.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H -#define MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H +#pragma once #include "py/obj.h" #include "common-hal/microcontroller/Pin.h" @@ -37,11 +36,9 @@ typedef struct { // Find the alarm object that caused us to wake up or create an equivalent one. mp_obj_t alarm_touch_touchalarm_find_triggered_alarm(const size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_touch_touchalarm_create_wakeup_alarm(void); +mp_obj_t alarm_touch_touchalarm_record_wake_alarm(void); // Check for the wake up alarm from pretend deep sleep. void alarm_touch_touchalarm_set_alarm(const bool deep_sleep, const size_t n_alarms, const mp_obj_t *alarms); void alarm_touch_touchalarm_prepare_for_deep_sleep(void); bool alarm_touch_touchalarm_woke_this_cycle(void); void alarm_touch_touchalarm_reset(void); - -#endif // MICROPY_INCLUDED_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index 0ed1ecd547..e67f641900 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -64,11 +64,11 @@ #include "lib/tinyusb/src/device/usbd.h" -#ifdef CIRCUITPY_AUDIOBUSIO +#if CIRCUITPY_AUDIOBUSIO #include "common-hal/audiobusio/I2SOut.h" #endif -#ifdef CIRCUITPY_AUDIOPWMIO +#if CIRCUITPY_AUDIOPWMIO #include "common-hal/audiopwmio/PWMAudioOut.h" #endif diff --git a/ports/raspberrypi/common-hal/alarm/SleepMemory.h b/ports/raspberrypi/common-hal/alarm/SleepMemory.h index 59d0429c3b..14848cd5a0 100644 --- a/ports/raspberrypi/common-hal/alarm/SleepMemory.h +++ b/ports/raspberrypi/common-hal/alarm/SleepMemory.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM_SLEEPMEMORY_H -#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM_SLEEPMEMORY_H +#pragma once #include "py/obj.h" @@ -34,5 +33,3 @@ typedef struct { } alarm_sleep_memory_obj_t; extern void alarm_sleep_memory_reset(void); - -#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM_SLEEPMEMORY_H diff --git a/ports/raspberrypi/common-hal/alarm/__init__.c b/ports/raspberrypi/common-hal/alarm/__init__.c index 9cfa390116..656d15af5a 100644 --- a/ports/raspberrypi/common-hal/alarm/__init__.c +++ b/ports/raspberrypi/common-hal/alarm/__init__.c @@ -96,6 +96,10 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; +// Non-heap alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// This object lives across VM instantiations, so none of these objects can contain references to the heap. +alarm_wake_alarm_union_t alarm_wake_alarm; + void alarm_reset(void) { alarm_sleep_memory_reset(); alarm_pin_pinalarm_reset(); @@ -131,17 +135,17 @@ bool common_hal_alarm_woken_from_sleep(void) { return _get_wakeup_cause() != RP_SLEEP_WAKEUP_UNDEF; } -mp_obj_t common_hal_alarm_create_wake_alarm(void) { +mp_obj_t common_hal_alarm_record_wake_alarm(void) { // If woken from deep sleep, create a copy alarm similar to what would have // been passed in originally. Otherwise, just return none uint8_t cause = _get_wakeup_cause(); switch (cause) { case RP_SLEEP_WAKEUP_RTC: { - return alarm_time_timealarm_create_wakeup_alarm(); + return alarm_time_timealarm_record_wake_alarm(); } case RP_SLEEP_WAKEUP_GPIO: { - return alarm_pin_pinalarm_create_wakeup_alarm(); + return alarm_pin_pinalarm_record_wake_alarm(); } case RP_SLEEP_WAKEUP_UNDEF: diff --git a/ports/raspberrypi/common-hal/alarm/__init__.h b/ports/raspberrypi/common-hal/alarm/__init__.h index 3d5d86f8dc..284e3ddb4e 100644 --- a/ports/raspberrypi/common-hal/alarm/__init__.h +++ b/ports/raspberrypi/common-hal/alarm/__init__.h @@ -24,10 +24,11 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM__INIT__H -#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM__INIT__H +#pragma once #include "common-hal/alarm/SleepMemory.h" +#include "common-hal/alarm/pin/PinAlarm.h" +#include "common-hal/alarm/time/TimeAlarm.h" #include "hardware/regs/clocks.h" @@ -35,8 +36,12 @@ #define RP_SLEEP_WAKEUP_GPIO 1 #define RP_SLEEP_WAKEUP_RTC 2 +typedef union { + alarm_pin_pinalarm_obj_t pin_alarm; + alarm_time_timealarm_obj_t time_alarm; +} alarm_wake_alarm_union_t; + +extern alarm_wake_alarm_union_t alarm_wake_alarm; extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; extern void alarm_reset(void); - -#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM__INIT__H diff --git a/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.c b/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.c index 28ea60d682..0af27cb44f 100644 --- a/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.c +++ b/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.c @@ -26,10 +26,10 @@ #include "py/runtime.h" +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/pin/PinAlarm.h" #include "shared-bindings/microcontroller/__init__.h" #include "common-hal/microcontroller/__init__.h" -#include "shared-bindings/microcontroller/Pin.h" #include "pico/stdlib.h" #include "hardware/gpio.h" @@ -94,10 +94,12 @@ mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t return mp_const_none; } -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void) { - alarm_pin_pinalarm_obj_t *alarm = m_new_obj(alarm_pin_pinalarm_obj_t); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void) { + alarm_pin_pinalarm_obj_t *const alarm = &alarm_wake_alarm.pin_alarm; + alarm->base.type = &alarm_pin_pinalarm_type; // TODO: how to obtain the correct pin from memory? + alarm->pin = NULL; return alarm; } diff --git a/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.h b/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.h index d31ac6c878..9dcbf2b848 100644 --- a/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.h +++ b/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.h @@ -24,9 +24,13 @@ * THE SOFTWARE. */ +#pragma once + #include "py/obj.h" #include "py/objtuple.h" +#include "shared-bindings/microcontroller/Pin.h" + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -36,7 +40,7 @@ typedef struct { } alarm_pin_pinalarm_obj_t; mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void); void alarm_pin_pinalarm_reset(void); void alarm_pin_pinalarm_light_reset(void); diff --git a/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.c b/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.c index 16ac8fc31e..9bfe5cf262 100644 --- a/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.c +++ b/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.c @@ -26,6 +26,7 @@ #include "py/runtime.h" +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/time/TimeAlarm.h" #include "shared-bindings/time/__init__.h" @@ -58,12 +59,13 @@ mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj return mp_const_none; } -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void) { - alarm_time_timealarm_obj_t *timer = m_new_obj(alarm_time_timealarm_obj_t); - timer->base.type = &alarm_time_timealarm_type; +mp_obj_t alarm_time_timealarm_record_wake_alarm(void) { + alarm_time_timealarm_obj_t *const alarm = &alarm_wake_alarm.time_alarm; + + alarm->base.type = &alarm_time_timealarm_type; // TODO: Set monotonic_time based on the RTC state. - timer->monotonic_time = 0.0f; - return timer; + alarm->monotonic_time = 0.0f; + return alarm; } bool alarm_time_timealarm_woke_this_cycle(void) { diff --git a/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.h b/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.h index d5248551ae..0b4c8bce06 100644 --- a/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.h +++ b/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.h @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#pragma once #include "py/obj.h" @@ -33,7 +34,7 @@ typedef struct { } alarm_time_timealarm_obj_t; mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void); +mp_obj_t alarm_time_timealarm_record_wake_alarm(void); void alarm_time_timealarm_reset(void); void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); diff --git a/ports/raspberrypi/common-hal/alarm/touch/TouchAlarm.h b/ports/raspberrypi/common-hal/alarm/touch/TouchAlarm.h index f631293511..6badde145f 100644 --- a/ports/raspberrypi/common-hal/alarm/touch/TouchAlarm.h +++ b/ports/raspberrypi/common-hal/alarm/touch/TouchAlarm.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H -#define MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H +#pragma once #include "py/obj.h" #include "common-hal/microcontroller/Pin.h" @@ -34,5 +33,3 @@ typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; } alarm_touch_touchalarm_obj_t; - -#endif // MICROPY_INCLUDED_RASPBERRYPI_COMMON_HAL_ALARM_TOUCH_TOUCHALARM_H diff --git a/ports/stm/common-hal/alarm/SleepMemory.h b/ports/stm/common-hal/alarm/SleepMemory.h index 0e9fc54904..14848cd5a0 100644 --- a/ports/stm/common-hal/alarm/SleepMemory.h +++ b/ports/stm/common-hal/alarm/SleepMemory.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_SLEEPMEMORY_H -#define MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_SLEEPMEMORY_H +#pragma once #include "py/obj.h" @@ -34,5 +33,3 @@ typedef struct { } alarm_sleep_memory_obj_t; extern void alarm_sleep_memory_reset(void); - -#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_SLEEPMEMORY_H diff --git a/ports/stm/common-hal/alarm/__init__.c b/ports/stm/common-hal/alarm/__init__.c index 26099b0503..78d50b41a8 100644 --- a/ports/stm/common-hal/alarm/__init__.c +++ b/ports/stm/common-hal/alarm/__init__.c @@ -47,6 +47,10 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; +// Non-heap alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// This object lives across VM instantiations, so none of these objects can contain references to the heap. +alarm_wake_alarm_union_t alarm_wake_alarm; + STATIC stm_sleep_source_t true_deep_wake_reason; void alarm_reset(void) { @@ -81,16 +85,16 @@ bool common_hal_alarm_woken_from_sleep(void) { return alarm_get_wakeup_cause() != STM_WAKEUP_UNDEF; } -mp_obj_t common_hal_alarm_create_wake_alarm(void) { +mp_obj_t common_hal_alarm_record_wake_alarm(void) { // If woken from deep sleep, create a copy alarm similar to what would have // been passed in originally. Otherwise, just return none stm_sleep_source_t cause = alarm_get_wakeup_cause(); switch (cause) { case STM_WAKEUP_RTC: { - return alarm_time_timealarm_create_wakeup_alarm(); + return alarm_time_timealarm_record_wake_alarm(); } case STM_WAKEUP_GPIO: { - return alarm_pin_pinalarm_create_wakeup_alarm(); + return alarm_pin_pinalarm_record_wake_alarm(); } case STM_WAKEUP_UNDEF: default: diff --git a/ports/stm/common-hal/alarm/__init__.h b/ports/stm/common-hal/alarm/__init__.h index 6c72364fc3..c87cb3b1eb 100644 --- a/ports/stm/common-hal/alarm/__init__.h +++ b/ports/stm/common-hal/alarm/__init__.h @@ -24,10 +24,11 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM__INIT__H -#define MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM__INIT__H +#pragma once #include "common-hal/alarm/SleepMemory.h" +#include "common-hal/alarm/pin/PinAlarm.h" +#include "common-hal/alarm/time/TimeAlarm.h" extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; @@ -37,10 +38,15 @@ typedef enum { STM_WAKEUP_RTC } stm_sleep_source_t; +typedef union { + alarm_pin_pinalarm_obj_t pin_alarm; + alarm_time_timealarm_obj_t time_alarm; +} alarm_wake_alarm_union_t; + +extern alarm_wake_alarm_union_t alarm_wake_alarm; + #define STM_ALARM_FLAG (RTC->BKP0R) extern void alarm_set_wakeup_reason(stm_sleep_source_t reason); -stm_sleep_source_t alarm_get_wakeup_cause(void); +extern stm_sleep_source_t alarm_get_wakeup_cause(void); extern void alarm_reset(void); - -#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM__INIT__H diff --git a/ports/stm/common-hal/alarm/pin/PinAlarm.c b/ports/stm/common-hal/alarm/pin/PinAlarm.c index 0e5fa9e94d..161bd9fea6 100644 --- a/ports/stm/common-hal/alarm/pin/PinAlarm.c +++ b/ports/stm/common-hal/alarm/pin/PinAlarm.c @@ -26,6 +26,7 @@ #include "py/runtime.h" +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/pin/PinAlarm.h" #include "peripherals/exti.h" @@ -100,8 +101,9 @@ mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t return mp_const_none; } -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void) { - alarm_pin_pinalarm_obj_t *alarm = m_new_obj(alarm_pin_pinalarm_obj_t); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void) { + alarm_pin_pinalarm_obj_t *const alarm = &alarm_wake_alarm.pin_alarm; + alarm->base.type = &alarm_pin_pinalarm_type; // TODO: replace this if/when other WKUP pins are supported alarm->pin = &pin_PA00; diff --git a/ports/stm/common-hal/alarm/pin/PinAlarm.h b/ports/stm/common-hal/alarm/pin/PinAlarm.h index bc52849a53..77e0c6143e 100644 --- a/ports/stm/common-hal/alarm/pin/PinAlarm.h +++ b/ports/stm/common-hal/alarm/pin/PinAlarm.h @@ -24,12 +24,13 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_PINALARM_H -#define MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_PINALARM_H +#pragma once #include "py/obj.h" #include "py/objtuple.h" +#include "shared-bindings/microcontroller/Pin.h" + typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; @@ -38,11 +39,9 @@ typedef struct { } alarm_pin_pinalarm_obj_t; mp_obj_t alarm_pin_pinalarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_pin_pinalarm_create_wakeup_alarm(void); +mp_obj_t alarm_pin_pinalarm_record_wake_alarm(void); void alarm_pin_pinalarm_reset(void); void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); void alarm_pin_pinalarm_prepare_for_deep_sleep(void); bool alarm_pin_pinalarm_woke_this_cycle(void); - -#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_PINALARM_H diff --git a/ports/stm/common-hal/alarm/time/TimeAlarm.c b/ports/stm/common-hal/alarm/time/TimeAlarm.c index 6d508cd8cc..9ba6ed7658 100644 --- a/ports/stm/common-hal/alarm/time/TimeAlarm.c +++ b/ports/stm/common-hal/alarm/time/TimeAlarm.c @@ -26,6 +26,7 @@ #include "py/runtime.h" +#include "shared-bindings/alarm/__init__.h" #include "shared-bindings/alarm/time/TimeAlarm.h" #include "shared-bindings/time/__init__.h" #include "peripherals/rtc.h" @@ -53,12 +54,13 @@ mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj return mp_const_none; } -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void) { - alarm_time_timealarm_obj_t *timer = m_new_obj(alarm_time_timealarm_obj_t); - timer->base.type = &alarm_time_timealarm_type; +mp_obj_t alarm_time_timealarm_record_wake_alarm(void) { + alarm_time_timealarm_obj_t *const alarm = &alarm_wake_alarm.time_alarm; + + alarm->base.type = &alarm_time_timealarm_type; // TODO: Set monotonic_time based on the RTC state. - timer->monotonic_time = 0.0f; - return timer; + alarm->monotonic_time = 0.0f; + return alarm; } // This is run in the timer task. We use it to wake the main CircuitPython task. diff --git a/ports/stm/common-hal/alarm/time/TimeAlarm.h b/ports/stm/common-hal/alarm/time/TimeAlarm.h index 48531ebdf6..e3b9caadcd 100644 --- a/ports/stm/common-hal/alarm/time/TimeAlarm.h +++ b/ports/stm/common-hal/alarm/time/TimeAlarm.h @@ -24,8 +24,7 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_TIMEALARM_H -#define MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_TIMEALARM_H +#pragma once #include "py/obj.h" @@ -35,12 +34,10 @@ typedef struct { } alarm_time_timealarm_obj_t; mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); -mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void); +mp_obj_t alarm_time_timealarm_record_wake_alarm(void); bool alarm_time_timealarm_woke_this_cycle(void); void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); void alarm_time_timealarm_reset(void); void alarm_time_timealarm_prepare_for_deep_sleep(void); - -#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_TIMEALARM_H diff --git a/ports/stm/common-hal/alarm/touch/TouchAlarm.h b/ports/stm/common-hal/alarm/touch/TouchAlarm.h index 6c89353f93..59f202c69d 100644 --- a/ports/stm/common-hal/alarm/touch/TouchAlarm.h +++ b/ports/stm/common-hal/alarm/touch/TouchAlarm.h @@ -24,12 +24,9 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_TOUCHALARM_H -#define MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_TOUCHALARM_H +#pragma once typedef struct { mp_obj_base_t base; const mcu_pin_obj_t *pin; } alarm_touch_touchalarm_obj_t; - -#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_TOUCHALARM_H diff --git a/shared-bindings/alarm/__init__.h b/shared-bindings/alarm/__init__.h index ea86e44f2f..82bfae4286 100644 --- a/shared-bindings/alarm/__init__.h +++ b/shared-bindings/alarm/__init__.h @@ -55,7 +55,7 @@ extern void common_hal_alarm_pretending_deep_sleep(void); extern mp_obj_t shared_alarm_get_wake_alarm(void); // Creates a new alarm object after exiting deep sleep (real or fake) -extern mp_obj_t common_hal_alarm_create_wake_alarm(void); +extern mp_obj_t common_hal_alarm_record_wake_alarm(void); // Saves alarm to global array void shared_alarm_save_wake_alarm(mp_obj_t alarm);