This is a generic low-level USB driver interface. More...
This is a generic low-level USB driver interface.
usbdev specifies a common USB device API for low level USB interfaces. The interface is split in a definition for the USB interface hardware and for individual USB endpoints.
The driver interface is split in two separate parts. One part is a global USB device interface, the other is an endpoint control API.
The usb device driver can manage parts of the USB interface itself such as the pull up resistor state or the USB speed.
The endpoint control API manages a single endpoint. This allows for a modular approach where multiple USB functions/interfaces can be multiplexed over a single USB interface. Each interface can be implemented as a separate module. The interface handler does not have to care about the usb device itself or it's endpoint number. It can simply request an available endpoint from the usb device with the usbdev_new_ep function.
Each interface handler can request multiple endpoints from the usbdev device. It must supply the expected maximum endpoint transfer size and other properties of the endpoint. A pointer to an usbdev endpoint is returned if an endpoint fitting the requirements is available.
Data transmission is done via the usbdev_ep_xmit function. A buffer and length must be supplied. Received data from the host ends up at this buffer automatically by the low level drivers after reception. For reception the maximum expected transfer length must be supplied, equal to the maximum endpoint transfer size. The data received can be less than this.
For transmitting data back to the host, a similar approach is used. The data to be transmitted is supplied as buffer and the usbdev_ep_xmit function is called with the buffer and the size of the data.
To ensure that the data buffers adhere to the restrictions of the low level USB peripheral memory interface, the specific usbdev_ep_buf_t data type must be used. It behaves as a regular uint8_t
byte buffer, but is instantiated with the attributes to ensure that the low level DMA interface can use it.
A callback function is required for signalling events from the driver. The USBDEV_EVENT_ESR is special in that it indicates that the USB peripheral had an interrupt that needs to be serviced in a non-interrupt context. This requires the usbdev_esr function to be called. In the case that the event is signalled via the usbdev_t::epcb callback, the usbdev_ep_esr should be called to service the endpoint specific events from the peripheral.
Files | |
file | usbdev.h |
Definitions low-level USB driver interface. | |
file | usbdev_synopsys_dwc2.h |
Low level USB FS/HS driver definitions for MCUs with Synopsys DWC2 IP core. | |
Data Structures | |
struct | usbdev |
usbdev device descriptor More... | |
struct | usbdev_ep |
usbdev endpoint descriptor More... | |
struct | usbdev_driver |
usbdev driver functions More... | |
Macros | |
#define | USBDEV_CPU_DMA_REQUIREMENTS |
USBDEV endpoint buffer CPU-specific requirements. | |
#define | usbdev_ep_buf_t USBDEV_CPU_DMA_REQUIREMENTS uint8_t |
Instantiation type for usbdev endpoint buffers. | |
#define | USBDEV_CPU_SET_ADDR_AFTER_STATUS 1 |
USBDEV specific requirement for setting the device address. | |
#define | USBDEV_NUM_ENDPOINTS 8 |
Number of USB IN and OUT endpoints allocated. | |
Typedefs | |
typedef struct usbdev | usbdev_t |
usbdev_t forward declaration | |
typedef struct usbdev_ep | usbdev_ep_t |
usbdev_ep_t forward declaration | |
typedef void(* | usbdev_event_cb_t) (usbdev_t *usbdev, usbdev_event_t event) |
Event callback for signaling usbdev event to upper layers. | |
typedef void(* | usbdev_ep_event_cb_t) (usbdev_ep_t *ep, usbdev_event_t event) |
Event callback for signaling endpoint events to upper layers. | |
typedef struct usbdev_driver | usbdev_driver_t |
usbdev driver functions | |
Enumerations | |
enum | usbdev_event_t { USBDEV_EVENT_ESR = 1 , USBDEV_EVENT_HOST_CONNECT , USBDEV_EVENT_HOST_DISCONNECT , USBDEV_EVENT_RESET , USBDEV_EVENT_SOF , USBDEV_EVENT_SUSPEND , USBDEV_EVENT_RESUME , USBDEV_EVENT_TR_COMPLETE , USBDEV_EVENT_TR_STALL } |
List of event types that can be send from the device driver to the upper layer. More... | |
Functions | |
void | usbdev_init_lowlevel (void) |
Low level USB peripheral driver initialization. | |
usbdev_t * | usbdev_get_ctx (unsigned num) |
Retrieve usbdev context from the peripheral. | |
static void | usbdev_init (usbdev_t *dev) |
Initialize the USB peripheral device. | |
static usbdev_ep_t * | usbdev_new_ep (usbdev_t *dev, usb_ep_type_t type, usb_ep_dir_t dir, size_t size) |
Retrieve an USB endpoint of the specified type. | |
static int | usbdev_get (usbdev_t *dev, usbopt_t opt, void *value, size_t max_len) |
Get an option value from a given usb device. | |
static int | usbdev_set (usbdev_t *dev, usbopt_t opt, const void *value, size_t value_len) |
Set an option value for a given usb device. | |
static void | usbdev_esr (usbdev_t *dev) |
a driver's user-space event service handler | |
static void | usbdev_ep0_stall (usbdev_t *dev) |
Stall both OUT and IN packets on endpoint 0 until a setup packet is received. | |
static void | usbdev_ep_init (usbdev_ep_t *ep) |
Initialize the USB endpoint. | |
static void | usbdev_ep_stall (usbdev_ep_t *ep, bool enable) |
Enable or disable the stall condition on the USB endpoint. | |
static int | usbdev_ep_get (usbdev_ep_t *ep, usbopt_ep_t opt, void *value, size_t max_len) |
Get an option value from a given usb device endpoint. | |
static int | usbdev_ep_set (usbdev_ep_t *ep, usbopt_ep_t opt, const void *value, size_t value_len) |
Set an option value for a given usb device endpoint. | |
static void | usbdev_ep_esr (usbdev_ep_t *ep) |
an endpoint's user-space event handler | |
static int | usbdev_ep_xmit (usbdev_ep_t *ep, uint8_t *buf, size_t len) |
Submit a buffer for a USB endpoint transmission. | |
#define USBDEV_CPU_DMA_REQUIREMENTS |
#define USBDEV_CPU_SET_ADDR_AFTER_STATUS 1 |
USBDEV specific requirement for setting the device address.
The address in the USB device can be set either directly after the SETUP stage on receipt of the SET ADDRESS Request
or after the associated status stage. When the USB device address has to be set depends on the hardware. If USBDEV_CPU_SET_ADDR_AFTER_STATUS
has the value 1 (default), the address is only set in the USB device after the status stage. Overwrite it with 0 in periph_cpu.h
to set the address already directly after the SETUP stage.
#define usbdev_ep_buf_t USBDEV_CPU_DMA_REQUIREMENTS uint8_t |
Instantiation type for usbdev endpoint buffers.
Functions passing around pointers to these buffers can still use uint8_t
for the argument type.
Example usage:
#define USBDEV_NUM_ENDPOINTS 8 |
typedef struct usbdev_driver usbdev_driver_t |
usbdev driver functions
Helpers (such as usbdev_init) are provided and should be used instead. Directly calling these functions is not recommended.
typedef void(* usbdev_ep_event_cb_t) (usbdev_ep_t *ep, usbdev_event_t event) |
typedef struct usbdev_ep usbdev_ep_t |
typedef void(* usbdev_event_cb_t) (usbdev_t *usbdev, usbdev_event_t event) |
enum usbdev_event_t |
List of event types that can be send from the device driver to the upper layer.
Enumerator | |
---|---|
USBDEV_EVENT_ESR | Driver needs it's ESR (event service routine) handled. |
USBDEV_EVENT_HOST_CONNECT | Host connection detected. A host has connected to the device. Detection should happen by detecting the USB power, but other detection mechanisms might be used. |
USBDEV_EVENT_HOST_DISCONNECT | Host disconnected from the device. Similar to USBDEV_EVENT_HOST_CONNECT, except that the host disconnected instead. |
USBDEV_EVENT_RESET | Line reset occurred. A line reset is a host initiated USB reset to the peripheral The peripheral driver must clears the following settings before emitting this event:
|
USBDEV_EVENT_SOF | Start of Frame received. A Start of Frame (SoF) packet is received from the host. |
USBDEV_EVENT_SUSPEND | USB suspend condition active. |
USBDEV_EVENT_RESUME | USB suspend condition no longer active. |
USBDEV_EVENT_TR_COMPLETE | Transaction completed event. An endpoint must emit this event after a transaction with the host occurred to indicate that the data in the buffer is used or new depending on the endpoint direction |
USBDEV_EVENT_TR_STALL | Transaction stall event. An endpoint should emit this event after a stall reply has been transmitted to the host |
|
inlinestatic |
Stall both OUT and IN packets on endpoint 0 until a setup packet is received.
(dev != NULL)
[in] | dev | USB device descriptor |
|
inlinestatic |
an endpoint's user-space event handler
(ep != NULL)
(ep->dev != NULL)
[in] | ep | USB endpoint descriptor to service |
|
inlinestatic |
Get an option value from a given usb device endpoint.
(ep != NULL)
(ep->dev != NULL)
[in] | ep | USB endpoint descriptor |
[in] | opt | option type |
[out] | value | pointer to store the option's value in |
[in] | max_len | maximum number of byte that fit into value |
value
< 0
on error
|
inlinestatic |
Initialize the USB endpoint.
(ep != NULL)
(ep->dev != NULL)
[in] | ep | USB endpoint descriptor |
|
inlinestatic |
Set an option value for a given usb device endpoint.
(ep != NULL)
(ep->dev != NULL)
[in] | ep | USB endpoint descriptor |
[in] | opt | option type |
[in] | value | pointer to the value |
[in] | value_len | the length of value |
value
< 0
on error
|
inlinestatic |
Enable or disable the stall condition on the USB endpoint.
[in] | ep | USB endpoint descriptor |
[in] | enable | True to set stall, false to disable stall |
|
inlinestatic |
Submit a buffer for a USB endpoint transmission.
When dealing with an OUT type endpoint, len
must be the maximum allowed transfer size for the endpoint. The host is allowed to transfer fewer bytes than len
.
(ep != NULL)
(ep->dev != NULL)
[in] | ep | USB endpoint descriptor |
[in,out] | buf | Buffer to submit for transmission |
[in] | len | length of the buffer in bytes to be transmitted or received |
|
inlinestatic |
a driver's user-space event service handler
(dev != NULL)
[in] | dev | USB device descriptor |
Get an option value from a given usb device.
(dev != NULL)
[in] | dev | USB device descriptor |
[in] | opt | option type |
[out] | value | pointer to store the option's value in |
[in] | max_len | maximal amount of byte that fit into value |
value
< 0
on error 0 usbdev_t * usbdev_get_ctx | ( | unsigned | num | ) |
Retrieve usbdev context from the peripheral.
num | usbdev peripheral number to retrieve |
num
|
inlinestatic |
Initialize the USB peripheral device.
(dev != NULL)
dev | usb device to initialize |
void usbdev_init_lowlevel | ( | void | ) |
Low level USB peripheral driver initialization.
This function prepares all usbdev peripherals available for initialization
|
inlinestatic |
Retrieve an USB endpoint of the specified type.
(dev != NULL)
[in] | dev | USB device descriptor |
[in] | type | USB endpoint type |
[in] | dir | USB endpoint direction |
[in] | size | Maximum USB endpoint buffer size used |
|
inlinestatic |
Set an option value for a given usb device.
(dev != NULL)
[in] | dev | USB device descriptor |
[in] | opt | option type |
[in] | value | value to set |
[in] | value_len | the length of value |
value
< 0
on error