alarm.wake_alarm works for pin and time. Pin & time can both be set at once

This commit is contained in:
Max Holliday 2021-10-10 12:01:04 -07:00
parent 752bf6e4e0
commit dcb5fd280a
6 changed files with 71 additions and 79 deletions

View File

@ -50,6 +50,7 @@ const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
// TODO: make a custom enum to avoid weird values like PM_SLEEPCFG_SLEEPMODE_BACKUP_Val? // TODO: make a custom enum to avoid weird values like PM_SLEEPCFG_SLEEPMODE_BACKUP_Val?
STATIC volatile uint32_t _target; STATIC volatile uint32_t _target;
STATIC bool fake_sleep; STATIC bool fake_sleep;
STATIC bool pin_wake;
void alarm_reset(void) { void alarm_reset(void) {
// Reset the alarm flag // Reset the alarm flag
@ -67,9 +68,9 @@ samd_sleep_source_t alarm_get_wakeup_cause(void) {
return SAMD_WAKEUP_RTC; return SAMD_WAKEUP_RTC;
} }
if (!fake_sleep && RSTC->RCAUSE.bit.BACKUP) { if (!fake_sleep && RSTC->RCAUSE.bit.BACKUP) {
// not able to detect PinAlarm wake since registers are getting reset // This is checked during rtc_init to cache TAMPID if necessary
// TODO: come up with a way to detect a TAMPER if (pin_wake ||RTC->MODE0.TAMPID.reg) {
if (RTC->MODE0.TAMPID.reg || RTC->MODE0.INTFLAG.bit.TAMPER) { pin_wake = true;
return SAMD_WAKEUP_GPIO; return SAMD_WAKEUP_GPIO;
} }
return SAMD_WAKEUP_RTC; return SAMD_WAKEUP_RTC;
@ -132,12 +133,6 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
shared_alarm_save_wake_alarm(wake_alarm); shared_alarm_save_wake_alarm(wake_alarm);
break; break;
} }
// TODO: the SAMD implementation of this (purportedly) disables interrupts
// Presumably this doesn't impact the RTC interrupts, somehow, or it would never wake up?
// Will it prevent an external interrupt from waking?
// port_idle_until_interrupt();
// Alternative would be `sleep(PM_SLEEPCFG_SLEEPMODE_IDLE2_Val)`, I think?
// ATTEMPT ------------------------------ // ATTEMPT ------------------------------
// This works but achieves same power consumption as time.sleep() // This works but achieves same power consumption as time.sleep()
@ -161,8 +156,6 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
__WFI(); // Wait For Interrupt __WFI(); // Wait For Interrupt
// Enable RTC interrupts // Enable RTC interrupts
NVIC_EnableIRQ(RTC_IRQn); NVIC_EnableIRQ(RTC_IRQn);
// END ATTEMPT ------------------------------ // END ATTEMPT ------------------------------
} }
if (mp_hal_is_interrupted()) { if (mp_hal_is_interrupted()) {
@ -183,8 +176,8 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
_target = RTC->MODE0.COMP[1].reg; _target = RTC->MODE0.COMP[1].reg;
port_disable_tick(); // TODO: Required for SAMD? port_disable_tick(); // TODO: Required for SAMD?
// Set a flag in the backup registers to indicate sleep wakeup // cache alarm flag since backup registers about to be reset
SAMD_ALARM_FLAG = 0x01; uint32_t _SAMD_ALARM_FLAG = SAMD_ALARM_FLAG;
// Clear the FPU interrupt because it can prevent us from sleeping. // Clear the FPU interrupt because it can prevent us from sleeping.
if (__get_FPSCR() & ~(0x9f)) { if (__get_FPSCR() & ~(0x9f)) {
@ -192,10 +185,6 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
(void)__get_FPSCR(); (void)__get_FPSCR();
} }
// TODO: Be able to set PinAlarm and TimeAlarm together
// PinAlarm (hacky way of checking if time alarm or pin alarm)
if (RTC->MODE0.INTENSET.bit.TAMPER) {
// Disable interrupts
NVIC_DisableIRQ(RTC_IRQn); NVIC_DisableIRQ(RTC_IRQn);
// Must disable the RTC before writing to EVCTRL and TMPCTRL // Must disable the RTC before writing to EVCTRL and TMPCTRL
RTC->MODE0.CTRLA.bit.ENABLE = 0; // Disable the RTC RTC->MODE0.CTRLA.bit.ENABLE = 0; // Disable the RTC
@ -209,49 +198,33 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
RTC->MODE0.CTRLA.reg = RTC_MODE0_CTRLA_PRESCALER_DIV1024 | // Set prescaler to 1024 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 RTC_MODE0_CTRLA_MODE_COUNT32; // Set RTC to mode 0, 32-bit timer
// TODO: map requested pin to limited selection of TAMPER pins // Check if we're setting TimeAlarm
RTC->MODE0.TAMPCTRL.bit.DEBNC2 = 1; // Edge triggered when INn is stable for 4 CLK_RTC_DEB periods if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_TIME) {
RTC->MODE0.TAMPCTRL.bit.TAMLVL2 = 1; // rising edge
// PA02 = IN2
RTC->MODE0.TAMPCTRL.bit.IN2ACT = 1; // WAKE on IN2 (doesn't save timestamp)
// Enable interrupts
NVIC_SetPriority(RTC_IRQn, 0);
NVIC_EnableIRQ(RTC_IRQn);
// Set interrupts for TAMPER or overflow
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER;
} else {
// TimeAlarm
// Retrieve COMP1 value before resetting RTC
NVIC_DisableIRQ(RTC_IRQn);
// Must disable the RTC before writing to EVCTRL and TMPCTRL
RTC->MODE0.CTRLA.bit.ENABLE = 0; // Disable the RTC
while (RTC->MODE0.SYNCBUSY.bit.ENABLE) { // Wait for synchronization
;
}
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
RTC->MODE0.COMP[1].reg = (_target / 1024) * 32; RTC->MODE0.COMP[1].reg = (_target / 1024) * 32;
while (RTC->MODE0.SYNCBUSY.reg) { while (RTC->MODE0.SYNCBUSY.reg) {
; ;
} }
}
// Check if we're setting PinAlarm
if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_PIN) {
RTC->MODE0.TAMPCTRL.bit.DEBNC2 = 1; // Edge triggered when INn is stable for 4 CLK_RTC_DEB periods
RTC->MODE0.TAMPCTRL.bit.TAMLVL2 = 1; // rising edge
// PA02 = IN2
RTC->MODE0.TAMPCTRL.bit.IN2ACT = 1; // WAKE on IN2 (doesn't save timestamp)
}
// Enable interrupts // Enable interrupts
NVIC_SetPriority(RTC_IRQn, 0); NVIC_SetPriority(RTC_IRQn, 0);
NVIC_EnableIRQ(RTC_IRQn); NVIC_EnableIRQ(RTC_IRQn);
// Set interrupts for COMPARE1 or overflow if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_TIME) {
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP1 | RTC_MODE1_INTENSET_OVF; // Set interrupts for COMPARE1
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP1;
} }
// Set-up Deep Sleep Mode if (_SAMD_ALARM_FLAG & SAMD_ALARM_FLAG_PIN) {
// RAM retention // Set interrupts for TAMPER pins
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER;
}
// Set-up Deep Sleep Mode & RAM retention
PM->BKUPCFG.reg = PM_BKUPCFG_BRAMCFG(0x2); // No RAM retention 0x2 partial:0x1 PM->BKUPCFG.reg = PM_BKUPCFG_BRAMCFG(0x2); // No RAM retention 0x2 partial:0x1
while (PM->BKUPCFG.bit.BRAMCFG != 0x2) { // Wait for synchronization while (PM->BKUPCFG.bit.BRAMCFG != 0x2) { // Wait for synchronization
; ;
@ -260,7 +233,6 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_BACKUP_Val) { while (PM->SLEEPCFG.bit.SLEEPMODE != PM_SLEEPCFG_SLEEPMODE_BACKUP_Val) {
; ;
} }
RTC->MODE0.CTRLA.bit.ENABLE = 1; // Enable the RTC RTC->MODE0.CTRLA.bit.ENABLE = 1; // Enable the RTC
while (RTC->MODE0.SYNCBUSY.bit.ENABLE) { // Wait for synchronization while (RTC->MODE0.SYNCBUSY.bit.ENABLE) { // Wait for synchronization
; ;
@ -282,10 +254,6 @@ void common_hal_alarm_pretending_deep_sleep(void) {
// to generate external interrupts again. See STM32 for reference. // to generate external interrupts again. See STM32 for reference.
if (!fake_sleep) { if (!fake_sleep) {
SAMD_ALARM_FLAG = 1;
while (RTC->MODE0.SYNCBUSY.reg) {
;
}
fake_sleep = true; fake_sleep = true;
} }
} }

View File

@ -32,8 +32,12 @@
extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj; extern const alarm_sleep_memory_obj_t alarm_sleep_memory_obj;
// This is the first byte of the BKUP register bank. // This is the first byte of the BKUP register bank.
// It stores whether the last wakeup was because of an alarm. // We use it to store which alarms are set.
#ifndef SAMD_ALARM_FLAG
#define SAMD_ALARM_FLAG (RTC->MODE0.BKUP[0].reg) #define SAMD_ALARM_FLAG (RTC->MODE0.BKUP[0].reg)
#define SAMD_ALARM_FLAG_TIME (_U_(0x1) << 0)
#define SAMD_ALARM_FLAG_PIN (_U_(0x1) << 1)
#endif
typedef enum { typedef enum {
SAMD_WAKEUP_UNDEF, SAMD_WAKEUP_UNDEF,

View File

@ -31,7 +31,6 @@
#include "hal/include/hal_gpio.h" #include "hal/include/hal_gpio.h"
// #include <stdio.h> // #include <stdio.h>
#include "shared-bindings/alarm/pin/PinAlarm.h" #include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/microcontroller/Pin.h"
@ -165,6 +164,7 @@ void alarm_pin_pinalarm_reset(void) {
// use, reset them. // use, reset them.
pinalarm_on = false; pinalarm_on = false;
woke_up = false; woke_up = false;
SAMD_ALARM_FLAG &= ~SAMD_ALARM_FLAG_PIN; // clear flag
// Disable TAMPER interrupt // Disable TAMPER interrupt
RTC->MODE0.INTENCLR.bit.TAMPER = 1; RTC->MODE0.INTENCLR.bit.TAMPER = 1;
// Disable TAMPER control // Disable TAMPER control
@ -195,6 +195,8 @@ void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_ob
mp_raise_ValueError(translate("Pin cannot wake from Deep Sleep")); mp_raise_ValueError(translate("Pin cannot wake from Deep Sleep"));
} }
pinalarm_on = true; pinalarm_on = true;
SAMD_ALARM_FLAG |= SAMD_ALARM_FLAG_PIN;
// Set tamper interrupt so deep sleep knows that's the intent // Set tamper interrupt so deep sleep knows that's the intent
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER; RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER;
common_hal_mcu_disable_interrupts(); common_hal_mcu_disable_interrupts();
@ -206,7 +208,7 @@ void alarm_pin_pinalarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_ob
// PA02 is n=2: IN2, LVL2, etc... // PA02 is n=2: IN2, LVL2, etc...
RTC->MODE0.TAMPCTRL.bit.DEBNC2 = 1; // Edge triggered when INn is stable for 4 CLK_RTC_DEB periods RTC->MODE0.TAMPCTRL.bit.DEBNC2 = 1; // Edge triggered when INn is stable for 4 CLK_RTC_DEB periods
RTC->MODE0.TAMPCTRL.bit.TAMLVL2 = alarm->value; // rising or falling edge RTC->MODE0.TAMPCTRL.bit.TAMLVL2 = alarm->value; // rising or falling edge
RTC->MODE0.TAMPCTRL.bit.IN2ACT = 0x1; // WAKE on IN2 (doesn't save timestamp) RTC->MODE0.TAMPCTRL.bit.IN2ACT = 1; // WAKE on IN2 (doesn't save timestamp)
common_hal_mcu_enable_interrupts(); common_hal_mcu_enable_interrupts();
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER; RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_TAMPER;
RTC->MODE0.CTRLA.bit.ENABLE = 1; // Enable the RTC RTC->MODE0.CTRLA.bit.ENABLE = 1; // Enable the RTC

View File

@ -27,6 +27,14 @@
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H #ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H #define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_PINALARM_H
// This is the first byte of the BKUP register bank.
// We use it to store which alarms are set.
#ifndef SAMD_ALARM_FLAG
#define SAMD_ALARM_FLAG (RTC->MODE0.BKUP[0].reg)
#define SAMD_ALARM_FLAG_TIME (_U_(0x1) << 0)
#define SAMD_ALARM_FLAG_PIN (_U_(0x1) << 1)
#endif
#include "py/obj.h" #include "py/obj.h"
#include "py/objtuple.h" #include "py/objtuple.h"

View File

@ -72,9 +72,9 @@ mp_obj_t alarm_time_timealarm_create_wakeup_alarm(void) {
return timer; return timer;
} }
void timer_callback(void) { void time_alarm_callback(void) {
if (timealarm_on) { if (timealarm_on) {
RTC->MODE0.INTENCLR.reg = RTC_MODE0_INTENCLR_CMP1 | RTC_MODE0_INTENCLR_CMP0 | RTC_MODE0_INTENCLR_OVF; // clear flags RTC->MODE0.INTENCLR.reg = RTC_MODE0_INTENCLR_CMP1; // clear flags
woke_up = true; woke_up = true;
timealarm_on = false; timealarm_on = false;
} }
@ -90,6 +90,7 @@ bool alarm_time_timealarm_woke_this_cycle(void) {
void alarm_time_timealarm_reset(void) { void alarm_time_timealarm_reset(void) {
timealarm_on = false; timealarm_on = false;
woke_up = false; woke_up = false;
SAMD_ALARM_FLAG &= ~SAMD_ALARM_FLAG_TIME; // clear flag
} }
void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) { void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
@ -133,13 +134,14 @@ void alarm_time_timealarm_set_alarms(bool deep_sleep, size_t n_alarms, const mp_
} }
RTC->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_CMP1; RTC->MODE0.INTFLAG.reg = RTC_MODE0_INTFLAG_CMP1;
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP1; RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_CMP1;
SAMD_ALARM_FLAG |= SAMD_ALARM_FLAG_TIME; // set TimeAlarm flag
// This is set for fake sleep. Max fake sleep time is ~72 hours // This is set for fake sleep. Max fake sleep time is ~72 hours
// True deep sleep isn't limited by this // True deep sleep isn't limited by this
// port_interrupt_after_ticks(wakeup_in_ticks); // port_interrupt_after_ticks(wakeup_in_ticks);
// printf("second t %lu, cmp0 %lu, cmp1 %lu\n", (uint32_t)port_get_raw_ticks(NULL),RTC->MODE0.COMP[0].reg,RTC->MODE0.COMP[1].reg); // printf("second t %lu, cmp0 %lu, cmp1 %lu\n", (uint32_t)port_get_raw_ticks(NULL),RTC->MODE0.COMP[0].reg,RTC->MODE0.COMP[1].reg);
// TODO: set up RTC->COMP[1] and create a callback pointing to // TODO: set up RTC->COMP[1] and create a callback pointing to
// timer_callback. See atmel-samd/supervisor/port.c -> _port_interrupt_after_ticks() // time_alarm_callback. See atmel-samd/supervisor/port.c -> _port_interrupt_after_ticks()
// for how to set this up. I don't know how you do the callback, though. You MUST use // for how to set this up. I don't know how you do the callback, though. You MUST use
// COMP[1], since port.c uses COMP[0] already and needs to set that for // COMP[1], since port.c uses COMP[0] already and needs to set that for
// things like the USB enumeration delay. // things like the USB enumeration delay.

View File

@ -27,6 +27,14 @@
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H #ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H #define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ALARM_TIMEALARM_H
// This is the first byte of the BKUP register bank.
// We use it to store which alarms are set.
#ifndef SAMD_ALARM_FLAG
#define SAMD_ALARM_FLAG (RTC->MODE0.BKUP[0].reg)
#define SAMD_ALARM_FLAG_TIME (_U_(0x1) << 0)
#define SAMD_ALARM_FLAG_PIN (_U_(0x1) << 1)
#endif
#include "py/obj.h" #include "py/obj.h"
typedef struct { typedef struct {
@ -36,7 +44,7 @@ typedef struct {
mp_obj_t alarm_time_timealarm_find_triggered_alarm(size_t n_alarms, const mp_obj_t *alarms); 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_create_wakeup_alarm(void);
void timer_callback(void); void time_alarm_callback(void);
bool alarm_time_timealarm_woke_this_cycle(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_set_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms);
void alarm_time_timealarm_reset(void); void alarm_time_timealarm_reset(void);