Mistake on this page?
Report an issue in GitHub or email us
Public Member Functions | Protected Member Functions
USBAudio Class Reference

USBAudio example. More...

#include <USBAudio.h>

Inheritance diagram for USBAudio:
USBDevice USBPhyEvents

Public Member Functions

 USBAudio (bool connect=true, uint32_t frequency_rx=48000, uint8_t channel_count_rx=1, uint32_t frequency_tx=8000, uint8_t channel_count_tx=1, uint32_t buffer_ms=10, uint16_t vendor_id=0x7bb8, uint16_t product_id=0x1111, uint16_t product_release=0x0100)
 Basic constructor. More...
 
 USBAudio (USBPhy *phy, uint32_t frequency_rx, uint8_t channel_count_rx, uint32_t frequency_tx, uint8_t channel_count_tx, uint32_t buffer_ms, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
 Fully featured constructor. More...
 
virtual ~USBAudio ()
 Destroy this object. More...
 
void connect ()
 Connect USBAudio. More...
 
void disconnect ()
 Disconnect USBAudio. More...
 
bool read (uint8_t *buf, uint32_t size)
 Read audio data. More...
 
void read_nb (uint8_t *buf, uint32_t size, uint32_t *actual)
 Nonblocking audio data read. More...
 
uint32_t read_overflows (bool clear=false)
 Return the number read packets dropped due to overflow. More...
 
bool read_ready ()
 Check if the audio read channel is open. More...
 
void read_wait_ready ()
 Wait until the audio read channel is open. More...
 
bool write (uint8_t *buf, uint32_t size)
 Write audio data. More...
 
void write_nb (uint8_t *buf, uint32_t size, uint32_t *actual)
 Nonblocking audio data write. More...
 
uint32_t write_underflows (bool clear=false)
 Return the number write packets not sent due to underflow. More...
 
bool write_ready ()
 Check if the audio write channel is open. More...
 
void write_wait_ready ()
 Wait until the audio write channel is open. More...
 
float get_volume ()
 Get current volume between 0.0 and 1.0. More...
 
void attach (mbed::Callback< void()> &cb)
 Attach a Callback to update the volume. More...
 
void attach_tx (mbed::Callback< void(AudioEvent)> &cb)
 attach a Callback to Tx Done More...
 
void attach_rx (mbed::Callback< void(AudioEvent)> &cb)
 attach a Callback to Rx Done More...
 

Protected Member Functions

virtual void callback_state_change (DeviceState new_state)
 Called when USB changes state. More...
 
virtual void callback_request (const setup_packet_t *setup)
 Called by USBDevice on Endpoint0 request. More...
 
virtual void callback_request_xfer_done (const setup_packet_t *setup, bool aborted)
 Called by USBDevice on data stage completion. More...
 
void init ()
 Initialize this instance. More...
 
void deinit ()
 Power down this instance. More...
 
bool configured ()
 Check if the device is configured. 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=NULL)
 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...
 
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...
 
void complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0)
 Called to complete the setup stage of a callback request. 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

USBAudio example.

#include "mbed.h"
#include "USBAudio.h"
// Audio loopback example use:
// 1. Select "Mbed Audio" as your sound device
// 2. Play a song or audio file
// 3. Record the output using a program such as Audacity
int main() {
USBAudio audio(true, 44100, 2, 44100, 2);
printf("Looping audio\r\n");
static uint8_t buf[128];
while (true) {
if (!audio.read(buf, sizeof(buf))) {
memset(buf, 0, sizeof(buf));
}
audio.write(buf, sizeof(buf));
}
}

Definition at line 69 of file USBAudio.h.

Constructor & Destructor Documentation

USBAudio ( bool  connect = true,
uint32_t  frequency_rx = 48000,
uint8_t  channel_count_rx = 1,
uint32_t  frequency_tx = 8000,
uint8_t  channel_count_tx = 1,
uint32_t  buffer_ms = 10,
uint16_t  vendor_id = 0x7bb8,
uint16_t  product_id = 0x1111,
uint16_t  product_release = 0x0100 
)

Basic constructor.

Construct this object optionally connecting.

Note
Do not use this constructor in derived classes.
Parameters
connectCall connect on initialization
frequency_rxfrequency in Hz (default: 48000)
channel_count_rxchannel number (1 or 2) (default: 1)
frequency_txfrequency in Hz (default: 8000)
channel_count_txchannel number (1 or 2) (default: 1)
buffer_mstime audio can be buffered without overflowing in milliseconds
vendor_idYour vendor_id
product_idYour product_id
product_releaseYour product_release
USBAudio ( USBPhy phy,
uint32_t  frequency_rx,
uint8_t  channel_count_rx,
uint32_t  frequency_tx,
uint8_t  channel_count_tx,
uint32_t  buffer_ms,
uint16_t  vendor_id,
uint16_t  product_id,
uint16_t  product_release 
)

Fully featured constructor.

Construct this object with the supplied USBPhy and parameters. The user this object is responsible for calling connect() or init().

Note
Derived classes must use this constructor and call init() or connect() themselves. Derived classes should also call deinit() in their destructor. This ensures that no interrupts can occur when the object is partially constructed or destroyed.
Parameters
phyUSB phy to use
frequency_rxfrequency in Hz (default: 48000)
channel_count_rxchannel number (1 or 2) (default: 1)
frequency_txfrequency in Hz (default: 8000)
channel_count_txchannel number (1 or 2) (default: 1)
buffer_mstime audio can be buffered without overflowing in milliseconds
vendor_idYour vendor_id
product_idYour product_id
product_releaseYour product_release
virtual ~USBAudio ( )
virtual

Destroy this object.

Any classes which inherit from this class must call deinit before this destructor runs.

Member Function Documentation

virtual void assert_locked ( )
protectedvirtualinherited

Assert that the current thread of execution holds the lock.

void attach ( mbed::Callback< void()> &  cb)

Attach a Callback to update the volume.

Parameters
cbCallback to attach
void attach_rx ( mbed::Callback< void(AudioEvent)> &  cb)

attach a Callback to Rx Done

Parameters
cbCallback to attach
void attach_tx ( mbed::Callback< void(AudioEvent)> &  cb)

attach a Callback to Tx Done

Parameters
cbCallback to attach
virtual void callback_power ( bool  powered)
protectedvirtualinherited

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.

virtual void callback_request ( const setup_packet_t setup)
protectedvirtual

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

Implements USBDevice.

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

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

Implements USBDevice.

virtual void callback_reset ( )
protectedvirtualinherited

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, USBCDC, and USBTester.

Definition at line 370 of file USBDevice.h.

virtual void callback_sof ( int  frame_number)
protectedvirtualinherited

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.

virtual void callback_state_change ( DeviceState  new_state)
protectedvirtual

Called when USB changes state.

Parameters
new_stateThe new state of the USBDevice

Warning: Called in ISR context

Implements USBDevice.

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

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
void complete_request_xfer_done ( bool  success)
protectedinherited

Called to complete the data stage of a callback request.

Parameters
successtrue if the operation was successful, false otherwise
void complete_set_configuration ( bool  success)
protectedinherited

Called to complete a set configuration command.

Parameters
successtrue if the configuration was set, false otherwise
void complete_set_interface ( bool  success)
protectedinherited

Called to complete a set interface command.

Parameters
successtrue if the interface was set, false otherwise
bool configured ( )
inherited

Check if the device is configured.

Returns
true if configured, false otherwise
void connect ( )

Connect USBAudio.

void deinit ( )
inherited

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.

void disconnect ( )

Disconnect USBAudio.

This unblocks all calls to read_ready and write_ready.

void endpoint_abort ( usb_ep_t  endpoint)
inherited

Abort the current transfer on this endpoint.

Parameters
endpointendpoint with transfer to abort
Note
This endpoint must already have been setup with endpoint_add
bool endpoint_add ( usb_ep_t  endpoint,
uint32_t  max_packet,
usb_ep_type_t  type,
mbed::Callback< void()>  callback = NULL 
)
inherited

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
bool endpoint_add ( usb_ep_t  endpoint,
uint32_t  max_packet,
usb_ep_type_t  type,
void(T::*)()  callback 
)
inherited

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.

uint32_t endpoint_max_packet_size ( usb_ep_t  endpoint)
inherited

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
void endpoint_remove ( usb_ep_t  endpoint)
inherited

Remove an endpoint.

Parameters
endpointEndpoint to disable
Note
This endpoint must already have been setup with endpoint_add
void endpoint_remove_all ( )
inherited

Remove all non-zero endpoints.

void endpoint_stall ( usb_ep_t  endpoint)
inherited

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
const usb_ep_table_t* endpoint_table ( )
protectedinherited

Get the endpoint table of this device.

Returns
Endpoint table of the USBPhy attached to this USBDevice
void endpoint_unstall ( usb_ep_t  endpoint)
inherited

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
uint8_t* find_descriptor ( uint8_t  descriptor_type,
uint8_t  index = 0 
)
protectedinherited

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
float get_volume ( )

Get current volume between 0.0 and 1.0.

Returns
volume
void init ( )
inherited

Initialize this instance.

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

virtual void lock ( )
protectedvirtualinherited

Acquire exclusive access to this instance USBDevice.

bool read ( uint8_t *  buf,
uint32_t  size 
)

Read audio data.

Parameters
bufpointer on a buffer which will be filled with audio data
sizesize to read
Returns
true if successful
uint32_t read_finish ( usb_ep_t  endpoint)
inherited

Get the status of a read.

Parameters
endpointendpoint to get the status of
Returns
number of bytes read by this endpoint
void read_nb ( uint8_t *  buf,
uint32_t  size,
uint32_t *  actual 
)

Nonblocking audio data read.

Read the available audio data.

Parameters
bufpointer on a buffer which will be filled with audio data
sizesize to read
actualsize actually read
Note
This function is safe to call from USBAudio callbacks.
uint32_t read_overflows ( bool  clear = false)

Return the number read packets dropped due to overflow.

Parameters
clearReset the overflow count back to 0
Returns
Number of packets dropped due to overflow
bool read_ready ( )

Check if the audio read channel is open.

Returns
true if the audio read channel open, false otherwise
bool read_start ( usb_ep_t  endpoint,
uint8_t *  buffer,
uint32_t  size 
)
inherited

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
void read_wait_ready ( )

Wait until the audio read channel is open.

void sof_disable ( )
inherited

Disable the start of frame interrupt.

Stop calling USBDevice::callback_sof.

void sof_enable ( )
inherited

Enable the start of frame interrupt.

Call USBDevice::callback_sof on every frame.

virtual void start_process ( )
protectedvirtualinherited

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

Implements USBPhyEvents.

virtual void unlock ( )
protectedvirtualinherited

Release exclusive access to this instance USBDevice.

bool write ( uint8_t *  buf,
uint32_t  size 
)

Write audio data.

Parameters
bufpointer to audio data to write
sizesize to write
Returns
true if successful
uint32_t write_finish ( usb_ep_t  endpoint)
inherited

Get the status of a write.

Parameters
endpointendpoint to get the status of
Returns
number of bytes sent by this endpoint
void write_nb ( uint8_t *  buf,
uint32_t  size,
uint32_t *  actual 
)

Nonblocking audio data write.

Write the available audio data.

Parameters
bufpointer to audio data to write
sizesize to write
actualactual size written
Note
This function is safe to call from USBAudio callbacks.
bool write_ready ( )

Check if the audio write channel is open.

Returns
true if the audio write channel open, false otherwise
bool write_start ( usb_ep_t  endpoint,
uint8_t *  buffer,
uint32_t  size 
)
inherited

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
uint32_t write_underflows ( bool  clear = false)

Return the number write packets not sent due to underflow.

Parameters
clearReset the underflow count back to 0
Returns
Number of packets that should have been sent but weren't due to overflow
void write_wait_ready ( )

Wait until the audio write channel is open.

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.