Skip to content

stm32/usb: Add support for using TinyUSB stack. #15592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Contributor

@andrewleech andrewleech commented Aug 3, 2024

Summary

This PR 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.

TinyUSB can be enabled in a build with #define MICROPY_HW_TINYUSB_STACK (1)
It's also enabled by default with MICROPY_PREVIEW_VERSION_2

From the command line it can be enabled with:

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

Testing

Development and testing so far has been on the STM32WB55 and STM32F765

I've been more recently using and testing this in conjunction with shared/tinyusb: Add support for USB Network (NCM) interface. #16459 on the STM32F765 where the builtin CDC and NCM endpoints are both working very well.

I haven't yet done significant testing of dynamic / runtime usb devices.

Trade-offs and Alternatives

Currently the TinyUSB integration matches other ports for features, so CDC is enabled for repl use and machine.USBDevice support is available by default.

This means however the existing stm32 / pyb usb support has been removed, including pyb.USB_VCP and pyb.usb_mode.

Features like mass storage, hid and a second vcp could in theory be added via machine.USBDevice however this hasn't been tested and is a breaking change for the stm32 port.

TODO: test lightsleep behaviour / compared with #8304 (comment)

Copy link

codecov bot commented Aug 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.54%. Comparing base (2dada06) to head (6aef189).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #15592   +/-   ##
=======================================
  Coverage   98.54%   98.54%           
=======================================
  Files         169      169           
  Lines       21898    21898           
=======================================
  Hits        21579    21579           
  Misses        319      319           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

github-actions bot commented Aug 3, 2024

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:  +344 +0.088% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@andrewleech andrewleech force-pushed the stm32_tinyusb branch 2 times, most recently from 1b2bbff to a8e3d9d Compare August 3, 2024 13:29
@dpgeorge
Copy link
Member

dpgeorge commented Aug 5, 2024

For now I (intend to) maintain back-compatibility with the MICROPY_HW_ENABLE_STM_USB flag to revert to the stm stack instead of tinyusb.

Yes, please do allow support for either the STM or TinyUSB stack.

@@ -583,7 +583,7 @@ int pyexec_friendly_repl(void) {
for (;;) {
input_restart:

#if MICROPY_HW_ENABLE_USB
#if MICROPY_HW_ENABLE_USB && 0
if (usb_vcp_is_enabled()) {
// If the user gets to here and interrupts are disabled then
// they'll never see the prompt, traceback etc. The USB REPL needs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dpgeorge This block in the shared pyexec code appears to be stm32 specific! Other ports use the MICROPY_HW_ENABLE_USBDEV define and don't define usb_vcp_is_enabled etc.
This little feature to re-enable any disabled interrupts once a device hits repl does sound convenient, would it be worth extending this to all ports? Or does this feel like something users haven't needed and should be removed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon we can just remove this. It's pretty hacky and doesn't generalise well to other ports.

@andrewleech andrewleech force-pushed the stm32_tinyusb branch 3 times, most recently from 1964763 to 4c7f096 Compare August 13, 2024 00:24
@andrewleech andrewleech force-pushed the stm32_tinyusb branch 2 times, most recently from 3864708 to 0f058f7 Compare May 15, 2025 11:29
@andrewleech andrewleech changed the title Draft: stm32/usb: Add support for using TinyUSB stack by default. stm32/usb: Add support for using TinyUSB stack. May 22, 2025
@dpgeorge dpgeorge added this to the release-1.26.0 milestone Jun 3, 2025
}


void pyb_usbd_init(void) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed for stm hal, adds code size.

* @retval None
*/
void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) {
void pyb_usbd_init_fs(void) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make these static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants