Skip to content

Commit 9240146

Browse files
committed
Make light sleep store true alarm object to global map
1 parent 66a1583 commit 9240146

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@
4040
#include "supervisor/shared/workflow.h"
4141

4242
STATIC uint8_t true_deep_wake_reason;
43+
STATIC mp_obj_t most_recent_alarm;
4344

4445
void alarm_reset(void) {
46+
most_recent_alarm = NULL;
4547
// Reset the alarm flag
4648
STM_ALARM_FLAG = 0x00;
4749
// alarm_sleep_memory_reset();
@@ -93,6 +95,10 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
9395
}
9496

9597
mp_obj_t common_hal_alarm_get_wake_alarm(void) {
98+
// If we woke from light sleep, override with that alarm
99+
if (most_recent_alarm != NULL) {
100+
return most_recent_alarm;
101+
}
96102
return _get_wake_alarm(0, NULL);
97103
}
98104

@@ -106,9 +112,8 @@ STATIC void _idle_until_alarm(void) {
106112
// Poll for alarms.
107113
while (!mp_hal_is_interrupted()) {
108114
RUN_BACKGROUND_TASKS;
109-
// Allow ctrl-C interrupt.
115+
// Detect if interrupt was alarm or ctrl-C interrupt.
110116
if (common_hal_alarm_woken_from_sleep()) {
111-
shared_alarm_save_wake_alarm();
112117
return;
113118
}
114119
port_idle_until_interrupt();
@@ -128,11 +133,19 @@ mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj
128133
}
129134

130135
mp_obj_t wake_alarm = _get_wake_alarm(n_alarms, alarms);
131-
alarm_reset();
136+
137+
// TODO: make this less roundabout
138+
most_recent_alarm = wake_alarm;
139+
shared_alarm_save_wake_alarm();
140+
141+
// Can't use alarm_reset since it resets most_recent_alarm
142+
alarm_pin_pinalarm_reset();
143+
alarm_time_timealarm_reset();
132144
return wake_alarm;
133145
}
134146

135147
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) {
148+
most_recent_alarm = NULL;
136149
_setup_sleep_alarms(true, n_alarms, alarms);
137150
}
138151

@@ -147,9 +160,6 @@ void NORETURN common_hal_alarm_enter_deep_sleep(void) {
147160

148161
// Set a flag in the backup registers to indicate sleep wakeup
149162
STM_ALARM_FLAG = 0x01;
150-
// HAL_PWR_EnableBkUpAccess();
151-
// __HAL_RCC_BACKUPRESET_FORCE();
152-
// __HAL_RCC_BACKUPRESET_RELEASE();
153163

154164
HAL_PWR_EnterSTANDBYMode();
155165

0 commit comments

Comments
 (0)