Skip to content

Commit 40118bc

Browse files
committed
Add board_deinit for use with sleep
This changes lots of files to unify `board.h` across ports. It adds `board_deinit` when CIRCUITPY_ALARM is set. `main.c` uses it to deinit the board before deep sleeping (even when pretending.) Deep sleep is now a two step process for the port. First, the port should prepare to deep sleep based on the given alarms. It should set alarms for both deep and pretend sleep. In particular, the pretend versions should be set immediately so that we don't miss an alarm as we shutdown. These alarms should also wake from `port_idle_until_interrupt` which is used when pretending to deep sleep. Second, when real deep sleeping, `alarm_enter_deep_sleep` is called. The port should set any alarms it didn't during prepare based on data it saved internally during prepare. ESP32-S2 sleep is a bit reorganized to locate more logic with TimeAlarm. This will help it scale to more alarm types. Fixes #3786
1 parent 1df0334 commit 40118bc

File tree

241 files changed

+588
-673
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+588
-673
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ update-frozen-libraries:
265265
@echo "Updating all frozen libraries to latest tagged version."
266266
cd frozen; for library in *; do cd $$library; ../../tools/git-checkout-latest-tag.sh; cd ..; done
267267

268-
one-of-each: samd21 samd51 esp32s2 litex mimxrt10xx nrf stm
268+
one-of-each: samd21 litex mimxrt10xx nrf stm
269269

270270
samd21:
271271
$(MAKE) -C ports/atmel-samd BOARD=trinket_m0

locale/circuitpython.pot

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-11-27 23:57-0500\n"
11+
"POT-Creation-Date: 2020-12-08 09:56-0800\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -498,8 +498,8 @@ msgstr ""
498498
msgid "Buffer must be a multiple of 512 bytes"
499499
msgstr ""
500500

501-
#: shared-bindings/bitbangio/I2C.c shared-bindings/busdevice/I2CDevice.c
502-
#: shared-bindings/busio/I2C.c
501+
#: shared-bindings/adafruit_bus_device/I2CDevice.c
502+
#: shared-bindings/bitbangio/I2C.c shared-bindings/busio/I2C.c
503503
msgid "Buffer must be at least length 1"
504504
msgstr ""
505505

@@ -1296,7 +1296,7 @@ msgstr ""
12961296
msgid "No DMA channel found"
12971297
msgstr ""
12981298

1299-
#: shared-module/busdevice/I2CDevice.c
1299+
#: shared-module/adafruit_bus_device/I2CDevice.c
13001300
#, c-format
13011301
msgid "No I2C device at address: %x"
13021302
msgstr ""
@@ -1503,6 +1503,7 @@ msgstr ""
15031503
msgid "Pin does not have ADC capabilities"
15041504
msgstr ""
15051505

1506+
#: shared-bindings/adafruit_bus_device/SPIDevice.c
15061507
#: shared-bindings/digitalio/DigitalInOut.c
15071508
msgid "Pin is input only"
15081509
msgstr ""
@@ -1555,7 +1556,11 @@ msgid "Prefix buffer must be on the heap"
15551556
msgstr ""
15561557

15571558
#: main.c
1558-
msgid "Press any key to enter the REPL. Use CTRL-D to reload."
1559+
msgid "Press any key to enter the REPL. Use CTRL-D to reload.\n"
1560+
msgstr ""
1561+
1562+
#: main.c
1563+
msgid "Pretending to deep sleep until alarm, any key or file write.\n"
15591564
msgstr ""
15601565

15611566
#: shared-bindings/digitalio/DigitalInOut.c
@@ -2026,6 +2031,10 @@ msgstr ""
20262031
msgid "WiFi password must be between 8 and 63 characters"
20272032
msgstr ""
20282033

2034+
#: main.c
2035+
msgid "Woken up by alarm.\n"
2036+
msgstr ""
2037+
20292038
#: ports/nrf/common-hal/_bleio/PacketBuffer.c
20302039
msgid "Writes not supported on Characteristic"
20312040
msgstr ""

main.c

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "background.h"
4747
#include "mpconfigboard.h"
4848
#include "supervisor/background_callback.h"
49+
#include "supervisor/board.h"
4950
#include "supervisor/cpu.h"
5051
#include "supervisor/filesystem.h"
5152
#include "supervisor/memory.h"
@@ -64,8 +65,6 @@
6465
#include "shared-bindings/microcontroller/Processor.h"
6566
#include "shared-bindings/supervisor/Runtime.h"
6667

67-
#include "boards/board.h"
68-
6968
#if CIRCUITPY_ALARM
7069
#include "shared-bindings/alarm/__init__.h"
7170
#endif
@@ -303,13 +302,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
303302
if (result.return_code & PYEXEC_FORCED_EXIT) {
304303
return reload_requested;
305304
}
306-
307-
#if CIRCUITPY_ALARM
308-
if (result.return_code & PYEXEC_DEEP_SLEEP) {
309-
common_hal_alarm_enter_deep_sleep();
310-
// Does not return.
311-
}
312-
#endif
313305
}
314306

315307
// Program has finished running.
@@ -326,32 +318,55 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
326318

327319
rgb_status_animation_t animation;
328320
prep_rgb_status_animation(&result, found_main, safe_mode, &animation);
321+
bool asleep = false;
329322
while (true) {
330-
331323
RUN_BACKGROUND_TASKS;
332324
if (reload_requested) {
325+
#if CIRCUITPY_ALARM
326+
if (asleep) {
327+
board_init();
328+
}
329+
#endif
333330
supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD);
334331
reload_requested = false;
335332
return true;
336333
}
337334

338335
if (serial_connected() && serial_bytes_available()) {
336+
#if CIRCUITPY_ALARM
337+
if (asleep) {
338+
board_init();
339+
}
340+
#endif
339341
// Skip REPL if reload was requested.
340342
bool ctrl_d = serial_read() == CHAR_CTRL_D;
341343
if (ctrl_d) {
342344
supervisor_set_run_reason(RUN_REASON_REPL_RELOAD);
343345
}
344-
return (ctrl_d);
346+
return ctrl_d;
345347
}
346348

349+
// Check for a deep sleep alarm and restart the VM. This can happen if
350+
// an alarm alerts faster than our USB delay or if we pretended to deep
351+
// sleep.
352+
#if CIRCUITPY_ALARM
353+
if (asleep && alarm_woken_from_sleep()) {
354+
serial_write_compressed(translate("Woken up by alarm.\n"));
355+
board_init();
356+
supervisor_set_run_reason(RUN_REASON_STARTUP);
357+
// TODO: Reset any volatile memory the user may have access to.
358+
return true;
359+
}
360+
#endif
361+
347362
if (!serial_connected_before_animation && serial_connected()) {
348363
if (!serial_connected_at_start) {
349364
print_code_py_status_message(safe_mode);
350365
}
351366

352367
print_safe_mode_message(safe_mode);
353368
serial_write("\n");
354-
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload."));
369+
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload.\n"));
355370
}
356371
if (serial_connected_before_animation && !serial_connected()) {
357372
serial_connected_at_start = false;
@@ -360,12 +375,47 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
360375

361376
// Refresh the ePaper display if we have one. That way it'll show an error message.
362377
#if CIRCUITPY_DISPLAYIO
378+
// Don't refresh the display if we're about to deep sleep.
379+
#if CIRCUITPY_ALARM
380+
refreshed_epaper_display = refreshed_epaper_display || result.return_code & PYEXEC_DEEP_SLEEP;
381+
#endif
363382
if (!refreshed_epaper_display) {
364383
refreshed_epaper_display = maybe_refresh_epaperdisplay();
365384
}
366385
#endif
367386

368-
tick_rgb_status_animation(&animation);
387+
// Sleep until our next interrupt.
388+
#if CIRCUITPY_ALARM
389+
if (result.return_code & PYEXEC_DEEP_SLEEP) {
390+
// Make sure we have been awake long enough for USB to connect (enumeration delay).
391+
int64_t connecting_delay_ticks = CIRCUITPY_USB_CONNECTED_SLEEP_DELAY * 1024 - port_get_raw_ticks(NULL);
392+
if (connecting_delay_ticks > 0) {
393+
// Set when we've waited long enough so that we wake up from the
394+
// sleep_until_interrupt below and loop around to the real deep
395+
// sleep in the else clause.
396+
port_interrupt_after_ticks(connecting_delay_ticks);
397+
// Deep sleep if we're not connected to a host.
398+
} else if (!asleep) {
399+
asleep = true;
400+
new_status_color(BLACK);
401+
board_deinit();
402+
if (!supervisor_workflow_active()) {
403+
// Enter true deep sleep. When we wake up we'll be back at the
404+
// top of main(), not in this loop.
405+
alarm_enter_deep_sleep();
406+
// Does not return.
407+
} else {
408+
serial_write_compressed(translate("Pretending to deep sleep until alarm, any key or file write.\n"));
409+
}
410+
}
411+
}
412+
#endif
413+
414+
if (!asleep) {
415+
tick_rgb_status_animation(&animation);
416+
} else {
417+
port_idle_until_interrupt();
418+
}
369419
}
370420
}
371421

ports/atmel-samd/boards/8086_commander/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828

2929
void board_init(void) {
3030
}

ports/atmel-samd/boards/aloriumtech_evo_m51/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// Author: Bryan Craker
2929
// Date: 2020-05-20
3030

31-
#include "boards/board.h"
31+
#include "supervisor/board.h"
3232
#include "mpconfigboard.h"
3333

3434
void board_init(void) {

ports/atmel-samd/boards/arduino_mkr1300/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "mpconfigboard.h"
2929
#include "hal/include/hal_gpio.h"
3030

ports/atmel-samd/boards/arduino_mkrzero/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "mpconfigboard.h"
2929
#include "hal/include/hal_gpio.h"
3030

ports/atmel-samd/boards/arduino_nano_33_iot/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "mpconfigboard.h"
2929
#include "hal/include/hal_gpio.h"
3030

ports/atmel-samd/boards/arduino_zero/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "mpconfigboard.h"
2929
#include "hal/include/hal_gpio.h"
3030

ports/atmel-samd/boards/bast_pro_mini_m0/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828

2929
void board_init(void)
3030
{

ports/atmel-samd/boards/bdmicro_vina_d21/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "mpconfigboard.h"
2929

3030
void board_init(void)

ports/atmel-samd/boards/bdmicro_vina_d51/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "mpconfigboard.h"
2929

3030
void board_init(void)

ports/atmel-samd/boards/blm_badge/board.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "supervisor/shared/board.h"
2929

3030
void board_init(void) {
@@ -35,5 +35,5 @@ bool board_requests_safe_mode(void) {
3535
}
3636

3737
void reset_board(void) {
38-
board_reset_user_neopixels();
38+
board_reset_user_neopixels(&pin_PA05, 10);
3939
}

ports/atmel-samd/boards/board.h

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

ports/atmel-samd/boards/capablerobot_usbhub/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828
#include "mpconfigboard.h"
2929
#include "hal/include/hal_gpio.h"
3030
#include "common-hal/microcontroller/Pin.h"

ports/atmel-samd/boards/catwan_usbstick/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "boards/board.h"
27+
#include "supervisor/board.h"
2828

2929
void board_init(void)
3030
{

ports/atmel-samd/boards/circuitbrains_basic_m0/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#include "boards/board.h"
28+
#include "supervisor/board.h"
2929
#include "mpconfigboard.h"
3030
#include "hal/include/hal_gpio.h"
3131

ports/atmel-samd/boards/circuitbrains_deluxe_m4/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
#include "boards/board.h"
28+
#include "supervisor/board.h"
2929
#include "mpconfigboard.h"
3030
#include "hal/include/hal_gpio.h"
3131

0 commit comments

Comments
 (0)