ChibiOS/HAL  6.1.0
USB Driver

Generic USB Driver. More...

Collaboration diagram for USB Driver:

Detailed Description

Generic USB Driver.

This module implements a generic USB (Universal Serial Bus) driver supporting device-mode operations.

Precondition
In order to use the USB driver the HAL_USE_USB option must be enabled in halconf.h.

Driver State Machine

The driver implements a state machine internally, not all the driver functionalities can be used in any moment, any transition not explicitly shown in the following diagram has to be considered an error and shall be captured by an assertion (if enabled).

dot_inline_dotgraph_17.png

USB Operations

The USB driver is quite complex and USB is complex in itself, it is recommended to study the USB specification before trying to use the driver.

USB Implementation

The USB driver abstracts the inner details of the underlying USB hardware. The driver works asynchronously and communicates with the application using callbacks. The application is responsible of the descriptors and strings required by the USB device class to be implemented and of the handling of the specific messages sent over the endpoint zero. Standard messages are handled internally to the driver. The application can use hooks in order to handle custom messages or override the handling of the default handling of standard messages.

USB Endpoints

USB endpoints are the objects that the application uses to exchange data with the host. There are two kind of endpoints:

The driver invokes a callback after finishing an IN or OUT transaction. States diagram for OUT endpoints in transaction mode:

dot_inline_dotgraph_18.png



States diagram for IN endpoints in transaction mode:

dot_inline_dotgraph_19.png



USB Callbacks

The USB driver uses callbacks in order to interact with the application. There are several kinds of callbacks to be handled:

Macros

#define USB_USE_WAIT   FALSE
 Enables synchronous APIs. More...
 
#define USB_MAX_ENDPOINTS   4
 Maximum endpoint address. More...
 
#define USB_EP0_STATUS_STAGE   USB_EP0_STATUS_STAGE_SW
 Status stage handling method. More...
 
#define USB_SET_ADDRESS_MODE   USB_LATE_SET_ADDRESS
 The address can be changed immediately upon packet reception. More...
 
#define USB_SET_ADDRESS_ACK_HANDLING   USB_SET_ADDRESS_ACK_SW
 Method for set address acknowledge. More...
 
#define usb_lld_get_frame_number(usbp)   0
 Returns the current frame number. More...
 
#define usb_lld_get_transaction_size(usbp, ep)   ((usbp)->epc[ep]->out_state->rxcnt)
 Returns the exact size of a receive transaction. More...
 
#define usb_lld_connect_bus(usbp)
 Connects the USB device. More...
 
#define usb_lld_disconnect_bus(usbp)
 Disconnect the USB device. More...
 
#define usb_lld_wakeup_host(usbp)
 Start of host wake-up procedure. More...
 

Helper macros for USB descriptors

#define USB_DESC_INDEX(i)   ((uint8_t)(i))
 Helper macro for index values into descriptor strings. More...
 
#define USB_DESC_BYTE(b)   ((uint8_t)(b))
 Helper macro for byte values into descriptor strings. More...
 
#define USB_DESC_WORD(w)
 Helper macro for word values into descriptor strings. More...
 
#define USB_DESC_BCD(bcd)
 Helper macro for BCD values into descriptor strings. More...
 
#define USB_DESC_DEVICE_SIZE   18U
 
#define USB_DESC_DEVICE(bcdUSB, bDeviceClass, bDeviceSubClass, bDeviceProtocol, bMaxPacketSize, idVendor, idProduct, bcdDevice, iManufacturer, iProduct, iSerialNumber, bNumConfigurations)
 Device Descriptor helper macro. More...
 
#define USB_DESC_CONFIGURATION_SIZE   9U
 Configuration Descriptor size. More...
 
#define USB_DESC_CONFIGURATION(wTotalLength, bNumInterfaces, bConfigurationValue, iConfiguration, bmAttributes, bMaxPower)
 Configuration Descriptor helper macro. More...
 
#define USB_DESC_INTERFACE_SIZE   9U
 Interface Descriptor size. More...
 
#define USB_DESC_INTERFACE(bInterfaceNumber, bAlternateSetting, bNumEndpoints, bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol, iInterface)
 Interface Descriptor helper macro. More...
 
#define USB_DESC_INTERFACE_ASSOCIATION_SIZE   8U
 Interface Association Descriptor size. More...
 
#define USB_DESC_INTERFACE_ASSOCIATION(bFirstInterface, bInterfaceCount, bFunctionClass, bFunctionSubClass, bFunctionProcotol, iInterface)
 Interface Association Descriptor helper macro. More...
 
#define USB_DESC_ENDPOINT_SIZE   7U
 Endpoint Descriptor size. More...
 
#define USB_DESC_ENDPOINT(bEndpointAddress, bmAttributes, wMaxPacketSize, bInterval)
 Endpoint Descriptor helper macro. More...
 

Endpoint types and settings

#define USB_EP_MODE_TYPE   0x0003U
 
#define USB_EP_MODE_TYPE_CTRL   0x0000U
 
#define USB_EP_MODE_TYPE_ISOC   0x0001U
 
#define USB_EP_MODE_TYPE_BULK   0x0002U
 
#define USB_EP_MODE_TYPE_INTR   0x0003U
 

Macro Functions

#define usbGetDriverStateI(usbp)   ((usbp)->state)
 Returns the driver state. More...
 
#define usbConnectBus(usbp)   usb_lld_connect_bus(usbp)
 Connects the USB device. More...
 
#define usbDisconnectBus(usbp)   usb_lld_disconnect_bus(usbp)
 Disconnect the USB device. More...
 
#define usbGetFrameNumberX(usbp)   usb_lld_get_frame_number(usbp)
 Returns the current frame number. More...
 
#define usbGetTransmitStatusI(usbp, ep)   (((usbp)->transmitting & (uint16_t)((unsigned)1U << (unsigned)(ep))) != 0U)
 Returns the status of an IN endpoint. More...
 
#define usbGetReceiveStatusI(usbp, ep)   (((usbp)->receiving & (uint16_t)((unsigned)1U << (unsigned)(ep))) != 0U)
 Returns the status of an OUT endpoint. More...
 
#define usbGetReceiveTransactionSizeX(usbp, ep)   usb_lld_get_transaction_size(usbp, ep)
 Returns the exact size of a receive transaction. More...
 
#define usbSetupTransfer(usbp, buf, n, endcb)
 Request transfer setup. More...
 
#define usbReadSetup(usbp, ep, buf)   usb_lld_read_setup(usbp, ep, buf)
 Reads a setup packet from the dedicated packet buffer. More...
 

Low level driver helper macros

#define _usb_isr_invoke_event_cb(usbp, evt)
 Common ISR code, usb event callback. More...
 
#define _usb_isr_invoke_sof_cb(usbp)
 Common ISR code, SOF callback. More...
 
#define _usb_isr_invoke_setup_cb(usbp, ep)
 Common ISR code, setup packet callback. More...
 
#define _usb_isr_invoke_in_cb(usbp, ep)
 Common ISR code, IN endpoint callback. More...
 
#define _usb_isr_invoke_out_cb(usbp, ep)
 Common ISR code, OUT endpoint event. More...
 

PLATFORM configuration options

#define PLATFORM_USB_USE_USB1   FALSE
 USB driver enable switch. More...
 

Typedefs

typedef struct USBDriver USBDriver
 Type of a structure representing an USB driver. More...
 
typedef uint8_t usbep_t
 Type of an endpoint identifier. More...
 
typedef void(* usbcallback_t) (USBDriver *usbp)
 Type of an USB generic notification callback. More...
 
typedef void(* usbepcallback_t) (USBDriver *usbp, usbep_t ep)
 Type of an USB endpoint callback. More...
 
typedef void(* usbeventcb_t) (USBDriver *usbp, usbevent_t event)
 Type of an USB event notification callback. More...
 
typedef bool(* usbreqhandler_t) (USBDriver *usbp)
 Type of a requests handler callback. More...
 
typedef const USBDescriptor *(* usbgetdescriptor_t) (USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang)
 Type of an USB descriptor-retrieving callback. More...
 

Data Structures

struct  USBDescriptor
 Type of an USB descriptor. More...
 
struct  USBInEndpointState
 Type of an IN endpoint state structure. More...
 
struct  USBOutEndpointState
 Type of an OUT endpoint state structure. More...
 
struct  USBEndpointConfig
 Type of an USB endpoint configuration structure. More...
 
struct  USBConfig
 Type of an USB driver configuration structure. More...
 
struct  USBDriver
 Structure representing an USB driver. More...
 

Functions

static void set_address (USBDriver *usbp)
 SET ADDRESS transaction callback. More...
 
static bool default_handler (USBDriver *usbp)
 Standard requests handler. More...
 
void usbInit (void)
 USB Driver initialization. More...
 
void usbObjectInit (USBDriver *usbp)
 Initializes the standard part of a USBDriver structure. More...
 
void usbStart (USBDriver *usbp, const USBConfig *config)
 Configures and activates the USB peripheral. More...
 
void usbStop (USBDriver *usbp)
 Deactivates the USB peripheral. More...
 
void usbInitEndpointI (USBDriver *usbp, usbep_t ep, const USBEndpointConfig *epcp)
 Enables an endpoint. More...
 
void usbDisableEndpointsI (USBDriver *usbp)
 Disables all the active endpoints. More...
 
void usbStartReceiveI (USBDriver *usbp, usbep_t ep, uint8_t *buf, size_t n)
 Starts a receive transaction on an OUT endpoint. More...
 
void usbStartTransmitI (USBDriver *usbp, usbep_t ep, const uint8_t *buf, size_t n)
 Starts a transmit transaction on an IN endpoint. More...
 
msg_t usbReceive (USBDriver *usbp, usbep_t ep, uint8_t *buf, size_t n)
 Performs a receive transaction on an OUT endpoint. More...
 
msg_t usbTransmit (USBDriver *usbp, usbep_t ep, const uint8_t *buf, size_t n)
 Performs a transmit transaction on an IN endpoint. More...
 
bool usbStallReceiveI (USBDriver *usbp, usbep_t ep)
 Stalls an OUT endpoint. More...
 
bool usbStallTransmitI (USBDriver *usbp, usbep_t ep)
 Stalls an IN endpoint. More...
 
void usbWakeupHost (USBDriver *usbp)
 Host wake-up procedure. More...
 
void _usb_reset (USBDriver *usbp)
 USB reset routine. More...
 
void _usb_suspend (USBDriver *usbp)
 USB suspend routine. More...
 
void _usb_wakeup (USBDriver *usbp)
 USB wake-up routine. More...
 
void _usb_ep0setup (USBDriver *usbp, usbep_t ep)
 Default EP0 SETUP callback. More...
 
void _usb_ep0in (USBDriver *usbp, usbep_t ep)
 Default EP0 IN callback. More...
 
void _usb_ep0out (USBDriver *usbp, usbep_t ep)
 Default EP0 OUT callback. More...
 
void usb_lld_init (void)
 Low level USB driver initialization. More...
 
void usb_lld_start (USBDriver *usbp)
 Configures and activates the USB peripheral. More...
 
void usb_lld_stop (USBDriver *usbp)
 Deactivates the USB peripheral. More...
 
void usb_lld_reset (USBDriver *usbp)
 USB low level reset routine. More...
 
void usb_lld_set_address (USBDriver *usbp)
 Sets the USB address. More...
 
void usb_lld_init_endpoint (USBDriver *usbp, usbep_t ep)
 Enables an endpoint. More...
 
void usb_lld_disable_endpoints (USBDriver *usbp)
 Disables all the active endpoints except the endpoint zero. More...
 
usbepstatus_t usb_lld_get_status_out (USBDriver *usbp, usbep_t ep)
 Returns the status of an OUT endpoint. More...
 
usbepstatus_t usb_lld_get_status_in (USBDriver *usbp, usbep_t ep)
 Returns the status of an IN endpoint. More...
 
void usb_lld_read_setup (USBDriver *usbp, usbep_t ep, uint8_t *buf)
 Reads a setup packet from the dedicated packet buffer. More...
 
void usb_lld_prepare_receive (USBDriver *usbp, usbep_t ep)
 Prepares for a receive operation. More...
 
void usb_lld_prepare_transmit (USBDriver *usbp, usbep_t ep)
 Prepares for a transmit operation. More...
 
void usb_lld_start_out (USBDriver *usbp, usbep_t ep)
 Starts a receive operation on an OUT endpoint. More...
 
void usb_lld_start_in (USBDriver *usbp, usbep_t ep)
 Starts a transmit operation on an IN endpoint. More...
 
void usb_lld_stall_out (USBDriver *usbp, usbep_t ep)
 Brings an OUT endpoint in the stalled state. More...
 
void usb_lld_stall_in (USBDriver *usbp, usbep_t ep)
 Brings an IN endpoint in the stalled state. More...
 
void usb_lld_clear_out (USBDriver *usbp, usbep_t ep)
 Brings an OUT endpoint in the active state. More...
 
void usb_lld_clear_in (USBDriver *usbp, usbep_t ep)
 Brings an IN endpoint in the active state. More...
 

Enumerations

Variables

USBDriver USBD1
 USB1 driver identifier. More...
 
union {
   USBInEndpointState   in
 IN EP0 state. More...
 
   USBOutEndpointState   out
 OUT EP0 state. More...
 
ep0_state
 EP0 state. More...
 
static const USBEndpointConfig ep0config
 EP0 initialization structure. More...
 

Macro Definition Documentation

#define USB_DESC_INDEX (   i)    ((uint8_t)(i))

Helper macro for index values into descriptor strings.

Definition at line 93 of file hal_usb.h.

#define USB_DESC_BYTE (   b)    ((uint8_t)(b))

Helper macro for byte values into descriptor strings.

Definition at line 98 of file hal_usb.h.

#define USB_DESC_WORD (   w)
Value:
(uint8_t)((w) & 255U), \
(uint8_t)(((w) >> 8) & 255U)

Helper macro for word values into descriptor strings.

Definition at line 103 of file hal_usb.h.

#define USB_DESC_BCD (   bcd)
Value:
(uint8_t)((bcd) & 255U), \
(uint8_t)(((bcd) >> 8) & 255)

Helper macro for BCD values into descriptor strings.

Definition at line 110 of file hal_usb.h.

#define USB_DESC_DEVICE (   bcdUSB,
  bDeviceClass,
  bDeviceSubClass,
  bDeviceProtocol,
  bMaxPacketSize,
  idVendor,
  idProduct,
  bcdDevice,
  iManufacturer,
  iProduct,
  iSerialNumber,
  bNumConfigurations 
)
Value:
USB_DESC_BYTE(USB_DESC_DEVICE_SIZE), \
USB_DESC_BYTE(USB_DESCRIPTOR_DEVICE), \
USB_DESC_BCD(bcdUSB), \
USB_DESC_BYTE(bDeviceClass), \
USB_DESC_BYTE(bDeviceSubClass), \
USB_DESC_BYTE(bDeviceProtocol), \
USB_DESC_BYTE(bMaxPacketSize), \
USB_DESC_WORD(idVendor), \
USB_DESC_WORD(idProduct), \
USB_DESC_BCD(bcdDevice), \
USB_DESC_INDEX(iManufacturer), \
USB_DESC_INDEX(iProduct), \
USB_DESC_INDEX(iSerialNumber), \
USB_DESC_BYTE(bNumConfigurations)
#define USB_DESC_BYTE(b)
Helper macro for byte values into descriptor strings.
Definition: hal_usb.h:98
#define USB_DESC_WORD(w)
Helper macro for word values into descriptor strings.
Definition: hal_usb.h:103
#define USB_DESC_INDEX(i)
Helper macro for index values into descriptor strings.
Definition: hal_usb.h:93
#define USB_DESC_BCD(bcd)
Helper macro for BCD values into descriptor strings.
Definition: hal_usb.h:110

Device Descriptor helper macro.

Definition at line 122 of file hal_usb.h.

#define USB_DESC_CONFIGURATION_SIZE   9U

Configuration Descriptor size.

Definition at line 144 of file hal_usb.h.

#define USB_DESC_CONFIGURATION (   wTotalLength,
  bNumInterfaces,
  bConfigurationValue,
  iConfiguration,
  bmAttributes,
  bMaxPower 
)
Value:
USB_DESC_BYTE(USB_DESCRIPTOR_CONFIGURATION), \
USB_DESC_WORD(wTotalLength), \
USB_DESC_BYTE(bNumInterfaces), \
USB_DESC_BYTE(bConfigurationValue), \
USB_DESC_INDEX(iConfiguration), \
USB_DESC_BYTE(bmAttributes), \
USB_DESC_BYTE(bMaxPower)
#define USB_DESC_BYTE(b)
Helper macro for byte values into descriptor strings.
Definition: hal_usb.h:98
#define USB_DESC_WORD(w)
Helper macro for word values into descriptor strings.
Definition: hal_usb.h:103
#define USB_DESC_INDEX(i)
Helper macro for index values into descriptor strings.
Definition: hal_usb.h:93
#define USB_DESC_CONFIGURATION_SIZE
Configuration Descriptor size.
Definition: hal_usb.h:144

Configuration Descriptor helper macro.

Definition at line 149 of file hal_usb.h.

#define USB_DESC_INTERFACE_SIZE   9U

Interface Descriptor size.

Definition at line 164 of file hal_usb.h.

#define USB_DESC_INTERFACE (   bInterfaceNumber,
  bAlternateSetting,
  bNumEndpoints,
  bInterfaceClass,
  bInterfaceSubClass,
  bInterfaceProtocol,
  iInterface 
)
Value:
USB_DESC_BYTE(USB_DESCRIPTOR_INTERFACE), \
USB_DESC_BYTE(bInterfaceNumber), \
USB_DESC_BYTE(bAlternateSetting), \
USB_DESC_BYTE(bNumEndpoints), \
USB_DESC_BYTE(bInterfaceClass), \
USB_DESC_BYTE(bInterfaceSubClass), \
USB_DESC_BYTE(bInterfaceProtocol), \
USB_DESC_INDEX(iInterface)
#define USB_DESC_BYTE(b)
Helper macro for byte values into descriptor strings.
Definition: hal_usb.h:98
#define USB_DESC_INDEX(i)
Helper macro for index values into descriptor strings.
Definition: hal_usb.h:93
#define USB_DESC_INTERFACE_SIZE
Interface Descriptor size.
Definition: hal_usb.h:164

Interface Descriptor helper macro.

Definition at line 169 of file hal_usb.h.

#define USB_DESC_INTERFACE_ASSOCIATION_SIZE   8U

Interface Association Descriptor size.

Definition at line 186 of file hal_usb.h.

#define USB_DESC_INTERFACE_ASSOCIATION (   bFirstInterface,
  bInterfaceCount,
  bFunctionClass,
  bFunctionSubClass,
  bFunctionProcotol,
  iInterface 
)
Value:
USB_DESC_BYTE(USB_DESCRIPTOR_INTERFACE_ASSOCIATION), \
USB_DESC_BYTE(bFirstInterface), \
USB_DESC_BYTE(bInterfaceCount), \
USB_DESC_BYTE(bFunctionClass), \
USB_DESC_BYTE(bFunctionSubClass), \
USB_DESC_BYTE(bFunctionProcotol), \
USB_DESC_INDEX(iInterface)
#define USB_DESC_BYTE(b)
Helper macro for byte values into descriptor strings.
Definition: hal_usb.h:98
#define USB_DESC_INDEX(i)
Helper macro for index values into descriptor strings.
Definition: hal_usb.h:93
#define USB_DESC_INTERFACE_ASSOCIATION_SIZE
Interface Association Descriptor size.
Definition: hal_usb.h:186

Interface Association Descriptor helper macro.

Definition at line 191 of file hal_usb.h.

#define USB_DESC_ENDPOINT_SIZE   7U

Endpoint Descriptor size.

Definition at line 207 of file hal_usb.h.

#define USB_DESC_ENDPOINT (   bEndpointAddress,
  bmAttributes,
  wMaxPacketSize,
  bInterval 
)
Value:
USB_DESC_BYTE(USB_DESCRIPTOR_ENDPOINT), \
USB_DESC_BYTE(bEndpointAddress), \
USB_DESC_BYTE(bmAttributes), \
USB_DESC_WORD(wMaxPacketSize), \
USB_DESC_BYTE(bInterval)
#define USB_DESC_BYTE(b)
Helper macro for byte values into descriptor strings.
Definition: hal_usb.h:98
#define USB_DESC_WORD(w)
Helper macro for word values into descriptor strings.
Definition: hal_usb.h:103
#define USB_DESC_ENDPOINT_SIZE
Endpoint Descriptor size.
Definition: hal_usb.h:207

Endpoint Descriptor helper macro.

Definition at line 212 of file hal_usb.h.

#define USB_EP_MODE_TYPE   0x0003U

Endpoint type mask.

Definition at line 226 of file hal_usb.h.

#define USB_EP_MODE_TYPE_CTRL   0x0000U

Control endpoint.

Definition at line 227 of file hal_usb.h.

#define USB_EP_MODE_TYPE_ISOC   0x0001U

Isochronous endpoint.

Definition at line 228 of file hal_usb.h.

#define USB_EP_MODE_TYPE_BULK   0x0002U

Bulk endpoint.

Definition at line 229 of file hal_usb.h.

#define USB_EP_MODE_TYPE_INTR   0x0003U

Interrupt endpoint.

Definition at line 230 of file hal_usb.h.

#define USB_USE_WAIT   FALSE

Enables synchronous APIs.

Note
Disabling this option saves both code and data space.

Definition at line 245 of file hal_usb.h.

#define usbGetDriverStateI (   usbp)    ((usbp)->state)

Returns the driver state.

Parameters
[in]usbppointer to the USBDriver object
Returns
The driver state.
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 391 of file hal_usb.h.

Referenced by obnotify(), sduSOFHookI(), usbReceive(), and usbTransmit().

#define usbConnectBus (   usbp)    usb_lld_connect_bus(usbp)

Connects the USB device.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 400 of file hal_usb.h.

#define usbDisconnectBus (   usbp)    usb_lld_disconnect_bus(usbp)

Disconnect the USB device.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 409 of file hal_usb.h.

#define usbGetFrameNumberX (   usbp)    usb_lld_get_frame_number(usbp)

Returns the current frame number.

Parameters
[in]usbppointer to the USBDriver object
Returns
The current frame number.
Function Class:This is an X-Class API, this function can be invoked from any context.

Definition at line 419 of file hal_usb.h.

#define usbGetTransmitStatusI (   usbp,
  ep 
)    (((usbp)->transmitting & (uint16_t)((unsigned)1U << (unsigned)(ep))) != 0U)

Returns the status of an IN endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
The operation status.
Return values
falseEndpoint ready.
trueEndpoint transmitting.
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 432 of file hal_usb.h.

Referenced by obnotify(), sduSOFHookI(), usbStallTransmitI(), and usbStartTransmitI().

#define usbGetReceiveStatusI (   usbp,
  ep 
)    (((usbp)->receiving & (uint16_t)((unsigned)1U << (unsigned)(ep))) != 0U)

Returns the status of an OUT endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
The operation status.
Return values
falseEndpoint ready.
trueEndpoint receiving.
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 446 of file hal_usb.h.

Referenced by usbStallReceiveI(), and usbStartReceiveI().

#define usbGetReceiveTransactionSizeX (   usbp,
  ep 
)    usb_lld_get_transaction_size(usbp, ep)

Returns the exact size of a receive transaction.

The received size can be different from the size specified in usbStartReceiveI() because the last packet could have a size different from the expected one.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
Received data size.
Function Class:This is an X-Class API, this function can be invoked from any context.

Definition at line 461 of file hal_usb.h.

Referenced by _usb_ep0out(), and sduDataReceived().

#define usbSetupTransfer (   usbp,
  buf,
  n,
  endcb 
)
Value:
{ \
(usbp)->ep0next = (buf); \
(usbp)->ep0n = (n); \
(usbp)->ep0endcb = (endcb); \
}

Request transfer setup.

This macro is used by the request handling callbacks in order to prepare a transaction over the endpoint zero.

Parameters
[in]usbppointer to the USBDriver object
[in]bufpointer to a buffer for the transaction data
[in]nnumber of bytes to be transferred
[in]endcbcallback to be invoked after the transfer or NULL
Function Class:Special function, this function has special requirements see the notes.

Definition at line 476 of file hal_usb.h.

Referenced by default_handler(), and sduRequestsHook().

#define usbReadSetup (   usbp,
  ep,
  buf 
)    usb_lld_read_setup(usbp, ep, buf)

Reads a setup packet from the dedicated packet buffer.

This function must be invoked in the context of the setup_cb callback in order to read the received setup packet.

Precondition
In order to use this function the endpoint must have been initialized as a control endpoint.
Note
This function can be invoked both in thread and IRQ context.
Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
[out]bufbuffer where to copy the packet data
Function Class:Special function, this function has special requirements see the notes.

Definition at line 496 of file hal_usb.h.

Referenced by _usb_ep0setup().

#define _usb_isr_invoke_event_cb (   usbp,
  evt 
)
Value:
{ \
if (((usbp)->config->event_cb) != NULL) { \
(usbp)->config->event_cb(usbp, evt); \
} \
}

Common ISR code, usb event callback.

Parameters
[in]usbppointer to the USBDriver object
[in]evtUSB event code
Function Class:Not an API, this function is for internal use only.

Definition at line 511 of file hal_usb.h.

Referenced by _usb_ep0in(), _usb_ep0out(), _usb_ep0setup(), _usb_reset(), _usb_suspend(), _usb_wakeup(), default_handler(), and set_address().

#define _usb_isr_invoke_sof_cb (   usbp)
Value:
{ \
if (((usbp)->config->sof_cb) != NULL) { \
(usbp)->config->sof_cb(usbp); \
} \
}

Common ISR code, SOF callback.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 524 of file hal_usb.h.

#define _usb_isr_invoke_setup_cb (   usbp,
  ep 
)
Value:
{ \
(usbp)->epc[ep]->setup_cb(usbp, ep); \
}

Common ISR code, setup packet callback.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 538 of file hal_usb.h.

#define _usb_isr_invoke_in_cb (   usbp,
  ep 
)
Value:
{ \
(usbp)->transmitting &= ~(1 << (ep)); \
if ((usbp)->epc[ep]->in_cb != NULL) { \
(usbp)->epc[ep]->in_cb(usbp, ep); \
} \
osalThreadResumeI(&(usbp)->epc[ep]->in_state->thread, MSG_OK); \
}
static void osalSysLockFromISR(void)
Enters a critical zone from ISR context.
Definition: osal.h:550
static void osalSysUnlockFromISR(void)
Leaves a critical zone from ISR context.
Definition: osal.h:560
void osalThreadResumeI(thread_reference_t *trp, msg_t msg)
Wakes up a thread waiting on a thread reference object.
Definition: osal.c:230

Common ISR code, IN endpoint callback.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 551 of file hal_usb.h.

#define _usb_isr_invoke_out_cb (   usbp,
  ep 
)
Value:
{ \
(usbp)->receiving &= ~(1 << (ep)); \
if ((usbp)->epc[ep]->out_cb != NULL) { \
(usbp)->epc[ep]->out_cb(usbp, ep); \
} \
osalThreadResumeI(&(usbp)->epc[ep]->out_state->thread, \
}
static void osalSysLockFromISR(void)
Enters a critical zone from ISR context.
Definition: osal.h:550
static void osalSysUnlockFromISR(void)
Leaves a critical zone from ISR context.
Definition: osal.h:560
void osalThreadResumeI(thread_reference_t *trp, msg_t msg)
Wakes up a thread waiting on a thread reference object.
Definition: osal.c:230
#define usbGetReceiveTransactionSizeX(usbp, ep)
Returns the exact size of a receive transaction.
Definition: hal_usb.h:461

Common ISR code, OUT endpoint event.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 578 of file hal_usb.h.

#define USB_MAX_ENDPOINTS   4

Maximum endpoint address.

Definition at line 37 of file hal_usb_lld.h.

Referenced by _usb_reset(), _usb_suspend(), usbDisableEndpointsI(), usbObjectInit(), usbStart(), usbStartReceiveI(), usbStartTransmitI(), and usbStop().

#define USB_EP0_STATUS_STAGE   USB_EP0_STATUS_STAGE_SW

Status stage handling method.

Definition at line 42 of file hal_usb_lld.h.

#define USB_SET_ADDRESS_MODE   USB_LATE_SET_ADDRESS

The address can be changed immediately upon packet reception.

Definition at line 47 of file hal_usb_lld.h.

#define USB_SET_ADDRESS_ACK_HANDLING   USB_SET_ADDRESS_ACK_SW

Method for set address acknowledge.

Definition at line 52 of file hal_usb_lld.h.

#define PLATFORM_USB_USE_USB1   FALSE

USB driver enable switch.

If set to TRUE the support for USB1 is included.

Note
The default is FALSE.

Definition at line 68 of file hal_usb_lld.h.

#define usb_lld_get_frame_number (   usbp)    0

Returns the current frame number.

Parameters
[in]usbppointer to the USBDriver object
Returns
The current frame number.
Function Class:Not an API, this function is for internal use only.

Definition at line 306 of file hal_usb_lld.h.

#define usb_lld_get_transaction_size (   usbp,
  ep 
)    ((usbp)->epc[ep]->out_state->rxcnt)

Returns the exact size of a receive transaction.

The received size can be different from the size specified in usbStartReceiveI() because the last packet could have a size different from the expected one.

Precondition
The OUT endpoint must have been configured in transaction mode in order to use this function.
Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
Received data size.
Function Class:Not an API, this function is for internal use only.

Definition at line 322 of file hal_usb_lld.h.

#define usb_lld_connect_bus (   usbp)

Connects the USB device.

Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 330 of file hal_usb_lld.h.

#define usb_lld_disconnect_bus (   usbp)

Disconnect the USB device.

Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 337 of file hal_usb_lld.h.

#define usb_lld_wakeup_host (   usbp)

Start of host wake-up procedure.

Function Class:Not an API, this function is for internal use only.

Definition at line 344 of file hal_usb_lld.h.

Referenced by usbWakeupHost().

Typedef Documentation

typedef struct USBDriver USBDriver

Type of a structure representing an USB driver.

Definition at line 259 of file hal_usb.h.

typedef uint8_t usbep_t

Type of an endpoint identifier.

Definition at line 264 of file hal_usb.h.

typedef void(* usbcallback_t) (USBDriver *usbp)

Type of an USB generic notification callback.

Parameters
[in]usbppointer to the USBDriver object triggering the callback

Definition at line 333 of file hal_usb.h.

typedef void(* usbepcallback_t) (USBDriver *usbp, usbep_t ep)

Type of an USB endpoint callback.

Parameters
[in]usbppointer to the USBDriver object triggering the callback
[in]ependpoint number

Definition at line 342 of file hal_usb.h.

typedef void(* usbeventcb_t) (USBDriver *usbp, usbevent_t event)

Type of an USB event notification callback.

Parameters
[in]usbppointer to the USBDriver object triggering the callback
[in]eventevent type

Definition at line 351 of file hal_usb.h.

typedef bool(* usbreqhandler_t) (USBDriver *usbp)

Type of a requests handler callback.

The request is encoded in the usb_setup buffer.

Parameters
[in]usbppointer to the USBDriver object triggering the callback
Returns
The request handling exit code.
Return values
falseRequest not recognized by the handler.
trueRequest handled.

Definition at line 363 of file hal_usb.h.

typedef const USBDescriptor*(* usbgetdescriptor_t) (USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang)

Type of an USB descriptor-retrieving callback.

Definition at line 368 of file hal_usb.h.

Enumeration Type Documentation

enum usbstate_t

Type of a driver state machine possible states.

Enumerator
USB_UNINIT 

Not initialized.

USB_STOP 

Stopped.

USB_READY 

Ready, after bus reset.

USB_SELECTED 

Address assigned.

USB_ACTIVE 

Active, configuration selected.

USB_SUSPENDED 

Suspended, low power mode.

Definition at line 269 of file hal_usb.h.

Type of an endpoint status.

Enumerator
EP_STATUS_DISABLED 

Endpoint not active.

EP_STATUS_STALLED 

Endpoint opened but stalled.

EP_STATUS_ACTIVE 

Active endpoint.

Definition at line 281 of file hal_usb.h.

Type of an endpoint zero state machine states.

Enumerator
USB_EP0_STP_WAITING 

Waiting for SETUP data.

USB_EP0_IN_TX 

Transmitting.

USB_EP0_IN_WAITING_TX0 

Waiting transmit 0.

USB_EP0_IN_SENDING_STS 

Sending status.

USB_EP0_OUT_WAITING_STS 

Waiting status.

USB_EP0_OUT_RX 

Receiving.

USB_EP0_ERROR 

Error, EP0 stalled.

Definition at line 290 of file hal_usb.h.

enum usbevent_t

Type of an enumeration of the possible USB events.

Enumerator
USB_EVENT_RESET 

Driver has been reset by host.

USB_EVENT_ADDRESS 

Address assigned.

USB_EVENT_CONFIGURED 

Configuration selected.

USB_EVENT_UNCONFIGURED 

Configuration removed.

USB_EVENT_SUSPEND 

Entering suspend mode.

USB_EVENT_WAKEUP 

Leaving suspend mode.

USB_EVENT_STALLED 

Endpoint 0 error, stalled.

Definition at line 303 of file hal_usb.h.

Function Documentation

static void set_address ( USBDriver usbp)
static

SET ADDRESS transaction callback.

Parameters
[in]usbppointer to the USBDriver object

Definition at line 64 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::address, USBDriver::setup, USBDriver::state, USB_EVENT_ADDRESS, usb_lld_set_address(), and USB_SELECTED.

Referenced by default_handler().

Here is the call graph for this function:

static bool default_handler ( USBDriver usbp)
static

Standard requests handler.

This is the standard requests default handler, most standard requests are handled here, the user can override the standard handling using the requests_hook_cb hook in the USBConfig structure.

Parameters
[in]usbppointer to the USBDriver object
Returns
The request handling exit code.
Return values
falseRequest not recognized by the handler or error.
trueRequest handled.

Definition at line 84 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::config, USBDriver::configuration, EP_STATUS_ACTIVE, EP_STATUS_DISABLED, EP_STATUS_STALLED, USBConfig::get_descriptor_cb, osalSysLockFromISR(), osalSysUnlockFromISR(), set_address(), USBDriver::setup, USBDriver::state, USBDriver::status, USBDescriptor::ud_size, USBDescriptor::ud_string, USB_ACTIVE, USB_EVENT_CONFIGURED, USB_EVENT_UNCONFIGURED, usb_lld_clear_in(), usb_lld_clear_out(), usb_lld_get_status_in(), usb_lld_get_status_out(), usb_lld_stall_in(), usb_lld_stall_out(), USB_SELECTED, usbDisableEndpointsI(), and usbSetupTransfer.

Referenced by _usb_ep0setup().

Here is the call graph for this function:

void usbInit ( void  )

USB Driver initialization.

Note
This function is implicitly invoked by halInit(), there is no need to explicitly initialize the driver.
Function Class:Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 270 of file hal_usb.c.

References usb_lld_init().

Referenced by halInit().

Here is the call graph for this function:

void usbObjectInit ( USBDriver usbp)

Initializes the standard part of a USBDriver structure.

Parameters
[out]usbppointer to the USBDriver object
Function Class:Initializer, this function just initializes an object and can be invoked before the kernel is initialized.

Definition at line 282 of file hal_usb.c.

References USBDriver::config, USBDriver::in_params, USBDriver::out_params, USBDriver::receiving, USBDriver::state, USBDriver::transmitting, USB_MAX_ENDPOINTS, and USB_STOP.

Referenced by usb_lld_init().

void usbStart ( USBDriver usbp,
const USBConfig config 
)

Configures and activates the USB peripheral.

Parameters
[in]usbppointer to the USBDriver object
[in]configpointer to the USBConfig object
Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 303 of file hal_usb.c.

References USBDriver::config, USBDriver::epc, osalDbgAssert, osalDbgCheck, osalSysLock(), osalSysUnlock(), USBDriver::state, usb_lld_start(), USB_MAX_ENDPOINTS, USB_READY, and USB_STOP.

Here is the call graph for this function:

void usbStop ( USBDriver usbp)

Deactivates the USB peripheral.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 327 of file hal_usb.c.

References USBDriver::config, USBDriver::epc, USBEndpointConfig::in_state, osalDbgAssert, osalDbgCheck, osalOsRescheduleS(), osalSysLock(), osalSysUnlock(), osalThreadResumeI(), USBEndpointConfig::out_state, USBDriver::state, USBInEndpointState::thread, USBOutEndpointState::thread, USB_ACTIVE, usb_lld_stop(), USB_MAX_ENDPOINTS, USB_READY, USB_SELECTED, USB_STOP, and USB_SUSPENDED.

Here is the call graph for this function:

void usbInitEndpointI ( USBDriver usbp,
usbep_t  ep,
const USBEndpointConfig epcp 
)

Enables an endpoint.

This function enables an endpoint, both IN and/or OUT directions depending on the configuration structure.

Note
This function must be invoked in response of a SET_CONFIGURATION or SET_INTERFACE message.
Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
[in]epcpthe endpoint configuration
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 375 of file hal_usb.c.

References USBDriver::epc, USBEndpointConfig::in_state, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, USBEndpointConfig::out_state, USBDriver::state, USB_ACTIVE, and usb_lld_init_endpoint().

Here is the call graph for this function:

void usbDisableEndpointsI ( USBDriver usbp)

Disables all the active endpoints.

This function disables all the active endpoints except the endpoint zero.

Note
This function must be invoked in response of a SET_CONFIGURATION message with configuration number zero.
Parameters
[in]usbppointer to the USBDriver object
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 410 of file hal_usb.c.

References USBDriver::epc, USBEndpointConfig::in_state, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, osalThreadResumeI(), USBEndpointConfig::out_state, USBDriver::receiving, USBDriver::state, USBInEndpointState::thread, USBOutEndpointState::thread, USBDriver::transmitting, USB_ACTIVE, usb_lld_disable_endpoints(), and USB_MAX_ENDPOINTS.

Referenced by default_handler().

Here is the call graph for this function:

void usbStartReceiveI ( USBDriver usbp,
usbep_t  ep,
uint8_t *  buf,
size_t  n 
)

Starts a receive transaction on an OUT endpoint.

Note
This function is meant to be called from ISR context outside critical zones because there is a potentially slow operation inside.
Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
[out]bufbuffer where to copy the received data
[in]ntransaction size. It is recommended a multiple of the packet size because the excess is discarded.
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 453 of file hal_usb.c.

References USBDriver::epc, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, USBEndpointConfig::out_state, USBDriver::receiving, USBOutEndpointState::rxbuf, USBOutEndpointState::rxcnt, USBOutEndpointState::rxsize, USBOutEndpointState::thread, usb_lld_start_out(), USB_MAX_ENDPOINTS, and usbGetReceiveStatusI.

Referenced by _usb_ep0in(), _usb_ep0setup(), and usbReceive().

Here is the call graph for this function:

void usbStartTransmitI ( USBDriver usbp,
usbep_t  ep,
const uint8_t *  buf,
size_t  n 
)

Starts a transmit transaction on an IN endpoint.

Note
This function is meant to be called from ISR context outside critical zones because there is a potentially slow operation inside.
Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
[in]bufbuffer where to fetch the data to be transmitted
[in]ntransaction size
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 492 of file hal_usb.c.

References USBDriver::epc, USBEndpointConfig::in_state, osalDbgAssert, osalDbgCheck, osalDbgCheckClassI, USBInEndpointState::thread, USBDriver::transmitting, USBInEndpointState::txbuf, USBInEndpointState::txcnt, USBInEndpointState::txsize, usb_lld_start_in(), USB_MAX_ENDPOINTS, and usbGetTransmitStatusI.

Referenced by _usb_ep0in(), _usb_ep0out(), _usb_ep0setup(), obnotify(), sduDataTransmitted(), sduSOFHookI(), and usbTransmit().

Here is the call graph for this function:

msg_t usbReceive ( USBDriver usbp,
usbep_t  ep,
uint8_t *  buf,
size_t  n 
)

Performs a receive transaction on an OUT endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
[out]bufbuffer where to copy the received data
[in]ntransaction size. It is recommended a multiple of the packet size because the excess is discarded.
Returns
The received effective data size, it can be less than the amount specified.
Return values
MSG_RESETdriver not in USB_ACTIVE state or the operation has been aborted by an USB reset or a transition to the USB_SUSPENDED state.
Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 536 of file hal_usb.c.

References USBDriver::epc, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), USBEndpointConfig::out_state, USBOutEndpointState::thread, USB_ACTIVE, usbGetDriverStateI, and usbStartReceiveI().

Here is the call graph for this function:

msg_t usbTransmit ( USBDriver usbp,
usbep_t  ep,
const uint8_t *  buf,
size_t  n 
)

Performs a transmit transaction on an IN endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
[in]bufbuffer where to fetch the data to be transmitted
[in]ntransaction size
Returns
The operation status.
Return values
MSG_OKoperation performed successfully.
MSG_RESETdriver not in USB_ACTIVE state or the operation has been aborted by an USB reset or a transition to the USB_SUSPENDED state.
Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 569 of file hal_usb.c.

References USBDriver::epc, USBEndpointConfig::in_state, osalSysLock(), osalSysUnlock(), osalThreadSuspendS(), USBInEndpointState::thread, USB_ACTIVE, usbGetDriverStateI, and usbStartTransmitI().

Here is the call graph for this function:

bool usbStallReceiveI ( USBDriver usbp,
usbep_t  ep 
)

Stalls an OUT endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
The operation status.
Return values
falseEndpoint stalled.
trueEndpoint busy, not stalled.
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 599 of file hal_usb.c.

References osalDbgCheck, osalDbgCheckClassI, usb_lld_stall_out(), and usbGetReceiveStatusI.

Here is the call graph for this function:

bool usbStallTransmitI ( USBDriver usbp,
usbep_t  ep 
)

Stalls an IN endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
The operation status.
Return values
falseEndpoint stalled.
trueEndpoint busy, not stalled.
Function Class:This is an I-Class API, this function can be invoked from within a system lock zone by both threads and interrupt handlers.

Definition at line 624 of file hal_usb.c.

References osalDbgCheck, osalDbgCheckClassI, usb_lld_stall_in(), and usbGetTransmitStatusI.

Here is the call graph for this function:

void usbWakeupHost ( USBDriver usbp)

Host wake-up procedure.

Note
It is silently ignored if the USB device is not in the USB_SUSPENDED state.
Parameters
[in]usbppointer to the USBDriver object
Function Class:Normal API, this function can be invoked by regular system threads but not from within a lock zone.

Definition at line 646 of file hal_usb.c.

References USBDriver::state, usb_lld_wakeup_host, and USB_SUSPENDED.

void _usb_reset ( USBDriver usbp)

USB reset routine.

This function must be invoked when an USB bus reset condition is detected.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 663 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::address, USBDriver::configuration, USBDriver::ep0state, USBDriver::epc, USBEndpointConfig::in_state, osalSysLockFromISR(), osalSysUnlockFromISR(), osalThreadResumeI(), USBEndpointConfig::out_state, USBDriver::receiving, USBDriver::state, USBDriver::status, USBInEndpointState::thread, USBOutEndpointState::thread, USBDriver::transmitting, USB_EP0_STP_WAITING, USB_EVENT_RESET, usb_lld_reset(), USB_MAX_ENDPOINTS, and USB_READY.

Here is the call graph for this function:

void _usb_suspend ( USBDriver usbp)

USB suspend routine.

This function must be invoked when an USB bus suspend condition is detected.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 713 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::epc, USBEndpointConfig::in_state, osalSysLockFromISR(), osalSysUnlockFromISR(), osalThreadResumeI(), USBEndpointConfig::out_state, USBDriver::saved_state, USBDriver::state, USBInEndpointState::thread, USBOutEndpointState::thread, USB_EVENT_SUSPEND, USB_MAX_ENDPOINTS, and USB_SUSPENDED.

Here is the call graph for this function:

void _usb_wakeup ( USBDriver usbp)

USB wake-up routine.

This function must be invoked when an USB bus wake-up condition is detected.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 753 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::saved_state, USBDriver::state, and USB_EVENT_WAKEUP.

void _usb_ep0setup ( USBDriver usbp,
usbep_t  ep 
)

Default EP0 SETUP callback.

This function is used by the low level driver as default handler for EP0 SETUP events.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number, always zero
Function Class:Not an API, this function is for internal use only.

Definition at line 772 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::config, default_handler(), USBDriver::ep0n, USBDriver::ep0next, USBDriver::ep0state, osalSysLockFromISR(), osalSysUnlockFromISR(), USBConfig::requests_hook_cb, USBDriver::setup, USB_EP0_ERROR, USB_EP0_IN_SENDING_STS, USB_EP0_IN_TX, USB_EP0_OUT_RX, USB_EP0_OUT_WAITING_STS, USB_EP0_STP_WAITING, USB_EVENT_STALLED, usb_lld_stall_in(), usb_lld_stall_out(), usbReadSetup, usbStartReceiveI(), and usbStartTransmitI().

Here is the call graph for this function:

void _usb_ep0in ( USBDriver usbp,
usbep_t  ep 
)

Default EP0 IN callback.

This function is used by the low level driver as default handler for EP0 IN events.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number, always zero
Function Class:Not an API, this function is for internal use only.

Definition at line 880 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::ep0endcb, USBDriver::ep0n, USBDriver::ep0state, USBDriver::epc, USBEndpointConfig::in_maxsize, osalDbgAssert, osalSysLockFromISR(), osalSysUnlockFromISR(), USBDriver::setup, USB_EP0_ERROR, USB_EP0_IN_SENDING_STS, USB_EP0_IN_TX, USB_EP0_IN_WAITING_TX0, USB_EP0_OUT_RX, USB_EP0_OUT_WAITING_STS, USB_EP0_STP_WAITING, USB_EVENT_STALLED, usb_lld_stall_in(), usb_lld_stall_out(), usbStartReceiveI(), and usbStartTransmitI().

Here is the call graph for this function:

void _usb_ep0out ( USBDriver usbp,
usbep_t  ep 
)

Default EP0 OUT callback.

This function is used by the low level driver as default handler for EP0 OUT events.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number, always zero
Function Class:Not an API, this function is for internal use only.

Definition at line 947 of file hal_usb.c.

References _usb_isr_invoke_event_cb, USBDriver::ep0endcb, USBDriver::ep0state, osalDbgAssert, osalSysLockFromISR(), osalSysUnlockFromISR(), USB_EP0_ERROR, USB_EP0_IN_SENDING_STS, USB_EP0_IN_TX, USB_EP0_IN_WAITING_TX0, USB_EP0_OUT_RX, USB_EP0_OUT_WAITING_STS, USB_EP0_STP_WAITING, USB_EVENT_STALLED, usb_lld_stall_in(), usb_lld_stall_out(), usbGetReceiveTransactionSizeX, and usbStartTransmitI().

Here is the call graph for this function:

void usb_lld_init ( void  )

Low level USB driver initialization.

Function Class:Not an API, this function is for internal use only.

Definition at line 99 of file hal_usb_lld.c.

References usbObjectInit().

Referenced by usbInit().

Here is the call graph for this function:

void usb_lld_start ( USBDriver usbp)

Configures and activates the USB peripheral.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 114 of file hal_usb_lld.c.

References USBDriver::state, and USB_STOP.

Referenced by usbStart().

void usb_lld_stop ( USBDriver usbp)

Deactivates the USB peripheral.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 135 of file hal_usb_lld.c.

References USBDriver::state, and USB_READY.

Referenced by usbStop().

void usb_lld_reset ( USBDriver usbp)

USB low level reset routine.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 156 of file hal_usb_lld.c.

References ep0config, USBDriver::epc, and usb_lld_init_endpoint().

Referenced by _usb_reset().

Here is the call graph for this function:

void usb_lld_set_address ( USBDriver usbp)

Sets the USB address.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 172 of file hal_usb_lld.c.

Referenced by set_address().

void usb_lld_init_endpoint ( USBDriver usbp,
usbep_t  ep 
)

Enables an endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 186 of file hal_usb_lld.c.

Referenced by usb_lld_reset(), and usbInitEndpointI().

void usb_lld_disable_endpoints ( USBDriver usbp)

Disables all the active endpoints except the endpoint zero.

Parameters
[in]usbppointer to the USBDriver object
Function Class:Not an API, this function is for internal use only.

Definition at line 200 of file hal_usb_lld.c.

Referenced by usbDisableEndpointsI().

usbepstatus_t usb_lld_get_status_out ( USBDriver usbp,
usbep_t  ep 
)

Returns the status of an OUT endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
The endpoint status.
Return values
EP_STATUS_DISABLEDThe endpoint is not active.
EP_STATUS_STALLEDThe endpoint is stalled.
EP_STATUS_ACTIVEThe endpoint is active.
Function Class:Not an API, this function is for internal use only.

Definition at line 218 of file hal_usb_lld.c.

References EP_STATUS_DISABLED.

Referenced by default_handler().

usbepstatus_t usb_lld_get_status_in ( USBDriver usbp,
usbep_t  ep 
)

Returns the status of an IN endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Returns
The endpoint status.
Return values
EP_STATUS_DISABLEDThe endpoint is not active.
EP_STATUS_STALLEDThe endpoint is stalled.
EP_STATUS_ACTIVEThe endpoint is active.
Function Class:Not an API, this function is for internal use only.

Definition at line 238 of file hal_usb_lld.c.

References EP_STATUS_DISABLED.

Referenced by default_handler().

void usb_lld_read_setup ( USBDriver usbp,
usbep_t  ep,
uint8_t *  buf 
)

Reads a setup packet from the dedicated packet buffer.

This function must be invoked in the context of the setup_cb callback in order to read the received setup packet.

Precondition
In order to use this function the endpoint must have been initialized as a control endpoint.
Postcondition
The endpoint is ready to accept another packet.
Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
[out]bufbuffer where to copy the packet data
Function Class:Not an API, this function is for internal use only.

Definition at line 260 of file hal_usb_lld.c.

void usb_lld_prepare_receive ( USBDriver usbp,
usbep_t  ep 
)

Prepares for a receive operation.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 276 of file hal_usb_lld.c.

void usb_lld_prepare_transmit ( USBDriver usbp,
usbep_t  ep 
)

Prepares for a transmit operation.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 291 of file hal_usb_lld.c.

void usb_lld_start_out ( USBDriver usbp,
usbep_t  ep 
)

Starts a receive operation on an OUT endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 306 of file hal_usb_lld.c.

Referenced by usbStartReceiveI().

void usb_lld_start_in ( USBDriver usbp,
usbep_t  ep 
)

Starts a transmit operation on an IN endpoint.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 321 of file hal_usb_lld.c.

Referenced by usbStartTransmitI().

void usb_lld_stall_out ( USBDriver usbp,
usbep_t  ep 
)

Brings an OUT endpoint in the stalled state.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 336 of file hal_usb_lld.c.

Referenced by _usb_ep0in(), _usb_ep0out(), _usb_ep0setup(), default_handler(), and usbStallReceiveI().

void usb_lld_stall_in ( USBDriver usbp,
usbep_t  ep 
)

Brings an IN endpoint in the stalled state.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 351 of file hal_usb_lld.c.

Referenced by _usb_ep0in(), _usb_ep0out(), _usb_ep0setup(), default_handler(), and usbStallTransmitI().

void usb_lld_clear_out ( USBDriver usbp,
usbep_t  ep 
)

Brings an OUT endpoint in the active state.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 366 of file hal_usb_lld.c.

Referenced by default_handler().

void usb_lld_clear_in ( USBDriver usbp,
usbep_t  ep 
)

Brings an IN endpoint in the active state.

Parameters
[in]usbppointer to the USBDriver object
[in]ependpoint number
Function Class:Not an API, this function is for internal use only.

Definition at line 381 of file hal_usb_lld.c.

Referenced by default_handler().

Variable Documentation

USBDriver USBD1

USB1 driver identifier.

Definition at line 41 of file hal_usb_lld.c.

union { ... } ep0_state

EP0 state.

Note
It is an union because IN and OUT endpoints are never used at the same time for EP0.
USBInEndpointState { ... } in

IN EP0 state.

Definition at line 57 of file hal_usb_lld.c.

USBOutEndpointState { ... } out

OUT EP0 state.

Definition at line 61 of file hal_usb_lld.c.

const USBEndpointConfig ep0config
static
Initial value:
= {
0x40,
0x40,
&ep0_state.in,
&ep0_state.out
}
void _usb_ep0setup(USBDriver *usbp, usbep_t ep)
Default EP0 SETUP callback.
Definition: hal_usb.c:772
#define USB_EP_MODE_TYPE_CTRL
Definition: hal_usb.h:227
static union @11 ep0_state
EP0 state.
void _usb_ep0in(USBDriver *usbp, usbep_t ep)
Default EP0 IN callback.
Definition: hal_usb.c:880
void _usb_ep0out(USBDriver *usbp, usbep_t ep)
Default EP0 OUT callback.
Definition: hal_usb.c:947

EP0 initialization structure.

Definition at line 67 of file hal_usb_lld.c.

Referenced by usb_lld_reset().