summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2025-11-27 15:00:32 +1100
committerDamien George <damien@micropython.org>2025-11-29 23:35:20 +1100
commit63c94fe73ed5c52d4d7ccbf4ddcc2a1e3178d21b (patch)
tree6539eae8f252f7318e0c0111423c9a3a39e365bc
parentf43810b1cd4142474a99df4e08d51a97eb9af00a (diff)
stm32: Fix init sequence of USB hardware and TinyUSB stack.
This commit fixes the initialization sequence for TinyUSB when enabled on the stm32 port: - Following other ports, `mp_usbd_init()` should be called just after running `boot.py`, to give the user a chance to configure USB. - Hardware initialization (via `pyb_usbd_init()`) should only occur once, the first time TinyUSB is started up. This is achieved by adding a hook to the shared TinyUSB bindings to call `pyb_usbd_init()`, and only do the hardware init if TinyUSB was not already initialized. Also, `pyb_usbd_init()` is renamed `mp_usbd_ll_init()` to make it match with the rest of the stared TinyUSB binding code. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/stm32/main.c11
-rw-r--r--ports/stm32/mpconfigboard_common.h1
-rw-r--r--ports/stm32/mphalport.h1
-rw-r--r--ports/stm32/usbd_conf.c8
-rw-r--r--ports/stm32/usbd_conf.h2
5 files changed, 15 insertions, 8 deletions
diff --git a/ports/stm32/main.c b/ports/stm32/main.c
index 6f7413694..8085a5e25 100644
--- a/ports/stm32/main.c
+++ b/ports/stm32/main.c
@@ -611,14 +611,9 @@ soft_reset:
pyb_can_init0();
#endif
- #if MICROPY_HW_ENABLE_USB
- #if MICROPY_HW_TINYUSB_STACK
- pyb_usbd_init();
- mp_usbd_init();
- #else
+ #if MICROPY_HW_STM_USB_STACK && MICROPY_HW_ENABLE_USB
pyb_usb_init0();
#endif
- #endif
#if MICROPY_PY_MACHINE_I2S
machine_i2s_init0();
@@ -690,6 +685,10 @@ soft_reset:
}
#endif
+ #if MICROPY_HW_TINYUSB_STACK && MICROPY_HW_ENABLE_USBDEV
+ mp_usbd_init();
+ #endif
+
#if MICROPY_HW_HAS_MMA7660
// MMA accel: init and reset
accel_init();
diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h
index 1ce42055d..e21f474d7 100644
--- a/ports/stm32/mpconfigboard_common.h
+++ b/ports/stm32/mpconfigboard_common.h
@@ -261,6 +261,7 @@
#if MICROPY_HW_TINYUSB_STACK
#ifndef MICROPY_HW_ENABLE_USBDEV
#define MICROPY_HW_ENABLE_USBDEV (1)
+#define MICROPY_HW_TINYUSB_LL_INIT mp_usbd_ll_init
#endif
#ifndef MICROPY_HW_USB_CDC
diff --git a/ports/stm32/mphalport.h b/ports/stm32/mphalport.h
index 098a848fb..50aa45697 100644
--- a/ports/stm32/mphalport.h
+++ b/ports/stm32/mphalport.h
@@ -1,6 +1,7 @@
// We use the ST Cube HAL library for most hardware peripherals
#include STM32_HAL_H
#include "pin.h"
+#include "usbd_conf.h"
#include "py/ringbuf.h"
#include "shared/runtime/interrupt_char.h"
diff --git a/ports/stm32/usbd_conf.c b/ports/stm32/usbd_conf.c
index be56b2202..e5ac9311d 100644
--- a/ports/stm32/usbd_conf.c
+++ b/ports/stm32/usbd_conf.c
@@ -32,6 +32,7 @@
#include "usbd_core.h"
#include "py/obj.h"
#include "py/mphal.h"
+#include "shared/tinyusb/mp_usbd.h"
#include "irq.h"
#include "usb.h"
@@ -334,7 +335,12 @@ static void mp_usbd_ll_init_hs(void) {
#if MICROPY_HW_TINYUSB_STACK
-void pyb_usbd_init(void) {
+void mp_usbd_ll_init(void) {
+ // Only initialize the USB hardware once.
+ if (tusb_inited()) {
+ return;
+ }
+
#if MICROPY_HW_USB_FS
mp_usbd_ll_init_fs();
#endif
diff --git a/ports/stm32/usbd_conf.h b/ports/stm32/usbd_conf.h
index cb0457982..5829d6870 100644
--- a/ports/stm32/usbd_conf.h
+++ b/ports/stm32/usbd_conf.h
@@ -65,7 +65,7 @@
#define USBD_HS_NUM_FIFO (1 + USBD_HS_NUM_TX_FIFO)
#if MICROPY_HW_TINYUSB_STACK
-void pyb_usbd_init(void);
+void mp_usbd_ll_init(void);
#endif
#endif // MICROPY_INCLUDED_STM32_USBD_CONF_H