Skip to content

Commit 61fb33e

Browse files
committed
usbd: Changes to match new low-level API.
This won't be exactly the final API, but allows examples to keep working. Signed-off-by: Angus Gratton <[email protected]>
1 parent db1566b commit 61fb33e

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

micropython/usbd/device.py

+21-23
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get():
4848
# MicroPython _USBDevice object
4949
#
5050
# (note this isn't the low-level object, the low-level object is
51-
# _get()._usbd.)
51+
# get()._usbd.)
5252
global _dev
5353
if not _dev:
5454
_dev = _USBDevice()
@@ -64,12 +64,12 @@ def __init__(self):
6464
self._itfs = {} # Mapping from interface number to interface object, set by init()
6565
self._eps = {} # Mapping from endpoint address to interface object, set by _open_cb()
6666
self._ep_cbs = {} # Mapping from endpoint address to Optional[xfer callback]
67-
self._usbd = machine.USBD() # low-level USBD object
67+
self._usbd = machine.USBDevice() # low-level API
6868

6969
def init( # noqa: PLR0913 TODO: find a way to pass fewer arguments without wasting RAM
7070
self,
7171
*itfs,
72-
static_drivers=False,
72+
builtin_drivers=False,
7373
active=True,
7474
manufacturer_str=None,
7575
product_str=None,
@@ -88,24 +88,29 @@ def init( # noqa: PLR0913 TODO: find a way to pass fewer arguments without wast
8888
# device and configuration descriptor fields
8989

9090
_usbd = self._usbd
91-
static = _usbd.static
9291

93-
# Putting None for any strings that should fall back to the "static" value
92+
_usbd.active(False)
93+
94+
builtin = _usbd.builtin_driver = (
95+
_usbd.BUILTIN_DEFAULT if builtin_drivers else _usbd.BUILTIN_NONE
96+
)
97+
98+
# Putting None for any strings that should fall back to the "built-in" value
9499
# Indexes in this list depends on _USB_STR_MANUF, _USB_STR_PRODUCT, _USB_STR_SERIAL
95100
strs = [None, manufacturer_str, product_str, serial_str]
96101

97102
# Build the device descriptor
98103
FMT = "<BBHBBBBHHHBBBB"
99104
# read the static descriptor fields
100-
f = struct.unpack(FMT, static.desc_dev)
105+
f = struct.unpack(FMT, builtin.desc_dev)
101106

102107
def maybe_set(value, idx):
103-
# Override a numeric descriptor value or keep static value f[idx] if 'value' is None
108+
# Override a numeric descriptor value or keep builtin value f[idx] if 'value' is None
104109
if value is not None:
105110
return value
106111
return f[idx]
107112

108-
# Either copy each descriptor field directly from the static device descriptor, or 'maybe'
113+
# Either copy each descriptor field directly from the builtin device descriptor, or 'maybe'
109114
# set it to the custom value from the object
110115
desc_dev = struct.pack(
111116
FMT,
@@ -128,16 +133,11 @@ def maybe_set(value, idx):
128133
# Iterate interfaces to build the configuration descriptor
129134

130135
# Keep track of the interface and endpoint indexes
131-
if static_drivers:
132-
itf_num = static.itf_max
133-
ep_num = max(static.ep_max, 1) # Endpoint 0 always reserved for control
134-
while len(strs) < static.str_max:
135-
strs.append(None) # Reserve other string indexed used by static drivers
136-
initial_cfg = static.desc_cfg
137-
else:
138-
itf_num = 0
139-
ep_num = 1
140-
initial_cfg = b"\x00" * _STD_DESC_CONFIG_LEN # Reserved space for config descriptor
136+
itf_num = builtin.itf_max
137+
ep_num = max(builtin.ep_max, 1) # Endpoint 0 always reserved for control
138+
while len(strs) < builtin.str_max:
139+
strs.append(None) # Reserve other string indexes used by builtin drivers
140+
initial_cfg = builtin.desc_cfg or (b"\x00" * _STD_DESC_CONFIG_LEN)
141141

142142
self._itfs = {}
143143

@@ -191,18 +191,16 @@ def maybe_set(value, idx):
191191
max_power_ma,
192192
)
193193

194-
# Initialise the USB device
195-
_usbd.init(
194+
_usbd.config(
196195
desc_dev,
197196
desc.b,
198197
strs,
199-
static_drivers,
200198
self._open_itf_cb,
201199
self._reset_cb,
202200
self._control_xfer_cb,
203201
self._xfer_cb,
204-
active,
205202
)
203+
_usbd.active(active)
206204

207205
def _open_itf_cb(self, desc):
208206
# Singleton callback from TinyUSB custom class driver, when USB host does
@@ -266,7 +264,7 @@ def _submit_xfer(self, ep_addr, data, done_cb=None):
266264
if self._ep_cbs[ep_addr]:
267265
raise RuntimeError("xfer_pending")
268266

269-
# USBD callback may be called immediately, before Python execution
267+
# USBDevice callback may be called immediately, before Python execution
270268
# continues, so set it first.
271269
#
272270
# To allow xfer_pending checks to work, store True instead of None.

micropython/usbd/examples/cdc_repl_example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
cdc = CDC()
77
cdc.init(timeout=0) # zero timeout makes this non-blocking, suitable for os.dupterm()
88

9-
# pass static_drivers=True so that we get the static USB-CDC alongside,
9+
# pass builtin_drivers=True so that we get the static USB-CDC alongside,
1010
# if it's available.
11-
usbd.device.get().init(cdc, static_drivers=True)
11+
usbd.device.get().init(cdc, builtin_drivers=True)
1212

1313
print("Waiting for USB host to configure the interface...")
1414

micropython/usbd/examples/hid_mouse_example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
def mouse_example():
1313
m = MouseInterface()
1414

15-
# Note: static_drivers=True means that if there's a USB-CDC REPL
15+
# Note: builtin_drivers=True means that if there's a USB-CDC REPL
1616
# available then it will appear as well as the HID device.
17-
usbd.device.get().init(m, static_drivers=True)
17+
usbd.device.get().init(m, builtin_drivers=True)
1818

1919
# wait for host to enumerate as a HID device...
2020
while not m.is_open():

0 commit comments

Comments
 (0)