|
57 | 57 | #include "supervisor/shared/status_leds.h" |
58 | 58 | #include "supervisor/shared/stack.h" |
59 | 59 | #include "supervisor/serial.h" |
| 60 | +#include "supervisor/usb.h" |
| 61 | + |
| 62 | +#include "shared-bindings/microcontroller/__init__.h" |
60 | 63 |
|
61 | 64 | #include "boards/board.h" |
62 | 65 |
|
|
85 | 88 | #include "common-hal/canio/CAN.h" |
86 | 89 | #endif |
87 | 90 |
|
| 91 | +#if CIRCUITPY_SLEEP |
| 92 | +#include "shared-bindings/sleep/__init__.h" |
| 93 | +#endif |
| 94 | + |
88 | 95 | void do_str(const char *src, mp_parse_input_kind_t input_kind) { |
89 | 96 | mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); |
90 | 97 | if (lex == NULL) { |
@@ -312,6 +319,14 @@ bool run_code_py(safe_mode_t safe_mode) { |
312 | 319 | bool refreshed_epaper_display = false; |
313 | 320 | #endif |
314 | 321 | rgb_status_animation_t animation; |
| 322 | + bool ok = result->return_code != PYEXEC_EXCEPTION; |
| 323 | + #if CIRCUITPY_SLEEP |
| 324 | + // If USB isn't enumerated then deep sleep. |
| 325 | + if (ok && !supervisor_workflow_active() && supervisor_ticks_ms64() > CIRCUITPY_USB_ENUMERATION_DELAY * 1024) { |
| 326 | + common_hal_sleep_deep_sleep(); |
| 327 | + } |
| 328 | + #endif |
| 329 | + // Show the animation every N seconds. |
315 | 330 | prep_rgb_status_animation(&result, found_main, safe_mode, &animation); |
316 | 331 | while (true) { |
317 | 332 | RUN_BACKGROUND_TASKS; |
@@ -344,8 +359,24 @@ bool run_code_py(safe_mode_t safe_mode) { |
344 | 359 | refreshed_epaper_display = maybe_refresh_epaperdisplay(); |
345 | 360 | } |
346 | 361 | #endif |
347 | | - |
348 | | - tick_rgb_status_animation(&animation); |
| 362 | + bool animation_done = tick_rgb_status_animation(&animation); |
| 363 | + if (animation_done && supervisor_workflow_active()) { |
| 364 | + #if CIRCUITPY_SLEEP |
| 365 | + int64_t remaining_enumeration_wait = CIRCUITPY_USB_ENUMERATION_DELAY * 1024 - supervisor_ticks_ms64(); |
| 366 | + // If USB isn't enumerated then deep sleep after our waiting period. |
| 367 | + if (ok && remaining_enumeration_wait < 0) { |
| 368 | + common_hal_sleep_deep_sleep(); |
| 369 | + return; // Doesn't actually get here. |
| 370 | + } |
| 371 | + #endif |
| 372 | + // Wake up every so often to flash the error code. |
| 373 | + if (!ok) { |
| 374 | + port_interrupt_after_ticks(CIRCUITPY_FLASH_ERROR_PERIOD * 1024); |
| 375 | + } else { |
| 376 | + port_interrupt_after_ticks(remaining_enumeration_wait); |
| 377 | + } |
| 378 | + port_sleep_until_interrupt(); |
| 379 | + } |
349 | 380 | } |
350 | 381 | } |
351 | 382 |
|
@@ -392,7 +423,9 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) { |
392 | 423 | if (!skip_boot_output) { |
393 | 424 | // Wait 1.5 seconds before opening CIRCUITPY_BOOT_OUTPUT_FILE for write, |
394 | 425 | // in case power is momentary or will fail shortly due to, say a low, battery. |
395 | | - mp_hal_delay_ms(1500); |
| 426 | + if (common_hal_sleep_get_reset_reason() == RESET_REASON_POWER_VALID) { |
| 427 | + mp_hal_delay_ms(1500); |
| 428 | + } |
396 | 429 |
|
397 | 430 | // USB isn't up, so we can write the file. |
398 | 431 | filesystem_set_internal_writable_by_usb(false); |
@@ -511,7 +544,7 @@ int __attribute__((used)) main(void) { |
511 | 544 | } |
512 | 545 | if (exit_code == PYEXEC_FORCED_EXIT) { |
513 | 546 | if (!first_run) { |
514 | | - serial_write_compressed(translate("soft reboot\n")); |
| 547 | + serial_write_compressed(translate("\n\n------ soft reboot ------\n")); |
515 | 548 | } |
516 | 549 | first_run = false; |
517 | 550 | skip_repl = run_code_py(safe_mode); |
|
0 commit comments