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>
This commit is contained in:
Angus Gratton 2024-01-18 08:52:16 +11:00 committed by Damien George
parent 5d83bbca60
commit 00ba6aaae4
8 changed files with 38 additions and 21 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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");
}

View File

@ -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).

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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

View File

@ -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);