Skip to content

Commit 4404ee8

Browse files
committed
usbd: Update to handle memoryview request
Previously 'request' was marshalled to and from a tuple for each control transfer. This makes for ergonomic Python but a lot of heap allocations, especially as not every field is read on every request type. This has a matching code change in micropython to support it. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent bf6132f commit 4404ee8

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

micropython/usbd/cdc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def get_endpoint_descriptors(self, ep_addr, str_idx):
295295

296296
def handle_interface_control_xfer(self, stage, request):
297297
# Handle class-specific interface control transfers
298-
bmRequestType, bRequest, wValue, _, wLength = request
298+
bmRequestType, bRequest, wValue, _, wLength = struct.unpack("BBHHH", request)
299299
recipient, req_type, req_dir = split_bmRequestType(bmRequestType)
300300
if stage == STAGE_SETUP:
301301
if req_type == REQ_TYPE_CLASS:

micropython/usbd/device.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ def _control_xfer_cb(self, stage, request):
352352
# The TinyUSB class driver framework only calls this function for
353353
# particular types of control transfer, other standard control transfers
354354
# are handled by TinyUSB itself.
355-
bmRequestType, _, _, wIndex, _ = request
356-
recipient, _, _ = split_bmRequestType(bmRequestType)
355+
wIndex = request[4] + (request[5] << 8)
356+
recipient, _, _ = split_bmRequestType(request[0])
357357

358358
itf = None
359359
result = None
@@ -376,7 +376,7 @@ def _control_xfer_cb(self, stage, request):
376376
# At time this code was written, only the control transfers shown
377377
# above are passed to the class driver callback. See
378378
# invoke_class_control() in tinyusb usbd.c
379-
print(f"Unexpected control request type {bmRequestType:#x}")
379+
print(f"Unexpected control request type {request[0]:#x}")
380380
return False
381381

382382
# Accept the following possible replies from handle_NNN_control_xfer():
@@ -532,11 +532,13 @@ def handle_device_control_xfer(self, stage, request):
532532
# Parameters:
533533
#
534534
# - stage is one of utils.STAGE_SETUP, utils.STAGE_DATA, utils.STAGE_ACK.
535-
# - request is a tuple of (bmRequestType, bRequest, wValue, wIndex,
536-
# - wLength), as per USB 2.0 specification 9.3 USB Device Requests, p250.
537535
#
538-
# The function can call split_bmRequestType() to split bmRequestType into
539-
# (Recipient, Type, Direction).
536+
# - request is a memoryview into a USB request packet, as per USB 2.0
537+
# specification 9.3 USB Device Requests, p250. the memoryview is only
538+
# valid while the callback is running.
539+
#
540+
# The function can call split_bmRequestType(request[0]) to split
541+
# bmRequestType into (Recipient, Type, Direction).
540542
#
541543
# Result, any of:
542544
#

micropython/usbd/hid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def get_hid_descriptor(self):
134134

135135
def handle_interface_control_xfer(self, stage, request):
136136
# Handle standard and class-specific interface control transfers for HID devices.
137-
bmRequestType, bRequest, wValue, _, wLength = request
137+
bmRequestType, bRequest, wValue, _, wLength = struct.unpack("BBHHH", request)
138138

139139
recipient, req_type, _ = split_bmRequestType(bmRequestType)
140140

0 commit comments

Comments
 (0)