USB Component  Version 6.4
MDK-Professional Middleware for USB Device and Host
 All Data Structures Functions Variables Enumerations Enumerator Groups Pages
ADC: Audio Device Class Functions

Implement application specific behavior of an Audio Device Class (ADC) USB Device. More...

Content

 Configuration
 Configuration of the USB Device ADC Class in µVision.
 

Functions

void USBD_ADCn_Initialize (void)
 Called during USBD_Initialize to initialize the USB ADC class Device.
 
void USBD_ADCn_Uninitialize (void)
 Called during USBD_Uninitialize to de-initialize the USB ADC class Device.
 
void USBD_ADCn_SpeakerStatusEvent (bool active)
 Callback function called when speaker activity (interface) setting changed event.
 
void USBD_ADCn_SpeakerMuteEvent (uint8_t ch, bool cur)
 Callback function called when speaker mute setting changed event.
 
void USBD_ADCn_SpeakerVolumeEvent (uint8_t ch, uint16_t cur)
 Callback function called when speaker volume setting changed event.
 
void USBD_ADCn_MicrophoneStatusEvent (bool active)
 Callback function called when microphone activity (interface) setting changed event.
 
void USBD_ADCn_MicrophoneMuteEvent (uint8_t ch, bool cur)
 Callback function called when microphone mute setting changed event.
 
void USBD_ADCn_MicrophoneVolumeEvent (uint8_t ch, uint16_t cur)
 Callback function called when microphone volume setting changed event.
 
usbStatus USBD_ADC_SpeakerSetVolumeRange (uint8_t instance, uint8_t ch, uint16_t min, uint16_t max, uint16_t res, uint16_t cur)
 Set range for speaker volume control.
 
usbStatus USBD_ADC_MicrophoneSetVolumeRange (uint8_t instance, uint8_t ch, uint16_t min, uint16_t max, uint16_t res, uint16_t cur)
 Set range for microphone volume (level) control.
 
uint32_t USBD_ADC_ReceivedSamplesAvailable (uint8_t instance)
 Number of audio samples received from USB Host and available to be read.
 
uint32_t USBD_ADC_WrittenSamplesPending (uint8_t instance)
 Number of audio samples written and pending to be sent to USB Host.
 
int32_t USBD_ADC_ReadSamples (uint8_t instance, void *buf, int32_t num)
 Read audio samples received from USB Host.
 
int32_t USBD_ADC_WriteSamples (uint8_t instance, const void *buf, int32_t num)
 Write audio samples to be transferred to USB Host.
 

Description

Implement application specific behavior of an Audio Device Class (ADC) USB Device.

Use the following class specific functions to customize the functionality of an Audio Device Class (ADC) Device. Adapt these functions in the user code template file USBD_User_ADC_n.c.

Refer to:

The USB Component allows multiple instances of the ADC class. This feature is used to create USB Composite Devices. Each ADC class instance has a separate files and interface functions:

This documentation uses n as a placeholder for the instance number 0 - 3. Most applications only require one instance of an ADC class. For the first ADC class instance the instance number is 0:

Software Structure

The handling for the ADC class endpoint events is implemented in USBD_ADC_Thread which is started by USBD_Initialize. Each instance of an ADC class runs an instance of the USBD_ADC_Thread. This thread handles the Isochronous IN/OUT Endpoints of the USB ADC Device.

msc_inline_mscgraph_1

Implementation

To create an USB Device with an ADC class:

User Code Templates
There are two user code templates available that help to add support for an ADC device:

  1. USBD_User_ADC.c contains all the callback functions that need to be implemented by the user.
  2. USBD_User_ADC_Headphone.c is a code template for the application specific functionality of a USB ADC Device containing stereo speakers and mono microphone.

User Code Template USBD_User_ADC.c

The following source code can be used to implement the application specific behavior of an USB ADC Device.

/*------------------------------------------------------------------------------
* MDK Middleware - Component ::USB:Device
* Copyright (c) 2004-2014 ARM Germany GmbH. All rights reserved.
*------------------------------------------------------------------------------
* Name: USBD_User_ADC_n.c
* Purpose: USB Device Audio Device Class (ADC) User module
* Rev.: V6.2
*----------------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include "rl_usb.h"
// Called during USBD_Initialize to initialize the USB ADC class Device.
void USBD_ADCn_Initialize (void) {
// Add code for initialization
}
// Called during USBD_Uninitialize to un-initialize the USB ADC class Device.
// Add code for de-initialization
}
// Callback function called when speaker activity (interface) setting changed event.
// \param[in] active activity status.
void USBD_ADCn_SpeakerStatusEvent (bool active) {
// Add code for speaker activity setting change
}
// Callback function called when speaker mute setting changed event.
// \param[in] ch channel index.
// - value 0: master channel
// - value 1: left speaker (in stereo mode)
// - value 2: right speaker (in stereo mode)
// \param[in] cur current setting.
void USBD_ADCn_SpeakerMuteEvent (uint8_t ch, bool cur) {
// Add code for speaker mute setting change
}
// Callback function called when speaker volume setting changed event.
// \param[in] ch channel index.
// - value 0: master channel
// - value 1: left speaker (in stereo mode)
// - value 2: right speaker (in stereo mode)
// \param[in] cur current setting.
void USBD_ADCn_SpeakerVolumeEvent (uint8_t ch, uint16_t cur) {
// Add code for speaker volume setting change
}
// Callback function called when microphone activity (interface) setting changed event.
// \param[in] active activity status.
void USBD_ADCn_MicrophoneStatusEvent (bool active) {
// Add code for microphone activity setting change
}
// Callback function called when microphone mute setting changed event.
// \param[in] ch channel index.
// - value 0: master channel
// - value 1: left microphone (in stereo mode)
// - value 2: right microphone (in stereo mode)
// \param[in] cur current setting.
void USBD_ADCn_MicrophoneMuteEvent (uint8_t ch, bool cur) {
// Add code for microphone mute setting change
}
// Callback function called when microphone volume setting changed event.
// \param[in] ch channel index.
// - value 0: master channel
// - value 1: left microphone (in stereo mode)
// - value 2: right microphone (in stereo mode)
// \param[in] cur current setting.
void USBD_ADCn_MicrophoneVolumeEvent (uint8_t ch, uint16_t cur) {
// Add code for microphone volume setting change
}

User Code Template USBD_User_ADC_Headphone.c

The following source code can be used to implement the application specific behavior of an USB ADC Headphone Device.

/*------------------------------------------------------------------------------
* MDK Middleware - Component ::USB:Device
* Copyright (c) 2004-2015 ARM Germany GmbH. All rights reserved.
*------------------------------------------------------------------------------
* Name: USBD_User_ADC_Headphone_n.c
* Purpose: USB Device Audio Device Class (ADC) User module
* Template for USB Audio Device Class (ADC) Headphone
* Rev.: V6.3
*----------------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
#include "rl_usb.h"
#include ".\RTE\USB\USBD_Config_ADC_n.h"
#include "Board_Audio.h"
#define RECORD_BUFFER_SIZE_USB (USBD_ADC0_IN_BUF_SIZE)
#define PLAYBACK_BUFFER_SIZE_USB (USBD_ADC0_OUT_BUF_SIZE)
#define RECORD_CHANNELS 1 // Mono microphone = 1 channel (stereo not implemented)
#define RECORD_BUFFER_SIZE_AUDIO 64 // In samples (64 mono samples at 32 kHz = 2 ms of samples)
#define RECORD_SAMPLE_CORRECTION_COUNTER (100/2) // 100 ms / 2 ms of samples (compensates 625 ppm clock difference)
#define PLAYBACK_CHANNELS 2 // Stereo speakers = 2 channels (mono not implemented)
#define PLAYBACK_BUFFER_SIZE_AUDIO 128 // Out samples (128 stereo samples at 32 kHz = 2 ms samples)
#define PLAYBACK_SAMPLE_CORRECTION_COUNTER (100/2) // 100 ms / 2 ms of samples (compensates 625 ppm clock difference)
static osThreadId play_buf_tid = 0 ;
static volatile bool play = false;
static uint8_t play_buf_index = 0 ;
static int16_t play_buf[2][PLAYBACK_BUFFER_SIZE_AUDIO+2] = { 0 };
static int32_t play_num = { 0 };
static int32_t play_add = 0 ;
static uint16_t play_sample_correction_cnt = 0 ;
static uint8_t rec_buf_index = 0 ;
static int16_t rec_buf[2][RECORD_BUFFER_SIZE_AUDIO+1] = { 0 };
static int32_t rec_num = 0 ;
static int32_t rec_add = 0 ;
static uint16_t rec_sample_correction_cnt = 0 ;
// Thread: waits for USB buffer to be half full of samples and then starts playback.
// \param[in] arg not used.
void AudioPlaybackInitialBufferingThreadn (void const *arg) {
uint32_t play_samples_available;
int32_t play_num0;
while (1) {
play_samples_available = USBD_ADC_ReceivedSamplesAvailable (n);
if (!play && (play_samples_available > (PLAYBACK_BUFFER_SIZE_USB / 2))) {
// If play is not active and USB buffer is half full of samples
play = true;
play_num0 = USBD_ADC_ReadSamples (n, (void *)(&play_buf[0][0]), PLAYBACK_BUFFER_SIZE_AUDIO);
play_num = USBD_ADC_ReadSamples (n, (void *)(&play_buf[1][0]), PLAYBACK_BUFFER_SIZE_AUDIO);
play_buf_index = 1;
// Send initial data, further sending will happen from audio callback
Audio_SendData (&play_buf[0][0], play_num0);
}
if (play && !play_samples_available) {
// If play was active but there is no more samples than play has stopped
play = false;
}
osDelay(10);
}
}
osThreadDef (AudioPlaybackInitialBufferingThreadn, osPriorityNormal, 1, 0);
// Audio Events Handling Callback function
// \param[in] event notification mask
static void AudioCallback (uint32_t event) {
uint32_t rec_samples_pending;
int32_t rec_num_last;
uint8_t rec_correct_sample;
uint32_t play_samples_available;
int32_t play_num_last;
uint8_t play_correct_sample;
// Handling of recording samples
if (event & AUDIO_EVENT_RECEIVE_COMPLETE) {
rec_num_last = rec_num;
rec_num = RECORD_BUFFER_SIZE_AUDIO;
// Start new audio data reception
Audio_ReceiveData (&rec_buf[rec_buf_index ^ 1][0], rec_num);
rec_correct_sample = 0;
if (rec_sample_correction_cnt++ >= RECORD_SAMPLE_CORRECTION_COUNTER) {
// Correction interval counter for example if interval is 50 ms
// the clock difference of 625 ppm will be compensated at 32 kHz
// sampling rate
rec_sample_correction_cnt = 0;
rec_correct_sample = 1;
}
rec_samples_pending = USBD_ADC_WrittenSamplesPending (n);
if (rec_samples_pending < RECORD_BUFFER_SIZE_USB/8) {
rec_add = 0;
} else if (rec_samples_pending < RECORD_BUFFER_SIZE_USB/4) {
rec_add = 1;
} else if ((rec_samples_pending > 3*RECORD_BUFFER_SIZE_USB/8) &&
(rec_samples_pending < 5*RECORD_BUFFER_SIZE_USB/8)) {
rec_add = 0;
} else if (rec_samples_pending > 3*RECORD_BUFFER_SIZE_USB/4) {
rec_add = -1;
}
if (rec_correct_sample) { // Correct sample interval is active
if (rec_add == -1) { // Removing 1 sample (1 sample for mono microphone)
// Sample N-2 is average of N-3, N-2, N-1 and N samples
rec_buf[rec_buf_index][rec_num_last-2] = (rec_buf[rec_buf_index][rec_num_last-3] +
rec_buf[rec_buf_index][rec_num_last-2] +
rec_buf[rec_buf_index][rec_num_last-1] +
rec_buf[rec_buf_index][rec_num_last ])/4;
// Sample N moved to N-1
rec_buf[rec_buf_index][rec_num_last-1] = rec_buf[rec_buf_index][rec_num_last ];
}
if (rec_add == 1) { // Adding 1 new sample (1 sample for mono microphone)
// Sample N moved to N+1
rec_buf[rec_buf_index][rec_num_last+1] = rec_buf[rec_buf_index][rec_num_last ];
// Sample N is average of N-1 and N+1 samples
rec_buf[rec_buf_index][rec_num_last] = (rec_buf[rec_buf_index][rec_num_last-1] +
rec_buf[rec_buf_index][rec_num_last+1])/2;
}
}
// Send last received buffer of samples to USB
USBD_ADC_WriteSamples (n, &rec_buf[rec_buf_index][0], rec_num_last + (rec_add*RECORD_CHANNELS*rec_correct_sample));
rec_buf_index ^= 1; // Change active buffer index
}
// Handling of playback samples
if (event & AUDIO_EVENT_SEND_COMPLETE) {
if (play) {
play_num_last = play_num;
play_correct_sample = 0;
if (play_sample_correction_cnt++ >= PLAYBACK_SAMPLE_CORRECTION_COUNTER) {
// Correction interval counter for example if interval is 50 ms
// the clock difference of 625 ppm will be compensated at 32 kHz
// sampling rate
play_sample_correction_cnt = 0;
play_correct_sample = 1;
}
play_samples_available = USBD_ADC_ReceivedSamplesAvailable (0);
if (play_samples_available < PLAYBACK_BUFFER_SIZE_USB/8) {
rec_add = 0;
} else if (play_samples_available < PLAYBACK_BUFFER_SIZE_USB/4) {
rec_add = 1;
} else if ((play_samples_available > 3*PLAYBACK_BUFFER_SIZE_USB/8) &&
(play_samples_available < 5*PLAYBACK_BUFFER_SIZE_USB/8)) {
rec_add = 0;
} else if (play_samples_available > 3*PLAYBACK_BUFFER_SIZE_USB/4) {
rec_add = -1;
}
if (play_correct_sample) { // Correct sample interval is active
if (play_add == -1) { // Removing 1 sample (actually 2 for stereo (1 for each channel))
if (play_num >= 8) {
// Sample N-2 is average of N-3, N-2, N-1 and N samples
play_buf[play_buf_index][play_num-6] = (play_buf[play_buf_index][play_num-8] +
play_buf[play_buf_index][play_num-6] +
play_buf[play_buf_index][play_num-4] +
play_buf[play_buf_index][play_num-2])/4;
play_buf[play_buf_index][play_num-5] = (play_buf[play_buf_index][play_num-7] +
play_buf[play_buf_index][play_num-5] +
play_buf[play_buf_index][play_num-3] +
play_buf[play_buf_index][play_num-1])/4;
// Sample N moved to N-1
play_buf[play_buf_index][play_num-4] = play_buf[play_buf_index][play_num-2];
play_buf[play_buf_index][play_num-3] = play_buf[play_buf_index][play_num-1];
}
}
if (play_add == 1) { // Adding 1 new sample (actually 2 for stereo (1 for each channel))
if (play_num >= 4) {
// Sample N moved to N+1
play_buf[play_buf_index][play_num] = play_buf[play_buf_index][play_num-2];
play_buf[play_buf_index][play_num+1] = play_buf[play_buf_index][play_num-1];
// Sample N is average of N-1 and N+1 samples
play_buf[play_buf_index][play_num-2] = (play_buf[play_buf_index][play_num-4] +
play_buf[play_buf_index][play_num ])/2;
play_buf[play_buf_index][play_num-1] = (play_buf[play_buf_index][play_num-3] +
play_buf[play_buf_index][play_num+1])/2;
}
}
}
if (play_num_last > 0) {
// Start new audio data transmission
Audio_SendData (&play_buf[play_buf_index][0], play_num + (play_add*PLAYBACK_CHANNELS*play_correct_sample));
play_buf_index ^= 1; // Change active buffer index
// Read next buffer of samples from USB
play_num = USBD_ADC_ReadSamples (n, (void *)(&play_buf[play_buf_index][0]), PLAYBACK_BUFFER_SIZE_AUDIO);
}
}
}
}
// Called during USBD_Initialize to initialize the USB ADC class Device.
void USBD_ADCn_Initialize (void) {
// Set stereo speakers volume range to 0 to 100 with step 1, initially positioned at 50 %
USBD_ADC_SpeakerSetVolumeRange (n, 0, 0, 100, 1, 50);
// Initialize audio codec for:
// - stereo speakers
// - sampling rate 32 kHz
// - 16 bits per sample
// - not muted
// - volume at 50 %
//
// NOTE !!!
// - These values must correspond to settings in USBD_Config_ADC_n.h file
Audio_Initialize (&AudioCallback);
Audio_SetDataFormat (AUDIO_STREAM_OUT, AUDIO_DATA_16_STEREO);
Audio_SetFrequency (AUDIO_STREAM_OUT, 32000);
Audio_SetMute (AUDIO_STREAM_OUT, AUDIO_CHANNEL_MASTER, false);
Audio_SetVolume (AUDIO_STREAM_OUT, AUDIO_CHANNEL_MASTER, 50);
Audio_Start (AUDIO_STREAM_OUT);
// Set microphone volume range to 0 to 100 with step 1, initially positioned at 50 %
USBD_ADC_MicrophoneSetVolumeRange (n, 0, 0, 100, 1, 50);
// Initialize audio codec for:
// - mono microphone
// - sampling rate 32 kHz
// - 16 bits per sample
// - not muted
// - volume at 50 %
//
// NOTE !!!
// - These values must correspond to settings in USBD_Config_ADC_n.h file
Audio_SetFrequency (AUDIO_STREAM_IN, 32000);
Audio_SetDataFormat (AUDIO_STREAM_IN, AUDIO_DATA_16_MONO);
Audio_SetMute (AUDIO_STREAM_IN, AUDIO_CHANNEL_MASTER, false);
Audio_SetVolume (AUDIO_STREAM_IN, AUDIO_CHANNEL_MASTER, 50);
}
// Called during USBD_Uninitialize to un-initialize the USB ADC class Device.
Audio_Uninitialize ();
}
// Callback function called when speaker activity (interface) setting changed event.
// \param[in] active activity status.
void USBD_ADCn_SpeakerStatusEvent (bool active) {
if (active) { // If playback was activated
Audio_Start (AUDIO_STREAM_OUT); // Start playback audio stream
// Start initial playback samples buffering thread
play_buf_tid = osThreadCreate (osThread (AudioPlaybackInitialBufferingThreadn), NULL);
} else { // If playback was de-activated
if (play_buf_tid) { // If initial playback samples buffering thread is active
// Kill initial playback samples buffering thread
if (osThreadTerminate (play_buf_tid) == osOK) { play_buf_tid = 0; }
}
Audio_Stop (AUDIO_STREAM_OUT); // Stop playback audio stream
}
}
// Callback function called when speaker mute setting changed event.
// \param[in] ch channel index.
// \param[in] cur current setting.
void USBD_ADCn_SpeakerMuteEvent (uint8_t ch, bool cur) {
Audio_SetMute (AUDIO_STREAM_OUT, AUDIO_CHANNEL_MASTER, cur);
}
// Callback function called when speaker volume setting changed event.
// \param[in] ch channel index.
// \param[in] cur current setting.
void USBD_ADCn_SpeakerVolumeEvent (uint8_t ch, uint16_t cur) {
Audio_SetVolume (AUDIO_STREAM_OUT, ch, cur);
}
// Callback function called when microphone activity (interface) setting changed event.
// \param[in] active activity status.
void USBD_ADCn_MicrophoneStatusEvent (bool active) {
if (active) { // If recording was activated
rec_num = RECORD_BUFFER_SIZE_AUDIO; // Receive samples
rec_buf_index = 0; // Current receiving buffer index is 0
Audio_ReceiveData(&rec_buf[0][0],rec_num);// Start initial audio data reception
Audio_Start (AUDIO_STREAM_IN); // Start recording audio stream
} else { // If recording was de-activated
Audio_Stop (AUDIO_STREAM_IN); // Stop recording audio stream
}
}
// Callback function called when microphone mute setting changed event.
// \param[in] ch channel index.
// \param[in] cur current setting.
void USBD_ADCn_MicrophoneMuteEvent (uint8_t ch, bool cur) {
Audio_SetMute (AUDIO_STREAM_IN, AUDIO_CHANNEL_MASTER, cur);
}
// Callback function called when microphone volume setting changed event.
// \param[in] ch channel index.
// \param[in] cur current setting.
void USBD_ADCn_MicrophoneVolumeEvent (uint8_t ch, uint16_t cur) {
Audio_SetVolume (AUDIO_STREAM_IN, ch, cur);
}

Function Documentation

usbStatus USBD_ADC_MicrophoneSetVolumeRange ( uint8_t  instance,
uint8_t  ch,
uint16_t  min,
uint16_t  max,
uint16_t  res,
uint16_t  cur 
)

Set range for microphone volume (level) control.

Parameters
[in]instanceinstance of ADC class.
[in]chchannel index.
  • value 0: master channel
  • value 1: left microphone (in stereo mode)
  • value 2: right microphone (in stereo mode)
[in]minminimum volume value.
[in]maxmaximum volume value.
[in]resvolume resolution.
[in]curcurrent volume value.
Returns
status code that indicates the execution status of the function as defined with usbStatus.

The function USBD_ADC_MicrophoneSetVolumeRange sets the range for the microphone volume control.

The argument instance specifies the instance of the ADC class to be used.

The argument ch specifies the channel on the instance.

The argument min sets the minimum volume level.

The argument max sets the maximum volume level.

The argument res defines the volume resolution.

The argument cur sets the current volume level.

Code Example

USBD_ADC_MicrophoneSetVolumeRange (0, 0, 0, 100, 1, 50);
int32_t USBD_ADC_ReadSamples ( uint8_t  instance,
void *  buf,
int32_t  num 
)

Read audio samples received from USB Host.

Parameters
[in]instanceinstance of ADC class.
[out]bufbuffer that receives samples.
[in]nummaximum number of samples to read.
Returns
number of samples read or execution status.
  • value >= 0: number of samples read
  • value < 0: error occurred, -value is execution status as defined with usbStatus

The function USBD_ADC_ReadSamples reads audio samples that have been received from an USB Host.

The argument instance specifies the instance of the ADC class to be used.

The argument buf is a pointer to the buffer that stores the received samples. The data type is uint8_t, uint16_t or uint32_t and is specified by the USBD_ADCn_OUT_BBITRESOLUTION in USBD_Config_ADC_n.h file (USBD_ADCn_OUT_BBITRESOLUTION = 8 -> uint8_t, USBD_ADCn_OUT_BBITRESOLUTION = 16 -> uint16_t, USBD_ADCn_OUT_BBITRESOLUTION >= 24 -> uint32_t). If stereo mode is used channels are encoded one sample per channel (0 L, 0 R, 1 L, 1 R, ...)

The argument num specifies the maximum number of samples to be read. For stereo it specifies maximum number of sample pairs (left, right) to be read.

Code Example

uint16_t spkr_data[1024];
USBD_ADC_ReadSamples (0, (void *)spkr_data, 1024);
uint32_t USBD_ADC_ReceivedSamplesAvailable ( uint8_t  instance)

Number of audio samples received from USB Host and available to be read.

Parameters
[in]instanceinstance of ADC class.
Returns
number of samples available to be read.

The function USBD_ADC_ReceivedSamplesAvailable signals the number of audio samples that have been received from the USB Host and are available to be read.

The argument instance specifies the instance of the ADC class to be used.

Code Example

uint16_t spkr_data[1024];
// If number of samples available is more than 1024 read new samples
USBD_ADC_ReadSamples (0, (void *)spkr_data, 1024);
}
usbStatus USBD_ADC_SpeakerSetVolumeRange ( uint8_t  instance,
uint8_t  ch,
uint16_t  min,
uint16_t  max,
uint16_t  res,
uint16_t  cur 
)

Set range for speaker volume control.

Parameters
[in]instanceinstance of ADC class.
[in]chchannel index.
  • value 0: master channel
  • value 1: left speaker (in stereo mode)
  • value 2: right speaker (in stereo mode)
[in]minminimum volume value.
[in]maxmaximum volume value.
[in]resvolume resolution.
[in]curcurrent volume value.
Returns
status code that indicates the execution status of the function as defined with usbStatus.

The function USBD_ADC_SpeakerSetVolumeRange sets the range for the speaker volume control.

The argument instance specifies the instance of the ADC class to be used.

The argument ch specifies the channel on the instance.

The argument min sets the minimum volume level.

The argument max sets the maximum volume level.

The argument res defines the volume resolution.

The argument cur sets the current volume level.

Code Example

USBD_ADC_SpeakerSetVolumeRange (0, 0, 0, 100, 1, 50);
int32_t USBD_ADC_WriteSamples ( uint8_t  instance,
const void *  buf,
int32_t  num 
)

Write audio samples to be transferred to USB Host.

Parameters
[in]instanceinstance of ADC class.
[in]bufbuffer containing samples to write.
[in]nummaximum number of samples to write.
Returns
number of samples written or execution status.
  • value >= 0: number of samples written for sending
  • value < 0: error occurred, -value is execution status as defined with usbStatus

The function USBD_ADC_WriteSamples writes audio samples to be transferred to an USB Host.

The argument instance specifies the instance of the ADC class to be used.

The argument buf is a pointer to the buffer containing the samples to be written. The data type is uint8_t, uint16_t or uint32_t and is specified by the USBD_ADCn_IN_BBITRESOLUTION in USBD_Config_ADC_n.h file (USBD_ADCn_IN_BBITRESOLUTION = 8 -> uint8_t, USBD_ADCn_IN_BBITRESOLUTION = 16 -> uint16_t, USBD_ADCn_IN_BBITRESOLUTION >= 24 -> uint32_t). If stereo mode is used channels are encoded one sample per channel (0 L, 0 R, 1 L, 1 R, ...)

The argument num specifies the maximum number of samples to be written. For stereo it specifies maximum number of sample pairs (left, right) to be written.

Code Example

uint16_t mic_data[1024];
USBD_ADC_WriteSamples (0, (void *)mic_data, 1024);
uint32_t USBD_ADC_WrittenSamplesPending ( uint8_t  instance)

Number of audio samples written and pending to be sent to USB Host.

Parameters
[in]instanceinstance of ADC class.
Returns
number of samples ready to be sent.

The function USBD_ADC_WrittenSamplesPending signals the number of audio samples that have been written and are pending to be sent to the USB Host.

The argument instance specifies the instance of the ADC class to be used.

Code Example

uint16_t mic_data[1024];
// If number of samples pending is below 1024 write more samples
USBD_ADC_WriteSamples (0, (void *)mic_data, 1024);
}
USBD_ADCn_Initialize ( void  )

Called during USBD_Initialize to initialize the USB ADC class Device.

Returns
none.

The function USBD_ADCn_Initialize initializes the hardware resources of the Audio Device Class USB Device. It is called during USBD_Initialize. The function may be used to allocate resources and initialize peripherals. Modify this function to the application needs.

Code Example

void USBD_ADC0_Initialize (void) {
USBD_ADC_SpeakerSetVolumeRange (0, 0, 0, 100, 1, 50);
USBD_ADC_MicrophoneSetVolumeRange (0, 0, 0, 100, 1, 50);
}
USBD_ADCn_MicrophoneMuteEvent ( uint8_t  ch,
bool  cur 
)

Callback function called when microphone mute setting changed event.

Parameters
[in]chchannel index.
  • value 0: master channel
  • value 1: left microphone (in stereo mode)
  • value 2: right microphone (in stereo mode)
[in]curcurrent setting.
Returns
none.

The callback function USBD_ADCn_MicrophoneMuteEvent is called when the microphone went to mute/unmute.

The argument ch specifies the channel that is used.

The argument cur specifies the current mute setting.

Code Example

void USBD_ADC0_MicrophoneMuteEvent (uint8_t ch, bool cur) {
if (cur) {
// start scaling samples to 0
} else {
// start scaling samples to volume
}
}
USBD_ADCn_MicrophoneStatusEvent ( bool  active)

Callback function called when microphone activity (interface) setting changed event.

Parameters
[in]activeactivity status.
Returns
none.

The callback function USBD_ADCn_MicrophoneStatusEvent is called when a status change on the microphone has taken place.

The argument active specifies the activity status.

Code Example

void USBD_ADC0_MicrophoneStatusEvent (bool active) {
if (active) {
// start acquiring microphone samples
} else {
// stop acquiring microphone samples
}
}
USBD_ADCn_MicrophoneVolumeEvent ( uint8_t  ch,
uint16_t  cur 
)

Callback function called when microphone volume setting changed event.

Parameters
[in]chchannel index.
  • value 0: master channel
  • value 1: left microphone (in stereo mode)
  • value 2: right microphone (in stereo mode)
[in]curcurrent setting.
Returns
none.

The callback function USBD_ADCn_MicrophoneVolumeEvent is called when the microphone volume has changed.

The argument ch specifies the channel that is used.

The argument cur specifies the current volume setting.

Code Example

void USBD_ADC0_MicrophoneVolumeEvent (uint8_t ch, uint16_t cur) {
// store new volume and use it for scaling samples
}
USBD_ADCn_SpeakerMuteEvent ( uint8_t  ch,
bool  cur 
)

Callback function called when speaker mute setting changed event.

Parameters
[in]chchannel index.
  • value 0: master channel
  • value 1: left speaker (in stereo mode)
  • value 2: right speaker (in stereo mode)
[in]curcurrent setting.
Returns
none.

The callback function USBD_ADCn_SpeakerMuteEvent is called when the speaker went to mute/unmute.

The argument ch specifies the channel that is used.

The argument cur specifies the current mute setting.

Code Example

void USBD_ADC0_SpeakerMuteEvent (uint8_t ch, bool cur) {
if (cur) {
// start scaling samples to 0
} else {
// start scaling samples to volume
}
}
USBD_ADCn_SpeakerStatusEvent ( bool  active)

Callback function called when speaker activity (interface) setting changed event.

Parameters
[in]activeactivity status.
Returns
none.

The callback function USBD_ADCn_SpeakerStatusEvent is called when a status change on the speaker activity has taken place.

The argument active specifies the activity status.

Code Example

void USBD_ADC0_SpeakerStatusEvent (bool active) {
if (active) {
// start playing samples
} else {
// stop playing samples
}
}
USBD_ADCn_SpeakerVolumeEvent ( uint8_t  ch,
uint16_t  cur 
)

Callback function called when speaker volume setting changed event.

Parameters
[in]chchannel index.
  • value 0: master channel
  • value 1: left speaker (in stereo mode)
  • value 2: right speaker (in stereo mode)
[in]curcurrent setting.
Returns
none.

The callback function USBD_ADCn_SpeakerVolumeEvent is called when the speaker volume has changed.

The argument ch specifies the channel that is used.

The argument cur specifies the current volume setting.

Code Example

void USBD_ADC0_SpeakerVolumeEvent (uint8_t ch, uint16_t cur) {
// store new volume and use it for scaling samples
}
USBD_ADCn_Uninitialize ( void  )

Called during USBD_Uninitialize to de-initialize the USB ADC class Device.

Returns
none.

The function USBD_ADCn_Uninitialize de-initializes/releases the hardware resources of the Audio Device Class USB Device. It is called during USBD_Uninitialize. If USBD_ADCn_Initialize has been adapted to the application, USBD_ADCn_Uninitialize should release resources and de-initialize peripherals.