Skip to content

Commit dfa7e59

Browse files
committed
docs/library: Update machine.USBDevice documentation.
- Add stm32 (with TinyUSB) to list of supported ports - Add example showing how to query current USB class configuration using bitwise AND and equality operations - Update PR_DRAFT.md with combined class examples and query patterns Demonstrates checking individual class enablement (& operator) and exact configuration matching (== operator) for USB builtin_driver flags. Signed-off-by: Andrew Leech <[email protected]>
1 parent 9250bb5 commit dfa7e59

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

PR_DRAFT.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ Adds USB device class selection via `machine.USBDevice` API when using TinyUSB.
88
import machine
99

1010
usb = machine.USBDevice()
11-
usb.builtin_driver = usb.BUILTIN_CDC # Only CDC, no MSC
11+
usb.builtin_driver = usb.BUILTIN_CDC # Only CDC
12+
usb.builtin_driver = usb.BUILTIN_CDC | usb.BUILTIN_MSC # Both CDC and MSC
1213
usb.active(True)
14+
15+
# Query current configuration
16+
if usb.builtin_driver & usb.BUILTIN_CDC:
17+
print("CDC enabled")
18+
if usb.builtin_driver == (usb.BUILTIN_CDC | usb.BUILTIN_MSC):
19+
print("Both CDC and MSC enabled")
1320
```
1421

1522
Works in both runtime mode (full dynamic USB support) and static mode (basic class control only). USB classes remain compiled but only enabled drivers appear in descriptors and enumerate with the host.

docs/library/machine.USBDevice.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
class USBDevice -- USB Device driver
55
====================================
66

7-
.. note:: ``machine.USBDevice`` is currently only supported for esp32, rp2 and
8-
samd ports. Native USB support is also required, and not every board
9-
supports native USB.
7+
.. note:: ``machine.USBDevice`` is currently only supported for esp32, rp2,
8+
samd, and stm32 (with TinyUSB) ports. Native USB support is also
9+
required, and not every board supports native USB.
1010

1111
USBDevice provides a low-level Python API for implementing USB device functions using
1212
Python code. This includes both runtime device descriptor configuration and
@@ -390,6 +390,26 @@ Usage Examples
390390
usb.builtin_driver = usb.BUILTIN_CDC | usb.BUILTIN_NCM
391391
usb.active(True)
392392

393+
**Querying Current Configuration:**
394+
::
395+
396+
import machine
397+
398+
usb = machine.USBDevice()
399+
usb.active(False)
400+
usb.builtin_driver = usb.BUILTIN_CDC | usb.BUILTIN_MSC
401+
usb.active(True)
402+
403+
# Check if specific class is enabled using bitwise AND
404+
if usb.builtin_driver & usb.BUILTIN_CDC:
405+
print("CDC is enabled")
406+
if usb.builtin_driver & usb.BUILTIN_MSC:
407+
print("MSC is enabled")
408+
409+
# Check for exact configuration using equality
410+
if usb.builtin_driver == (usb.BUILTIN_CDC | usb.BUILTIN_MSC):
411+
print("Both CDC and MSC are enabled")
412+
393413
**Switching Configurations at Runtime:**
394414
::
395415

0 commit comments

Comments
 (0)