summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2024-01-18 08:52:16 +1100
committerDamien George <damien@micropython.org>2024-02-15 12:31:26 +1100
commit00ba6aaae4c815fc9685defc37a5df1424180c0e (patch)
tree4743d1da4a9ee2f076f70405a6403ed755a5b14e
parent5d83bbca60ea3f4071b9245daf8a41296072f918 (diff)
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 <angus@redyak.com.au>
-rw-r--r--ports/mimxrt/main.c6
-rw-r--r--ports/nrf/drivers/usb/usb_cdc.c4
-rw-r--r--ports/nrf/main.c6
-rw-r--r--ports/renesas-ra/main.c18
-rw-r--r--ports/rp2/main.c12
-rw-r--r--ports/samd/main.c4
-rw-r--r--ports/samd/samd_soc.c2
-rw-r--r--shared/tinyusb/mp_usbd.h7
8 files changed, 38 insertions, 21 deletions
diff --git a/ports/mimxrt/main.c b/ports/mimxrt/main.c
index 376d72f19..761c49174 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 3ca2acbf3..aef735497 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 9809ba0e2..dd9f232b8 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 761420423..5925606f9 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 e63b8c03f..70a67066f 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 74eb5e328..f051e961f 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 259640e93..5e6c5c4fc 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 83a8f8617..89f8bf0ee 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);