Skip to content

Commit b78e9fc

Browse files
committed
Improve STM reset reason
This causes safe mode to skip the wait for reset when waking up from an alarm. (It also means we don't flash the LED for it.)
1 parent 642fbcf commit b78e9fc

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

ports/stm/common-hal/alarm/__init__.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void alarm_set_wakeup_reason(stm_sleep_source_t reason) {
6464
true_deep_wake_reason = reason;
6565
}
6666

67-
STATIC stm_sleep_source_t _get_wakeup_cause(void) {
67+
stm_sleep_source_t alarm_get_wakeup_cause(void) {
6868
// If in light/fake sleep, check modules
6969
if (alarm_pin_pinalarm_woke_us_up()) {
7070
return STM_WAKEUP_GPIO;
@@ -73,18 +73,18 @@ STATIC stm_sleep_source_t _get_wakeup_cause(void) {
7373
return STM_WAKEUP_RTC;
7474
}
7575
// Check to see if we woke from deep sleep (reason set in port_init)
76-
if (true_deep_wake_reason) {
76+
if (true_deep_wake_reason != STM_WAKEUP_UNDEF) {
7777
return true_deep_wake_reason;
7878
}
7979
return STM_WAKEUP_UNDEF;
8080
}
8181

8282
bool common_hal_alarm_woken_from_sleep(void) {
83-
return _get_wakeup_cause() != STM_WAKEUP_UNDEF;
83+
return alarm_get_wakeup_cause() != STM_WAKEUP_UNDEF;
8484
}
8585

8686
STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
87-
stm_sleep_source_t cause = _get_wakeup_cause();
87+
stm_sleep_source_t cause = alarm_get_wakeup_cause();
8888
switch (cause) {
8989
case STM_WAKEUP_RTC: {
9090
return alarm_time_timealarm_get_wakeup_alarm(n_alarms, alarms);
@@ -156,6 +156,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
156156
}
157157

158158
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
159+
alarm_set_wakeup_reason(STM_WAKEUP_UNDEF);
159160
alarm_pin_pinalarm_prepare_for_deep_sleep();
160161
alarm_time_timealarm_prepare_for_deep_sleep();
161162
port_disable_tick();
@@ -182,6 +183,8 @@ void common_hal_alarm_pretending_deep_sleep(void) {
182183
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
183184
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
184185

186+
alarm_set_wakeup_reason(STM_WAKEUP_UNDEF);
187+
185188
port_idle_until_interrupt();
186189
}
187190

ports/stm/common-hal/alarm/__init__.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef enum {
4040
#define STM_ALARM_FLAG (RTC->BKP0R)
4141

4242
extern void alarm_set_wakeup_reason(stm_sleep_source_t reason);
43+
stm_sleep_source_t alarm_get_wakeup_cause(void);
4344
extern void alarm_reset(void);
4445

4546
#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM__INIT__H

ports/stm/common-hal/microcontroller/Processor.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include <math.h>
2828
#include "py/runtime.h"
2929

30+
#if CIRCUITPY_ALARM
31+
#include "common-hal/alarm/__init__.h"
32+
#endif
3033
#include "common-hal/microcontroller/Processor.h"
3134
#include "shared-bindings/microcontroller/ResetReason.h"
3235
#include "supervisor/shared/translate.h"
@@ -143,5 +146,10 @@ void common_hal_mcu_processor_get_uid(uint8_t raw_id[]) {
143146
}
144147

145148
mcu_reset_reason_t common_hal_mcu_processor_get_reset_reason(void) {
149+
#if CIRCUITPY_ALARM
150+
if (alarm_get_wakeup_cause() != STM_WAKEUP_UNDEF) {
151+
return RESET_REASON_DEEP_SLEEP_ALARM;
152+
}
153+
#endif
146154
return RESET_REASON_UNKNOWN;
147155
}

0 commit comments

Comments
 (0)