USB device stack API
API reference
There are two ways to transmit data, using the ‘low’ level read/write API or the ‘high’ level transfer API.
- Low level API
To transmit data to the host, the class driver should call usb_write(). Upon completion the registered endpoint callback will be called. Before sending another packet the class driver should wait for the completion of the previous write. When data is received, the registered endpoint callback is called. usb_read() should be used for retrieving the received data. For CDC ACM sample driver this happens via the OUT bulk endpoint handler (cdc_acm_bulk_out) mentioned in the endpoint array (cdc_acm_ep_data).
- High level API
The usb_transfer method can be used to transfer data to/from the host. The transfer API will automatically split the data transmission into one or more USB transaction(s), depending endpoint max packet size. The class driver does not have to implement endpoint callback and should set this callback to the generic usb_transfer_ep_callback.
- group _usb_device_core_api
USB Device Core Layer API.
Defines
-
USB_TRANS_READ
-
USB_TRANS_WRITE
-
USB_TRANS_NO_ZLP
-
USB_DEVICE_BOS_DESC_DEFINE_CAP
Helper macro to place the BOS compatibility descriptor in the right memory section.
Typedefs
-
typedef void (*usb_ep_callback)(uint8_t ep, enum usb_dc_ep_cb_status_code cb_status)
Callback function signature for the USB Endpoint status.
-
typedef int (*usb_request_handler)(struct usb_setup_packet *setup, int32_t *transfer_len, uint8_t **payload_data)
Callback function signature for class specific requests.
Function which handles Class specific requests corresponding to an interface number specified in the device descriptor table. For host to device direction the ‘len’ and ‘payload_data’ contain the length of the received data and the pointer to the received data respectively. For device to host class requests, ‘len’ and ‘payload_data’ should be set by the callback function with the length and the address of the data to be transmitted buffer respectively.
-
typedef void (*usb_interface_config)(struct usb_desc_header *head, uint8_t bInterfaceNumber)
Function for interface runtime configuration.
-
typedef void (*usb_transfer_callback)(uint8_t ep, int tsize, void *priv)
Callback function signature for transfer completion.
Functions
-
int usb_set_config(const uint8_t *usb_descriptor)
Configure USB controller.
Function to configure USB controller. Configuration parameters must be valid or an error is returned
- Parameters:
usb_descriptor – [in] USB descriptor table
- Returns:
0 on success, negative errno code on fail
-
int usb_deconfig(void)
Deconfigure USB controller.
This function returns the USB device to it’s initial state
- Returns:
0 on success, negative errno code on fail
-
int usb_enable(usb_dc_status_callback status_cb)
Enable the USB subsystem and associated hardware.
This function initializes the USB core subsystem and enables the corresponding hardware so that it can begin transmitting and receiving on the USB bus, as well as generating interrupts.
Class-specific initialization and registration must be performed by the user before invoking this, so that any data or events on the bus are processed correctly by the associated class handling code.
- Parameters:
status_cb – [in] Callback registered by user to notify about USB device controller state.
- Returns:
0 on success, negative errno code on fail.
-
int usb_disable(void)
Disable the USB device.
Function to disable the USB device. Upon success, the specified USB interface is clock gated in hardware, it is no longer capable of generating interrupts.
- Returns:
0 on success, negative errno code on fail
-
int usb_write(uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t *bytes_ret)
Write data to the specified endpoint.
Function to write data to the specified endpoint. The supplied usb_ep_callback will be called when transmission is done.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
data – [in] Pointer to data to write
data_len – [in] Length of data requested to write. This may be zero for a zero length status packet.
bytes_ret – [out] Bytes written to the EP FIFO. This value may be NULL if the application expects all bytes to be written
- Returns:
0 on success, negative errno code on fail
-
int usb_read(uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *ret_bytes)
Read data from the specified endpoint.
This function is called by the Endpoint handler function, after an OUT interrupt has been received for that EP. The application must only call this function through the supplied usb_ep_callback function.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
data – [in] Pointer to data buffer to write to
max_data_len – [in] Max length of data to read
ret_bytes – [out] Number of bytes read. If data is NULL and max_data_len is 0 the number of bytes available for read is returned.
- Returns:
0 on success, negative errno code on fail
-
int usb_ep_set_stall(uint8_t ep)
Set STALL condition on the specified endpoint.
This function is called by USB device class handler code to set stall condition on endpoint.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
- Returns:
0 on success, negative errno code on fail
-
int usb_ep_clear_stall(uint8_t ep)
Clears STALL condition on the specified endpoint.
This function is called by USB device class handler code to clear stall condition on endpoint.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
- Returns:
0 on success, negative errno code on fail
-
int usb_ep_read_wait(uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *read_bytes)
Read data from the specified endpoint.
This is similar to usb_ep_read, the difference being that, it doesn’t clear the endpoint NAKs so that the consumer is not bogged down by further upcalls till he is done with the processing of the data. The caller should reactivate ep by invoking usb_ep_read_continue() do so.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
data – [in] pointer to data buffer to write to
max_data_len – [in] max length of data to read
read_bytes – [out] Number of bytes read. If data is NULL and max_data_len is 0 the number of bytes available for read should be returned.
- Returns:
0 on success, negative errno code on fail.
-
int usb_ep_read_continue(uint8_t ep)
Continue reading data from the endpoint.
Clear the endpoint NAK and enable the endpoint to accept more data from the host. Usually called after usb_ep_read_wait() when the consumer is fine to accept more data. Thus these calls together acts as flow control mechanism.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
- Returns:
0 on success, negative errno code on fail.
-
void usb_transfer_ep_callback(uint8_t ep, enum usb_dc_ep_cb_status_code)
Transfer management endpoint callback.
If a USB class driver wants to use high-level transfer functions, driver needs to register this callback as usb endpoint callback.
-
int usb_transfer(uint8_t ep, uint8_t *data, size_t dlen, unsigned int flags, usb_transfer_callback cb, void *priv)
Start a transfer.
Start a usb transfer to/from the data buffer. This function is asynchronous and can be executed in IRQ context. The provided callback will be called on transfer completion (or error) in thread context.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
data – [in] Pointer to data buffer to write-to/read-from
dlen – [in] Size of data buffer
flags – [in] Transfer flags (USB_TRANS_READ, USB_TRANS_WRITE…)
cb – [in] Function called on transfer completion/failure
priv – [in] Data passed back to the transfer completion callback
- Returns:
0 on success, negative errno code on fail.
-
int usb_transfer_sync(uint8_t ep, uint8_t *data, size_t dlen, unsigned int flags)
Start a transfer and block-wait for completion.
Synchronous version of usb_transfer, wait for transfer completion before returning. A return value of zero can also mean that transfer was cancelled or that the endpoint is not ready. This is due to the design of transfers and usb_dc API.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
data – [in] Pointer to data buffer to write-to/read-from
dlen – [in] Size of data buffer
flags – [in] Transfer flags
- Returns:
number of bytes transferred on success, negative errno code on fail.
-
void usb_cancel_transfer(uint8_t ep)
Cancel any ongoing transfer on the specified endpoint.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
-
void usb_cancel_transfers(void)
Cancel all ongoing transfers.
-
bool usb_transfer_is_busy(uint8_t ep)
Check that transfer is ongoing for the endpoint.
- Parameters:
ep – [in] Endpoint address corresponding to the one listed in the device configuration table
- Returns:
true if transfer is ongoing, false otherwise.
-
int usb_wakeup_request(void)
Start the USB remote wakeup procedure.
Function to request a remote wakeup. This feature must be enabled in configuration, otherwise it will always return -ENOTSUP error.
- Returns:
0 on success, negative errno code on fail, i.e. when the bus is already active.
-
bool usb_get_remote_wakeup_status(void)
Get status of the USB remote wakeup feature.
- Returns:
true if remote wakeup has been enabled by the host, false otherwise.
-
void usb_bos_register_cap(void *hdr)
Register BOS capability descriptor.
This function should be used by the application to register BOS capability descriptors before the USB device stack is enabled.
- Parameters:
hdr – [in] Pointer to BOS capability descriptor
-
struct usb_ep_cfg_data
- #include <usb_device.h>
USB Endpoint Configuration.
This structure contains configuration for the endpoint.
Public Members
-
usb_ep_callback ep_cb
Callback function for notification of data received and available to application or transmit done, NULL if callback not required by application code.
-
uint8_t ep_addr
The number associated with the EP in the device configuration structure IN EP = 0x80 | <endpoint number> OUT EP = 0x00 | <endpoint number>
-
usb_ep_callback ep_cb
-
struct usb_interface_cfg_data
- #include <usb_device.h>
USB Interface Configuration.
This structure contains USB interface configuration.
Public Members
-
usb_request_handler class_handler
Handler for USB Class specific Control (EP 0) communications.
-
usb_request_handler vendor_handler
Handler for USB Vendor specific commands.
-
usb_request_handler custom_handler
The custom request handler gets a first chance at handling the request before it is handed over to the ‘chapter 9’ request handler.
return 0 on success, -EINVAL if the request has not been handled by the custom handler and instead needs to be handled by the core USB stack. Any other error code to denote failure within the custom handler.
-
usb_request_handler class_handler
-
struct usb_cfg_data
- #include <usb_device.h>
USB device configuration.
The Application instantiates this with given parameters added using the “usb_set_config” function. Once this function is called changes to this structure will result in undefined behavior. This structure may only be updated after calls to usb_deconfig
Public Members
-
const uint8_t *usb_device_description
USB device description, see http://www.beyondlogic.org/usbnutshell/usb5.shtml#DeviceDescriptors.
-
void *interface_descriptor
Pointer to interface descriptor.
-
usb_interface_config interface_config
Function for interface runtime configuration.
-
void (*cb_usb_status)(struct usb_cfg_data *cfg, enum usb_dc_status_code cb_status, const uint8_t *param)
Callback to be notified on USB connection status change.
-
struct usb_interface_cfg_data interface
USB interface (Class) handler and storage space.
-
uint8_t num_endpoints
Number of individual endpoints in the device configuration.
-
struct usb_ep_cfg_data *endpoint
Pointer to an array of endpoint structs of length equal to the number of EP associated with the device description, not including control endpoints.
-
const uint8_t *usb_device_description
-
USB_TRANS_READ