Skip to content

Commit

Permalink
ports: On cold boot, enable USB after boot.py completes.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
projectgus authored and dpgeorge committed Feb 15, 2024
1 parent 5d83bbc commit 00ba6aa
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 21 deletions.
6 changes: 4 additions & 2 deletions ports/mimxrt/main.c
Expand Up @@ -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"
Expand Down Expand Up @@ -63,7 +63,6 @@ void board_init(void);
int main(void) {
board_init();
ticks_init();
tusb_init();
pendsv_init();

#if MICROPY_PY_LWIP
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/drivers/usb/usb_cdc.c
Expand Up @@ -29,14 +29,14 @@

#if MICROPY_HW_USB_CDC

#include "tusb.h"
#include "nrfx.h"
#include "nrfx_power.h"
#include "nrfx_uart.h"
#include "py/ringbuf.h"
#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"
Expand Down Expand Up @@ -186,7 +186,7 @@ int usb_cdc_init(void)
tx_ringbuf.iget = 0;
tx_ringbuf.iput = 0;

tusb_init();
mp_usbd_init();

return 0;
}
Expand Down
6 changes: 4 additions & 2 deletions ports/nrf/main.c
Expand Up @@ -261,13 +261,15 @@ void NORETURN _start(void) {

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");
}
Expand Down
18 changes: 10 additions & 8 deletions ports/renesas-ra/main.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -366,14 +362,20 @@ int main(void) {
#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).
Expand Down
12 changes: 7 additions & 5 deletions ports/rp2/main.c
Expand Up @@ -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"
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions ports/samd/main.c
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions ports/samd/samd_soc.c
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions shared/tinyusb/mp_usbd.h
Expand Up @@ -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);
Expand Down

0 comments on commit 00ba6aa

Please sign in to comment.