|
28 | 28 | // extmod/modmachine.c via MICROPY_PY_MACHINE_INCLUDEFILE.
|
29 | 29 |
|
30 | 30 | #include "se_services.h"
|
| 31 | +#include "tusb.h" |
| 32 | + |
| 33 | +extern void dcd_uninit(void); |
31 | 34 |
|
32 | 35 | #define MICROPY_PY_MACHINE_EXTRA_GLOBALS \
|
33 | 36 | { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, \
|
@@ -70,15 +73,56 @@ static void mp_machine_set_freq(size_t n_args, const mp_obj_t *args) {
|
70 | 73 | mp_raise_NotImplementedError(NULL);
|
71 | 74 | }
|
72 | 75 |
|
73 |
| -static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { |
74 |
| - mp_int_t delay = -1; |
75 |
| - if (n_args == 1) { |
76 |
| - delay = mp_obj_get_int(args[0]); |
| 76 | +#if MICROPY_HW_ENABLE_USBDEV |
| 77 | +static void mp_machine_enable_usb(bool enable) { |
| 78 | + if (enable) { |
| 79 | + // Initialize TinyUSB and DCD. |
| 80 | + tusb_init(); |
| 81 | + } else { |
| 82 | + // Disconnect USB device. |
| 83 | + tud_disconnect(); |
| 84 | + // Deinitialize TinyUSB. |
| 85 | + tud_deinit(TUD_OPT_RHPORT); |
| 86 | + // Deinitialize DCD (disables IRQs). |
| 87 | + dcd_uninit(); |
77 | 88 | }
|
78 |
| - mp_hal_delay_ms(delay); |
| 89 | +} |
| 90 | +#endif |
| 91 | + |
| 92 | +static void mp_machine_lightsleep(size_t n_args, const mp_obj_t *args) { |
| 93 | + #if MICROPY_HW_ENABLE_USBDEV |
| 94 | + mp_machine_enable_usb(false); |
| 95 | + #endif |
| 96 | + |
| 97 | + #ifdef MICROPY_BOARD_ENTER_STANDBY |
| 98 | + MICROPY_BOARD_ENTER_STANDBY(); |
| 99 | + #endif |
| 100 | + |
| 101 | + // This enters the deepest possible CPU sleep state, without |
| 102 | + // losing CPU state. CPU and subsystem power will remain on. |
| 103 | + pm_core_enter_deep_sleep(); |
| 104 | + |
| 105 | + #ifdef MICROPY_BOARD_EXIT_STANDBY |
| 106 | + MICROPY_BOARD_EXIT_STANDBY(); |
| 107 | + #endif |
| 108 | + |
| 109 | + #if MICROPY_HW_ENABLE_USBDEV |
| 110 | + mp_machine_enable_usb(true); |
| 111 | + #endif |
79 | 112 | }
|
80 | 113 |
|
81 | 114 | NORETURN static void mp_machine_deepsleep(size_t n_args, const mp_obj_t *args) {
|
82 |
| - mp_machine_lightsleep(n_args, args); |
| 115 | + #if MICROPY_HW_ENABLE_USBDEV |
| 116 | + mp_machine_enable_usb(false); |
| 117 | + #endif |
| 118 | + |
| 119 | + #ifdef MICROPY_BOARD_ENTER_STOP |
| 120 | + MICROPY_BOARD_ENTER_STOP(); |
| 121 | + #endif |
| 122 | + |
| 123 | + // If power is removed from the subsystem, the function does |
| 124 | + // not return, and the CPU will reboot when/if the subsystem |
| 125 | + // is next powered up. |
| 126 | + pm_core_enter_deep_sleep_request_subsys_off(); |
83 | 127 | mp_machine_reset();
|
84 | 128 | }
|
0 commit comments