Skip to content

Commit cf93898

Browse files
committed
SleepMemory + set alarm.wake_alarm
1 parent 55f4110 commit cf93898

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

main.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ STATIC void start_mp(supervisor_allocation* heap) {
112112
reset_status_led();
113113
autoreload_stop();
114114
supervisor_workflow_reset();
115-
#if CIRCUITPY_ALARM
116-
alarm_reset();
117-
#endif
118115

119116
// Stack limit should be less than real stack size, so we have a chance
120117
// to recover from limit hit. (Limit is measured in bytes.)
@@ -158,6 +155,13 @@ STATIC void start_mp(supervisor_allocation* heap) {
158155

159156
mp_obj_list_init(mp_sys_argv, 0);
160157

158+
#if CIRCUITPY_ALARM
159+
// Record which alarm woke us up, if any. An object may be created so the heap must be functional.
160+
alarm_save_wakeup_alarm();
161+
// Reset alarm module only after we retrieved the wakeup alarm.
162+
alarm_reset();
163+
#endif
164+
161165
#if CIRCUITPY_NETWORK
162166
network_module_init();
163167
#endif
@@ -285,6 +289,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
285289
filesystem_flush();
286290
supervisor_allocation* heap = allocate_remaining_memory();
287291
start_mp(heap);
292+
288293
found_main = maybe_run_list(supported_filenames, &result);
289294
#if CIRCUITPY_FULL_BUILD
290295
if (!found_main){
@@ -354,7 +359,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
354359
serial_write_compressed(translate("Woken up by alarm.\n"));
355360
board_init();
356361
supervisor_set_run_reason(RUN_REASON_STARTUP);
357-
// TODO: Reset any volatile memory the user may have access to.
358362
return true;
359363
}
360364
#endif

ports/esp32s2/common-hal/alarm/time/TimeAlarm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ mp_obj_t alarm_time_timealarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *
5252
alarm_time_time_alarm_obj_t *timer = m_new_obj(alarm_time_time_alarm_obj_t);
5353
timer->base.type = &alarm_time_time_alarm_type;
5454
// TODO: Set monotonic_time based on the RTC state.
55+
timer->monotonic_time = 0.0f;
5556
return timer;
5657
}
5758

shared-bindings/alarm/__init__.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ STATIC mp_map_elem_t alarm_module_globals_table[] = {
197197
};
198198
STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
199199

200-
void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) {
200+
STATIC void alarm_set_wake_alarm(mp_obj_t alarm) {
201201
// Equivalent of:
202202
// alarm.wake_alarm = alarm
203203
mp_map_elem_t *elem =
@@ -207,6 +207,11 @@ void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) {
207207
}
208208
}
209209

210+
// Initialize .wake_alarm value.
211+
void alarm_save_wakeup_alarm(void) {
212+
alarm_set_wake_alarm(common_hal_alarm_get_wake_alarm());
213+
}
214+
210215
const mp_obj_module_t alarm_module = {
211216
.base = { &mp_type_module },
212217
.globals = (mp_obj_dict_t*)&alarm_module_globals,

shared-bindings/alarm/__init__.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ extern void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj
4343
// Deep sleep is entered outside of the VM so we omit the `common_hal_` prefix.
4444
extern NORETURN void alarm_enter_deep_sleep(void);
4545

46+
extern mp_obj_t common_hal_alarm_get_wake_alarm(void);
47+
4648
// Used by wake-up code.
47-
extern void common_hal_alarm_set_wake_alarm(mp_obj_t alarm);
49+
void alarm_save_wakeup_alarm(void);
50+
4851

4952
// True if an alarm is alerting. This is most useful for pretend deep sleep.
5053
extern bool alarm_woken_from_sleep(void);

0 commit comments

Comments
 (0)