@@ -48,7 +48,7 @@ def get():
48
48
# MicroPython _USBDevice object
49
49
#
50
50
# (note this isn't the low-level object, the low-level object is
51
- # _get ()._usbd.)
51
+ # get ()._usbd.)
52
52
global _dev
53
53
if not _dev :
54
54
_dev = _USBDevice ()
@@ -64,12 +64,12 @@ def __init__(self):
64
64
self ._itfs = {} # Mapping from interface number to interface object, set by init()
65
65
self ._eps = {} # Mapping from endpoint address to interface object, set by _open_cb()
66
66
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
68
68
69
69
def init ( # noqa: PLR0913 TODO: find a way to pass fewer arguments without wasting RAM
70
70
self ,
71
71
* itfs ,
72
- static_drivers = False ,
72
+ builtin_drivers = False ,
73
73
active = True ,
74
74
manufacturer_str = None ,
75
75
product_str = None ,
@@ -88,24 +88,29 @@ def init( # noqa: PLR0913 TODO: find a way to pass fewer arguments without wast
88
88
# device and configuration descriptor fields
89
89
90
90
_usbd = self ._usbd
91
- static = _usbd .static
92
91
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
94
99
# Indexes in this list depends on _USB_STR_MANUF, _USB_STR_PRODUCT, _USB_STR_SERIAL
95
100
strs = [None , manufacturer_str , product_str , serial_str ]
96
101
97
102
# Build the device descriptor
98
103
FMT = "<BBHBBBBHHHBBBB"
99
104
# read the static descriptor fields
100
- f = struct .unpack (FMT , static .desc_dev )
105
+ f = struct .unpack (FMT , builtin .desc_dev )
101
106
102
107
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
104
109
if value is not None :
105
110
return value
106
111
return f [idx ]
107
112
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'
109
114
# set it to the custom value from the object
110
115
desc_dev = struct .pack (
111
116
FMT ,
@@ -128,16 +133,11 @@ def maybe_set(value, idx):
128
133
# Iterate interfaces to build the configuration descriptor
129
134
130
135
# 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 )
141
141
142
142
self ._itfs = {}
143
143
@@ -191,18 +191,16 @@ def maybe_set(value, idx):
191
191
max_power_ma ,
192
192
)
193
193
194
- # Initialise the USB device
195
- _usbd .init (
194
+ _usbd .config (
196
195
desc_dev ,
197
196
desc .b ,
198
197
strs ,
199
- static_drivers ,
200
198
self ._open_itf_cb ,
201
199
self ._reset_cb ,
202
200
self ._control_xfer_cb ,
203
201
self ._xfer_cb ,
204
- active ,
205
202
)
203
+ _usbd .active (active )
206
204
207
205
def _open_itf_cb (self , desc ):
208
206
# 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):
266
264
if self ._ep_cbs [ep_addr ]:
267
265
raise RuntimeError ("xfer_pending" )
268
266
269
- # USBD callback may be called immediately, before Python execution
267
+ # USBDevice callback may be called immediately, before Python execution
270
268
# continues, so set it first.
271
269
#
272
270
# To allow xfer_pending checks to work, store True instead of None.
0 commit comments