Lightweight USB stack  1.0

Contains core API. More...

Data Structures

struct  usbd_ctlreq
 Represents generic USB control request. More...
 
struct  usbd_status
 
struct  usbd_device
 Represents a USB device data. More...
 

Macros

#define USB_EPTYPE_DBLBUF   0x04
 Doublebuffered endpoint (bulk endpoint only).
 

Typedefs

typedef void(* usbd_evt_callback) (usbd_device *dev, uint8_t event, uint8_t ep)
 Generic USB device event callback for events and endpoints processing. More...
 
typedef void(* usbd_rqc_callback) (usbd_device *dev, usbd_ctlreq *req)
 USB control transfer completed callback function. More...
 
typedef usbd_respond(* usbd_ctl_callback) (usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback)
 USB control callback function. More...
 
typedef usbd_respond(* usbd_dsc_callback) (usbd_ctlreq *req, void **address, uint16_t *dsize)
 USB get descriptor callback function. More...
 
typedef usbd_respond(* usbd_cfg_callback) (usbd_device *dev, uint8_t cfg)
 USB set configuration callback function. More...
 

Enumerations

enum  usbd_machine_state
 USB device machine states. More...
 
enum  usbd_ctl_state
 USB device control endpoint machine state. More...
 
enum  usbd_respond
 Reporting status results. More...
 

Functions

static void usbd_init (usbd_device *dev, const struct usbd_driver *drv, const uint8_t ep0size, uint32_t *buffer, const uint16_t bsize)
 Initializes device structure. More...
 
void usbd_poll (usbd_device *dev)
 Polls USB for events. More...
 
static void usbd_reg_control (usbd_device *dev, usbd_ctl_callback callback)
 Register callback for all control requests. More...
 
static void usbd_reg_config (usbd_device *dev, usbd_cfg_callback callback)
 Register callback for SET_CONFIG control request. More...
 
static void usbd_reg_descr (usbd_device *dev, usbd_dsc_callback callback)
 Register callback for GET_DESCRIPTOR control request. More...
 
static bool usbd_ep_config (usbd_device *dev, uint8_t ep, uint8_t eptype, uint16_t epsize)
 Configure endpoint. More...
 
static void usbd_ep_deconfig (usbd_device *dev, uint8_t ep)
 Deconfigure endpoint. More...
 
static void usbd_reg_endpoint (usbd_device *dev, uint8_t ep, usbd_evt_callback callback)
 Register endpoint callback. More...
 
static void usbd_reg_event (usbd_device *dev, uint8_t evt, usbd_evt_callback callback)
 Registers event callback. More...
 
static int32_t usbd_ep_write (usbd_device *dev, uint8_t ep, void *buf, uint16_t blen)
 Write data to endpoint. More...
 
static int32_t usbd_ep_read (usbd_device *dev, uint8_t ep, void *buf, uint16_t blen)
 Read data from endpoint. More...
 
static void usbd_ep_stall (usbd_device *dev, uint8_t ep)
 Stall endpoint. More...
 
static void usbd_ep_unstall (usbd_device *dev, uint8_t ep)
 Unstall endpoint. More...
 
static void usbd_enable (usbd_device *dev, bool enable)
 Enables or disables USB hardware. More...
 
static uint8_t usbd_connect (usbd_device *dev, bool connect)
 Connects or disconnects USB hardware to/from usb host. More...
 
static uint32_t usbd_getinfo (usbd_device *dev)
 Retrieves status and capabilities. More...
 

bmRequestType bitmapped field

#define USB_REQ_DIRECTION   (1 << 7)
 Request direction mask.
 
#define USB_REQ_HOSTTODEV   (0 << 7)
 Request direction is HOST to DEVICE.
 
#define USB_REQ_DEVTOHOST   (1 << 7)
 Request direction is DEVICE to HOST.
 
#define USB_REQ_TYPE   (3 << 5)
 Request type mask.
 
#define USB_REQ_STANDARD   (0 << 5)
 Standard request.
 
#define USB_REQ_CLASS   (1 << 5)
 Class specified request.
 
#define USB_REQ_VENDOR   (2 << 5)
 Vendor specified request.
 
#define USB_REQ_RECIPIENT   (3 << 0)
 Request recipient mask.
 
#define USB_REQ_DEVICE   (0 << 0)
 Request to device.
 
#define USB_REQ_INTERFACE   (1 << 0)
 Request to interface.
 
#define USB_REQ_ENDPOINT   (2 << 0)
 Request to endpoint.
 
#define USB_REQ_OTHER   (3 << 0)
 Other request.
 

Detailed Description

Contains core API.

Typedef Documentation

◆ usbd_evt_callback

typedef void(* usbd_evt_callback) (usbd_device *dev, uint8_t event, uint8_t ep)

Generic USB device event callback for events and endpoints processing.

Parameters
[in]devpointer to USB device
eventUSB event
epactive endpoint number
Note
endpoints with same indexes i.e. 0x01 and 0x81 shares same callback.

Definition at line 175 of file usbd_core.h.

◆ usbd_rqc_callback

typedef void(* usbd_rqc_callback) (usbd_device *dev, usbd_ctlreq *req)

USB control transfer completed callback function.

Parameters
[in]devpointer to USB device
[in]reqpointer to usb request structure
Note
usbd_device->complete_callback will be set to NULL after this callback completion.

Definition at line 182 of file usbd_core.h.

◆ usbd_ctl_callback

typedef usbd_respond(* usbd_ctl_callback) (usbd_device *dev, usbd_ctlreq *req, usbd_rqc_callback *callback)

USB control callback function.

Uses for the control request processing. Some requests will be handled by core if callback don't process it (returns FALSE). If request was not processed STALL PID will be issued.

  • GET_CONFIGURATION
  • SET_CONFIGURATION (passes to usbd_cfg_callback)
  • GET_DESCRIPTOR (passes to usbd_dsc_callback)
  • GET_STATUS
  • SET_FEATURE, CLEAR_FEATURE (endpoints only)
  • SET_ADDRESS
    Parameters
    [in]devpoints to USB device
    [in]reqpoints to usb control request
    [out]*callbackUSB control transfer completion callback, default is NULL (no callback)
    Returns
    usbd_respond status.

Definition at line 199 of file usbd_core.h.

◆ usbd_dsc_callback

typedef usbd_respond(* usbd_dsc_callback) (usbd_ctlreq *req, void **address, uint16_t *dsize)

USB get descriptor callback function.

Called when GET_DESCRIPTOR request issued

Parameters
[in]reqpointer to usb control request structure
[in,out]addresspointer to the descriptor in memory. Points to req->data by default. You can use this buffer.
[in,out]dsizedescriptor size. maximum buffer size by default.
Returns
usbd_ack if you passed the correct descriptor, usbd_fail otherwise.

Definition at line 209 of file usbd_core.h.

◆ usbd_cfg_callback

typedef usbd_respond(* usbd_cfg_callback) (usbd_device *dev, uint8_t cfg)

USB set configuration callback function.

called when SET_CONFIGURATION request issued

Parameters
[in]devpointer to USB device
[in]cfgconfiguration number.
Note
if config is 0 device endpoints should be de-configured
Returns
TRUE if success

Definition at line 218 of file usbd_core.h.

Enumeration Type Documentation

◆ usbd_machine_state

USB device machine states.

Enumerator
usbd_state_default 

Default.

usbd_state_addressed 

Addressed.

usbd_state_configured 

Configured.

Definition at line 113 of file usbd_core.h.

◆ usbd_ctl_state

USB device control endpoint machine state.

Enumerator
usbd_ctl_idle 

Idle stage. Awaiting for SETUP packet.

usbd_ctl_rxdata 

RX stage. Receiving DATA-OUT payload.

usbd_ctl_txdata 

TX stage. Transmitting DATA-IN payload.

usbd_ctl_ztxdata 

TX stage. Transmitting DATA-IN payload. Zero length packet maybe required..

usbd_ctl_lastdata 

TX stage. Last DATA-IN packed passed to buffer. Awaiting for the TX completion.

usbd_ctl_statusin 

STATUS-IN stage.

usbd_ctl_statusout 

STATUS-OUT stage.

Definition at line 122 of file usbd_core.h.

◆ usbd_respond

Reporting status results.

Enumerator
usbd_fail 

Function has an error, STALLPID will be issued.

usbd_ack 

Function completes request accepted ZLP or data will be send.

usbd_nak 

Function is busy. NAK handshake.

Definition at line 135 of file usbd_core.h.

Function Documentation

◆ usbd_init()

static void usbd_init ( usbd_device *  dev,
const struct usbd_driver drv,
const uint8_t  ep0size,
uint32_t *  buffer,
const uint16_t  bsize 
)
inlinestatic

Initializes device structure.

Parameters
devUSB device that will be initialized
drvPointer to hardware driver
ep0sizeControl endpoint 0 size
bufferPointer to control request data buffer (32-bit aligned)
bsizeSize of the data buffer

Definition at line 347 of file usbd_core.h.

◆ usbd_poll()

void usbd_poll ( usbd_device *  dev)

Polls USB for events.

Parameters
devPointer to device structure
Note
can be called as from main routine as from USB interrupt

◆ usbd_reg_control()

static void usbd_reg_control ( usbd_device *  dev,
usbd_ctl_callback  callback 
)
inlinestatic

Register callback for all control requests.

Parameters
devusb device _usbd_device
callbackuser control callback usbd_ctl_callback

Definition at line 366 of file usbd_core.h.

◆ usbd_reg_config()

static void usbd_reg_config ( usbd_device *  dev,
usbd_cfg_callback  callback 
)
inlinestatic

Register callback for SET_CONFIG control request.

Parameters
devdev usb device _usbd_device
callbackpointer to user usbd_cfg_callback

Definition at line 374 of file usbd_core.h.

◆ usbd_reg_descr()

static void usbd_reg_descr ( usbd_device *  dev,
usbd_dsc_callback  callback 
)
inlinestatic

Register callback for GET_DESCRIPTOR control request.

Parameters
devdev usb device _usbd_device
callbackpointer to user usbd_ctl_callback

Definition at line 382 of file usbd_core.h.

◆ usbd_ep_config()

static bool usbd_ep_config ( usbd_device *  dev,
uint8_t  ep,
uint8_t  eptype,
uint16_t  epsize 
)
inlinestatic

Configure endpoint.

Parameters
devdev usb device _usbd_device
ependpoint address. Use USB_EPDIR_ macros to set endpoint direction
eptypeendpoint type. Use USB_EPTYPE_* macros.
epsizeendpoint size in bytes
Returns
TRUE if success

Definition at line 390 of file usbd_core.h.

◆ usbd_ep_deconfig()

static void usbd_ep_deconfig ( usbd_device *  dev,
uint8_t  ep 
)
inlinestatic

Deconfigure endpoint.

Parameters
devdev usb device _usbd_device
ependpoint index
Note
if you have two one-direction single-buffered endpoints with same index (i.e. 0x02 and 0x82) both will be deconfigured.

Definition at line 398 of file usbd_core.h.

◆ usbd_reg_endpoint()

static void usbd_reg_endpoint ( usbd_device *  dev,
uint8_t  ep,
usbd_evt_callback  callback 
)
inlinestatic

Register endpoint callback.

Parameters
devdev usb device _usbd_device
ependpoint index
callbackpointer to user usbd_evt_callback callback for endpoint events

Definition at line 407 of file usbd_core.h.

◆ usbd_reg_event()

static void usbd_reg_event ( usbd_device *  dev,
uint8_t  evt,
usbd_evt_callback  callback 
)
inlinestatic

Registers event callback.

Parameters
devdev usb device _usbd_device
evtdevice event wants to be registered
callbackpointer to user usbd_evt_callback for this event

Definition at line 416 of file usbd_core.h.

◆ usbd_ep_write()

static int32_t usbd_ep_write ( usbd_device *  dev,
uint8_t  ep,
void *  buf,
uint16_t  blen 
)
inlinestatic

Write data to endpoint.

Parameters
devdev usb device _usbd_device
ependpoint index, hould belong to IN or CONTROL endpoint
bufpointer to data buffer
blensize of data will be written
Returns
number of written bytes

Definition at line 424 of file usbd_core.h.

◆ usbd_ep_read()

static int32_t usbd_ep_read ( usbd_device *  dev,
uint8_t  ep,
void *  buf,
uint16_t  blen 
)
inlinestatic

Read data from endpoint.

Parameters
devdev usb device _usbd_device
ependpoint index, should belong to OUT or CONTROL endpoint.
bufpointer to read buffer
blensize of the read buffer in bytes
Returns
size of the actually received data, -1 on error.
Note
if data does not fit buffer it will be truncated

Definition at line 432 of file usbd_core.h.

◆ usbd_ep_stall()

static void usbd_ep_stall ( usbd_device *  dev,
uint8_t  ep 
)
inlinestatic

Stall endpoint.

Parameters
devdev usb device _usbd_device
ependpoint address

Definition at line 440 of file usbd_core.h.

◆ usbd_ep_unstall()

static void usbd_ep_unstall ( usbd_device *  dev,
uint8_t  ep 
)
inlinestatic

Unstall endpoint.

Parameters
devdev usb device _usbd_device
ependpoint address

Definition at line 448 of file usbd_core.h.

◆ usbd_enable()

static void usbd_enable ( usbd_device *  dev,
bool  enable 
)
inlinestatic

Enables or disables USB hardware.

Parameters
devdev usb device _usbd_device
enableEnables USB when TRUE disables otherwise

Definition at line 456 of file usbd_core.h.

◆ usbd_connect()

static uint8_t usbd_connect ( usbd_device *  dev,
bool  connect 
)
inlinestatic

Connects or disconnects USB hardware to/from usb host.

Parameters
devdev usb device _usbd_device
connectConnects USB to host if TRUE, disconnects otherwise
Returns
lanes connection status. USB_LANES_STATUS

Definition at line 465 of file usbd_core.h.

◆ usbd_getinfo()

static uint32_t usbd_getinfo ( usbd_device *  dev)
inlinestatic

Retrieves status and capabilities.

Returns
current HW status, enumeration speed and capabilities USBD_HW_CAPS

Definition at line 471 of file usbd_core.h.