Skip to content

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.
⚠️ Please upload report for BASE (master@255d74b). Learn more about missing BASE report.
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #15592   +/-   ##
=========================================
  Coverage          ?   98.38%           
=========================================
  Files             ?      171           
  Lines             ?    22283           
  Branches          ?        0           
=========================================
  Hits              ?    21924           
  Misses            ?      359           
  Partials          ?        0           

☔ 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:   -60 -0.015% 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.

@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
@andrewleech andrewleech force-pushed the stm32_tinyusb branch 2 times, most recently from 162d4c7 to 6aef189 Compare May 27, 2025 05:57
@dpgeorge dpgeorge added this to the release-1.26.0 milestone Jun 3, 2025
@andrewleech andrewleech force-pushed the stm32_tinyusb branch 2 times, most recently from a3d0313 to 6f475eb Compare July 22, 2025 06:57
@dpgeorge
Copy link
Member

Code size change for stm32 is still very high....

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@andrewleech
Copy link
Contributor Author

I believe the size issue (when building with stm usb stack) has been resolved finally. Though I want to check exactly how it's now smaller...

On a positive note, switching to TinyUSB actually reduces the size by ~5.4KB.

@dpgeorge
Copy link
Member

dpgeorge commented Aug 7, 2025

I believe the size issue (when building with stm usb stack) has been resolved finally. Though I want to check exactly how it's now smaller...

Yes, that's a bit concerning that it's smaller. Probably something has been unintentionally removed/disabled.

Signed-off-by: Andrew Leech <andrew@alelec.net>
@hoihu
Copy link
Contributor

hoihu commented Aug 7, 2025

nice work, heads up from my side. would be cool to use all the „dynamic python-USB configuration“ staff in the stm32 port!

@andrewleech
Copy link
Contributor Author

I believe the size issue (when building with stm usb stack) has been resolved finally. Though I want to check exactly how it's now smaller...

Yes, that's a bit concerning that it's smaller. Probably something has been unintentionally removed/disabled.

Whoops, I sliced and diced the feature commit every which way trying to find the size difference befrore remember the other commit at the base of this branch: runtime/pyexec: Remove legacy usb irq enable code.

That's the drop in size.

@dpgeorge
Copy link
Member

I tried to test this on PYBD_SF6, PYBV10 and NUCLEO_WB55 but it did not work:

  • PYBD_SF6 uses both FS and HS ports, and the main one is the HS (secondary port), and from what I can tell this isn't supported by TinyUSB (or at least our configuration). So I gave up on this board.
  • PYBV10 I expected to work because it's simple and just uses FS. But enabling TinyUSB, nothing happens, no USB device appears.
  • NUCLEO_WB55 I also expected to work, but similar to PYBV10 no USB device appears. The UART REPL is still there and working, so the board starts up, but no native USB.

To enable TinyUSB I changed mpconfigboard_common.h:

--- a/ports/stm32/mpconfigboard_common.h
+++ b/ports/stm32/mpconfigboard_common.h
@@ -142,7 +142,7 @@
 
 // Select whether TinyUSB or legacy STM stack is used to provide USB.
 #ifndef MICROPY_HW_TINYUSB_STACK
-#define MICROPY_HW_TINYUSB_STACK (0)
+#define MICROPY_HW_TINYUSB_STACK (1)
 #endif

and it's definitely using that config option.

Did I do something wrong?

@@ -136,6 +136,24 @@
#define MICROPY_HW_ENABLE_USB (0)
#endif

#if MICROPY_HW_ENABLE_USB
#define MICROPY_HW_USB_CDC (1)
#define MICROPY_HW_USB_FS (1)
Copy link
Member

Choose a reason for hiding this comment

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

These options cannot be enabled unconditionally, because they may clash with board configurations (when using the STM USB stack).

Either just remove these lines (my preference, if that works) or at least only enable them if MICROPY_HW_TINYUSB_STACK is enabled.

@@ -256,6 +274,8 @@
// Windows needs a different PID to distinguish different device configurations.
#ifndef MICROPY_HW_USB_VID
#define MICROPY_HW_USB_VID (0xf055)
#define MICROPY_HW_USB_PID (0x9802)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment that this is only used in the TinyUSB configuration (and eventually we'll need to change this value depending on whether MSC is enabled etc).

@andrewleech
Copy link
Contributor Author

Thanks for the feedback @dpgeorge I'll address your specific review comments and test / fix it on the nucleo and pybd - I'll let you know when it's ready to test again

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.

4 participants