From ded134c346cd6c0f3c2823311163bb900b04bef5 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sat, 29 Oct 2022 16:26:36 -0400 Subject: [PATCH] store wake_alarm in a static object --- main.c | 21 ++++++++++--------- .../atmel-samd/common-hal/alarm/SleepMemory.h | 5 +---- ports/atmel-samd/common-hal/alarm/__init__.c | 13 +++++------- ports/atmel-samd/common-hal/alarm/__init__.h | 18 ++++++++++------ .../common-hal/alarm/pin/PinAlarm.c | 6 +++--- .../common-hal/alarm/pin/PinAlarm.h | 9 ++++---- .../common-hal/alarm/time/TimeAlarm.c | 6 ++++-- .../common-hal/alarm/time/TimeAlarm.h | 7 ++----- .../common-hal/alarm/touch/TouchAlarm.h | 5 +---- ports/atmel-samd/supervisor/port.c | 4 ---- .../espressif/common-hal/alarm/SleepMemory.h | 5 +---- ports/espressif/common-hal/alarm/__init__.c | 17 ++++++--------- ports/espressif/common-hal/alarm/__init__.h | 19 ++++++++++++----- .../common-hal/alarm/coproc/CoprocAlarm.c | 7 +++++-- .../common-hal/alarm/coproc/CoprocAlarm.h | 7 ++----- .../espressif/common-hal/alarm/pin/PinAlarm.c | 8 ++++--- .../espressif/common-hal/alarm/pin/PinAlarm.h | 6 +++++- .../common-hal/alarm/time/TimeAlarm.c | 5 ++++- .../common-hal/alarm/time/TimeAlarm.h | 3 ++- .../common-hal/alarm/touch/TouchAlarm.c | 6 ++++-- .../common-hal/alarm/touch/TouchAlarm.h | 7 ++----- ports/espressif/supervisor/port.c | 8 ------- ports/nrf/common-hal/alarm/SleepMemory.h | 5 +---- ports/nrf/common-hal/alarm/__init__.c | 14 +++++-------- ports/nrf/common-hal/alarm/__init__.h | 15 +++++++++---- ports/nrf/common-hal/alarm/pin/PinAlarm.c | 7 ++++--- ports/nrf/common-hal/alarm/pin/PinAlarm.h | 6 +++++- ports/nrf/common-hal/alarm/time/TimeAlarm.c | 6 ++++-- ports/nrf/common-hal/alarm/time/TimeAlarm.h | 3 ++- ports/nrf/common-hal/alarm/touch/TouchAlarm.c | 3 ++- ports/nrf/common-hal/alarm/touch/TouchAlarm.h | 7 ++----- ports/nrf/supervisor/port.c | 12 ++--------- .../common-hal/alarm/SleepMemory.h | 5 +---- ports/raspberrypi/common-hal/alarm/__init__.c | 11 ++++------ ports/raspberrypi/common-hal/alarm/__init__.h | 13 ++++++++---- .../common-hal/alarm/pin/PinAlarm.c | 6 ++++-- .../common-hal/alarm/pin/PinAlarm.h | 6 +++++- .../common-hal/alarm/time/TimeAlarm.c | 5 ++++- .../common-hal/alarm/time/TimeAlarm.h | 3 ++- .../common-hal/alarm/touch/TouchAlarm.h | 5 +---- ports/raspberrypi/supervisor/port.c | 8 ------- ports/stm/common-hal/alarm/SleepMemory.h | 5 +---- ports/stm/common-hal/alarm/__init__.c | 11 ++++------ ports/stm/common-hal/alarm/__init__.h | 16 +++++++++----- ports/stm/common-hal/alarm/pin/PinAlarm.c | 5 ++++- ports/stm/common-hal/alarm/pin/PinAlarm.h | 9 ++++---- ports/stm/common-hal/alarm/time/TimeAlarm.c | 5 ++++- ports/stm/common-hal/alarm/time/TimeAlarm.h | 7 ++----- ports/stm/common-hal/alarm/touch/TouchAlarm.h | 5 +---- ports/stm/supervisor/port.c | 12 ++++------- 50 files changed, 196 insertions(+), 211 deletions(-) diff --git a/main.c b/main.c index fea20d419b..10fdc65224 100644 --- a/main.c +++ b/main.c @@ -747,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. @@ -964,6 +963,9 @@ int __attribute__((used)) main(void) { // 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. @@ -1010,6 +1012,9 @@ int __attribute__((used)) main(void) { serial_write_compressed(translate("soft reboot\n")); } if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) { + // 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; @@ -1018,14 +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. - if (!simulate_reset) { - #if CIRCUITPY_ALARM - shared_alarm_save_wake_alarm(mp_const_none); - #endif - } + #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 2edd3ece12..96d6d9694a 100644 --- a/ports/atmel-samd/common-hal/alarm/__init__.c +++ b/ports/atmel-samd/common-hal/alarm/__init__.c @@ -48,12 +48,9 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; -// Static alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// 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. -static union { - alarm_pin_pinalarm_obj_t pin_alarm; - alarm_time_timealarm_obj_t time_alarm; -} wake_alarm; +alarm_wake_alarm_union_t alarm_wake_alarm; void alarm_reset(void) { // Reset the alarm flag @@ -83,13 +80,13 @@ mp_obj_t common_hal_alarm_record_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_record_wakeup_alarm(&wake_alarm.pin_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_record_wakeup_alarm(&wake_alarm.time_alarm); + return alarm_time_timealarm_record_wake_alarm(); } if (true_deep) { - return alarm_pin_pinalarm_record_wakeup_alarm(&wake_alarm.pin_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 a2c01f0a82..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,8 +128,8 @@ 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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *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)) { diff --git a/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.h b/ports/atmel-samd/common-hal/alarm/pin/PinAlarm.h index 6f767bbffc..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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *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 9c7c89f2c7..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,7 +58,9 @@ 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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm) { +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. diff --git a/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.h b/ports/atmel-samd/common-hal/alarm/time/TimeAlarm.h index b8af80869c..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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm); +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/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 0fafbdb13c..424acece78 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -385,10 +385,6 @@ safe_mode_t port_init(void) { } void reset_port(void) { - #if CIRCUITPY_ALARM - alarm_reset(); - #endif - #if CIRCUITPY_BUSIO reset_sercoms(); #endif 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 7faca8fc0c..02ce3e1082 100644 --- a/ports/espressif/common-hal/alarm/__init__.c +++ b/ports/espressif/common-hal/alarm/__init__.c @@ -58,14 +58,9 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; -// Static alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// 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. -static union { - alarm_pin_pinalarm_obj_t pin_alarm; - alarm_time_timealarm_obj_t time_alarm; - alarm_touch_touchalarm_obj_t touch_alarm; - alarm_coproc_coprocalarm_obj_t coproc_alarm; -} wake_alarm; +alarm_wake_alarm_union_t alarm_wake_alarm; void alarm_reset(void) { alarm_sleep_memory_reset(); @@ -105,21 +100,21 @@ mp_obj_t common_hal_alarm_record_wake_alarm(void) { esp_sleep_wakeup_cause_t cause = _get_wakeup_cause(); switch (cause) { case ESP_SLEEP_WAKEUP_TIMER: { - return alarm_time_timealarm_record_wakeup_alarm(&wake_alarm.time_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_record_wakeup_alarm(&wake_alarm.pin_alarm); + return alarm_pin_pinalarm_record_wake_alarm(); } case ESP_SLEEP_WAKEUP_TOUCHPAD: { - return alarm_touch_touchalarm_record_wakeup_alarm(&wake_alarm.touch_alarm); + return alarm_touch_touchalarm_record_wake_alarm(); } case ESP_SLEEP_WAKEUP_ULP: { - return alarm_coproc_coprocalarm_record_wakeup_alarm(&wake_alarm.coproc_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 8c45cc208c..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,7 +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_record_wakeup_alarm(alarm_coproc_coprocalarm_obj_t *alarm) { +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; } @@ -109,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_record_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 2078d8325f..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_record_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 3868e2f4a8..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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm) { +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,6 +135,8 @@ mp_obj_t alarm_pin_pinalarm_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm) } } + 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 d1d5d1e4f0..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_record_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 f0daabc897..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,7 +52,9 @@ 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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm) { +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. alarm->monotonic_time = 0.0f; diff --git a/ports/espressif/common-hal/alarm/time/TimeAlarm.h b/ports/espressif/common-hal/alarm/time/TimeAlarm.h index edb4d0236f..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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm); +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 35da811ad9..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,8 +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_record_wakeup_alarm(alarm_touch_touchalarm_obj_t *alarm) { - // Create TouchAlarm object. +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 c5cb8da3e9..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_record_wakeup_alarm(alarm_touch_touchalarm_obj_t *alarm); +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/espressif/supervisor/port.c b/ports/espressif/supervisor/port.c index 54e2026c7d..3106f4b991 100644 --- a/ports/espressif/supervisor/port.c +++ b/ports/espressif/supervisor/port.c @@ -60,10 +60,6 @@ #include "peripherals/rmt.h" #include "peripherals/timer.h" -#if CIRCUITPY_ALARM -#include "common-hal/alarm/__init__.h" -#endif - #if CIRCUITPY_COUNTIO || CIRCUITPY_ROTARYIO || CIRCUITPY_FREQUENCYIO #include "peripherals/pcnt.h" #endif @@ -342,10 +338,6 @@ safe_mode_t port_init(void) { } void reset_port(void) { - #if CIRCUITPY_ALARM - alarm_reset(); - #endif - // TODO deinit for esp32-camera #if CIRCUITPY_ESP32_CAMERA esp_camera_deinit(); 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 8501092c9d..b3f9979ae0 100644 --- a/ports/nrf/common-hal/alarm/__init__.c +++ b/ports/nrf/common-hal/alarm/__init__.c @@ -56,13 +56,9 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; -// Static alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// 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. -static union { - alarm_pin_pinalarm_obj_t pin_alarm; - alarm_time_timealarm_obj_t time_alarm; - alarm_touch_touchalarm_obj_t touch_alarm; -} wake_alarm; +alarm_wake_alarm_union_t alarm_wake_alarm; void alarm_reset(void) { alarm_sleep_memory_reset(); @@ -129,13 +125,13 @@ mp_obj_t common_hal_alarm_record_wake_alarm(void) { nrf_sleep_source_t cause = _get_wakeup_cause(); switch (cause) { case NRF_SLEEP_WAKEUP_TIMER: { - return alarm_time_timealarm_record_wakeup_alarm(&wake_alarm.time_alarm); + return alarm_time_timealarm_record_wake_alarm(); } case NRF_SLEEP_WAKEUP_TOUCHPAD: { - return alarm_touch_touchalarm_record_wakeup_alarm(&wake_alarm.touch_alarm); + return alarm_touch_touchalarm_record_wake_alarm(); } case NRF_SLEEP_WAKEUP_GPIO: { - return alarm_pin_pinalarm_record_wakeup_alarm(&wake_alarm.pin_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 b48f0b46a7..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,7 +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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm) { +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 3a16a50d8a..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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm); +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 af1ce694ee..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,7 +49,9 @@ 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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm) { +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. alarm->monotonic_time = 0.0f; diff --git a/ports/nrf/common-hal/alarm/time/TimeAlarm.h b/ports/nrf/common-hal/alarm/time/TimeAlarm.h index 2af5121e10..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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm); +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 9bd2fefcce..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_record_wakeup_alarm(alarm_touch_touchalarm_obj_t *alarm) { +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 d895414cd3..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_record_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 477829c2d2..e67f641900 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -64,15 +64,11 @@ #include "lib/tinyusb/src/device/usbd.h" -#if CIRCUITPY_ALARM -#include "common-hal/alarm/__init__.h" -#endif - -#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 @@ -216,10 +212,6 @@ safe_mode_t port_init(void) { } void reset_port(void) { - #if CIRCUITPY_ALARM - alarm_reset(); - #endif - #if CIRCUITPY_BUSIO i2c_reset(); spi_reset(); 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 c184ab95d9..656d15af5a 100644 --- a/ports/raspberrypi/common-hal/alarm/__init__.c +++ b/ports/raspberrypi/common-hal/alarm/__init__.c @@ -96,12 +96,9 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; -// Static alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// 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. -static union { - alarm_pin_pinalarm_obj_t pin_alarm; - alarm_time_timealarm_obj_t time_alarm; -} wake_alarm; +alarm_wake_alarm_union_t alarm_wake_alarm; void alarm_reset(void) { alarm_sleep_memory_reset(); @@ -144,11 +141,11 @@ mp_obj_t common_hal_alarm_record_wake_alarm(void) { uint8_t cause = _get_wakeup_cause(); switch (cause) { case RP_SLEEP_WAKEUP_RTC: { - return alarm_time_timealarm_record_wakeup_alarm(&wake_alarm.time_alarm); + return alarm_time_timealarm_record_wake_alarm(); } case RP_SLEEP_WAKEUP_GPIO: { - return alarm_pin_pinalarm_record_wakeup_alarm(&wake_alarm.pin_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 388cba3d8a..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,7 +94,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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm) { +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; diff --git a/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.h b/ports/raspberrypi/common-hal/alarm/pin/PinAlarm.h index 1e576b9382..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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm); +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 81eb78bcb1..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,7 +59,9 @@ 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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm) { +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. alarm->monotonic_time = 0.0f; diff --git a/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.h b/ports/raspberrypi/common-hal/alarm/time/TimeAlarm.h index 4d5569a569..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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm); +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/raspberrypi/supervisor/port.c b/ports/raspberrypi/supervisor/port.c index d1eff62d56..781d2b11d2 100644 --- a/ports/raspberrypi/supervisor/port.c +++ b/ports/raspberrypi/supervisor/port.c @@ -41,10 +41,6 @@ #include "shared-bindings/rtc/__init__.h" #include "shared-bindings/pwmio/PWMOut.h" -#if CIRCUITPY_ALARM -#include "common-hal/alarm/__init__.h" -#endif - #if CIRCUITPY_SSL #include "common-hal/ssl/__init__.h" #endif @@ -158,10 +154,6 @@ safe_mode_t port_init(void) { } void reset_port(void) { - #if CIRCUITPY_ALARM - alarm_reset(); - #endif - #if CIRCUITPY_BUSIO reset_i2c(); reset_spi(); 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 4763403842..78d50b41a8 100644 --- a/ports/stm/common-hal/alarm/__init__.c +++ b/ports/stm/common-hal/alarm/__init__.c @@ -47,12 +47,9 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; -// Static alarm object recording alarm (if any) that woke up CircuitPython after light or deep sleep. +// 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. -static union { - alarm_pin_pinalarm_obj_t pin_alarm; - alarm_time_timealarm_obj_t time_alarm; -} wake_alarm; +alarm_wake_alarm_union_t alarm_wake_alarm; STATIC stm_sleep_source_t true_deep_wake_reason; @@ -94,10 +91,10 @@ mp_obj_t common_hal_alarm_record_wake_alarm(void) { stm_sleep_source_t cause = alarm_get_wakeup_cause(); switch (cause) { case STM_WAKEUP_RTC: { - return alarm_time_timealarm_record_wakeup_alarm(&wake_alarm.time_alarm); + return alarm_time_timealarm_record_wake_alarm(); } case STM_WAKEUP_GPIO: { - return alarm_pin_pinalarm_record_wakeup_alarm(&wake_alarm.pin_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 50bd06b59a..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,7 +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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm) { +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 1490944fdf..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_record_wakeup_alarm(alarm_pin_pinalarm_obj_t *alarm); +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 a1ebd3fa05..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,7 +54,9 @@ 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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm) { +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. alarm->monotonic_time = 0.0f; diff --git a/ports/stm/common-hal/alarm/time/TimeAlarm.h b/ports/stm/common-hal/alarm/time/TimeAlarm.h index 05e77a90be..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_record_wakeup_alarm(alarm_time_timealarm_obj_t *alarm); +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/ports/stm/supervisor/port.c b/ports/stm/supervisor/port.c index adfce78cba..97bd1c4982 100644 --- a/ports/stm/supervisor/port.c +++ b/ports/stm/supervisor/port.c @@ -33,10 +33,7 @@ #include "common-hal/microcontroller/Pin.h" #include "shared-bindings/microcontroller/__init__.h" -#if CIRCUITPY_ALARM -#include "common-hal/alarm/__init__.h" -#endif -#if CIRCUITPY_AUDIOPWMIO +#ifdef CIRCUITPY_AUDIOPWMIO #include "common-hal/audiopwmio/PWMAudioOut.h" #endif #if CIRCUITPY_BUSIO @@ -60,6 +57,9 @@ #if CIRCUITPY_PULSEIO || CIRCUITPY_ALARM #include "peripherals/exti.h" #endif +#if CIRCUITPY_ALARM +#include "common-hal/alarm/__init__.h" +#endif #if CIRCUITPY_RTC #include "shared-bindings/rtc/__init__.h" #endif @@ -244,10 +244,6 @@ void SysTick_Handler(void) { void reset_port(void) { reset_all_pins(); - #if CIRCUITPY_ALARM - alarm_reset(); - #endif - #if CIRCUITPY_RTC rtc_reset(); #endif