stm32/usb: Add support for using TinyUSB stack.

This commit adapts the stm32 port to allow switching from STM USB stack to
TinyUSB stack.

Using TinyUSB improves consistancy with other MicroPython ports and brings
in the ability to use the runtime USB definition support recently added to
other TinyUSB based ports.

By default the existing STM USB stack is used.  TinyUSB can be enabled in a
board configuration with:

    #define MICROPY_HW_TINYUSB_STACK (1)

Or, it can be enabled from the command line with:

    make -C ports/stm32 CFLAGS_EXTRA='-DMICROPY_HW_TINYUSB_STACK=1'

Signed-off-by: Andrew Leech <andrew@alelec.net>
This commit is contained in:
Andrew Leech
2025-10-28 16:20:31 +11:00
committed by Damien George
parent 6b0e1c2701
commit 9bb266e311
14 changed files with 256 additions and 40 deletions

View File

@@ -88,6 +88,11 @@
#include "pyb_can.h"
#include "subghz.h"
#if MICROPY_HW_TINYUSB_STACK
#include "usbd_conf.h"
#include "shared/tinyusb/mp_usbd.h"
#endif
#if MICROPY_PY_THREAD
static pyb_thread_t pyb_thread_main;
#endif
@@ -275,14 +280,12 @@ static bool init_sdcard_fs(void) {
}
}
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_ENABLE_USB && !MICROPY_HW_TINYUSB_STACK
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
// if no USB MSC medium is selected then use the SD card
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_SDCARD;
}
#endif
#if MICROPY_HW_ENABLE_USB
// only use SD card as current directory if that's what the USB medium is
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_SDCARD)
#endif
@@ -606,8 +609,13 @@ soft_reset:
#endif
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_TINYUSB_STACK
pyb_usbd_init();
mp_usbd_init();
#else
pyb_usb_init0();
#endif
#endif
#if MICROPY_PY_MACHINE_I2S
machine_i2s_init0();
@@ -631,7 +639,7 @@ soft_reset:
}
#endif
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
// if the SD card isn't used as the USB MSC medium then use the internal flash
if (pyb_usb_storage_medium == PYB_USB_STORAGE_MEDIUM_NONE) {
pyb_usb_storage_medium = PYB_USB_STORAGE_MEDIUM_FLASH;
@@ -665,7 +673,7 @@ soft_reset:
// or whose initialisation can be safely deferred until after running
// boot.py.
#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_STM_USB_STACK
// init USB device to default setting if it was not already configured
if (!(pyb_usb_flags & PYB_USB_FLAG_USB_MODE_CALLED)) {
#if MICROPY_HW_USB_MSC
@@ -770,6 +778,9 @@ soft_reset_exit:
#else
MP_STATE_PORT(pyb_stdio_uart) = NULL;
#endif
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE && MICROPY_HW_TINYUSB_STACK
mp_usbd_deinit();
#endif
MICROPY_BOARD_END_SOFT_RESET(&state);