Working deep sleep via RTC reset, with debug pins

This commit is contained in:
Lucian Copeland 2021-03-21 13:15:44 -04:00
parent 6b2c9985ff
commit d8a2d69e10
5 changed files with 48 additions and 2 deletions

View File

@ -141,8 +141,11 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
//#define NORETURN __attribute__((noreturn))
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,1);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,0);
alarm_pin_pinalarm_prepare_for_deep_sleep();
//port_disable_tick();
port_disable_tick();
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
// alarm_touch_touchalarm_prepare_for_deep_sleep();
// HAL_PWR_EnableBkUpAccess();
// __HAL_RCC_BACKUPRESET_FORCE();

View File

@ -31,6 +31,8 @@
#include "supervisor/port.h"
#include "peripherals/rtc.h"
#include STM32_HAL_H
STATIC volatile bool woke_up;
void common_hal_alarm_time_timealarm_construct(alarm_time_timealarm_obj_t *self, mp_float_t monotonic_time) {

View File

@ -54,7 +54,7 @@ volatile bool alarmed_already[2];
bool peripherals_wkup_on = false;
static void (*wkup_callback)(void);
static void(*alarm_callbacks[2])(void);
static void (*alarm_callbacks[2])(void);
uint32_t stm32_peripherals_get_rtc_freq(void) {
return rtc_clock_frequency;
@ -154,11 +154,20 @@ void stm32_peripherals_rtc_disable_wakeup_timer(void) {
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
}
void stm32_peripherals_reset_alarms(void) {
HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A);
HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_B);
}
void stm32_peripherals_rtc_assign_alarm_callback(uint8_t alarm_idx, void(*callback)(void)) {
alarm_callbacks[alarm_idx] = callback;
}
void stm32_peripherals_rtc_set_alarm(uint8_t alarm_idx, uint32_t ticks) {
// TEMP: ping set alarm (called by port_interrupt_after_ticks and alarm_time_timealarm_set_alarms)
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,0);
uint64_t raw_ticks = stm32_peripherals_rtc_raw_ticks(NULL) + ticks;
RTC_AlarmTypeDef alarm;
@ -187,6 +196,7 @@ void stm32_peripherals_rtc_set_alarm(uint8_t alarm_idx, uint32_t ticks) {
alarm.Alarm = (alarm_idx == PERIPHERALS_ALARM_A) ? RTC_ALARM_A : RTC_ALARM_B;
HAL_RTC_SetAlarm_IT(&hrtc, &alarm, RTC_FORMAT_BIN);
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
alarmed_already[alarm_idx] = false;
}
@ -195,6 +205,8 @@ bool stm32_peripherals_rtc_alarm_triggered(uint8_t alarm_idx) {
}
void RTC_WKUP_IRQHandler(void) {
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7,0);
if (wkup_callback) {
wkup_callback();
}
@ -208,6 +220,10 @@ void RTC_Alarm_IRQHandler(void) {
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *_hrtc) {
// TEMP
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,0);
if (alarm_callbacks[PERIPHERALS_ALARM_A]) {
alarm_callbacks[PERIPHERALS_ALARM_A]();
}
@ -216,6 +232,10 @@ void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *_hrtc) {
}
void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *_hrtc) {
//TEMP
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_2,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_2,0);
if (alarm_callbacks[PERIPHERALS_ALARM_B]) {
alarm_callbacks[PERIPHERALS_ALARM_B]();
}

View File

@ -43,6 +43,7 @@ void stm32_peripherals_rtc_set_wakeup_mode_tick(void);
void stm32_peripherals_rtc_enable_wakeup_timer(void);
void stm32_peripherals_rtc_disable_wakeup_timer(void);
void stm32_peripherals_reset_alarms(void);
void stm32_peripherals_rtc_assign_alarm_callback(uint8_t alarm_idx, void(*callback)(void)) ;
void stm32_peripherals_rtc_set_alarm(uint8_t alarm_idx, uint32_t ticks);
bool stm32_peripherals_rtc_alarm_triggered(uint8_t alarm_idx);

View File

@ -173,10 +173,16 @@ safe_mode_t port_init(void) {
__HAL_RCC_PWR_CLK_ENABLE();
#endif
__HAL_RCC_BACKUPRESET_FORCE();
__HAL_RCC_BACKUPRESET_RELEASE();
stm32_peripherals_clocks_init();
stm32_peripherals_gpio_init();
stm32_peripherals_rtc_init();
__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
stm32_peripherals_reset_alarms();
// Turn off SysTick
SysTick->CTRL = 0;
@ -233,6 +239,20 @@ void reset_port(void) {
#if CIRCUITPY_PULSEIO || CIRCUITPY_ALARM
exti_reset();
#endif
// TEMP: set up interrupt logging pins
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// TEMP: ping port init
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,1);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_8,0);
}
void reset_to_bootloader(void) {