Skip to content

Commit 104a089

Browse files
committed
deep sleep working; deep sleep delay when connected
1 parent ef0830b commit 104a089

15 files changed

Lines changed: 143 additions & 155 deletions

File tree

main.c

Lines changed: 12 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@
6666

6767
#include "boards/board.h"
6868

69-
// REMOVE ***********
70-
#include "esp_log.h"
71-
7269
#if CIRCUITPY_ALARM
7370
#include "shared-bindings/alarm/__init__.h"
7471
#endif
@@ -98,12 +95,6 @@
9895
#include "common-hal/canio/CAN.h"
9996
#endif
10097

101-
// How long to wait for host to start connecting..
102-
#define CIRCUITPY_USB_CONNECTING_DELAY 1
103-
104-
// How long to flash errors on the RGB status LED before going to sleep (secs)
105-
#define CIRCUITPY_FLASH_ERROR_PERIOD 10
106-
10798
#if MICROPY_ENABLE_PYSTACK
10899
static size_t PLACE_IN_DTCM_BSS(_pystack[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]);
109100
#endif
@@ -259,17 +250,6 @@ STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
259250
}
260251
}
261252

262-
// Should we go into deep sleep when program finishes?
263-
// Normally we won't deep sleep if there was an error or if we are connected to a host
264-
// but either of those can be enabled.
265-
// It's ok to deep sleep if we're not connected to a host, but we need to make sure
266-
// we're giving enough time for USB to start to connect
267-
STATIC bool deep_sleep_allowed(void) {
268-
return
269-
(ok || supervisor_workflow_get_allow_deep_sleep_on_error()) &&
270-
!supervisor_workflow_connecting()
271-
(supervisor_ticks_ms64() > CIRCUITPY_USB_CONNECTING_DELAY * 1024);
272-
273253
STATIC bool run_code_py(safe_mode_t safe_mode) {
274254
bool serial_connected_at_start = serial_connected();
275255
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
@@ -290,10 +270,12 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
290270
if (safe_mode == NO_SAFE_MODE) {
291271
new_status_color(MAIN_RUNNING);
292272

293-
static const char * const supported_filenames[] = STRING_LIST("code.txt", "code.py", "main.py", "main.txt");
273+
static const char * const supported_filenames[] = STRING_LIST(
274+
"code.txt", "code.py", "main.py", "main.txt");
294275
#if CIRCUITPY_FULL_BUILD
295-
static const char * const double_extension_filenames[] = STRING_LIST("code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
296-
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
276+
static const char * const double_extension_filenames[] = STRING_LIST(
277+
"code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
278+
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
297279
#endif
298280

299281
stack_resize();
@@ -319,24 +301,16 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
319301
// Program has finished running.
320302

321303
// Display a different completion message if the user has no USB attached (cannot save files)
322-
if (!serial_connected_at_start && !deep_sleep_allowed()) {
304+
if (!serial_connected_at_start) {
323305
serial_write_compressed(translate("\nCode done running. Waiting for reload.\n"));
324306
}
325307

326308
bool serial_connected_before_animation = false;
327309
#if CIRCUITPY_DISPLAYIO
328310
bool refreshed_epaper_display = false;
329311
#endif
330-
rgb_status_animation_t animation;
331-
bool ok = result.return_code != PYEXEC_EXCEPTION;
332-
333-
#if CIRCUITPY_ALARM
334-
// Enable pin or time alarms before sleeping.
335-
// If immediate_wake is true, then there's an alarm that would trigger immediately,
336-
// so don't sleep.
337-
bool immediate_wake = !common_hal_alarm_enable_deep_sleep_alarms();
338-
#endif
339312

313+
rgb_status_animation_t animation;
340314
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
341315
while (true) {
342316

@@ -360,12 +334,10 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
360334
if (!serial_connected_at_start) {
361335
print_code_py_status_message(safe_mode);
362336
}
363-
// We won't be going into the REPL if we're going to sleep.
364-
if (!deep_sleep_allowed()) {
365-
print_safe_mode_message(safe_mode);
366-
serial_write("\n");
367-
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload."));
368-
}
337+
338+
print_safe_mode_message(safe_mode);
339+
serial_write("\n");
340+
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload."));
369341
}
370342
if (serial_connected_before_animation && !serial_connected()) {
371343
serial_connected_at_start = false;
@@ -379,37 +351,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
379351
}
380352
#endif
381353

382-
bool animation_done = false;
383-
384-
if (deep_sleep_allowed() && ok) {
385-
// Skip animation if everything is OK.
386-
animation_done = true;
387-
} else {
388-
animation_done = tick_rgb_status_animation(&animation);
389-
}
390-
// Do an error animation only once before deep-sleeping.
391-
if (animation_done) {
392-
if (immediate_wake) {
393-
// Don't sleep, we are already supposed to wake up.
394-
return true;
395-
}
396-
if (deep_sleep_allowed()) {
397-
common_hal_mcu_deep_sleep();
398-
// Does not return.
399-
}
400-
401-
// Wake up every so often to flash the error code.
402-
if (!ok) {
403-
port_interrupt_after_ticks(CIRCUITPY_FLASH_ERROR_PERIOD * 1024);
404-
} else {
405-
int64_t remaining_connecting_wait =
406-
CIRCUITPY_USB_CONNECTING_DELAY * 1024 - supervisor_ticks_ms64();
407-
if (remaining_connecting_wait > 0) {
408-
port_interrupt_after_ticks(remaining_connecting_wait);
409-
}
410-
port_sleep_until_interrupt();
411-
}
412-
}
354+
tick_rgb_status_animation(&animation);
413355
}
414356
}
415357

ports/atmel-samd/common-hal/microcontroller/__init__.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ void common_hal_mcu_reset(void) {
8484
reset();
8585
}
8686

87-
void common_hal_mcu_deep_sleep(void) {
88-
//deep sleep call here
89-
}
90-
9187
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
9288
// It currently only has properties, and no state.
9389
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

ports/cxd56/common-hal/microcontroller/__init__.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ void common_hal_mcu_reset(void) {
8181
boardctl(BOARDIOC_RESET, 0);
8282
}
8383

84-
void common_hal_mcu_deep_sleep(void) {
85-
//deep sleep call here
86-
}
87-
8884
STATIC const mp_rom_map_elem_t mcu_pin_globals_table[] = {
8985
{ MP_ROM_QSTR(MP_QSTR_UART2_RXD), MP_ROM_PTR(&pin_UART2_RXD) },
9086
{ MP_ROM_QSTR(MP_QSTR_UART2_TXD), MP_ROM_PTR(&pin_UART2_TXD) },

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

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
* This file is part of the MicroPython project, http://micropython.org/
33
*
44
* The MIT License (MIT)
@@ -77,12 +77,10 @@ mp_obj_t common_hal_alarm_get_wake_alarm(void) {
7777
return mp_const_none;
7878
}
7979

80-
mp_obj_t common_hal_alarm_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
81-
mp_raise_NotImplementedError(NULL);
82-
}
83-
84-
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) {
80+
STATIC void setup_alarms(size_t n_alarms, const mp_obj_t *alarms) {
8581
bool time_alarm_set = false;
82+
alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_NULL;
83+
8684
for (size_t i = 0; i < n_alarms; i++) {
8785
if (MP_OBJ_IS_TYPE(alarms[i], &alarm_pin_pin_alarm_type)) {
8886
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
@@ -91,32 +89,32 @@ void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *ala
9189
if (time_alarm_set) {
9290
mp_raise_ValueError(translate("Only one alarm.time alarm can be set."));
9391
}
92+
time_alarm = MP_OBJ_TO_PTR(alarms[i]);
9493
time_alarm_set = true;
9594
}
9695
}
9796

98-
_deep_sleep_alarms = mp_obj_new_tuple(n_alarms, alarms);
97+
if (time_alarm != MP_OBJ_NULL) {
98+
// Compute how long to actually sleep, considering the time now.
99+
mp_float_t now_secs = uint64_to_float(common_hal_time_monotonic_ms()) / 1000.0f;
100+
mp_float_t wakeup_in_secs = MAX(0.0f, time_alarm->monotonic_time - now_secs);
101+
esp_sleep_enable_timer_wakeup((uint64_t) (wakeup_in_secs * 1000000));
102+
}
99103
}
100104

101-
// Return false if we should wake up immediately because a time alarm is in the past
102-
// or otherwise already triggered.
103-
bool common_hal_alarm_enable_deep_sleep_alarms(void) {
104-
for (size_t i = 0; i < _deep_sleep_alarms->len; i++) {
105-
mp_obj_t alarm = _deep_sleep_alarms->items[i];
106-
if (MP_OBJ_IS_TYPE(alarm, &alarm_pin_pin_alarm_type)) {
107-
// TODO: handle pin alarms
108-
mp_raise_NotImplementedError(translate("PinAlarm deep sleep not yet implemented"));
109-
}
110-
else if (MP_OBJ_IS_TYPE(alarm, &alarm_time_time_alarm_type)) {
111-
alarm_time_time_alarm_obj_t *time_alarm = MP_OBJ_TO_PTR(alarm);
112-
mp_float_t now_secs = uint64_to_float(common_hal_time_monotonic_ms()) / 1000.0f;
113-
// Compute how long to actually sleep, considering hte time now.
114-
mp_float_t wakeup_in_secs = time_alarm->monotonic_time - now_secs;
115-
if (wakeup_in_secs <= 0.0f) {
116-
return false;
117-
}
118-
esp_sleep_enable_timer_wakeup((uint64_t) (wakeup_in_secs * 1000000));
119-
}
120-
}
121-
return true;
105+
mp_obj_t common_hal_alarm_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
106+
setup_alarms(n_alarms, alarms);
107+
108+
// Shut down wifi cleanly.
109+
esp_wifi_stop();
110+
esp_light_sleep_start();
111+
return common_hal_alarm_get_wake_alarm();
112+
}
113+
114+
void common_hal_alarm_exit_and_deep_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
115+
setup_alarms(n_alarms, alarms);
116+
117+
// Shut down wifi cleanly.
118+
esp_wifi_stop();
119+
esp_deep_sleep_start();
122120
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ void common_hal_mcu_reset(void) {
8181
while(1);
8282
}
8383

84-
void NORETURN common_hal_mcu_deep_sleep(void) {
85-
// Shut down wifi cleanly.
86-
esp_wifi_stop();
87-
esp_deep_sleep_start();
88-
}
89-
9084
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
9185
// It currently only has properties, and no state.
9286
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

ports/litex/common-hal/microcontroller/__init__.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ void common_hal_mcu_reset(void) {
8989
while(1);
9090
}
9191

92-
void common_hal_mcu_deep_sleep(void) {
93-
//deep sleep call here
94-
}
95-
9692
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
9793
// It currently only has properties, and no state.
9894
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

ports/mimxrt10xx/common-hal/microcontroller/__init__.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,6 @@ void common_hal_mcu_reset(void) {
8686
NVIC_SystemReset();
8787
}
8888

89-
void common_hal_mcu_deep_sleep(void) {
90-
//deep sleep call here
91-
}
92-
9389
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
9490
// It currently only has properties, and no state.
9591
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

ports/nrf/common-hal/microcontroller/__init__.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ void common_hal_mcu_reset(void) {
9595
reset_cpu();
9696
}
9797

98-
void common_hal_mcu_deep_sleep(void) {
99-
//deep sleep call here
100-
}
101-
10298
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
10399
// It currently only has properties, and no state.
104100
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@ void common_hal_mcu_reset(void) {
8181
NVIC_SystemReset();
8282
}
8383

84-
void common_hal_mcu_deep_sleep(void) {
85-
//deep sleep call here
86-
}
87-
8884
// The singleton microcontroller.Processor object, bound to microcontroller.cpu
8985
// It currently only has properties, and no state.
9086
const mcu_processor_obj_t common_hal_mcu_processor_obj = {

shared-bindings/_typing/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ FrameBuffer = Union[rgbmatrix.RGBMatrix]
5454
"""
5555

5656
Alarm = Union[
57-
alarm.pin.PinAlarm, alarm.time.DurationAlarm
57+
alarm.pin.PinAlarm, alarm.time.TimeAlarm
5858
]
5959
"""Classes that implement alarms for sleeping and asynchronous notification.
6060
6161
- `alarm.pin.PinAlarm`
62-
- `alarm.time.DurationAlarm`
62+
- `alarm.time.TimeAlarm`
6363
6464
You can use these alarms to wake from light or deep sleep.
6565
"""

0 commit comments

Comments
 (0)