From 00ba6aaae4c815fc9685defc37a5df1424180c0e Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Thu, 18 Jan 2024 08:52:16 +1100 Subject: [PATCH] ports: On cold boot, enable USB after boot.py completes. For mimxrt, nrf, renesas-ra, rp2 and samd ports, this commit implements similar behaviour to the stm32 port, where USB is only brought up after boot.py completes execution. Currently this doesn't add any useful functionality (and may break workflows that depend on USB-CDC being live in boot.py), however it's a precondition for more usable workflows with USB devices defined in Python (allows setting up USB interfaces in boot.py before the device enumerates for the first time). This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton --- ports/mimxrt/main.c | 6 ++++-- ports/nrf/drivers/usb/usb_cdc.c | 4 ++-- ports/nrf/main.c | 6 ++++-- ports/renesas-ra/main.c | 18 ++++++++++-------- ports/rp2/main.c | 12 +++++++----- ports/samd/main.c | 4 ++++ ports/samd/samd_soc.c | 2 -- shared/tinyusb/mp_usbd.h | 7 +++++++ 8 files changed, 38 insertions(+), 21 deletions(-) diff --git a/ports/mimxrt/main.c b/ports/mimxrt/main.c index 376d72f197..761c491742 100644 --- a/ports/mimxrt/main.c +++ b/ports/mimxrt/main.c @@ -34,8 +34,8 @@ #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" #include "shared/runtime/softtimer.h" +#include "shared/tinyusb/mp_usbd.h" #include "ticks.h" -#include "tusb.h" #include "led.h" #include "pendsv.h" #include "modmachine.h" @@ -63,7 +63,6 @@ void board_init(void); int main(void) { board_init(); ticks_init(); - tusb_init(); pendsv_init(); #if MICROPY_PY_LWIP @@ -115,6 +114,9 @@ int main(void) { // Execute user scripts. int ret = pyexec_file_if_exists("boot.py"); + + mp_usbd_init(); + if (ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; } diff --git a/ports/nrf/drivers/usb/usb_cdc.c b/ports/nrf/drivers/usb/usb_cdc.c index 3ca2acbf3d..aef7354972 100644 --- a/ports/nrf/drivers/usb/usb_cdc.c +++ b/ports/nrf/drivers/usb/usb_cdc.c @@ -29,7 +29,6 @@ #if MICROPY_HW_USB_CDC -#include "tusb.h" #include "nrfx.h" #include "nrfx_power.h" #include "nrfx_uart.h" @@ -37,6 +36,7 @@ #include "py/stream.h" #include "py/runtime.h" #include "shared/runtime/interrupt_char.h" +#include "shared/tinyusb/mp_usbd.h" #ifdef BLUETOOTH_SD #include "nrf_sdm.h" @@ -186,7 +186,7 @@ int usb_cdc_init(void) tx_ringbuf.iget = 0; tx_ringbuf.iput = 0; - tusb_init(); + mp_usbd_init(); return 0; } diff --git a/ports/nrf/main.c b/ports/nrf/main.c index 9809ba0e23..dd9f232b80 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -261,13 +261,15 @@ soft_reset: led_state(1, 0); + #if MICROPY_VFS || MICROPY_MBFS || MICROPY_MODULE_FROZEN + ret = pyexec_file_if_exists("boot.py"); + #endif + #if MICROPY_HW_USB_CDC usb_cdc_init(); #endif #if MICROPY_VFS || MICROPY_MBFS || MICROPY_MODULE_FROZEN - // run boot.py and main.py if they exist. - ret = pyexec_file_if_exists("boot.py"); if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL && ret != 0) { pyexec_file_if_exists("main.py"); } diff --git a/ports/renesas-ra/main.c b/ports/renesas-ra/main.c index 761420423e..5925606f9c 100644 --- a/ports/renesas-ra/main.c +++ b/ports/renesas-ra/main.c @@ -36,6 +36,7 @@ #include "shared/readline/readline.h" #include "shared/runtime/pyexec.h" #include "shared/runtime/softtimer.h" +#include "shared/tinyusb/mp_usbd.h" #include "lib/oofatfs/ff.h" #include "lib/littlefs/lfs1.h" #include "lib/littlefs/lfs1_util.h" @@ -63,7 +64,6 @@ #include "usrsw.h" #include "rtc.h" #include "storage.h" -#include "tusb.h" #if MICROPY_PY_LWIP #include "lwip/init.h" #include "lwip/apps/mdns.h" @@ -270,10 +270,6 @@ int main(void) { state.reset_mode = 1; state.log_soft_reset = false; - #if MICROPY_HW_ENABLE_USBDEV - tusb_init(); - #endif - #if MICROPY_PY_BLUETOOTH mp_bluetooth_hci_init(); #endif @@ -366,14 +362,20 @@ soft_reset: #endif // Run boot.py (or whatever else a board configures at this stage). - if (MICROPY_BOARD_RUN_BOOT_PY(&state) == BOARDCTRL_GOTO_SOFT_RESET_EXIT) { - goto soft_reset_exit; - } + int boot_res = MICROPY_BOARD_RUN_BOOT_PY(&state); // Now we initialise sub-systems that need configuration from boot.py, // or whose initialisation can be safely deferred until after running // boot.py. + #if MICROPY_HW_ENABLE_USBDEV + mp_usbd_init(); + #endif + + if (boot_res == BOARDCTRL_GOTO_SOFT_RESET_EXIT) { + goto soft_reset_exit; + } + // At this point everything is fully configured and initialised. // Run main.py (or whatever else a board configures at this stage). diff --git a/ports/rp2/main.c b/ports/rp2/main.c index e63b8c03f0..70a67066fc 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -38,7 +38,7 @@ #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" #include "shared/runtime/softtimer.h" -#include "tusb.h" +#include "shared/tinyusb/mp_usbd.h" #include "uart.h" #include "modmachine.h" #include "modrp2.h" @@ -86,12 +86,9 @@ int main(int argc, char **argv) { #endif #endif - #if MICROPY_HW_ENABLE_USBDEV - #if MICROPY_HW_USB_CDC + #if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC bi_decl(bi_program_feature("USB REPL")) #endif - tusb_init(); - #endif #if MICROPY_PY_THREAD bi_decl(bi_program_feature("thread support")) @@ -181,6 +178,11 @@ int main(int argc, char **argv) { // Execute user scripts. int ret = pyexec_file_if_exists("boot.py"); + + #if MICROPY_HW_ENABLE_USBDEV + mp_usbd_init(); + #endif + if (ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; } diff --git a/ports/samd/main.c b/ports/samd/main.c index 74eb5e3285..f051e961ff 100644 --- a/ports/samd/main.c +++ b/ports/samd/main.c @@ -33,6 +33,7 @@ #include "shared/runtime/gchelper.h" #include "shared/runtime/pyexec.h" #include "shared/runtime/softtimer.h" +#include "shared/tinyusb/mp_usbd.h" extern uint8_t _sstack, _estack, _sheap, _eheap; extern void adc_deinit_all(void); @@ -56,6 +57,9 @@ void samd_main(void) { // Execute user scripts. int ret = pyexec_file_if_exists("boot.py"); + + mp_usbd_init(); + if (ret & PYEXEC_FORCED_EXIT) { goto soft_reset_exit; } diff --git a/ports/samd/samd_soc.c b/ports/samd/samd_soc.c index 259640e93d..5e6c5c4fc2 100644 --- a/ports/samd/samd_soc.c +++ b/ports/samd/samd_soc.c @@ -62,8 +62,6 @@ static void usb_init(void) { PORT->Group[0].PMUX[12].reg = alt << 4 | alt; PORT->Group[0].PINCFG[24].reg = PORT_PINCFG_PMUXEN; PORT->Group[0].PINCFG[25].reg = PORT_PINCFG_PMUXEN; - - tusb_init(); } // Initialize the µs counter on TC 0/1 or TC4/5 diff --git a/shared/tinyusb/mp_usbd.h b/shared/tinyusb/mp_usbd.h index 83a8f8617c..89f8bf0ee9 100644 --- a/shared/tinyusb/mp_usbd.h +++ b/shared/tinyusb/mp_usbd.h @@ -28,6 +28,13 @@ #define MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_H #include "py/obj.h" +#include "tusb.h" + +static inline void mp_usbd_init(void) { + // Currently this is a thin wrapper around tusb_init(), however + // runtime USB support will require this to be extended. + tusb_init(); +} // Call this to explicitly run the TinyUSB device task. void mp_usbd_task(void);