|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Lucian Copeland for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/gc.h" |
| 28 | +#include "py/obj.h" |
| 29 | +#include "py/objtuple.h" |
| 30 | +#include "py/runtime.h" |
| 31 | +#include "lib/utils/interrupt_char.h" |
| 32 | + |
| 33 | +#include "shared-bindings/alarm/__init__.h" |
| 34 | +#include "shared-bindings/alarm/SleepMemory.h" |
| 35 | +#include "shared-bindings/alarm/pin/PinAlarm.h" |
| 36 | +#include "shared-bindings/alarm/time/TimeAlarm.h" |
| 37 | + |
| 38 | +#include "shared-bindings/microcontroller/__init__.h" |
| 39 | + |
| 40 | +#include "supervisor/port.h" |
| 41 | +#include "supervisor/workflow.h" |
| 42 | + |
| 43 | +// Singleton instance of SleepMemory. |
| 44 | +const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = { |
| 45 | + .base = { |
| 46 | + .type = &alarm_sleep_memory_type, |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +// TODO: make a custom enum to avoid weird values like PM_SLEEPCFG_SLEEPMODE_BACKUP_Val? |
| 51 | + |
| 52 | +void alarm_reset(void) { |
| 53 | + // Reset the alarm flag |
| 54 | + SAMD_ALARM_FLAG = 0x00; |
| 55 | + alarm_pin_pinalarm_reset(); |
| 56 | + alarm_time_timealarm_reset(); |
| 57 | +} |
| 58 | + |
| 59 | +samd_sleep_source_t alarm_get_wakeup_cause(void) { |
| 60 | + // If in light/fake sleep, check modules |
| 61 | + if (alarm_pin_pinalarm_woke_this_cycle()) { |
| 62 | + return SAMD_WAKEUP_GPIO; |
| 63 | + } |
| 64 | + if (alarm_time_timealarm_woke_this_cycle()) { |
| 65 | + return SAMD_WAKEUP_RTC; |
| 66 | + } |
| 67 | + // TODO: for deep sleep, manually determine how the chip woke up |
| 68 | + // TODO: try checking the interrupt flag tables for RTC TAMPER vs COMPARE |
| 69 | + return SAMD_WAKEUP_UNDEF; |
| 70 | +} |
| 71 | + |
| 72 | +bool common_hal_alarm_woken_from_sleep(void) { |
| 73 | + return alarm_get_wakeup_cause() != SAMD_WAKEUP_UNDEF; |
| 74 | +} |
| 75 | + |
| 76 | +mp_obj_t common_hal_alarm_create_wake_alarm(void) { |
| 77 | + // If woken from deep sleep, create a copy alarm similar to what would have |
| 78 | + // been passed in originally. Otherwise, just return none |
| 79 | + samd_sleep_source_t cause = alarm_get_wakeup_cause(); |
| 80 | + switch (cause) { |
| 81 | + case SAMD_WAKEUP_RTC: { |
| 82 | + return alarm_time_timealarm_create_wakeup_alarm(); |
| 83 | + } |
| 84 | + case SAMD_WAKEUP_GPIO: { |
| 85 | + return alarm_pin_pinalarm_create_wakeup_alarm(); |
| 86 | + } |
| 87 | + case SAMD_WAKEUP_UNDEF: |
| 88 | + default: |
| 89 | + // Not a deep sleep reset. |
| 90 | + break; |
| 91 | + } |
| 92 | + return mp_const_none; |
| 93 | +} |
| 94 | + |
| 95 | +// Set up light sleep or deep sleep alarms. |
| 96 | +STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) { |
| 97 | + alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms); |
| 98 | + alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms); |
| 99 | +} |
| 100 | + |
| 101 | +mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) { |
| 102 | + _setup_sleep_alarms(false, n_alarms, alarms); |
| 103 | + mp_obj_t wake_alarm = mp_const_none; |
| 104 | + |
| 105 | + while (!mp_hal_is_interrupted()) { |
| 106 | + RUN_BACKGROUND_TASKS; |
| 107 | + // Detect if interrupt was alarm or ctrl-C interrupt. |
| 108 | + if (common_hal_alarm_woken_from_sleep()) { |
| 109 | + samd_sleep_source_t cause = alarm_get_wakeup_cause(); |
| 110 | + switch (cause) { |
| 111 | + case SAMD_WAKEUP_RTC: { |
| 112 | + wake_alarm = alarm_time_timealarm_find_triggered_alarm(n_alarms,alarms); |
| 113 | + break; |
| 114 | + } |
| 115 | + case SAMD_WAKEUP_GPIO: { |
| 116 | + wake_alarm = alarm_pin_pinalarm_find_triggered_alarm(n_alarms,alarms); |
| 117 | + break; |
| 118 | + } |
| 119 | + default: |
| 120 | + // Should not reach this, if all light sleep types are covered correctly |
| 121 | + break; |
| 122 | + } |
| 123 | + shared_alarm_save_wake_alarm(wake_alarm); |
| 124 | + break; |
| 125 | + } |
| 126 | + // TODO: the SAMD implementation of this (purportedly) disables interrupts |
| 127 | + // Presumably this doesn't impact the RTC interrupts, somehow, or it would never wake up? |
| 128 | + // Will it prevent an external interrupt from waking? |
| 129 | + port_idle_until_interrupt(); |
| 130 | + // Alternative would be `sleep(PM_SLEEPCFG_SLEEPMODE_IDLE2_Val)`, I think? |
| 131 | + } |
| 132 | + |
| 133 | + if (mp_hal_is_interrupted()) { |
| 134 | + return mp_const_none; // Shouldn't be given to python code because exception handling should kick in. |
| 135 | + } |
| 136 | + |
| 137 | + alarm_reset(); |
| 138 | + return wake_alarm; |
| 139 | +} |
| 140 | + |
| 141 | +void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) { |
| 142 | + _setup_sleep_alarms(true, n_alarms, alarms); |
| 143 | +} |
| 144 | + |
| 145 | +void NORETURN common_hal_alarm_enter_deep_sleep(void) { |
| 146 | + alarm_pin_pinalarm_prepare_for_deep_sleep(); |
| 147 | + alarm_time_timealarm_prepare_for_deep_sleep(); |
| 148 | + port_disable_tick(); // TODO: Required for SAMD? |
| 149 | + |
| 150 | + // Set a flag in the backup registers to indicate sleep wakeup |
| 151 | + SAMD_ALARM_FLAG = 0x01; |
| 152 | + |
| 153 | + // TODO: make sure this actually works, or replace with extracted register version |
| 154 | + // sleep(PM_SLEEPCFG_SLEEPMODE_BACKUP_Val); |
| 155 | + |
| 156 | + // The above shuts down RAM, so we should never hit this |
| 157 | + while (1) { |
| 158 | + ; |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +void common_hal_alarm_pretending_deep_sleep(void) { |
| 163 | + // TODO: |
| 164 | + // If tamper detect interrupts cannot be used to wake from the Idle tier of sleep, |
| 165 | + // This section will need to re-initialize the pins to allow the PORT peripheral |
| 166 | + // to generate external interrupts again. See STM32 for reference. |
| 167 | +} |
| 168 | + |
| 169 | +void common_hal_alarm_gc_collect(void) { |
| 170 | + gc_collect_ptr(shared_alarm_get_wake_alarm()); |
| 171 | +} |
0 commit comments