Skip to content

Commit 82479b6

Browse files
authored
Merge pull request #4503 from hierophect/sleep-api-update
Sleep API changes and fix for main.c silent issue
2 parents 6097afd + 00178ca commit 82479b6

File tree

5 files changed

+34
-70
lines changed

5 files changed

+34
-70
lines changed

main.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ STATIC void start_mp(supervisor_allocation* heap) {
161161

162162
#if CIRCUITPY_ALARM
163163
// Record which alarm woke us up, if any. An object may be created so the heap must be functional.
164-
alarm_save_wake_alarm();
164+
shared_alarm_save_wake_alarm();
165165
// Reset alarm module only after we retrieved the wakeup alarm.
166166
alarm_reset();
167167
#endif
@@ -305,8 +305,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
305305
}
306306
#endif
307307

308-
// TODO: on deep sleep, make sure display is refreshed before sleeping (for e-ink).
309-
310308
cleanup_after_vm(heap);
311309

312310
if (result.return_code & PYEXEC_FORCED_EXIT) {
@@ -329,12 +327,12 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
329327

330328
rgb_status_animation_t animation;
331329
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
332-
bool asleep = false;
330+
bool fake_sleeping = false;
333331
while (true) {
334332
RUN_BACKGROUND_TASKS;
335333
if (reload_requested) {
336334
#if CIRCUITPY_ALARM
337-
if (asleep) {
335+
if (fake_sleeping) {
338336
board_init();
339337
}
340338
#endif
@@ -345,7 +343,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
345343

346344
if (serial_connected() && serial_bytes_available()) {
347345
#if CIRCUITPY_ALARM
348-
if (asleep) {
346+
if (fake_sleeping) {
349347
board_init();
350348
}
351349
#endif
@@ -361,7 +359,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
361359
// an alarm alerts faster than our USB delay or if we pretended to deep
362360
// sleep.
363361
#if CIRCUITPY_ALARM
364-
if (asleep && alarm_woken_from_sleep()) {
362+
if (fake_sleeping && common_hal_alarm_woken_from_sleep()) {
365363
serial_write_compressed(translate("Woken up by alarm.\n"));
366364
board_init();
367365
supervisor_set_run_reason(RUN_REASON_STARTUP);
@@ -400,20 +398,15 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
400398
if (result.return_code & PYEXEC_DEEP_SLEEP) {
401399
// Make sure we have been awake long enough for USB to connect (enumeration delay).
402400
int64_t connecting_delay_ticks = CIRCUITPY_USB_CONNECTED_SLEEP_DELAY * 1024 - port_get_raw_ticks(NULL);
403-
if (connecting_delay_ticks > 0) {
404-
// Set when we've waited long enough so that we wake up from the
405-
// port_idle_until_interrupt below and loop around to the real deep
406-
// sleep in the else clause.
407-
port_interrupt_after_ticks(connecting_delay_ticks);
408-
// Deep sleep if we're not connected to a host.
409-
} else if (!asleep) {
410-
asleep = true;
401+
// Until it's safe to decide whether we're real/fake sleeping, just run the RGB
402+
if (connecting_delay_ticks < 0 && !fake_sleeping) {
403+
fake_sleeping = true;
411404
new_status_color(BLACK);
412405
board_deinit();
413406
if (!supervisor_workflow_active()) {
414407
// Enter true deep sleep. When we wake up we'll be back at the
415408
// top of main(), not in this loop.
416-
alarm_enter_deep_sleep();
409+
common_hal_alarm_enter_deep_sleep();
417410
// Does not return.
418411
} else {
419412
serial_write_compressed(translate("Pretending to deep sleep until alarm, CTRL-C or file write.\n"));
@@ -422,15 +415,19 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
422415
}
423416
#endif
424417

425-
if (!asleep) {
418+
if (!fake_sleeping) {
426419
tick_rgb_status_animation(&animation);
427420
} else {
428421
// This waits until a pretend deep sleep alarm occurs. They are set
429422
// during common_hal_alarm_set_deep_sleep_alarms. On some platforms
430423
// it may also return due to another interrupt, that's why we check
431424
// for deep sleep alarms above. If it wasn't a deep sleep alarm,
432425
// then we'll idle here again.
433-
port_idle_until_interrupt();
426+
#if CIRCUITPY_ALARM
427+
common_hal_alarm_pretending_deep_sleep();
428+
#else
429+
port_idle_until_interrupt();
430+
#endif
434431
}
435432
}
436433
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ STATIC esp_sleep_wakeup_cause_t _get_wakeup_cause(void) {
7575
return esp_sleep_get_wakeup_cause();
7676
}
7777

78-
bool alarm_woken_from_sleep(void) {
78+
bool common_hal_alarm_woken_from_sleep(void) {
7979
return _get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED;
8080
}
8181

@@ -120,8 +120,8 @@ STATIC void _idle_until_alarm(void) {
120120
while (!mp_hal_is_interrupted()) {
121121
RUN_BACKGROUND_TASKS;
122122
// Allow ctrl-C interrupt.
123-
if (alarm_woken_from_sleep()) {
124-
alarm_save_wake_alarm();
123+
if (common_hal_alarm_woken_from_sleep()) {
124+
shared_alarm_save_wake_alarm();
125125
return;
126126
}
127127
port_idle_until_interrupt();
@@ -147,7 +147,7 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
147147
_setup_sleep_alarms(true, n_alarms, alarms);
148148
}
149149

150-
void NORETURN alarm_enter_deep_sleep(void) {
150+
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
151151
alarm_pin_pinalarm_prepare_for_deep_sleep();
152152
alarm_touch_touchalarm_prepare_for_deep_sleep();
153153

@@ -164,5 +164,5 @@ void NORETURN alarm_enter_deep_sleep(void) {
164164
}
165165

166166
void common_hal_alarm_gc_collect(void) {
167-
gc_collect_ptr(alarm_get_wake_alarm());
167+
gc_collect_ptr(shared_alarm_get_wake_alarm());
168168
}

ports/esp32s2/common-hal/alarm/pin/__init__.c

Lines changed: 0 additions & 37 deletions
This file was deleted.

shared-bindings/alarm/__init__.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void validate_objs_are_alarms(size_t n_args, const mp_obj_t *objs) {
9292
//| This allows the user to interrupt an existing program with ctrl-C,
9393
//| and to edit the files in CIRCUITPY, which would not be possible in true light sleep.
9494
//| Thus, to use light sleep and save significant power,
95-
// it may be necessary to disconnect from the host.
95+
//| it may be necessary to disconnect from the host.
9696
//| """
9797
//| ...
9898
//|
@@ -217,7 +217,7 @@ STATIC mp_map_elem_t alarm_module_globals_table[] = {
217217
STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
218218

219219
// Fetch value from module dict.
220-
mp_obj_t alarm_get_wake_alarm(void) {
220+
mp_obj_t shared_alarm_get_wake_alarm(void) {
221221
mp_map_elem_t *elem =
222222
mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP);
223223
if (elem) {
@@ -228,7 +228,7 @@ mp_obj_t alarm_get_wake_alarm(void) {
228228
}
229229

230230
// Initialize .wake_alarm value.
231-
void alarm_save_wake_alarm(void) {
231+
void shared_alarm_save_wake_alarm(void) {
232232
// Equivalent of:
233233
// alarm.wake_alarm = alarm
234234
mp_map_elem_t *elem =
@@ -242,3 +242,8 @@ const mp_obj_module_t alarm_module = {
242242
.base = { &mp_type_module },
243243
.globals = (mp_obj_dict_t *)&alarm_module_globals,
244244
};
245+
246+
extern void port_idle_until_interrupt(void);
247+
MP_WEAK void common_hal_alarm_pretending_deep_sleep(void) {
248+
port_idle_until_interrupt();
249+
}

shared-bindings/alarm/__init__.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,20 @@ extern mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const
4040
// call alarm_woken_from_sleep to see if we've been woken by an alarm and if so,
4141
// it will exit idle as if deep sleep was exited.
4242
extern void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms);
43-
// Deep sleep is entered outside of the VM so we omit the `common_hal_` prefix.
44-
extern NORETURN void alarm_enter_deep_sleep(void);
43+
44+
extern NORETURN void common_hal_alarm_enter_deep_sleep(void);
45+
extern void common_hal_alarm_pretending_deep_sleep(void);
4546

4647
// Fetches value from module dict.
47-
extern mp_obj_t alarm_get_wake_alarm(void);
48+
extern mp_obj_t shared_alarm_get_wake_alarm(void);
4849

4950
extern void common_hal_alarm_gc_collect(void);
5051
extern mp_obj_t common_hal_alarm_get_wake_alarm(void);
5152

5253
// Used by wake-up code.
53-
void alarm_save_wake_alarm(void);
54-
54+
void shared_alarm_save_wake_alarm(void);
5555

5656
// True if an alarm is alerting. This is most useful for pretend deep sleep.
57-
extern bool alarm_woken_from_sleep(void);
58-
57+
extern bool common_hal_alarm_woken_from_sleep(void);
5958

6059
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM___INIT___H

0 commit comments

Comments
 (0)