class USBDevice – USB Device driver — MicroPython latest documentation (original) (raw)

Note

machine.USBDevice is currently only supported for esp32, rp2 and samd ports. Native USB support is also required, and not every board supports native USB.

USBDevice provides a low-level Python API for implementing USB device functions using Python code.

Warning

This low-level API assumes familiarity with the USB standard. There are high-level usb driver modules in micropython-lib which provide a simpler interface and more built-in functionality.

Terminology

Lifecycle

Managing a runtime USB interface can be tricky, especially if you are communicating with MicroPython over a built-in USB-CDC serial port that’s part of the same USB device.

Constructors

class machine.USBDevice

Construct a USBDevice object.

Note

This object is a singleton, each call to this constructor returns the same object reference.

Methods

USBDevice.config(desc_dev, desc_cfg, desc_strs=None, open_itf_cb=None, reset_cb=None, control_xfer_cb=None, xfer_cb=None)

Configures the USBDevice singleton object with the USB runtime device state and callback functions:

USBDevice.active(self, [value] /)

Returns the current active state of this runtime USB device as a boolean. The runtime USB device is “active” when it is available to interact with the host, it doesn’t mean that a USB Host is actually present.

If the optional value argument is set to a truthy value, then the USB device will be activated.

If the optional value argument is set to a falsey value, then the USB device is deactivated. While the USB device is deactivated, it will not be detected by the USB Host.

To simulate a disconnect and a reconnect of the USB device, callactive(False) followed by active(True). This may be necessary if the runtime device configuration has changed, so that the host sees the new device.

USBDevice.builtin_driver

This attribute holds the current built-in driver configuration, and must be set to one of the USBDevice.BUILTIN_ named constants defined on this object.

By default it holds the value USBDevice.BUILTIN_NONE.

Runtime USB device must be inactive when setting this field. Call theUSBDevice.active() function to deactivate before setting if necessary (and again to activate after setting).

If this value is set to any value other than USBDevice.BUILTIN_NONE then the following restrictions apply to the USBDevice.config() arguments:

USBDevice.remote_wakeup(self)

Wake up host if we are in suspend mode and the REMOTE_WAKEUP feature is enabled by the host. This has to be enabled in the USB attributes, and on the host. Returns True if remote wakeup was enabled and active and the host was woken up.

USBDevice.submit_xfer(self, ep, buffer /)

Submit a USB transfer on endpoint number ep. buffer must be an object implementing the buffer interface, with read access forIN endpoints and write access for OUT endpoints.

Note

ep cannot be the control Endpoint number 0. Control transfers are built up through successive executions ofcontrol_xfer_cb, see above.

Returns True if successful, False if the transfer could not be queued (as USB device is not configured by host, or because another transfer is queued on this endpoint.)

When the USB host completes the transfer, the xfer_cb callback is called (see above).

Raises OSError with reason MP_EINVAL If the USB device is not active.

USBDevice.stall(self, ep, [stall] /)

Calling this function gets or sets the STALL state of a device endpoint.

ep is the number of the endpoint.

If the optional stall parameter is set, this is a boolean flag for the STALL state.

The return value is the current stall state of the endpoint (before any change made by this function).

An endpoint that is set to STALL may remain stalled until this function is called again, or STALL may be cleared automatically by the USB host.

Raises OSError with reason MP_EINVAL If the USB device is not active.

Constants

USBDevice.BUILTIN_NONE

USBDevice.BUILTIN_DEFAULT

USBDevice.BUILTIN_CDC

USBDevice.BUILTIN_MSC

USBDevice.BUILTIN_CDC_MSC

These constant objects hold the built-in descriptor data which is compiled into the MicroPython firmware. USBDevice.BUILTIN_NONE andUSBDevice.BUILTIN_DEFAULT are always present. Additional objects may be present depending on the firmware build configuration and the actual built-in drivers.

Note

Currently at most one of USBDevice.BUILTIN_CDC,USBDevice.BUILTIN_MSC and USBDevice.BUILTIN_CDC_MSC is defined and will be the same object as USBDevice.BUILTIN_DEFAULT. These constants are defined to allow run-time detection of the built-in driver (if any). Support for selecting one of multiple built-in driver configurations may be added in the future.

These values are assigned to USBDevice.builtin_driver to get/set the built-in configuration.

Each object contains the following read-only fields: