2021-09-29 13:44:15 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2021 Lucian Copeland for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/alarm/__init__.h"
|
|
|
|
#include "shared-bindings/alarm/SleepMemory.h"
|
|
|
|
#include "shared-bindings/alarm/pin/PinAlarm.h"
|
|
|
|
#include "shared-bindings/alarm/time/TimeAlarm.h"
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2021-10-24 18:49:14 -04:00
|
|
|
#include "shared/runtime/interrupt_char.h"
|
2021-09-29 13:44:15 -04:00
|
|
|
#include "samd/external_interrupts.h"
|
|
|
|
#include "supervisor/port.h"
|
|
|
|
#include "supervisor/workflow.h"
|
|
|
|
|
2022-02-17 09:00:26 -05:00
|
|
|
STATIC uint32_t TAMPID = 0;
|
|
|
|
|
2021-09-29 13:44:15 -04:00
|
|
|
// Singleton instance of SleepMemory.
|
|
|
|
const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
|
|
|
|
.base = {
|
|
|
|
.type = &alarm_sleep_memory_type,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
void alarm_reset(void) {
|
|
|
|
// Reset the alarm flag
|
|
|
|
alarm_pin_pinalarm_reset();
|
|
|
|
alarm_time_timealarm_reset();
|
|
|
|
}
|
|
|
|
|
2022-02-17 09:00:26 -05:00
|
|
|
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
|
|
|
|
// alarm from the deep sleep.
|
|
|
|
|
|
|
|
TAMPID = RTC->MODE0.TAMPID.reg;
|
2021-09-29 13:44:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_alarm_woken_from_sleep(void) {
|
2022-02-17 09:00:26 -05:00
|
|
|
return alarm_pin_pinalarm_woke_this_cycle() || alarm_time_timealarm_woke_this_cycle();
|
2021-09-29 13:44:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_alarm_create_wake_alarm(void) {
|
2022-02-17 09:00:26 -05:00
|
|
|
// 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.
|
|
|
|
bool true_deep = RSTC->RCAUSE.bit.BACKUP;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (alarm_time_timealarm_woke_this_cycle() || (true_deep && TAMPID == 0)) {
|
|
|
|
return alarm_time_timealarm_create_wakeup_alarm();
|
|
|
|
}
|
|
|
|
if (true_deep) {
|
|
|
|
return alarm_pin_pinalarm_create_wakeup_alarm(TAMPID);
|
2021-09-29 13:44:15 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up light sleep or deep sleep alarms.
|
|
|
|
STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
|
|
|
|
alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms);
|
2021-10-01 19:36:04 -04:00
|
|
|
alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms);
|
2021-09-29 13:44:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
|
|
|
|
_setup_sleep_alarms(false, n_alarms, alarms);
|
|
|
|
mp_obj_t wake_alarm = mp_const_none;
|
|
|
|
|
2022-02-17 09:00:26 -05:00
|
|
|
// This works but achieves same power consumption as time.sleep()
|
|
|
|
PM->SLEEPCFG.reg = PM_SLEEPCFG_SLEEPMODE_STANDBY;
|
|
|
|
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_STANDBY_Val) {
|
|
|
|
}
|
2022-02-21 05:21:42 -05:00
|
|
|
// STDBYCFG is left to be 0 to retain SYSRAM. Note that, even if
|
|
|
|
// RAMCFG_OFF is set here, SYSRAM seems to be retained, probably
|
|
|
|
// because RTC and/or USB keeps sleepwalking.
|
|
|
|
|
2021-09-29 13:44:15 -04:00
|
|
|
while (!mp_hal_is_interrupted()) {
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
// Detect if interrupt was alarm or ctrl-C interrupt.
|
2022-02-17 09:00:26 -05:00
|
|
|
if (alarm_time_timealarm_woke_this_cycle()) {
|
|
|
|
wake_alarm = alarm_time_timealarm_find_triggered_alarm(n_alarms,alarms);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (alarm_pin_pinalarm_woke_this_cycle()) {
|
|
|
|
wake_alarm = alarm_pin_pinalarm_find_triggered_alarm(n_alarms,alarms);
|
2021-09-29 13:44:15 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the FPU interrupt because it can prevent us from sleeping.
|
|
|
|
if (__get_FPSCR() & ~(0x9f)) {
|
2021-10-04 00:42:04 -04:00
|
|
|
__set_FPSCR(__get_FPSCR() & ~(0x9f));
|
|
|
|
(void)__get_FPSCR();
|
2021-09-29 13:44:15 -04:00
|
|
|
}
|
|
|
|
|
2022-02-17 09:00:26 -05:00
|
|
|
common_hal_mcu_disable_interrupts();
|
2021-09-29 13:44:15 -04:00
|
|
|
__DSB(); // Data Synchronization Barrier
|
|
|
|
__WFI(); // Wait For Interrupt
|
2022-02-17 09:00:26 -05:00
|
|
|
common_hal_mcu_enable_interrupts();
|
|
|
|
}
|
|
|
|
// Restore SLEEPCFG or port_idle_until_interrupt sleeps in STANDBY mode.
|
|
|
|
PM->SLEEPCFG.reg = PM_SLEEPCFG_SLEEPMODE_IDLE2;
|
|
|
|
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_IDLE2_Val) {
|
2021-10-04 00:42:04 -04:00
|
|
|
}
|
2022-02-17 09:00:26 -05:00
|
|
|
alarm_pin_pinalarm_deinit_alarms(n_alarms, alarms); // after care for alarm_pin_pinalarm_set_alarms
|
|
|
|
alarm_reset();
|
|
|
|
|
2021-09-29 13:44:15 -04:00
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
return mp_const_none; // Shouldn't be given to python code because exception handling should kick in.
|
|
|
|
}
|
|
|
|
|
|
|
|
return wake_alarm;
|
|
|
|
}
|
|
|
|
|
2022-09-12 09:48:41 -04:00
|
|
|
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms, size_t n_dios, digitalio_digitalinout_obj_t **preserve_dios) {
|
|
|
|
if (n_dios > 0) {
|
|
|
|
mp_raise_NotImplementedError_varg(translate("%q"), MP_QSTR_preserve_dios);
|
|
|
|
}
|
2021-09-29 13:44:15 -04:00
|
|
|
_setup_sleep_alarms(true, n_alarms, alarms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
|
|
|
|
alarm_pin_pinalarm_prepare_for_deep_sleep();
|
2021-10-01 19:36:04 -04:00
|
|
|
alarm_time_timealarm_prepare_for_deep_sleep();
|
2022-02-17 09:00:26 -05:00
|
|
|
// port_disable_tick(); // TODO: Required for SAMD?
|
2021-09-29 13:44:15 -04:00
|
|
|
|
2022-02-17 09:00:26 -05:00
|
|
|
// cache alarm flag and etc since RTC about to be reset
|
|
|
|
uint32_t _flag = SAMD_ALARM_FLAG; // RTC->MODE0.BKUP[0].reg
|
|
|
|
uint32_t _target = RTC->MODE0.COMP[1].reg;
|
|
|
|
uint32_t _tampctrl = RTC->MODE0.TAMPCTRL.reg;
|
2021-09-29 13:44:15 -04:00
|
|
|
|
|
|
|
// Clear the FPU interrupt because it can prevent us from sleeping.
|
|
|
|
if (__get_FPSCR() & ~(0x9f)) {
|
2021-10-04 00:42:04 -04:00
|
|
|
__set_FPSCR(__get_FPSCR() & ~(0x9f));
|
|
|
|
(void)__get_FPSCR();
|
2021-09-29 13:44:15 -04:00
|
|
|
}
|
|
|
|
|
2022-02-17 09:00:26 -05:00
|
|
|
common_hal_mcu_disable_interrupts();
|
2021-10-10 15:01:04 -04:00
|
|
|
// Must disable the RTC before writing to EVCTRL and TMPCTRL
|
|
|
|
RTC->MODE0.CTRLA.bit.SWRST = 1; // Software reset the RTC
|
|
|
|
while (RTC->MODE0.SYNCBUSY.bit.SWRST) { // Wait for synchronization
|
|
|
|
}
|
|
|
|
RTC->MODE0.CTRLA.reg = RTC_MODE0_CTRLA_PRESCALER_DIV1024 | // Set prescaler to 1024
|
|
|
|
RTC_MODE0_CTRLA_MODE_COUNT32; // Set RTC to mode 0, 32-bit timer
|
|
|
|
|
2022-02-17 09:00:26 -05:00
|
|
|
SAMD_ALARM_FLAG = _flag;
|
2021-10-10 15:01:04 -04:00
|
|
|
// Check if we're setting TimeAlarm
|
2022-02-17 09:00:26 -05:00
|
|
|
if (SAMD_ALARM_FLAG_TIME_CHK) {
|
|
|
|
RTC->MODE0.COMP[1].reg = _target;
|
2021-10-10 15:01:04 -04:00
|
|
|
while (RTC->MODE0.SYNCBUSY.reg) {
|
2021-10-04 00:42:04 -04:00
|
|
|
}
|
2021-10-10 15:01:04 -04:00
|
|
|
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP1;
|
|
|
|
}
|
2022-02-17 09:00:26 -05:00
|
|
|
// Check if we're setting PinAlarm
|
|
|
|
if (SAMD_ALARM_FLAG_PIN_CHK) {
|
|
|
|
RTC->MODE0.TAMPCTRL.reg = _tampctrl;
|
2021-09-29 13:44:15 -04:00
|
|
|
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER;
|
2021-10-04 16:55:44 -04:00
|
|
|
}
|
2022-02-17 09:00:26 -05:00
|
|
|
// Enable interrupts
|
|
|
|
common_hal_mcu_enable_interrupts();
|
2021-10-10 15:01:04 -04:00
|
|
|
|
2022-02-21 05:21:42 -05:00
|
|
|
// Set-up Deep Sleep Mode with backup RAM retention
|
2021-09-29 13:44:15 -04:00
|
|
|
PM->SLEEPCFG.reg = PM_SLEEPCFG_SLEEPMODE_BACKUP;
|
2021-10-04 00:42:04 -04:00
|
|
|
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_BACKUP_Val) {
|
|
|
|
}
|
2021-09-29 13:44:15 -04:00
|
|
|
RTC->MODE0.CTRLA.bit.ENABLE = 1; // Enable the RTC
|
2021-10-04 16:55:44 -04:00
|
|
|
while (RTC->MODE0.SYNCBUSY.bit.ENABLE) { // Wait for synchronization
|
|
|
|
}
|
2021-09-29 13:44:15 -04:00
|
|
|
|
|
|
|
__DSB(); // Data Synchronization Barrier
|
|
|
|
__WFI(); // Wait For Interrupt
|
|
|
|
|
|
|
|
// The above shuts down RAM and triggers a reset, so we should never hit this
|
|
|
|
while (1) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 05:21:42 -05:00
|
|
|
// Default common_hal_alarm_pretending_deep_sleep is defined in
|
|
|
|
// shared-bindings, which is used here. Note that "pretending" does
|
|
|
|
// not work on REPL; it only works for main.py (or code.py, ...).
|
2021-09-29 13:44:15 -04:00
|
|
|
|
|
|
|
void common_hal_alarm_gc_collect(void) {
|
|
|
|
gc_collect_ptr(shared_alarm_get_wake_alarm());
|
|
|
|
}
|