diff --git a/main.c b/main.c index 9979d9a59d..c9226bf3e5 100755 --- a/main.c +++ b/main.c @@ -163,7 +163,7 @@ STATIC void start_mp(supervisor_allocation* heap) { // Record which alarm woke us up, if any. An object may be created so the heap must be functional. shared_alarm_save_wake_alarm(); // Reset alarm module only after we retrieved the wakeup alarm. - common_hal_alarm_reset(); + alarm_reset(); #endif #if CIRCUITPY_NETWORK @@ -293,9 +293,13 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { stack_resize(); filesystem_flush(); supervisor_allocation* heap = allocate_remaining_memory(); + + // Prepare the VM state. Includes an alarm check/reset for sleep. start_mp(heap); + // This is where the user's python code is actually executed: found_main = maybe_run_list(supported_filenames, &result); + // If that didn't work, double check the extensions #if CIRCUITPY_FULL_BUILD if (!found_main){ found_main = maybe_run_list(double_extension_filenames, &result); @@ -305,6 +309,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) { } #endif + // Finished executing python code. Cleanup includes a board reset. cleanup_after_vm(heap); if (result.return_code & PYEXEC_FORCED_EXIT) { diff --git a/ports/esp32s2/common-hal/alarm/__init__.c b/ports/esp32s2/common-hal/alarm/__init__.c index 264a525cf7..696e3ec150 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.c +++ b/ports/esp32s2/common-hal/alarm/__init__.c @@ -54,7 +54,7 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { }, }; -void common_hal_alarm_reset(void) { +void alarm_reset(void) { alarm_sleep_memory_reset(); alarm_pin_pinalarm_reset(); alarm_time_timealarm_reset(); @@ -144,7 +144,7 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj } mp_obj_t wake_alarm = _get_wake_alarm(n_alarms, alarms); - alarm_pin_pinalarm_reset_alarms(n_alarms, alarms); + alarm_reset(); return wake_alarm; } diff --git a/ports/esp32s2/common-hal/alarm/__init__.h b/ports/esp32s2/common-hal/alarm/__init__.h index 2b46907778..0823ff842e 100644 --- a/ports/esp32s2/common-hal/alarm/__init__.h +++ b/ports/esp32s2/common-hal/alarm/__init__.h @@ -31,6 +31,6 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; -extern void common_hal_alarm_reset(void); +extern void alarm_reset(void); #endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM__INIT__H diff --git a/ports/esp32s2/common-hal/alarm/pin/PinAlarm.c b/ports/esp32s2/common-hal/alarm/pin/PinAlarm.c index 9ea9b9b998..7a743fc0f2 100644 --- a/ports/esp32s2/common-hal/alarm/pin/PinAlarm.c +++ b/ports/esp32s2/common-hal/alarm/pin/PinAlarm.c @@ -38,7 +38,7 @@ #include "components/soc/src/esp32s2/include/hal/gpio_ll.h" #include "components/xtensa/include/esp_debug_helpers.h" -void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu_pin_obj_t *pin, bool value, bool edge, bool pull) { +void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) { if (edge) { mp_raise_ValueError(translate("Cannot wake on pin edge. Only level.")); } @@ -51,7 +51,7 @@ void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu self->pull = pull; } -mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) { +const mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) { return self->pin; } diff --git a/ports/esp32s2/common-hal/alarm/pin/PinAlarm.h b/ports/esp32s2/common-hal/alarm/pin/PinAlarm.h index 93672c1f2d..4d86af4c3d 100644 --- a/ports/esp32s2/common-hal/alarm/pin/PinAlarm.h +++ b/ports/esp32s2/common-hal/alarm/pin/PinAlarm.h @@ -29,7 +29,7 @@ typedef struct { mp_obj_base_t base; - mcu_pin_obj_t *pin; + const mcu_pin_obj_t *pin; bool value; bool pull; } alarm_pin_pinalarm_obj_t; diff --git a/ports/stm/common-hal/alarm/__init__.c b/ports/stm/common-hal/alarm/__init__.c index 6b0d6f4681..14c484f1b0 100644 --- a/ports/stm/common-hal/alarm/__init__.c +++ b/ports/stm/common-hal/alarm/__init__.c @@ -45,7 +45,7 @@ #define STM_ALARM_FLAG RTC->BKP0R -void common_hal_alarm_reset(void) { +void alarm_reset(void) { // Reset the alarm flag STM_ALARM_FLAG = 0x00; // alarm_sleep_memory_reset(); @@ -131,7 +131,7 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj } mp_obj_t wake_alarm = _get_wake_alarm(n_alarms, alarms); - common_hal_alarm_reset(); + alarm_reset(); return wake_alarm; } diff --git a/ports/stm/common-hal/alarm/__init__.h b/ports/stm/common-hal/alarm/__init__.h index 18defefc0b..cb90dccb28 100644 --- a/ports/stm/common-hal/alarm/__init__.h +++ b/ports/stm/common-hal/alarm/__init__.h @@ -31,6 +31,6 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; -extern void common_hal_alarm_reset(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 3bff19b76c..d7f1c30398 100644 --- a/ports/stm/common-hal/alarm/pin/PinAlarm.c +++ b/ports/stm/common-hal/alarm/pin/PinAlarm.c @@ -43,7 +43,7 @@ STATIC void pin_alarm_callback(uint8_t num) { HAL_GPIO_EXTI_IRQHandler(pin_mask(num)); } -void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu_pin_obj_t *pin, bool value, bool edge, bool pull) { +void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) { if (!edge) { mp_raise_NotImplementedError(translate("Only edge detection is available on this hardware")); } @@ -70,7 +70,7 @@ void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu self->pull = pull; } -mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) { +const mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) { return self->pin; } @@ -112,6 +112,7 @@ void alarm_pin_pinalarm_reset(void) { HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1); alarm_pin_triggered = 0; woke_up = false; + deep_wkup_enabled = false; } // Deep sleep alarms don't actually make use of EXTI, but we pretend they're the same. @@ -135,17 +136,6 @@ void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_ob } } -void alarm_pin_pinalarm_reset_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) { - alarm_pin_triggered = 0; - deep_wkup_enabled = false; - for (size_t i = 0; i < n_alarms; i++) { - if (MP_OBJ_IS_TYPE(alarms[i], &alarm_pin_pinalarm_type)) { - alarm_pin_pinalarm_obj_t *alarm = MP_OBJ_TO_PTR(alarms[i]); - stm_peripherals_exti_disable(alarm->pin->number); - } - } -} - // If we don't have WKUP enabled, ensure it's disabled // TODO; is this really required? void alarm_pin_pinalarm_prepare_for_deep_sleep(void) { diff --git a/ports/stm/common-hal/alarm/pin/PinAlarm.h b/ports/stm/common-hal/alarm/pin/PinAlarm.h index b26930b103..2b94f1cb7e 100644 --- a/ports/stm/common-hal/alarm/pin/PinAlarm.h +++ b/ports/stm/common-hal/alarm/pin/PinAlarm.h @@ -29,14 +29,13 @@ typedef struct { mp_obj_base_t base; - mcu_pin_obj_t *pin; + const mcu_pin_obj_t *pin; bool value; bool pull; } alarm_pin_pinalarm_obj_t; 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_reset_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms); void alarm_pin_pinalarm_prepare_for_deep_sleep(void); mp_obj_t alarm_pin_pinalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms); bool alarm_pin_pinalarm_woke_us_up(void); diff --git a/shared-bindings/alarm/pin/PinAlarm.c b/shared-bindings/alarm/pin/PinAlarm.c index 04063e89c2..69e6be8c73 100644 --- a/shared-bindings/alarm/pin/PinAlarm.c +++ b/shared-bindings/alarm/pin/PinAlarm.c @@ -73,7 +73,7 @@ STATIC mp_obj_t alarm_pin_pinalarm_make_new(const mp_obj_type_t *type, mp_uint_t mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(0, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); + const mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); common_hal_alarm_pin_pinalarm_construct(self, pin, @@ -89,7 +89,7 @@ STATIC mp_obj_t alarm_pin_pinalarm_make_new(const mp_obj_type_t *type, mp_uint_t //| STATIC mp_obj_t alarm_pin_pinalarm_obj_get_pin(mp_obj_t self_in) { alarm_pin_pinalarm_obj_t *self = MP_OBJ_TO_PTR(self_in); - mcu_pin_obj_t *pin = common_hal_alarm_pin_pinalarm_get_pin(self); + const mcu_pin_obj_t *pin = common_hal_alarm_pin_pinalarm_get_pin(self); if (pin == NULL) { return mp_const_none; } diff --git a/shared-bindings/alarm/pin/PinAlarm.h b/shared-bindings/alarm/pin/PinAlarm.h index 48865009c3..ba74bab5f3 100644 --- a/shared-bindings/alarm/pin/PinAlarm.h +++ b/shared-bindings/alarm/pin/PinAlarm.h @@ -34,8 +34,8 @@ extern const mp_obj_type_t alarm_pin_pinalarm_type; -void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu_pin_obj_t *pin, bool value, bool edge, bool pull); -extern mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self); +void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull); +extern const mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self); extern bool common_hal_alarm_pin_pinalarm_get_value(alarm_pin_pinalarm_obj_t *self); extern bool common_hal_alarm_pin_pinalarm_get_edge(alarm_pin_pinalarm_obj_t *self); extern bool common_hal_alarm_pin_pinalarm_get_pull(alarm_pin_pinalarm_obj_t *self);