Mbed OS Reference
Loading...
Searching...
No Matches
USBDevice Class Referenceabstract

Core USB Device driver. More...

#include <USBDevice.h>

Inheritance diagram for USBDevice:
USBPhyEvents USBAudio USBCDC USBCDC_ECM USBHID USBMIDI USBMSD USBSerial USBKeyboard USBMouse USBMouseKeyboard

Data Structures

struct  setup_packet_t
 

Public Member Functions

 USBDevice (USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
 Instantiate a new USBDevice with the given parameters. More...
 
virtual ~USBDevice ()
 Cleanup this USBDevice. More...
 
void init ()
 Initialize this instance. More...
 
void deinit ()
 Power down this instance. More...
 
bool configured ()
 Check if the device is configured. More...
 
void connect ()
 Connect a device This method can also be used to resume USB operation when USB power is detected after it was suspended via USBDevice::deinit. More...
 
void disconnect ()
 Disconnect a device. More...
 
void sof_enable ()
 Enable the start of frame interrupt. More...
 
void sof_disable ()
 Disable the start of frame interrupt. More...
 
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, mbed::Callback< void()> callback=nullptr)
 Add an endpoint. More...
 
template<typename T >
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void(T::*callback)())
 Add an endpoint. More...
 
void endpoint_remove (usb_ep_t endpoint)
 Remove an endpoint. More...
 
void endpoint_remove_all ()
 Remove all non-zero endpoints. More...
 
void endpoint_stall (usb_ep_t endpoint)
 Stall an endpoint. More...
 
void endpoint_unstall (usb_ep_t endpoint)
 Un-stall an endpoint. More...
 
uint32_t endpoint_max_packet_size (usb_ep_t endpoint)
 Get the current maximum size for this endpoint. More...
 
void endpoint_abort (usb_ep_t endpoint)
 Abort the current transfer on this endpoint. More...
 
bool read_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 start a read on the given endpoint More...
 
uint32_t read_finish (usb_ep_t endpoint)
 Get the status of a read. More...
 
bool write_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 Write a data to the given endpoint. More...
 
uint32_t write_finish (usb_ep_t endpoint)
 Get the status of a write. More...
 

Protected Member Functions

virtual void callback_power (bool powered)
 Called by USBDevice layer on power state change. More...
 
virtual void callback_sof (int frame_number)
 Called by USBDevice layer on each new USB frame. More...
 
virtual void callback_reset ()
 Called by USBDevice layer on bus reset. More...
 
virtual void callback_state_change (DeviceState new_state)=0
 Called when USB changes state. More...
 
virtual void callback_request (const setup_packet_t *setup)=0
 Called by USBDevice on Endpoint0 request. More...
 
void complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0)
 Called to complete the setup stage of a callback request. More...
 
virtual void callback_request_xfer_done (const setup_packet_t *setup, bool aborted)=0
 Called by USBDevice on data stage completion. More...
 
void complete_request_xfer_done (bool success)
 Called to complete the data stage of a callback request. More...
 
void complete_set_configuration (bool success)
 Called to complete a set configuration command. More...
 
void complete_set_interface (bool success)
 Called to complete a set interface command. More...
 
uint8_t * find_descriptor (uint8_t descriptor_type, uint8_t index=0)
 Find a descriptor type inside the configuration descriptor. More...
 
const usb_ep_table_tendpoint_table ()
 Get the endpoint table of this device. More...
 
virtual void start_process ()
 Callback called to indicate the USB processing needs to be done. More...
 
virtual void lock ()
 Acquire exclusive access to this instance USBDevice. More...
 
virtual void unlock ()
 Release exclusive access to this instance USBDevice. More...
 
virtual void assert_locked ()
 Assert that the current thread of execution holds the lock. More...
 

Detailed Description

Core USB Device driver.

USB driver which wraps and provides synchronization for a USBPhy object.

Definition at line 38 of file USBDevice.h.

Constructor & Destructor Documentation

◆ USBDevice()

USBDevice ( USBPhy phy,
uint16_t  vendor_id,
uint16_t  product_id,
uint16_t  product_release 
)

Instantiate a new USBDevice with the given parameters.

Parameters
phyThe USBPhy providing physical USB access
vendor_idThe USB vendor ID
product_idThe USB product ID
product_releaseThe device release number

◆ ~USBDevice()

virtual ~USBDevice ( )
virtual

Cleanup this USBDevice.

This USBDevice must be uninitialized when the destructor is called or the behavior is undefined.

Member Function Documentation

◆ init()

void init ( )

Initialize this instance.

This function must be called before calling any other functions of this class, unless specifically

◆ deinit()

void deinit ( )

Power down this instance.

Disable interrupts and stop sending events. This method can be used for temporary power-saving; This call can allow USB to be temporarily disabled to permit power saving. However, it is up to the user to make sure all the transfers have concluded (for example when USB power is lost). USBDevice::connect can be used to resume USB operation.

◆ configured()

bool configured ( )

Check if the device is configured.

Returns
true if configured, false otherwise

◆ connect()

void connect ( )

Connect a device This method can also be used to resume USB operation when USB power is detected after it was suspended via USBDevice::deinit.

◆ disconnect()

void disconnect ( )

Disconnect a device.

◆ sof_enable()

void sof_enable ( )

Enable the start of frame interrupt.

Call USBDevice::callback_sof on every frame.

◆ sof_disable()

void sof_disable ( )

Disable the start of frame interrupt.

Stop calling USBDevice::callback_sof.

◆ endpoint_add() [1/2]

bool endpoint_add ( usb_ep_t  endpoint,
uint32_t  max_packet,
usb_ep_type_t  type,
mbed::Callback< void()>  callback = nullptr 
)

Add an endpoint.

Parameters
endpointEndpoint to enable
max_packetMaximum size of a packet which can be sent or received on this endpoint
typeEndpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO
callbackMethod pointer to be called when a packet is transferred
Returns
true if successful, false otherwise

◆ endpoint_add() [2/2]

bool endpoint_add ( usb_ep_t  endpoint,
uint32_t  max_packet,
usb_ep_type_t  type,
void(T::*)()  callback 
)

Add an endpoint.

Parameters
endpointEndpoint to enable
max_packetMaximum size of a packet which can be sent or received on this endpoint
typeEndpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO
callbackMethod pointer to be called when a packet is transferred
Returns
true if successful, false otherwise

Definition at line 162 of file USBDevice.h.

◆ endpoint_remove()

void endpoint_remove ( usb_ep_t  endpoint)

Remove an endpoint.

Parameters
endpointEndpoint to disable
Note
This endpoint must already have been setup with endpoint_add

◆ endpoint_remove_all()

void endpoint_remove_all ( )

Remove all non-zero endpoints.

◆ endpoint_stall()

void endpoint_stall ( usb_ep_t  endpoint)

Stall an endpoint.

If there is an ongoing transfer on this endpoint then it will be aborted.

Parameters
endpointEndpoint to stall
Note
You cannot stall endpoint 0 with this function
This endpoint must already have been setup with endpoint_add

◆ endpoint_unstall()

void endpoint_unstall ( usb_ep_t  endpoint)

Un-stall an endpoint.

Un-stalling an endpoint resets data toggle back to DATA0. Additionally, if there is an ongoing transfer on this endpoint it will be aborted.

Parameters
endpointEndpoint to un-stall
Note
This endpoint must already have been setup with endpoint_add

◆ endpoint_max_packet_size()

uint32_t endpoint_max_packet_size ( usb_ep_t  endpoint)

Get the current maximum size for this endpoint.

Return the currently configured maximum packet size, wMaxPacketSize, for this endpoint.

Note
This endpoint must already have been setup with endpoint_add

◆ endpoint_abort()

void endpoint_abort ( usb_ep_t  endpoint)

Abort the current transfer on this endpoint.

Parameters
endpointendpoint with transfer to abort
Note
This endpoint must already have been setup with endpoint_add

◆ read_start()

bool read_start ( usb_ep_t  endpoint,
uint8_t *  buffer,
uint32_t  size 
)

start a read on the given endpoint

Start a read on the given endpoint. The data buffer must remain unchanged until the transfer either completes or is aborted.

Parameters
endpointendpoint to read data from
bufferbuffer to fill with read data
sizeThe size of data to read. This must be greater than or equal to the max packet size for this endpoint
Returns
true if the read was completed, otherwise false
Note
This endpoint must already have been setup with endpoint_add

◆ read_finish()

uint32_t read_finish ( usb_ep_t  endpoint)

Get the status of a read.

Parameters
endpointendpoint to get the status of
Returns
number of bytes read by this endpoint

◆ write_start()

bool write_start ( usb_ep_t  endpoint,
uint8_t *  buffer,
uint32_t  size 
)

Write a data to the given endpoint.

Write data to an endpoint. The data sent must remain unchanged until the transfer either completes or is aborted.

Parameters
endpointendpoint to write data to
bufferdata to write
sizethe size of data to send. This must be less than or equal to the max packet size of this endpoint
Note
This endpoint must already have been setup with endpoint_add

◆ write_finish()

uint32_t write_finish ( usb_ep_t  endpoint)

Get the status of a write.

Parameters
endpointendpoint to get the status of
Returns
number of bytes sent by this endpoint

◆ callback_power()

virtual void callback_power ( bool  powered)
protectedvirtual

Called by USBDevice layer on power state change.

Parameters
poweredtrue if device is powered, false otherwise

Warning: Called in ISR context

Definition at line 342 of file USBDevice.h.

◆ callback_sof()

virtual void callback_sof ( int  frame_number)
protectedvirtual

Called by USBDevice layer on each new USB frame.

Callbacks are enabled and disabled by calling sof_enable and sof_disable.

Parameters
frame_numberThe current frame number

Warning: Called in ISR context

Definition at line 357 of file USBDevice.h.

◆ callback_reset()

virtual void callback_reset ( )
protectedvirtual

Called by USBDevice layer on bus reset.

complete_reset must be called after the device is fully reset.

Warning: Called in ISR context

Reimplemented in USBCDC_ECM, and USBCDC.

Definition at line 370 of file USBDevice.h.

◆ callback_state_change()

virtual void callback_state_change ( DeviceState  new_state)
protectedpure virtual

Called when USB changes state.

Parameters
new_stateThe new state of the USBDevice

Warning: Called in ISR context

Implemented in USBCDC_ECM, USBAudio, USBCDC, USBHID, and USBMIDI.

◆ callback_request()

virtual void callback_request ( const setup_packet_t setup)
protectedpure virtual

Called by USBDevice on Endpoint0 request.

This is used to handle extensions to standard requests and class specific requests. The function complete_request must be always be called in response to this callback.

Warning: Called in ISR context

Implemented in USBCDC_ECM, USBAudio, USBCDC, USBHID, and USBMIDI.

◆ complete_request()

void complete_request ( RequestResult  result,
uint8_t *  data = NULL,
uint32_t  size = 0 
)
protected

Called to complete the setup stage of a callback request.

Possible options that can be passed as a result are:

  • Receive - Start the data OUT phase of this control transfer
  • Send - Start the data IN phase of this control transfer
  • Success - Operation was a success so start the status phase
  • Failure - Operation failed or is unsupported so send a stall
  • PassThrough - Pass on the request for standard processing
Parameters
resultThe result of the setup phase.
dataBuffer to send or receive if the result is Send or Receive
sizeSize to transfer if the result is Send or Receive

◆ callback_request_xfer_done()

virtual void callback_request_xfer_done ( const setup_packet_t setup,
bool  aborted 
)
protectedpure virtual

Called by USBDevice on data stage completion.

The function complete_request_xfer_done must be always be called in response to this callback.

Parameters
setupSetup packet of the current request
abortedfalse if the operation was aborted, true otherwise

Warning: Called in ISR context

Implemented in USBCDC_ECM, USBAudio, USBCDC, USBHID, and USBMIDI.

◆ complete_request_xfer_done()

void complete_request_xfer_done ( bool  success)
protected

Called to complete the data stage of a callback request.

Parameters
successtrue if the operation was successful, false otherwise

◆ complete_set_configuration()

void complete_set_configuration ( bool  success)
protected

Called to complete a set configuration command.

Parameters
successtrue if the configuration was set, false otherwise

◆ complete_set_interface()

void complete_set_interface ( bool  success)
protected

Called to complete a set interface command.

Parameters
successtrue if the interface was set, false otherwise

◆ find_descriptor()

uint8_t * find_descriptor ( uint8_t  descriptor_type,
uint8_t  index = 0 
)
protected

Find a descriptor type inside the configuration descriptor.

Parameters
descriptor_typeType of descriptor to find
indexConfiguration descriptor index ( 0 if only one configuration present )
Returns
A descriptor of the given type or NULL if none were found

◆ endpoint_table()

const usb_ep_table_t * endpoint_table ( )
protected

Get the endpoint table of this device.

Returns
Endpoint table of the USBPhy attached to this USBDevice

◆ start_process()

virtual void start_process ( )
protectedvirtual

Callback called to indicate the USB processing needs to be done.

Implements USBPhyEvents.

◆ lock()

virtual void lock ( )
protectedvirtual

Acquire exclusive access to this instance USBDevice.

◆ unlock()

virtual void unlock ( )
protectedvirtual

Release exclusive access to this instance USBDevice.

◆ assert_locked()

virtual void assert_locked ( )
protectedvirtual

Assert that the current thread of execution holds the lock.