Skip to content

Commit

Permalink
bring in deep sleep / external wake functions
Browse files Browse the repository at this point in the history
  • Loading branch information
joeycastillo committed Sep 18, 2024
1 parent c02c89c commit d0ca6a0
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 131 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ SRCS += \
./watch-library/shared/watch/watch_common_display.c \
./watch-library/hardware/watch/watch.c \
./watch-library/hardware/watch/watch_adc.c \
./watch-library/hardware/watch/watch_deepsleep.c \
./watch-library/hardware/watch/watch_extint.c \
./watch-library/hardware/watch/watch_gpio.c \
./watch-library/hardware/watch/watch_private.c \
Expand Down
76 changes: 66 additions & 10 deletions app.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
#include <stdio.h>
#include "app.h"
#include "watch.h"
#include "watch_private.h"
#include "delay.h"
#include "usb.h"
#include "tusb.h"
#include "watch_usb_cdc.h"
#include "tcc.h"

uint8_t ticks = 0;
bool beep = false;

void cb_tick(void);
void cb_mode_btn_interrupt(void);
void cb_light_btn_interrupt(void);
void cb_alarm_btn_extwake(void);

void yield(void) {
tud_task();
cdc_task();
}

void app_init(void) {
// perform watch initialization first!
// initialize the watch hardware.
_watch_init();

// check if we are plugged into USB power.
Expand All @@ -27,20 +35,68 @@ void app_init(void) {
}

void app_setup(void) {
watch_enable_adc();
watch_enable_leds();
watch_enable_external_interrupts();

HAL_GPIO_BTN_LIGHT_in();
HAL_GPIO_BTN_LIGHT_pulldown();
HAL_GPIO_BTN_LIGHT_pmuxen(HAL_GPIO_PMUX_EIC);
HAL_GPIO_BTN_MODE_in();
HAL_GPIO_BTN_MODE_pulldown();
HAL_GPIO_BTN_MODE_pmuxen(HAL_GPIO_PMUX_EIC);

// Simple test sketch exercises RTC and EIC, plus LEDs, screen and buzzer.
// Periodic callback increments the tick counter.
watch_rtc_register_periodic_callback(cb_tick, 1);
// Light button turns on the LED.
watch_register_interrupt_callback(HAL_GPIO_BTN_LIGHT_pin(), cb_light_btn_interrupt, INTERRUPT_TRIGGER_RISING);
// Mode button beeps the piezo.
watch_register_interrupt_callback(HAL_GPIO_BTN_MODE_pin(), cb_mode_btn_interrupt, INTERRUPT_TRIGGER_RISING);
// Alarm buttton resets the tick counter.
watch_register_extwake_callback(HAL_GPIO_BTN_ALARM_pin(), cb_alarm_btn_extwake, true);

watch_enable_display();
watch_display_top_left("WA");
watch_display_top_right(" 0");
watch_display_main_line(" test ");
}

bool app_loop(void) {
uint16_t vcc = watch_get_vcc_voltage();
char buf[7];
snprintf(buf, 7, "%6d", vcc);
watch_display_main_line(buf);
printf("VCC: %d\n", vcc);

if (usb_is_enabled()) {
yield();
}

return false;
if (beep) {
watch_buzzer_play_note(BUZZER_NOTE_C8, 100);
beep = false;
}

char buf[4];
sprintf(buf, "%2d", ticks);
printf("ticks: %d\n", ticks);
watch_display_top_right(buf);

if (ticks >= 30) {
watch_enter_sleep_mode();
}

return !usb_is_enabled();
}

void cb_tick(void) {
ticks = ticks + 1;
watch_set_led_off();
}

void cb_light_btn_interrupt(void) {
watch_set_led_green();
}

void cb_mode_btn_interrupt(void) {
beep = true;
}

void cb_alarm_btn_extwake(void) {
watch_set_led_red();
ticks = 0;
}
180 changes: 83 additions & 97 deletions watch-library/hardware/watch/watch_deepsleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,94 +22,93 @@
* SOFTWARE.
*/

#include "hpl_systick_config.h"

#include "watch_extint.h"

// this warning only appears when you `make BOARD=OSO-SWAT-A1-02`. it's annoying,
// but i'd rather have it warn us at build-time than fail silently at run-time.
// besides, no one but me really has any of these boards anyway.
#if BTN_ALARM != GPIO(GPIO_PORTA, 2)
#warning This board revision does not support external wake on BTN_ALARM, so watch_register_extwake_callback will not work with it. Use watch_register_interrupt_callback instead.
#endif

void watch_register_extwake_callback(uint8_t pin, ext_irq_cb_t callback, bool level) {
uint32_t pinmux;
hri_rtc_tampctrl_reg_t config = RTC->MODE2.TAMPCTRL.reg;

switch (pin) {
case A4:
a4_callback = callback;
pinmux = PINMUX_PB00G_RTC_IN0;
config &= ~(3 << RTC_TAMPCTRL_IN0ACT_Pos);
config &= ~(1 << RTC_TAMPCTRL_TAMLVL0_Pos);
config |= 1 << RTC_TAMPCTRL_IN0ACT_Pos;
if (level) config |= 1 << RTC_TAMPCTRL_TAMLVL0_Pos;
break;
case A2:
a2_callback = callback;
pinmux = PINMUX_PB02G_RTC_IN1;
config &= ~(3 << RTC_TAMPCTRL_IN1ACT_Pos);
config &= ~(1 << RTC_TAMPCTRL_TAMLVL1_Pos);
config |= 1 << RTC_TAMPCTRL_IN1ACT_Pos;
if (level) config |= 1 << RTC_TAMPCTRL_TAMLVL1_Pos;
break;
case BTN_ALARM:
gpio_set_pin_pull_mode(pin, GPIO_PULL_DOWN);
btn_alarm_callback = callback;
pinmux = PINMUX_PA02G_RTC_IN2;
config &= ~(3 << RTC_TAMPCTRL_IN2ACT_Pos);
config &= ~(1 << RTC_TAMPCTRL_TAMLVL2_Pos);
config |= 1 << RTC_TAMPCTRL_IN2ACT_Pos;
if (level) config |= 1 << RTC_TAMPCTRL_TAMLVL2_Pos;
break;
default:
return;
#include "watch_deepsleep.h"
#include "app.h"
#include "watch.h"
#include "watch_private.h"

void sleep(const uint8_t mode) {
PM->SLEEPCFG.bit.SLEEPMODE = mode;

// wait for the mode set to actually take, per SLEEPCFG note in data sheet:
// "A small latency happens between the store instruction and actual writing
// of the SLEEPCFG register due to bridges. Software has to make sure the
// SLEEPCFG register reads the wanted value before issuing WFI instruction."
while(PM->SLEEPCFG.bit.SLEEPMODE != mode);

__DSB();
__WFI();
}

void watch_register_extwake_callback(uint8_t pin, watch_cb_t callback, bool level) {
uint32_t config = RTC->MODE2.TAMPCTRL.reg;

if (pin == HAL_GPIO_BTN_ALARM_pin()) {
HAL_GPIO_BTN_ALARM_in();
HAL_GPIO_BTN_ALARM_pulldown();
HAL_GPIO_BTN_ALARM_pmuxen(HAL_GPIO_PMUX_RTC);
btn_alarm_callback = callback;
config &= ~(3 << RTC_TAMPCTRL_IN2ACT_Pos);
config &= ~(1 << RTC_TAMPCTRL_TAMLVL2_Pos);
config |= 1 << RTC_TAMPCTRL_IN2ACT_Pos;
if (level) config |= 1 << RTC_TAMPCTRL_TAMLVL2_Pos;
} else if (pin == HAL_GPIO_A2_pin()) {
HAL_GPIO_A2_in();
HAL_GPIO_A2_pmuxen(HAL_GPIO_PMUX_RTC);
a2_callback = callback;
config &= ~(3 << RTC_TAMPCTRL_IN1ACT_Pos);
config &= ~(1 << RTC_TAMPCTRL_TAMLVL1_Pos);
config |= 1 << RTC_TAMPCTRL_IN1ACT_Pos;
if (level) config |= 1 << RTC_TAMPCTRL_TAMLVL1_Pos;
} else if (pin == HAL_GPIO_A4_pin()) {
HAL_GPIO_A4_in();
HAL_GPIO_A4_pmuxen(HAL_GPIO_PMUX_RTC);
a4_callback = callback;
config &= ~(3 << RTC_TAMPCTRL_IN0ACT_Pos);
config &= ~(1 << RTC_TAMPCTRL_TAMLVL0_Pos);
config |= 1 << RTC_TAMPCTRL_IN0ACT_Pos;
if (level) config |= 1 << RTC_TAMPCTRL_TAMLVL0_Pos;
}
gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
gpio_set_pin_function(pin, pinmux);

// disable the RTC
RTC->MODE2.CTRLA.bit.ENABLE = 0;
while (RTC->MODE2.SYNCBUSY.bit.ENABLE); // wait for RTC to be disabled

// update the configuration
RTC->MODE2.TAMPCTRL.reg = config;

// re-enable the RTC
RTC->MODE2.CTRLA.bit.ENABLE = 1;
while (RTC->MODE2.SYNCBUSY.bit.ENABLE); // wait for RTC to be enabled

NVIC_ClearPendingIRQ(RTC_IRQn);
NVIC_EnableIRQ(RTC_IRQn);
RTC->MODE2.INTENSET.reg = RTC_MODE2_INTENSET_TAMPER;
}

void watch_disable_extwake_interrupt(uint8_t pin) {
hri_rtc_tampctrl_reg_t config = hri_rtc_get_TAMPCTRL_reg(RTC, 0xFFFFFFFF);

switch (pin) {
case A4:
a4_callback = NULL;
config &= ~(3 << RTC_TAMPCTRL_IN0ACT_Pos);
break;
case A2:
a2_callback = NULL;
config &= ~(3 << RTC_TAMPCTRL_IN1ACT_Pos);
break;
case BTN_ALARM:
btn_alarm_callback = NULL;
config &= ~(3 << RTC_TAMPCTRL_IN2ACT_Pos);
break;
default:
return;
uint32_t config = RTC->MODE2.TAMPCTRL.reg;

if (pin == HAL_GPIO_BTN_ALARM_pin()) {
btn_alarm_callback = NULL;
config &= ~(3 << RTC_TAMPCTRL_IN2ACT_Pos);
} else if (pin == HAL_GPIO_A2_pin()) {
a2_callback = NULL;
config &= ~(3 << RTC_TAMPCTRL_IN1ACT_Pos);
}
else if (pin == HAL_GPIO_A4_pin()) {
a4_callback = NULL;
config &= ~(3 << RTC_TAMPCTRL_IN0ACT_Pos);
}

// disable the RTC
RTC->MODE2.CTRLA.bit.ENABLE = 0;
while (RTC->MODE2.SYNCBUSY.bit.ENABLE); // wait for RTC to be disabled

// update the configuration
RTC->MODE2.TAMPCTRL.reg = config;

if (hri_rtcmode0_get_CTRLA_ENABLE_bit(RTC)) {
hri_rtcmode0_clear_CTRLA_ENABLE_bit(RTC);
hri_rtcmode0_wait_for_sync(RTC, RTC_MODE0_SYNCBUSY_ENABLE);
}
hri_rtc_write_TAMPCTRL_reg(RTC, config);
hri_rtcmode0_set_CTRLA_ENABLE_bit(RTC);
// re-enable the RTC
RTC->MODE2.CTRLA.bit.ENABLE = 1;
}

void watch_store_backup_data(uint32_t data, uint8_t reg) {
Expand All @@ -135,20 +134,27 @@ static void _watch_disable_all_pins_except_rtc(void) {
// same with RTC/IN[1] and PB02
if (config & RTC_TAMPCTRL_IN1ACT_Msk) portb_pins_to_disable &= 0xFFFFFFFB;

// port A: always keep PA02 configured as-is; that's our ALARM button.
gpio_set_port_direction(0, 0xFFFFFFFB, GPIO_DIRECTION_OFF);
// port A: that last B is to always keep PA02 configured as-is; that's our ALARM button.
PORT->Group[0].DIRCLR.reg = 0xFFFFFFFB;
// WRCONFIG can only set half the pins at a time, so we need two writes. This sets pins 0-15.
PORT->Group[0].WRCONFIG.reg = PORT_WRCONFIG_WRPINCFG | 0xFFFB;
// ...and adding the HWSEL flag configures 16-31.
PORT->Group[0].WRCONFIG.reg = PORT_WRCONFIG_HWSEL | PORT_WRCONFIG_WRPINCFG | 0xffff;

// port B: disable all pins we didn't save above.
gpio_set_port_direction(1, portb_pins_to_disable, GPIO_DIRECTION_OFF);
PORT->Group[1].DIRCLR.reg = portb_pins_to_disable;
PORT->Group[1].WRCONFIG.reg = PORT_WRCONFIG_WRPINCFG | (portb_pins_to_disable & 0xFFFF);
PORT->Group[1].WRCONFIG.reg = PORT_WRCONFIG_HWSEL | PORT_WRCONFIG_WRPINCFG | (portb_pins_to_disable >> 16);
}

static void _watch_disable_all_peripherals_except_slcd(void) {
_watch_disable_tcc();
watch_disable_adc();
watch_disable_external_interrupts();
watch_disable_i2c();
// TODO: replace this with a proper function when we remove the debug UART
SERCOM3->USART.CTRLA.reg &= ~SERCOM_USART_CTRLA_ENABLE;
MCLK->APBCMASK.reg &= ~MCLK_APBCMASK_SERCOM3;
/// TODO: Actually disable all these peripherals! #SecondMovement
// watch_disable_i2c();
// SERCOM3->USART.CTRLA.reg &= ~SERCOM_USART_CTRLA_ENABLE;
// MCLK->APBCMASK.reg &= ~MCLK_APBCMASK_SERCOM3;
}

void watch_enter_sleep_mode(void) {
Expand All @@ -161,41 +167,21 @@ void watch_enter_sleep_mode(void) {
// disable brownout detector interrupt, which could inadvertently wake us up.
SUPC->INTENCLR.bit.BOD33DET = 1;

// per Microchip datasheet clarification DS80000782,
// work around silicon erratum 1.8.4 by disabling the SysTick interrupt, which is
// enabled as part of driver init, before going to sleep.
SysTick->CTRL = SysTick->CTRL & ~(CONF_SYSTICK_TICKINT << SysTick_CTRL_TICKINT_Pos);

// disable all pins
_watch_disable_all_pins_except_rtc();

// enter standby (4); we basically hang out here until an interrupt wakes us.
sleep(4);

// and we awake! re-enable the brownout detector and SysTick interrupt
// and we awake! re-enable the brownout detector
SUPC->INTENSET.bit.BOD33DET = 1;
SysTick->CTRL = SysTick->CTRL | (CONF_SYSTICK_TICKINT << SysTick_CTRL_TICKINT_Pos);

// call app_setup so the app can re-enable everything we disabled.
app_setup();

// and call app_wake_from_standby (since main won't have a chance to do it)
app_wake_from_standby();
}

void watch_enter_deep_sleep_mode(void) {
// identical to sleep mode except we disable the LCD first.
slcd_sync_deinit(&SEGMENT_LCD_0);
hri_mclk_clear_APBCMASK_SLCD_bit(SLCD);

watch_enter_sleep_mode();
}

void watch_enter_backup_mode(void) {
watch_rtc_disable_all_periodic_callbacks();
_watch_disable_all_peripherals_except_slcd();
slcd_sync_deinit(&SEGMENT_LCD_0);
hri_mclk_clear_APBCMASK_SLCD_bit(SLCD);
_watch_disable_all_pins_except_rtc();

// go into backup sleep mode (5). when we exit, the reset controller will take over.
Expand Down
2 changes: 1 addition & 1 deletion watch-library/shared/watch/watch.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ typedef void (*watch_cb_t)(void);
// #include "watch_spi.h"
// #include "watch_uart.h"
// #include "watch_storage.h"
// #include "watch_deepsleep.h"
#include "watch_deepsleep.h"

/** @brief Interrupt handler for the SYSTEM interrupt, which handles MCLK,
* OSC32KCTRL, OSCCTRL, PAC, PM and SUPC.
Expand Down
Loading

0 comments on commit d0ca6a0

Please sign in to comment.