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>
This commit is contained in:
Damien George
2025-11-27 15:00:32 +11:00
parent f43810b1cd
commit 63c94fe73e
5 changed files with 15 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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