|
ChibiOS/RT
2.5.1 |
|
Generic USB Driver.
This module implements a generic USB (Universal Serial Bus) driver supporting device-mode operations.
HAL_USE_USB option must be enabled in halconf.h.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).
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.
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 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:
States diagram for IN endpoints in transaction mode:
The USB driver uses callbacks in order to interact with the application. There are several kinds of callbacks to be handled:
Data Structures | |
| struct | USBDescriptor |
| Type of an USB descriptor. More... | |
| struct | stm32_usb_t |
| USB registers block. More... | |
| struct | stm32_usb_descriptor_t |
| USB descriptor registers block. 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 | |
| void | usbInit (void) |
| USB Driver initialization. | |
| void | usbObjectInit (USBDriver *usbp) |
Initializes the standard part of a USBDriver structure. | |
| void | usbStart (USBDriver *usbp, const USBConfig *config) |
| Configures and activates the USB peripheral. | |
| void | usbStop (USBDriver *usbp) |
| Deactivates the USB peripheral. | |
| void | usbInitEndpointI (USBDriver *usbp, usbep_t ep, const USBEndpointConfig *epcp) |
| Enables an endpoint. | |
| void | usbDisableEndpointsI (USBDriver *usbp) |
| Disables all the active endpoints. | |
| void | usbPrepareReceive (USBDriver *usbp, usbep_t ep, uint8_t *buf, size_t n) |
| Prepares for a receive transaction on an OUT endpoint. | |
| void | usbPrepareTransmit (USBDriver *usbp, usbep_t ep, const uint8_t *buf, size_t n) |
| Prepares for a transmit transaction on an IN endpoint. | |
| void | usbPrepareQueuedReceive (USBDriver *usbp, usbep_t ep, InputQueue *iqp, size_t n) |
| Prepares for a receive transaction on an OUT endpoint. | |
| void | usbPrepareQueuedTransmit (USBDriver *usbp, usbep_t ep, OutputQueue *oqp, size_t n) |
| Prepares for a transmit transaction on an IN endpoint. | |
| bool_t | usbStartReceiveI (USBDriver *usbp, usbep_t ep) |
| Starts a receive transaction on an OUT endpoint. | |
| bool_t | usbStartTransmitI (USBDriver *usbp, usbep_t ep) |
| Starts a transmit transaction on an IN endpoint. | |
| bool_t | usbStallReceiveI (USBDriver *usbp, usbep_t ep) |
| Stalls an OUT endpoint. | |
| bool_t | usbStallTransmitI (USBDriver *usbp, usbep_t ep) |
| Stalls an IN endpoint. | |
| void | _usb_reset (USBDriver *usbp) |
| USB reset routine. | |
| void | _usb_ep0setup (USBDriver *usbp, usbep_t ep) |
| Default EP0 SETUP callback. | |
| void | _usb_ep0in (USBDriver *usbp, usbep_t ep) |
| Default EP0 IN callback. | |
| void | _usb_ep0out (USBDriver *usbp, usbep_t ep) |
| Default EP0 OUT callback. | |
| CH_IRQ_HANDLER (STM32_USB1_HP_HANDLER) | |
| USB high priority interrupt handler. | |
| CH_IRQ_HANDLER (STM32_USB1_LP_HANDLER) | |
| USB low priority interrupt handler. | |
| void | usb_lld_init (void) |
| Low level USB driver initialization. | |
| void | usb_lld_start (USBDriver *usbp) |
| Configures and activates the USB peripheral. | |
| void | usb_lld_stop (USBDriver *usbp) |
| Deactivates the USB peripheral. | |
| void | usb_lld_reset (USBDriver *usbp) |
| USB low level reset routine. | |
| void | usb_lld_set_address (USBDriver *usbp) |
| Sets the USB address. | |
| void | usb_lld_init_endpoint (USBDriver *usbp, usbep_t ep) |
| Enables an endpoint. | |
| void | usb_lld_disable_endpoints (USBDriver *usbp) |
| Disables all the active endpoints except the endpoint zero. | |
| usbepstatus_t | usb_lld_get_status_out (USBDriver *usbp, usbep_t ep) |
| Returns the status of an OUT endpoint. | |
| usbepstatus_t | usb_lld_get_status_in (USBDriver *usbp, usbep_t ep) |
| Returns the status of an IN endpoint. | |
| void | usb_lld_read_setup (USBDriver *usbp, usbep_t ep, uint8_t *buf) |
| Reads a setup packet from the dedicated packet buffer. | |
| void | usb_lld_prepare_receive (USBDriver *usbp, usbep_t ep) |
| Prepares for a receive operation. | |
| void | usb_lld_prepare_transmit (USBDriver *usbp, usbep_t ep) |
| Prepares for a transmit operation. | |
| void | usb_lld_start_out (USBDriver *usbp, usbep_t ep) |
| Starts a receive operation on an OUT endpoint. | |
| void | usb_lld_start_in (USBDriver *usbp, usbep_t ep) |
| Starts a transmit operation on an IN endpoint. | |
| void | usb_lld_stall_out (USBDriver *usbp, usbep_t ep) |
| Brings an OUT endpoint in the stalled state. | |
| void | usb_lld_stall_in (USBDriver *usbp, usbep_t ep) |
| Brings an IN endpoint in the stalled state. | |
| void | usb_lld_clear_out (USBDriver *usbp, usbep_t ep) |
| Brings an OUT endpoint in the active state. | |
| void | usb_lld_clear_in (USBDriver *usbp, usbep_t ep) |
| Brings an IN endpoint in the active state. | |
Variables | |
| USBDriver | USBD1 |
| USB1 driver identifier. | |
Helper macros for USB descriptors | |
| #define | USB_DESC_INDEX(i) ((uint8_t)(i)) |
| Helper macro for index values into descriptor strings. | |
| #define | USB_DESC_BYTE(b) ((uint8_t)(b)) |
| Helper macro for byte values into descriptor strings. | |
| #define | USB_DESC_WORD(w) |
| Helper macro for word values into descriptor strings. | |
| #define | USB_DESC_BCD(bcd) |
| Helper macro for BCD values into descriptor strings. | |
| #define | USB_DESC_DEVICE(bcdUSB, bDeviceClass, bDeviceSubClass,bDeviceProtocol, bMaxPacketSize, idVendor,idProduct, bcdDevice, iManufacturer,iProduct, iSerialNumber, bNumConfigurations) |
| Device Descriptor helper macro. | |
| #define | USB_DESC_CONFIGURATION(wTotalLength, bNumInterfaces,bConfigurationValue, iConfiguration,bmAttributes, bMaxPower) |
| Configuration Descriptor helper macro. | |
| #define | USB_DESC_INTERFACE(bInterfaceNumber, bAlternateSetting,bNumEndpoints, bInterfaceClass,bInterfaceSubClass, bInterfaceProtocol,iInterface) |
| Interface Descriptor helper macro. | |
| #define | USB_DESC_INTERFACE_ASSOCIATION(bFirstInterface,bInterfaceCount, bFunctionClass,bFunctionSubClass, bFunctionProcotol,iInterface) |
| Interface Association Descriptor helper macro. | |
| #define | USB_DESC_ENDPOINT(bEndpointAddress, bmAttributes, wMaxPacketSize,bInterval) |
| Endpoint Descriptor helper macro. | |
Endpoint types and settings | |
| #define | USB_EP_MODE_TYPE 0x0003 |
| #define | USB_EP_MODE_TYPE_CTRL 0x0000 |
| #define | USB_EP_MODE_TYPE_ISOC 0x0001 |
| #define | USB_EP_MODE_TYPE_BULK 0x0002 |
| #define | USB_EP_MODE_TYPE_INTR 0x0003 |
| #define | USB_EP_MODE_LINEAR_BUFFER 0x0000 |
| #define | USB_EP_MODE_QUEUE_BUFFER 0x0010 |
Macro Functions | |
| #define | usbGetDriverStateI(usbp) ((usbp)->state) |
| Returns the driver state. | |
| #define | usbFetchWord(p) ((uint16_t)*(p) | ((uint16_t)*((p) + 1) << 8)) |
| Fetches a 16 bits word value from an USB message. | |
| #define | usbConnectBus(usbp) usb_lld_connect_bus(usbp) |
| Connects the USB device. | |
| #define | usbDisconnectBus(usbp) usb_lld_disconnect_bus(usbp) |
| Disconnect the USB device. | |
| #define | usbGetFrameNumber(usbp) usb_lld_get_frame_number(usbp) |
| Returns the current frame number. | |
| #define | usbGetTransmitStatusI(usbp, ep) ((usbp)->transmitting & (1 << (ep))) |
| Returns the status of an IN endpoint. | |
| #define | usbGetReceiveStatusI(usbp, ep) ((usbp)->receiving & (1 << (ep))) |
| Returns the status of an OUT endpoint. | |
| #define | usbGetReceiveTransactionSizeI(usbp, ep) usb_lld_get_transaction_size(usbp, ep) |
| Returns the exact size of a receive transaction. | |
| #define | usbSetupTransfer(usbp, buf, n, endcb) |
| Request transfer setup. | |
| #define | usbReadSetup(usbp, ep, buf) usb_lld_read_setup(usbp, ep, buf) |
| Reads a setup packet from the dedicated packet buffer. | |
Low Level driver helper macros | |
| #define | _usb_isr_invoke_event_cb(usbp, evt) |
| Common ISR code, usb event callback. | |
| #define | _usb_isr_invoke_sof_cb(usbp) |
| Common ISR code, SOF callback. | |
| #define | _usb_isr_invoke_setup_cb(usbp, ep) |
| Common ISR code, setup packet callback. | |
| #define | _usb_isr_invoke_in_cb(usbp, ep) |
| Common ISR code, IN endpoint callback. | |
| #define | _usb_isr_invoke_out_cb(usbp, ep) |
| Common ISR code, OUT endpoint event. | |
Register aliases | |
| #define | RXADDR1 TXADDR0 |
| #define | TXADDR1 RXADDR0 |
Defines | |
| #define | USB_ENDOPOINTS_NUMBER 7 |
| Number of the available endpoints. | |
| #define | STM32_USB_BASE (APB1PERIPH_BASE + 0x5C00) |
| USB registers block numeric address. | |
| #define | STM32_USBRAM_BASE (APB1PERIPH_BASE + 0x6000) |
| USB RAM numeric address. | |
| #define | STM32_USB ((stm32_usb_t *)STM32_USB_BASE) |
| Pointer to the USB registers block. | |
| #define | STM32_USBRAM ((uint32_t *)STM32_USBRAM_BASE) |
| Pointer to the USB RAM. | |
| #define | USB_PMA_SIZE 512 |
| Size of the dedicated packet memory. | |
| #define | EPR_TOGGLE_MASK |
| Mask of all the toggling bits in the EPR register. | |
| #define | USB_GET_DESCRIPTOR(ep) |
| Returns an endpoint descriptor pointer. | |
| #define | USB_ADDR2PTR(addr) ((uint32_t *)((addr) * 2 + STM32_USBRAM_BASE)) |
| Converts from a PMA address to a physical address. | |
| #define | USB_MAX_ENDPOINTS USB_ENDOPOINTS_NUMBER |
| Maximum endpoint address. | |
| #define | USB_SET_ADDRESS_MODE USB_LATE_SET_ADDRESS |
| This device requires the address change after the status packet. | |
| #define | STM32_USB_USE_USB1 FALSE |
| USB1 driver enable switch. | |
| #define | STM32_USB_LOW_POWER_ON_SUSPEND FALSE |
| Enables the USB device low power mode on suspend. | |
| #define | STM32_USB_USB1_HP_IRQ_PRIORITY 13 |
| USB1 interrupt priority level setting. | |
| #define | STM32_USB_USB1_LP_IRQ_PRIORITY 14 |
| USB1 interrupt priority level setting. | |
| #define | usb_lld_get_frame_number(usbp) (STM32_USB->FNR & FNR_FN_MASK) |
| Returns the current frame number. | |
| #define | usb_lld_get_transaction_size(usbp, ep) ((usbp)->epc[ep]->out_state->rxcnt) |
| Returns the exact size of a receive transaction. | |
| #define | usb_lld_get_packet_size(usbp, ep) ((size_t)USB_GET_DESCRIPTOR(ep)->RXCOUNT & RXCOUNT_COUNT_MASK) |
| Returns the exact size of a received packet. | |
Typedefs | |
| typedef struct USBDriver | USBDriver |
| Type of a structure representing an USB driver. | |
| typedef uint8_t | usbep_t |
| Type of an endpoint identifier. | |
| typedef void(* | usbcallback_t )(USBDriver *usbp) |
| Type of an USB generic notification callback. | |
| typedef void(* | usbepcallback_t )(USBDriver *usbp, usbep_t ep) |
| Type of an USB endpoint callback. | |
| typedef void(* | usbeventcb_t )(USBDriver *usbp, usbevent_t event) |
| Type of an USB event notification callback. | |
| typedef bool_t(* | usbreqhandler_t )(USBDriver *usbp) |
| Type of a requests handler callback. | |
| typedef const USBDescriptor *(* | usbgetdescriptor_t )(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang) |
| Type of an USB descriptor-retrieving callback. | |
Enumerations | |
| enum | usbstate_t { USB_UNINIT = 0, USB_STOP = 1, USB_READY = 2, USB_SELECTED = 3, USB_ACTIVE = 4 } |
| Type of a driver state machine possible states. More... | |
| enum | usbepstatus_t { EP_STATUS_DISABLED = 0, EP_STATUS_STALLED = 1, EP_STATUS_ACTIVE = 2 } |
| Type of an endpoint status. More... | |
| enum | usbep0state_t { USB_EP0_WAITING_SETUP, USB_EP0_TX, USB_EP0_WAITING_STS, USB_EP0_RX, USB_EP0_SENDING_STS, USB_EP0_ERROR } |
| Type of an endpoint zero state machine states. More... | |
| enum | usbevent_t { USB_EVENT_RESET = 0, USB_EVENT_ADDRESS = 1, USB_EVENT_CONFIGURED = 2, USB_EVENT_SUSPEND = 3, USB_EVENT_WAKEUP = 4, USB_EVENT_STALLED = 5 } |
| Type of an enumeration of the possible USB events. More... | |
| void usbInit | ( | void | ) |
USB Driver initialization.
halInit(), there is no need to explicitly initialize the driver.Definition at line 228 of file usb.c.
References usb_lld_init().
Referenced by halInit().

| void usbObjectInit | ( | USBDriver * | usbp | ) |
Initializes the standard part of a USBDriver structure.
| [out] | usbp | pointer to the USBDriver object |
Definition at line 240 of file usb.c.
References USBDriver::config, USBDriver::param, USBDriver::receiving, USBDriver::state, USBDriver::transmitting, and USB_STOP.
Referenced by usb_lld_init().
Configures and activates the USB peripheral.
Definition at line 257 of file usb.c.
References USBDriver::config, USBDriver::epc, USBDriver::state, usb_lld_start(), USB_MAX_ENDPOINTS, USB_READY, and USB_STOP.

| void usbStop | ( | USBDriver * | usbp | ) |
Deactivates the USB peripheral.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 280 of file usb.c.
References USBDriver::state, usb_lld_stop(), USB_READY, and USB_STOP.

| 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.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| [in] | epcp | the endpoint configuration |
Definition at line 305 of file usb.c.
References USBDriver::epc, USBEndpointConfig::in_state, USBEndpointConfig::out_state, USBDriver::state, USB_ACTIVE, and usb_lld_init_endpoint().

| void usbDisableEndpointsI | ( | USBDriver * | usbp | ) |
Disables all the active endpoints.
This function disables all the active endpoints except the endpoint zero.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 338 of file usb.c.
References USBDriver::epc, USBDriver::receiving, USBDriver::state, USBDriver::transmitting, usb_lld_disable_endpoints(), USB_MAX_ENDPOINTS, and USB_SELECTED.

Prepares for a receive transaction on an OUT endpoint.
usbStartReceiveI(). | [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| [out] | buf | buffer where to copy the received data |
| [in] | n | transaction size |
Definition at line 367 of file usb.c.
References USBDriver::epc, USBEndpointConfig::out_state, USBOutEndpointState::rxcnt, USBOutEndpointState::rxqueued, USBOutEndpointState::rxsize, and usb_lld_prepare_receive().
Referenced by _usb_ep0in(), and _usb_ep0setup().

Prepares for a transmit transaction on an IN endpoint.
usbStartTransmitI(). | [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| [in] | buf | buffer where to fetch the data to be transmitted |
| [in] | n | transaction size |
Definition at line 392 of file usb.c.
References USBDriver::epc, USBEndpointConfig::in_state, USBInEndpointState::txcnt, USBInEndpointState::txqueued, USBInEndpointState::txsize, and usb_lld_prepare_transmit().
Referenced by _usb_ep0in(), _usb_ep0out(), and _usb_ep0setup().

Prepares for a receive transaction on an OUT endpoint.
usbStartReceiveI(). | [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| [in] | iqp | input queue to be filled with incoming data |
| [in] | n | transaction size |
Definition at line 421 of file usb.c.
References USBDriver::epc, USBEndpointConfig::out_state, USBOutEndpointState::rxcnt, USBOutEndpointState::rxqueued, USBOutEndpointState::rxsize, and usb_lld_prepare_receive().

Prepares for a transmit transaction on an IN endpoint.
usbStartTransmitI(). | [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| [in] | oqp | output queue to be fetched for outgoing data |
| [in] | n | transaction size |
Definition at line 447 of file usb.c.
References USBDriver::epc, USBEndpointConfig::in_state, USBInEndpointState::txcnt, USBInEndpointState::txqueued, USBInEndpointState::txsize, and usb_lld_prepare_transmit().

Starts a receive transaction on an OUT endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| FALSE | Operation started successfully. |
| TRUE | Endpoint busy, operation not started. |
Definition at line 473 of file usb.c.
References USBDriver::receiving, usb_lld_start_out(), and usbGetReceiveStatusI.
Referenced by _usb_ep0in(), and _usb_ep0setup().

Starts a transmit transaction on an IN endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| FALSE | Operation started successfully. |
| TRUE | Endpoint busy, operation not started. |
Definition at line 500 of file usb.c.
References USBDriver::transmitting, usb_lld_start_in(), and usbGetTransmitStatusI.
Referenced by _usb_ep0in(), _usb_ep0out(), and _usb_ep0setup().

Stalls an OUT endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| FALSE | Endpoint stalled. |
| TRUE | Endpoint busy, not stalled. |
Definition at line 525 of file usb.c.
References usb_lld_stall_out(), and usbGetReceiveStatusI.

Stalls an IN endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| FALSE | Endpoint stalled. |
| TRUE | Endpoint busy, not stalled. |
Definition at line 549 of file usb.c.
References usb_lld_stall_in(), and usbGetTransmitStatusI.

| void _usb_reset | ( | USBDriver * | usbp | ) |
USB reset routine.
This function must be invoked when an USB bus reset condition is detected.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 570 of file usb.c.
References USBDriver::address, USBDriver::configuration, USBDriver::ep0state, USBDriver::epc, USBDriver::receiving, USBDriver::state, USBDriver::status, USBDriver::transmitting, USB_EP0_WAITING_SETUP, usb_lld_reset(), USB_MAX_ENDPOINTS, and USB_READY.
Referenced by CH_IRQ_HANDLER(), and usb_lld_start().

Default EP0 SETUP callback.
This function is used by the low level driver as default handler for EP0 SETUP events.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number, always zero |
Definition at line 601 of file usb.c.
References _usb_isr_invoke_event_cb, USBDriver::config, USBDriver::ep0n, USBDriver::ep0next, USBDriver::ep0state, USBConfig::requests_hook_cb, USBDriver::setup, USB_EP0_ERROR, USB_EP0_RX, USB_EP0_SENDING_STS, USB_EP0_TX, USB_EP0_WAITING_SETUP, USB_EP0_WAITING_STS, USB_EVENT_STALLED, usb_lld_stall_in(), usb_lld_stall_out(), usbFetchWord, usbPrepareReceive(), usbPrepareTransmit(), usbReadSetup, usbStartReceiveI(), and usbStartTransmitI().

Default EP0 IN callback.
This function is used by the low level driver as default handler for EP0 IN events.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number, always zero |
Definition at line 685 of file usb.c.
References _usb_isr_invoke_event_cb, USBDriver::ep0endcb, USBDriver::ep0n, USBDriver::ep0state, USBDriver::epc, USBEndpointConfig::in_maxsize, USBDriver::setup, USB_EP0_ERROR, USB_EP0_SENDING_STS, USB_EP0_TX, USB_EP0_WAITING_SETUP, USB_EP0_WAITING_STS, USB_EVENT_STALLED, usb_lld_stall_in(), usb_lld_stall_out(), usbFetchWord, usbPrepareReceive(), usbPrepareTransmit(), usbStartReceiveI(), and usbStartTransmitI().

Default EP0 OUT callback.
This function is used by the low level driver as default handler for EP0 OUT events.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number, always zero |
Definition at line 739 of file usb.c.
References _usb_isr_invoke_event_cb, USBDriver::ep0endcb, USBDriver::ep0state, USB_EP0_ERROR, USB_EP0_RX, USB_EP0_SENDING_STS, USB_EP0_WAITING_SETUP, USB_EP0_WAITING_STS, USB_EVENT_STALLED, usb_lld_stall_in(), usb_lld_stall_out(), usbGetReceiveTransactionSizeI, usbPrepareTransmit(), and usbStartTransmitI().

| CH_IRQ_HANDLER | ( | STM32_USB1_HP_HANDLER | ) |
| CH_IRQ_HANDLER | ( | STM32_USB1_LP_HANDLER | ) |
USB low priority interrupt handler.
Definition at line 291 of file usb_lld.c.
References _usb_isr_invoke_event_cb, _usb_isr_invoke_in_cb, _usb_isr_invoke_out_cb, _usb_isr_invoke_setup_cb, _usb_isr_invoke_sof_cb, _usb_reset(), USBDriver::epc, USBEndpointConfig::in_maxsize, USBEndpointConfig::in_state, USBEndpointConfig::out_state, USBOutEndpointState::rxcnt, stm32_usb_descriptor_t::RXCOUNT0, USBOutEndpointState::rxpkts, USBOutEndpointState::rxqueued, USBOutEndpointState::rxsize, STM32_USB, USBInEndpointState::txcnt, USBInEndpointState::txqueued, USBInEndpointState::txsize, USB_EVENT_RESET, USB_EVENT_SUSPEND, USB_EVENT_WAKEUP, USB_GET_DESCRIPTOR, usb_lld_start_in(), and USBD1.

| void usb_lld_init | ( | void | ) |
Low level USB driver initialization.
Definition at line 433 of file usb_lld.c.
References usbObjectInit().
Referenced by usbInit().

| void usb_lld_start | ( | USBDriver * | usbp | ) |
Configures and activates the USB peripheral.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 446 of file usb_lld.c.
References _usb_reset(), rccEnableUSB, USBDriver::state, STM32_USB, STM32_USB_USB1_HP_IRQ_PRIORITY, STM32_USB_USB1_LP_IRQ_PRIORITY, and USB_STOP.
Referenced by usbStart().

| void usb_lld_stop | ( | USBDriver * | usbp | ) |
Deactivates the USB peripheral.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 479 of file usb_lld.c.
References rccDisableUSB, USBDriver::state, STM32_USB, and USB_STOP.
Referenced by usbStop().
| void usb_lld_reset | ( | USBDriver * | usbp | ) |
USB low level reset routine.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 501 of file usb_lld.c.
References USBDriver::config, USBDriver::epc, USBConfig::sof_cb, STM32_USB, and usb_lld_init_endpoint().
Referenced by _usb_reset().

| void usb_lld_set_address | ( | USBDriver * | usbp | ) |
Sets the USB address.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 531 of file usb_lld.c.
References USBDriver::address, and STM32_USB.
Enables an endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 544 of file usb_lld.c.
References USBEndpointConfig::ep_mode, USBDriver::epc, USBEndpointConfig::in_cb, USBEndpointConfig::in_maxsize, USBEndpointConfig::out_cb, USBEndpointConfig::out_maxsize, stm32_usb_descriptor_t::RXADDR0, stm32_usb_descriptor_t::RXCOUNT0, stm32_usb_descriptor_t::TXADDR0, stm32_usb_descriptor_t::TXCOUNT0, USB_EP_MODE_TYPE, USB_EP_MODE_TYPE_BULK, USB_EP_MODE_TYPE_INTR, USB_EP_MODE_TYPE_ISOC, and USB_GET_DESCRIPTOR.
Referenced by usb_lld_reset(), and usbInitEndpointI().
| void usb_lld_disable_endpoints | ( | USBDriver * | usbp | ) |
Disables all the active endpoints except the endpoint zero.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 596 of file usb_lld.c.
References USB_ENDOPOINTS_NUMBER.
Referenced by usbDisableEndpointsI().
| usbepstatus_t usb_lld_get_status_out | ( | USBDriver * | usbp, |
| usbep_t | ep | ||
| ) |
Returns the status of an OUT endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| EP_STATUS_DISABLED | The endpoint is not active. |
| EP_STATUS_STALLED | The endpoint is stalled. |
| EP_STATUS_ACTIVE | The endpoint is active. |
Definition at line 621 of file usb_lld.c.
References EP_STATUS_ACTIVE, EP_STATUS_DISABLED, EP_STATUS_STALLED, and STM32_USB.
| usbepstatus_t usb_lld_get_status_in | ( | USBDriver * | usbp, |
| usbep_t | ep | ||
| ) |
Returns the status of an IN endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| EP_STATUS_DISABLED | The endpoint is not active. |
| EP_STATUS_STALLED | The endpoint is stalled. |
| EP_STATUS_ACTIVE | The endpoint is active. |
Definition at line 646 of file usb_lld.c.
References EP_STATUS_ACTIVE, EP_STATUS_DISABLED, EP_STATUS_STALLED, and STM32_USB.
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.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| [out] | buf | buffer where to copy the packet data |
Definition at line 673 of file usb_lld.c.
References stm32_usb_descriptor_t::RXADDR0, USB_ADDR2PTR, and USB_GET_DESCRIPTOR.
Prepares for a receive operation.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 695 of file usb_lld.c.
References USBDriver::epc, USBEndpointConfig::out_maxsize, USBEndpointConfig::out_state, USBOutEndpointState::rxpkts, and USBOutEndpointState::rxsize.
Referenced by usbPrepareQueuedReceive(), and usbPrepareReceive().
Prepares for a transmit operation.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 714 of file usb_lld.c.
References USBDriver::epc, USBEndpointConfig::in_maxsize, USBEndpointConfig::in_state, USBInEndpointState::txqueued, USBInEndpointState::txsize, and USB_GET_DESCRIPTOR.
Referenced by usbPrepareQueuedTransmit(), and usbPrepareTransmit().
Starts a receive operation on an OUT endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 739 of file usb_lld.c.
Referenced by usbStartReceiveI().
Starts a transmit operation on an IN endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 754 of file usb_lld.c.
Referenced by CH_IRQ_HANDLER(), and usbStartTransmitI().
Brings an OUT endpoint in the stalled state.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 769 of file usb_lld.c.
Referenced by _usb_ep0in(), _usb_ep0out(), _usb_ep0setup(), and usbStallReceiveI().
Brings an IN endpoint in the stalled state.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 784 of file usb_lld.c.
Referenced by _usb_ep0in(), _usb_ep0out(), _usb_ep0setup(), and usbStallTransmitI().
| #define USB_DESC_INDEX | ( | i | ) | ((uint8_t)(i)) |
| #define USB_DESC_BYTE | ( | b | ) | ((uint8_t)(b)) |
| #define USB_DESC_WORD | ( | w | ) |
| #define USB_DESC_BCD | ( | bcd | ) |
| #define USB_DESC_DEVICE | ( | bcdUSB, | |
| bDeviceClass, | |||
| bDeviceSubClass, | |||
| bDeviceProtocol, | |||
| bMaxPacketSize, | |||
| idVendor, | |||
| idProduct, | |||
| bcdDevice, | |||
| iManufacturer, | |||
| iProduct, | |||
| iSerialNumber, | |||
| bNumConfigurations | |||
| ) |
USB_DESC_BYTE(18), \ 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)
Device Descriptor helper macro.
| #define USB_DESC_CONFIGURATION | ( | wTotalLength, | |
| bNumInterfaces, | |||
| bConfigurationValue, | |||
| iConfiguration, | |||
| bmAttributes, | |||
| bMaxPower | |||
| ) |
USB_DESC_BYTE(9), \ 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)
Configuration Descriptor helper macro.
| #define USB_DESC_INTERFACE | ( | bInterfaceNumber, | |
| bAlternateSetting, | |||
| bNumEndpoints, | |||
| bInterfaceClass, | |||
| bInterfaceSubClass, | |||
| bInterfaceProtocol, | |||
| iInterface | |||
| ) |
USB_DESC_BYTE(9), \ 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)
Interface Descriptor helper macro.
| #define USB_DESC_INTERFACE_ASSOCIATION | ( | bFirstInterface, | |
| bInterfaceCount, | |||
| bFunctionClass, | |||
| bFunctionSubClass, | |||
| bFunctionProcotol, | |||
| iInterface | |||
| ) |
USB_DESC_BYTE(8), \ 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)
Interface Association Descriptor helper macro.
| #define USB_DESC_ENDPOINT | ( | bEndpointAddress, | |
| bmAttributes, | |||
| wMaxPacketSize, | |||
| bInterval | |||
| ) |
USB_DESC_BYTE(7), \ USB_DESC_BYTE(USB_DESCRIPTOR_ENDPOINT), \ USB_DESC_BYTE(bEndpointAddress), \ USB_DESC_BYTE(bmAttributes), \ USB_DESC_WORD(wMaxPacketSize), \ USB_DESC_BYTE(bInterval)
Endpoint Descriptor helper macro.
| #define USB_EP_MODE_TYPE 0x0003 |
| #define USB_EP_MODE_TYPE_ISOC 0x0001 |
| #define USB_EP_MODE_TYPE_BULK 0x0002 |
| #define USB_EP_MODE_TYPE_INTR 0x0003 |
| #define usbGetDriverStateI | ( | usbp | ) | ((usbp)->state) |
| #define usbFetchWord | ( | p | ) | ((uint16_t)*(p) | ((uint16_t)*((p) + 1) << 8)) |
Fetches a 16 bits word value from an USB message.
| [in] | p | pointer to the 16 bits word |
Definition at line 358 of file usb.h.
Referenced by _usb_ep0in(), and _usb_ep0setup().
| #define usbConnectBus | ( | usbp | ) | usb_lld_connect_bus(usbp) |
| #define usbDisconnectBus | ( | usbp | ) | usb_lld_disconnect_bus(usbp) |
| #define usbGetFrameNumber | ( | usbp | ) | usb_lld_get_frame_number(usbp) |
| #define usbGetTransmitStatusI | ( | usbp, | |
| ep | |||
| ) | ((usbp)->transmitting & (1 << (ep))) |
Returns the status of an IN endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| FALSE | Endpoint ready. |
| TRUE | Endpoint transmitting. |
Definition at line 399 of file usb.h.
Referenced by usbStallTransmitI(), and usbStartTransmitI().
| #define usbGetReceiveStatusI | ( | usbp, | |
| ep | |||
| ) | ((usbp)->receiving & (1 << (ep))) |
Returns the status of an OUT endpoint.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| FALSE | Endpoint ready. |
| TRUE | Endpoint receiving. |
Definition at line 412 of file usb.h.
Referenced by usbStallReceiveI(), and usbStartReceiveI().
| #define usbGetReceiveTransactionSizeI | ( | 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.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 426 of file usb.h.
Referenced by _usb_ep0out().
| #define usbSetupTransfer | ( | usbp, | |
| buf, | |||
| n, | |||
| endcb | |||
| ) |
{ \
(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.
| [in] | usbp | pointer to the USBDriver object |
| [in] | buf | pointer to a buffer for the transaction data |
| [in] | n | number of bytes to be transferred |
| [in] | endcb | callback to be invoked after the transfer or NULL |
| #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.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| [out] | buf | buffer where to copy the packet data |
Definition at line 461 of file usb.h.
Referenced by _usb_ep0setup().
| #define _usb_isr_invoke_event_cb | ( | usbp, | |
| evt | |||
| ) |
{ \
if (((usbp)->config->event_cb) != NULL) \
(usbp)->config->event_cb(usbp, evt); \
}
Common ISR code, usb event callback.
| [in] | usbp | pointer to the USBDriver object |
| [in] | evt | USB event code |
Definition at line 476 of file usb.h.
Referenced by _usb_ep0in(), _usb_ep0out(), _usb_ep0setup(), and CH_IRQ_HANDLER().
| #define _usb_isr_invoke_sof_cb | ( | usbp | ) |
{ \
if (((usbp)->config->sof_cb) != NULL) \
(usbp)->config->sof_cb(usbp); \
}
Common ISR code, SOF callback.
| [in] | usbp | pointer to the USBDriver object |
Definition at line 488 of file usb.h.
Referenced by CH_IRQ_HANDLER().
| #define _usb_isr_invoke_setup_cb | ( | usbp, | |
| ep | |||
| ) |
{ \
(usbp)->epc[ep]->setup_cb(usbp, ep); \
}
Common ISR code, setup packet callback.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 501 of file usb.h.
Referenced by CH_IRQ_HANDLER().
| #define _usb_isr_invoke_in_cb | ( | usbp, | |
| ep | |||
| ) |
{ \
(usbp)->transmitting &= ~(1 << (ep)); \
(usbp)->epc[ep]->in_cb(usbp, ep); \
}
Common ISR code, IN endpoint callback.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 513 of file usb.h.
Referenced by CH_IRQ_HANDLER().
| #define _usb_isr_invoke_out_cb | ( | usbp, | |
| ep | |||
| ) |
{ \
(usbp)->receiving &= ~(1 << (ep)); \
(usbp)->epc[ep]->out_cb(usbp, ep); \
}
Common ISR code, OUT endpoint event.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
Definition at line 526 of file usb.h.
Referenced by CH_IRQ_HANDLER().
| #define USB_ENDOPOINTS_NUMBER 7 |
Number of the available endpoints.
This value does not include the endpoint 0 which is always present.
Definition at line 38 of file stm32_usb.h.
Referenced by usb_lld_disable_endpoints().
| #define STM32_USB_BASE (APB1PERIPH_BASE + 0x5C00) |
USB registers block numeric address.
Definition at line 115 of file stm32_usb.h.
| #define STM32_USBRAM_BASE (APB1PERIPH_BASE + 0x6000) |
USB RAM numeric address.
Definition at line 120 of file stm32_usb.h.
| #define STM32_USB ((stm32_usb_t *)STM32_USB_BASE) |
Pointer to the USB registers block.
Definition at line 125 of file stm32_usb.h.
Referenced by CH_IRQ_HANDLER(), usb_lld_clear_in(), usb_lld_clear_out(), usb_lld_get_status_in(), usb_lld_get_status_out(), usb_lld_reset(), usb_lld_set_address(), usb_lld_start(), and usb_lld_stop().
| #define STM32_USBRAM ((uint32_t *)STM32_USBRAM_BASE) |
Pointer to the USB RAM.
Definition at line 130 of file stm32_usb.h.
| #define USB_PMA_SIZE 512 |
Size of the dedicated packet memory.
Definition at line 135 of file stm32_usb.h.
| #define EPR_TOGGLE_MASK |
(EPR_STAT_TX_MASK | EPR_DTOG_TX | \
EPR_STAT_RX_MASK | EPR_DTOG_RX | \
EPR_SETUP)
Mask of all the toggling bits in the EPR register.
Definition at line 140 of file stm32_usb.h.
| #define USB_GET_DESCRIPTOR | ( | ep | ) |
((stm32_usb_descriptor_t *)((uint32_t)STM32_USBRAM_BASE + \ (uint32_t)STM32_USB->BTABLE * 2 + \ (uint32_t)(ep) * \ sizeof(stm32_usb_descriptor_t)))
Returns an endpoint descriptor pointer.
Definition at line 233 of file stm32_usb.h.
Referenced by CH_IRQ_HANDLER(), usb_lld_init_endpoint(), usb_lld_prepare_transmit(), and usb_lld_read_setup().
| #define USB_ADDR2PTR | ( | addr | ) | ((uint32_t *)((addr) * 2 + STM32_USBRAM_BASE)) |
Converts from a PMA address to a physical address.
Definition at line 242 of file stm32_usb.h.
Referenced by usb_lld_read_setup().
| #define USB_MAX_ENDPOINTS USB_ENDOPOINTS_NUMBER |
Maximum endpoint address.
Definition at line 43 of file usb_lld.h.
Referenced by _usb_reset(), usbDisableEndpointsI(), and usbStart().
| #define USB_SET_ADDRESS_MODE USB_LATE_SET_ADDRESS |
| #define STM32_USB_USE_USB1 FALSE |
| #define STM32_USB_LOW_POWER_ON_SUSPEND FALSE |
| #define STM32_USB_USB1_HP_IRQ_PRIORITY 13 |
USB1 interrupt priority level setting.
Definition at line 74 of file usb_lld.h.
Referenced by usb_lld_start().
| #define STM32_USB_USB1_LP_IRQ_PRIORITY 14 |
USB1 interrupt priority level setting.
Definition at line 81 of file usb_lld.h.
Referenced by usb_lld_start().
| #define usb_lld_get_frame_number | ( | usbp | ) | (STM32_USB->FNR & FNR_FN_MASK) |
| #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.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| #define usb_lld_get_packet_size | ( | usbp, | |
| ep | |||
| ) | ((size_t)USB_GET_DESCRIPTOR(ep)->RXCOUNT & RXCOUNT_COUNT_MASK) |
Returns the exact size of a received packet.
| [in] | usbp | pointer to the USBDriver object |
| [in] | ep | endpoint number |
| typedef void(* usbcallback_t)(USBDriver *usbp) |
| typedef void(* usbepcallback_t)(USBDriver *usbp, usbep_t ep) |
| typedef void(* usbeventcb_t)(USBDriver *usbp, usbevent_t event) |
| typedef bool_t(* usbreqhandler_t)(USBDriver *usbp) |
Type of a requests handler callback.
The request is encoded in the usb_setup buffer.
| [in] | usbp | pointer to the USBDriver object triggering the callback |
| FALSE | Request not recognized by the handler. |
| TRUE | Request handled. |
| typedef const USBDescriptor*(* usbgetdescriptor_t)(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang) |
| enum usbstate_t |
| enum usbepstatus_t |
| enum usbep0state_t |
| enum usbevent_t |
Type of an enumeration of the possible USB events.