Skip to content

Commit eeed037

Browse files
projectgusdpgeorge
authored andcommitted
extmod/machine_usb_device: Document xfer_cb result value, add enums.
This callback argument was previously mis-labelled as a boolean, but it's actually the tusb_xfer_result_t values from TinyUSB. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 02f2683 commit eeed037

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

docs/library/machine.USBDevice.rst

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ Methods
155155
The callback has three arguments:
156156

157157
1. The Endpoint number for the completed transfer.
158-
2. Result value: ``True`` if the transfer succeeded, ``False``
159-
otherwise.
160-
3. Number of bytes successfully transferred. In the case of a
161-
"short" transfer, The result is ``True`` and ``xferred_bytes``
158+
2. Result value. This is an integer which is ``0`` (`XFER_SUCCESS`) on
159+
success, or one of the non-zero values `XFER_FAILED` or `XFER_STALLED`
160+
if the transfer failed.
161+
3. Number of bytes successfully transferred. In the case of a "short"
162+
transfer, the result is ``0`` (`XFER_SUCCESS`) and ``xferred_bytes``
162163
will be smaller than the length of the buffer submitted for the
163164
transfer.
164165

@@ -301,4 +302,20 @@ Constants
301302
- ``desc_cfg`` - ``bytes`` object containing the complete built-in USB
302303
configuration descriptor.
303304

305+
.. data:: USBDevice.XFER_SUCCESS
306+
.. data:: USBDevice.XFER_FAILED
307+
.. data:: USBDevice.XFER_STALLED
308+
309+
These are integer constants that represent the possible transfer
310+
result values passed to the ``xfer_cb`` callback (see
311+
`USBDevice.config`).
312+
313+
- ``XFER_SUCCESS`` has value ``0`` and indicates the transfer was
314+
successful.
315+
- ``XFER_FAILED`` indicates the transfer failed due to low-level
316+
integrity errors.
317+
- ``XFER_STALLED`` indicates that the host has stalled this endpoint.
318+
319+
All failure values are non-zero integers.
320+
304321
.. _usb driver modules in micropython-lib: https://github.com/micropython/micropython-lib/tree/master/micropython/usb#readme

extmod/machine_usb_device.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ static const mp_rom_map_elem_t usb_device_locals_dict_table[] = {
302302
{ MP_ROM_QSTR(MP_QSTR_BUILTIN_CDC_MSC), MP_ROM_PTR(&mp_type_usb_device_builtin_default) },
303303
#endif
304304
#endif // !HAS_BUILTIN_DRIVERS
305+
306+
// xfer_cb result values
307+
// These are a subset of tusb_xfer_result_t
308+
{ MP_ROM_QSTR(MP_QSTR_XFER_SUCCESS), MP_OBJ_NEW_SMALL_INT(XFER_RESULT_SUCCESS) },
309+
{ MP_ROM_QSTR(MP_QSTR_XFER_FAILED), MP_OBJ_NEW_SMALL_INT(XFER_RESULT_FAILED) },
310+
{ MP_ROM_QSTR(MP_QSTR_XFER_STALLED), MP_OBJ_NEW_SMALL_INT(XFER_RESULT_STALLED) },
311+
// Some values of tusb_xfer_result_t are not exposed here:
312+
// - XFER_RESULT_TIMEOUT only appears if you call the "sync" API subset, or in one
313+
// case from the samd host controller.
314+
// - XFER_RESULT_INVALID only appears in the host controller APIs
305315
};
306316
static MP_DEFINE_CONST_DICT(usb_device_locals_dict, usb_device_locals_dict_table);
307317

0 commit comments

Comments
 (0)