thinking through deep sleep stuff

This commit is contained in:
Joey Castillo
2021-08-02 16:14:47 -04:00
parent 34945d78e9
commit 8b92a1b44a
5 changed files with 206 additions and 121 deletions

View File

@@ -1,6 +1,6 @@
/*
* Copyright (c) 2021, Joey Castillo
* SAML22 starter project is Copyright (c) 2014-2017, Alex Taradov <alex@taradov.com>
* UART methods are Copyright (c) 2014-2017, Alex Taradov <alex@taradov.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -82,26 +82,35 @@ void uart_puts(char *s) {
while (*s) uart_putc(*s++);
}
//-----------------------------------------------------------------------------
static void sys_init(void) {
uart_puts("init_mcu\n");
init_mcu();
uart_puts("watch_init\n");
watch_init();
uart_puts("app_init\n");
app_init();
}
//-----------------------------------------------------------------------------
int main(void) {
// Temporary, for debugging.
uart_init(115200);
sys_init();
// ASF code. Initialize the MCU with configuration options from Atmel Studio.
init_mcu();
// User code. Give the app a chance to initialize its data structures and state.
app_init();
// At this point, if the RTC peripheral is enabled, we are waking from BACKUP.
if (watch_rtc_is_enabled()) {
// User code. Give the application a chance to restore state from backup registers.
app_wake_from_deep_sleep();
}
// Watch library code. Set initial parameters for the device and enable the RTC.
watch_init();
// User code. Give the app a chance to enable and set up peripherals.
app_setup();
while (1) {
app_loop();
app_prepare_for_sleep();
sleep(4);
app_wake_from_sleep();
bool can_sleep = app_loop();
if (can_sleep) {
app_prepare_for_sleep();
sleep(4);
app_wake_from_sleep();
}
}
return 0;

View File

@@ -1,15 +1,8 @@
/*
* watch.c
*
* Created: 4/25/2021 10:22:10 AM
* Author: joeycastillo
*/
#include "watch.h"
#include <stdlib.h>
void watch_init() {
// use switching regulator
// Use switching regulator for lower power consumption.
SUPC->VREG.bit.SEL = 1;
while(!SUPC->STATUS.bit.VREGRDY);
@@ -17,9 +10,8 @@ void watch_init() {
CALENDAR_0_init();
calendar_enable(&CALENDAR_0);
// TODO: use performance level 0?
// _set_performance_level(0);
// hri_pm_write_PLCFG_PLDIS_bit(PM, true);
// Not sure if this belongs in every app -- is there a power impact?
delay_driver_init();
}
static const uint8_t Character_Set[] =
@@ -258,6 +250,10 @@ void watch_set_led_off() {
}
}
bool watch_rtc_is_enabled() {
return RTC->MODE0.CTRLA.bit.ENABLE;
}
void watch_set_date_time(struct calendar_date_time date_time) {
calendar_set_date(&CALENDAR_0, &date_time.date);
calendar_set_time(&CALENDAR_0, &date_time.time);
@@ -314,7 +310,7 @@ void watch_enable_pull_down(const uint8_t pin) {
gpio_set_pin_pull_mode(pin, GPIO_PULL_DOWN);
}
bool watch_get_pin_level(const uint8_t pin, const bool level) {
bool watch_get_pin_level(const uint8_t pin) {
return gpio_get_pin_level(pin);
}
@@ -348,3 +344,23 @@ void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length) {
i2c_m_sync_set_slaveaddr(&I2C_0, addr, I2C_M_SEVEN);
io_read(I2C_0_io, buf, length);
}
void watch_store_backup_data(uint32_t data, uint8_t reg) {
if (reg < 8) {
RTC->MODE0.BKUP[reg].reg = data;
}
}
uint32_t watch_get_backup_data(uint8_t reg) {
if (reg < 8) {
return RTC->MODE0.BKUP[reg].reg;
}
return 0;
}
void watch_enter_deep_sleep(){
// Not yet implemented.
// TODO: enable tamper interrupt on ALARM pin.
// sleep(5);
}

View File

@@ -1,55 +1,52 @@
/*
* Watch.h
*
* Created: 4/25/2021 8:29:16 AM
* Author: joeycastillo
*/
#ifndef WATCH_H_
#define WATCH_H_
#include <stdint.h>
#include "driver_init.h"
#include "hpl_calendar.h"
#include "hal_ext_irq.h"
void watch_init();
void watch_enable_display();
void watch_display_pixel(uint8_t com, uint8_t seg);
void watch_display_string(char *string, uint8_t position);
void watch_enable_led(bool pwm);
void watch_disable_led(bool pwm);
void watch_set_led_color(uint16_t red, uint16_t green);
void watch_set_led_red();
void watch_set_led_green();
void watch_set_led_yellow();
void watch_set_led_off();
void watch_set_date_time(struct calendar_date_time date_time);
void watch_get_date_time(struct calendar_date_time *date_time);
void watch_enable_tick_callback(ext_irq_cb_t callback);
void watch_enable_analog(const uint8_t pin);
void watch_enable_buttons();
void watch_register_button_callback(const uint32_t pin, ext_irq_cb_t callback);
void watch_enable_digital_input(const uint8_t pin);
void watch_enable_pull_up(const uint8_t pin);
void watch_enable_pull_down(const uint8_t pin);
bool watch_get_pin_level(const uint8_t pin, const bool level);
void watch_enable_digital_output(const uint8_t pin);
void watch_disable_digital_output(const uint8_t pin);
void watch_set_pin_level(const uint8_t pin, const bool level);
struct io_descriptor *I2C_0_io;
void watch_enable_i2c();
void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length);
void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length);
#ifndef WATCH_H_
#define WATCH_H_
#include <stdint.h>
#include "driver_init.h"
#include "hpl_calendar.h"
#include "hal_ext_irq.h"
void watch_init();
void watch_enable_display();
void watch_display_pixel(uint8_t com, uint8_t seg);
void watch_display_string(char *string, uint8_t position);
void watch_enable_led(bool pwm);
void watch_disable_led(bool pwm);
void watch_set_led_color(uint16_t red, uint16_t green);
void watch_set_led_red();
void watch_set_led_green();
void watch_set_led_yellow();
void watch_set_led_off();
bool watch_rtc_is_enabled();
void watch_set_date_time(struct calendar_date_time date_time);
void watch_get_date_time(struct calendar_date_time *date_time);
void watch_enable_tick_callback(ext_irq_cb_t callback);
void watch_enable_analog(const uint8_t pin);
void watch_enable_buttons();
void watch_register_button_callback(const uint32_t pin, ext_irq_cb_t callback);
void watch_enable_digital_input(const uint8_t pin);
void watch_enable_pull_up(const uint8_t pin);
void watch_enable_pull_down(const uint8_t pin);
bool watch_get_pin_level(const uint8_t pin);
void watch_enable_digital_output(const uint8_t pin);
void watch_disable_digital_output(const uint8_t pin);
void watch_set_pin_level(const uint8_t pin, const bool level);
struct io_descriptor *I2C_0_io;
void watch_enable_i2c();
void watch_i2c_send(int16_t addr, uint8_t *buf, uint16_t length);
void watch_i2c_receive(int16_t addr, uint8_t *buf, uint16_t length);
void watch_store_backup_data(uint32_t data, uint8_t reg);
uint32_t watch_get_backup_data(uint8_t reg);
void watch_enter_deep_sleep();
#endif /* WATCH_H_ */