3.4.14. usb_device — Universal Serial Bus - Device

A USB device is powered and enumerated by a USB host.

The implementation of this module aims to be simple, but yet flexible. It’s possible to change the USB configuration descriptors at runtime by stopping the current driver, initialize a new driver and start the new driver. For simple devices only a single configuration is normally needed.

Using the USB device module is fairly easy. First write the USB descriptors, then initialize the class drivers, then initialize the USB device driver and then start it.

See the test code below for an example usage.


Class driver modules:


Source code: src/drivers/network/usb_device.h, src/drivers/network/usb_device.c

Test code: tst/drivers/hardware/network/usb_device/main.c


Functions

int usb_device_module_init(void)

Initialize the USB device module. This function must be called before calling any other function in this module.

The module will only be initialized once even if this function is called multiple times.

Return
zero(0) or negative error code.

int usb_device_init(struct usb_device_driver_t * self_p, struct usb_device_t * dev_p, struct usb_device_driver_base_t ** drivers_pp, int drivers_max, FAR const union usb_descriptor_t ** descriptors_pp)

Initialize the USB device driver object from given configuration.

Return
zero(0) or negative error code.
Parameters
  • self_p: Driver object to be initialized.
  • dev_p: USB device to use.
  • drivers_pp: An array of initialized drivers.
  • drivers_max: Length of the drivers array.
  • descriptors_pp: A NULL terminated array of USB descriptors.

int usb_device_start(struct usb_device_driver_t *self_p)

Start the USB device device using given driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

int usb_device_stop(struct usb_device_driver_t *self_p)

Stop the USB device device referenced by driver object.

Return
zero(0) or negative error code.
Parameters
  • self_p: Initialized driver object.

ssize_t usb_device_write(struct usb_device_driver_t *self_p, int endpoint, const void *buf_p, size_t size)

Write data to given endpoint.

Return
Number of bytes written or negative error code.
Parameters
  • self_p: Initialized driver object.
  • endpoint: Endpoint to write to.
  • buf_p: Buffer to write.
  • size: Number of bytes to write.

ssize_t usb_device_read_isr(struct usb_device_driver_t *self_p, int endpoint, void *buf_p, size_t size)

Read data from given endpoint from an isr or with the system lock taken.

Return
Number of bytes read or negative error code.
Parameters
  • self_p: Initialized driver object.
  • endpoint: Endpoint to read data from.
  • buf_p: Buffer to read into.
  • size: Number of bytes to read.

ssize_t usb_device_write_isr(struct usb_device_driver_t *self_p, int endpoint, const void *buf_p, size_t size)

Write data to given endpoint from an isr or with the system lock taken.

Return
Number of bytes written or negative error code.
Parameters
  • self_p: Initialized driver object.
  • endpoint: Endpoint to write to.
  • buf_p: Buffer to write.
  • size: Number of bytes to write.