USB DEVICE protocol stack, see USB Device Stack Library page for detailed documentation.

.

Data Structures

struct USBD_Init_TypeDef
USB Device stack initialization structure.
struct USBD_Callbacks_TypeDef
USB Device stack callback structure.

Typedefs

typedef void(* USBD_UsbResetCb_TypeDef ) (void)
USB Reset callback function.
typedef void(* USBD_SofIntCb_TypeDef ) (uint16_t sofNr)
USB Start Of Frame (SOF) interrupt callback function.
typedef void(* USBD_DeviceStateChangeCb_TypeDef ) ( USBD_State_TypeDef oldState, USBD_State_TypeDef newState)
USB State change callback function.
typedef bool(* USBD_IsSelfPoweredCb_TypeDef ) (void)
USB power mode callback function.
typedef int(* USBD_SetupCmdCb_TypeDef ) (const USB_Setup_TypeDef *setup)
USB setup request callback function.
typedef struct USBD_Callbacks_TypeDef USBD_Callbacks_TypeDef
USB Device stack callback structure.

Enumerations

enum USBD_State_TypeDef {
USBD_STATE_NONE = 0,
USBD_STATE_ATTACHED = 1,
USBD_STATE_POWERED = 2,
USBD_STATE_DEFAULT = 3,
USBD_STATE_ADDRESSED = 4,
USBD_STATE_CONFIGURED = 4,
USBD_STATE_SUSPENDED = 6,
USBD_STATE_LASTMARKER = 7
}
USB device state enumerator.

Functions

void USBD_AbortAllTransfers (void)
Abort all pending transfers.
int USBD_AbortTransfer (int epAddr)
Abort a pending transfer on a specific endpoint.
void USBD_Connect (void)
Start USB device operation.
void USBD_Disconnect (void)
Stop USB device operation.
bool USBD_EpIsBusy (int epAddr)
Check if an endpoint is busy doing a transfer.
USBD_State_TypeDef USBD_GetUsbState (void)
Get current USB device state.
const char * USBD_GetUsbStateName ( USBD_State_TypeDef state)
Get a string naming a device USB state.
int USBD_Init (const USBD_Init_TypeDef *p)
Initializes USB device hardware and internal protocol stack data structures, then connects the data-line (D+ or D-) pullup resistor to signal host that enumeration can begin.
int USBD_Read (int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a read (OUT) transfer on an endpoint.
int USBD_RemoteWakeup (void)
Perform a remote wakeup signalling sequence.
bool USBD_SafeToEnterEM2 (void)
int USBD_StallEp (int epAddr)
Set an endpoint in the stalled (halted) state.
void USBD_Stop (void)
Stop USB device stack operation.
int USBD_UnStallEp (int epAddr)
Reset stall state on a stalled (halted) endpoint.
int USBD_Write (int epAddr, void *data, int byteCount, USB_XferCompleteCb_TypeDef callback)
Start a write (IN) transfer on an endpoint.
void usbSuspendDsr (void)
USB suspend delayed service routine.

Detailed Description

See em_usbd.c for source code.

Typedef Documentation

Callback functions used by the device stack to signal events or query status to/from the application. See USBD_Init_TypeDef . Assign members to NULL if your application don't need a specific callback.

typedef void(* USBD_DeviceStateChangeCb_TypeDef) ( USBD_State_TypeDef oldState, USBD_State_TypeDef newState)

Called whenever the device change state.

Parameters
[in] oldState The device USB state just leaved. See USBD_State_TypeDef .
[in] newState New (the current) USB device state. See USBD_State_TypeDef .
typedef bool(* USBD_IsSelfPoweredCb_TypeDef) (void)

Called whenever the device stack needs to query if the device is currently self- or bus-powered. Typically when host has issued an GET_STATUS setup command.

Returns
True if self-powered, false otherwise.
typedef int(* USBD_SetupCmdCb_TypeDef) (const USB_Setup_TypeDef *setup)

Called on each setup request received from host. This gives the application a possibility to extend or override standard requests, and to implement class or vendor specific requests. Return USB_STATUS_OK if the request is handled, return USB_STATUS_REQ_ERR if it is an illegal request or return USB_STATUS_REQ_UNHANDLED to pass the request on to the default request handler.

Parameters
[in] setup Pointer to an USB setup packet. See USB_Setup_TypeDef .
Returns
An appropriate status/error code. See USB_Status_TypeDef .
typedef void(* USBD_SofIntCb_TypeDef) (uint16_t sofNr)

Called at each SOF interrupt (if enabled),

Parameters
[in] sofNr Current frame number. The value rolls over to 0 after 16383 (0x3FFF).
typedef void(* USBD_UsbResetCb_TypeDef) (void)

Called whenever USB reset signalling is detected on the USB port.

Enumeration Type Documentation

Enumerator
USBD_STATE_NONE

Device state is undefined/unknown.

USBD_STATE_ATTACHED

Device state is ATTACHED.

USBD_STATE_POWERED

Device state is POWERED.

USBD_STATE_DEFAULT

Device state is DEFAULT.

USBD_STATE_ADDRESSED

Device state is ADDRESSED.

USBD_STATE_CONFIGURED

Device state is CONFIGURED.

USBD_STATE_SUSPENDED

Device state is SUSPENDED.

USBD_STATE_LASTMARKER

Device state enum end marker.

Function Documentation

void USBD_AbortAllTransfers ( void )

Aborts transfers for all endpoints currently in use. Pending transfers on the default endpoint (EP0) are not aborted.

References USB_STATUS_EP_ABORTED .

int USBD_AbortTransfer ( int epAddr )
Parameters
[in] epAddr The address of the endpoint to abort.

References assert , NULL , USB_STATUS_EP_ABORTED , USB_STATUS_OK , USBD_STATE_ADDRESSED , and USBD_STATE_CONFIGURED .

void USBD_Connect ( void )

Device operation is started by connecting a pullup resistor on the appropriate USB data line.

void USBD_Disconnect ( void )

Device operation is stopped by disconnecting the pullup resistor from the appropriate USB data line. Often referred to as a "soft" disconnect.

References USBD_STATE_SUSPENDED .

Referenced by USBD_Stop() .

bool USBD_EpIsBusy ( int epAddr )
Parameters
[in] epAddr The address of the endpoint to check.
Returns
True if endpoint is busy, false otherwise.

References assert , and NULL .

Referenced by USBD_RemoteWakeup() .

USBD_State_TypeDef USBD_GetUsbState ( void )
Returns
Device USB state. See USBD_State_TypeDef .

Referenced by USBD_Read() , and USBD_Write() .

const char * USBD_GetUsbStateName ( USBD_State_TypeDef state )
Parameters
[in] state Device USB state. See USBD_State_TypeDef .
Returns
State name string pointer.

References USBD_STATE_LASTMARKER .

int USBD_Read ( int epAddr,
void * data,
int byteCount,
USB_XferCompleteCb_TypeDef callback
)
Note
The transfer buffer length must be a multiple of 4 bytes in length and WORD (4 byte) aligned. When allocating the buffer, round buffer length up. If it is possible that the host will send more data than your device expects, round buffer size up to the next multiple of maxpacket size.
Parameters
[in] epAddr Endpoint address.
[in] data Pointer to transfer data buffer.
[in] byteCount Transfer length.
[in] callback Function to be called on transfer completion. Supply NULL if no callback is needed. See USB_XferCompleteCb_TypeDef .
Returns
USB_STATUS_OK on success, else an appropriate error code.

References assert , NULL , USB_STATUS_DEVICE_UNCONFIGURED , USB_STATUS_EP_BUSY , USB_STATUS_EP_STALLED , USB_STATUS_OK , USBD_GetUsbState() , and USBD_STATE_CONFIGURED .

int USBD_RemoteWakeup ( void )
Note
It is the responsibility of the application to ensure that remote wakeup is not attempted before the device has been suspended for at least 5 miliseconds. This function should not be called from within an interrupt handler.
Returns
USB_STATUS_OK on success, else an appropriate error code.

References elapsedTimeInt16u , FIFO_DEQUEUE , halCommonGetInt16uMillisecondTick() , MEMSET , NULL , USB_STATUS_ILLEGAL , USB_STATUS_OK , USB_STATUS_TIMEOUT , USBD_EpIsBusy() , USBD_STATE_SUSPENDED , USBD_Write() , usbForceTxData() , and usbTxData() .

bool USBD_SafeToEnterEM2 ( void )
int USBD_StallEp ( int epAddr )
Parameters
[in] epAddr The address of the endpoint to stall.
Returns
USB_STATUS_OK on success, else an appropriate error code.

References assert , NULL , USB_STATUS_ILLEGAL , and USB_STATUS_OK .

void USBD_Stop ( void )

The data-line pullup resistor is turned off, USB interrupts are disabled, and finally the USB pins are disabled.

References USBD_Disconnect() , and USBD_STATE_NONE .

int USBD_UnStallEp ( int epAddr )
Parameters
[in] epAddr The address of the endpoint to un-stall.
Returns
USB_STATUS_OK on success, else an appropriate error code.

References assert , NULL , USB_STATUS_ILLEGAL , and USB_STATUS_OK .

int USBD_Write ( int epAddr,
void * data,
int byteCount,
USB_XferCompleteCb_TypeDef callback
)
Parameters
[in] epAddr Endpoint address.
[in] data Pointer to transfer data buffer. This buffer must be WORD (4 byte) aligned.
[in] byteCount Transfer length.
[in] callback Function to be called on transfer completion. Supply NULL if no callback is needed. See USB_XferCompleteCb_TypeDef .
Returns
USB_STATUS_OK on success, else an appropriate error code.

References assert , NULL , USB_STATUS_DEVICE_UNCONFIGURED , USB_STATUS_EP_BUSY , USB_STATUS_EP_STALLED , USB_STATUS_ILLEGAL , USB_STATUS_OK , USBD_GetUsbState() , and USBD_STATE_CONFIGURED .

Referenced by USBD_RemoteWakeup() .

void usbSuspendDsr ( void )

This function keeps the device in a low power state in order to meet USB specification during USB suspend state.

References emberStackPowerDown() , halSleep() , SLEEPMODE_IDLE , and USBD_STATE_SUSPENDED .