USB Component  Version 6.17.0
MDK Middleware for USB Device and Host Communication

User API reference of the Audio Device Class. More...

Functions

void USBD_ADCn_Initialize (void)
 Callback function called during USBD_Initialize to initialize the USB ADC class instance. More...
 
void USBD_ADCn_Uninitialize (void)
 Callback function called during USBD_Uninitialize to de-initialize the USB ADC class instance. More...
 
void USBD_ADCn_PlayStart (void)
 Callback function called when play started. More...
 
void USBD_ADCn_PlayStop (void)
 Callback function called when play stopped. More...
 
void USBD_ADCn_SetSpeakerMute (uint8_t ch, bool on)
 Callback function called when speaker mute setting changed. More...
 
void USBD_ADCn_SetSpeakerVolume (uint8_t ch, uint16_t vol)
 Callback function called when speaker volume setting changed. More...
 
void USBD_ADCn_RecordStart (void)
 Callback function called when recording started. More...
 
void USBD_ADCn_RecordStop (void)
 Callback function called when recording stopped. More...
 
void USBD_ADCn_SetMicrophoneMute (uint8_t ch, bool on)
 Callback function called when microphone mute setting changed. More...
 
void USBD_ADCn_SetMicrophoneVolume (uint8_t ch, uint16_t vol)
 Callback function called when microphone volume setting changed. More...
 
void USBD_ADCn_ReceivedSamples (void)
 Callback function called when data in USB buffer for speaker samples reaches half-full from empty. More...
 
usbStatus USBD_ADC_SetSpeakerVolumeRange (uint8_t instance, uint8_t ch, uint16_t min, uint16_t max, uint16_t res, uint16_t cur)
 Set range for speaker volume control. More...
 
usbStatus USBD_ADC_SetMicrophoneVolumeRange (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. More...
 
uint32_t USBD_ADC_ReceivedSamplesAvailable (uint8_t instance)
 Number of audio samples received from USB Host and available to be read. More...
 
uint32_t USBD_ADC_WrittenSamplesPending (uint8_t instance)
 Number of audio samples written and pending to be sent to USB Host. More...
 
int32_t USBD_ADC_ReadSamples (uint8_t instance, void *buf, int32_t num)
 Read audio samples received from USB Host. More...
 
int32_t USBD_ADC_WriteSamples (uint8_t instance, const void *buf, int32_t num)
 Write audio samples to be transferred to USB Host. More...
 

Description

User API reference of the Audio Device Class.

Function Documentation

◆ USBD_ADCn_Initialize()

USBD_ADCn_Initialize ( void  )

Callback function called during USBD_Initialize to initialize the USB ADC class instance.

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's needs.

Code Example

void USBD_ADC0_Initialize (void) {
USBD_ADC_SetSpeakerVolumeRange (0, 0, 0, 100, 1, 50);
USBD_ADC_SetMicrophoneVolumeRange (0, 0, 0, 100, 1, 50);
}

◆ USBD_ADCn_Uninitialize()

USBD_ADCn_Uninitialize ( void  )

Callback function called during USBD_Uninitialize to de-initialize the USB ADC class instance.

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.

◆ USBD_ADCn_PlayStart()

USBD_ADCn_PlayStart ( void  )

Callback function called when play started.

Returns
none.

The callback function USBD_ADCn_PlayStart is called when a playback was started by the USB Host.

Code Example

void USBD_ADC0_PlayStart (void) {
Audio_Start (AUDIO_STREAM_OUT); // Start playback audio stream
}

◆ USBD_ADCn_PlayStop()

USBD_ADCn_PlayStop ( void  )

Callback function called when play stopped.

Returns
none.

The callback function USBD_ADCn_PlayStart is called when a playback was stopped by the USB Host.

Code Example

void USBD_ADC0_PlayStop (void) {
Audio_Stop (AUDIO_STREAM_OUT); // Stop playback audio stream
}

◆ USBD_ADCn_SetSpeakerMute()

USBD_ADCn_SetSpeakerMute ( uint8_t  ch,
bool  on 
)

Callback function called when speaker mute setting changed.

Parameters
[in]chchannel index :
  • value 0 : master channel
  • value 1 : left speaker (in stereo mode)
  • value 2 : right speaker (in stereo mode)
[in]oncurrent mute setting :
  • value false : mute off
  • value true : mute on
Returns
none.

The callback function USBD_ADCn_SetSpeakerMute is called when the speaker mute setting was changed by the USB Host.

The argument ch specifies the channel that is used.

The argument on specifies the activated mute setting.

Code Example

void USBD_ADC0_SetSpeakerMute (uint8_t ch, bool on) {
if (on) {
// start scaling samples to 0
} else {
// start scaling samples to volume
}
}

◆ USBD_ADCn_SetSpeakerVolume()

USBD_ADCn_SetSpeakerVolume ( uint8_t  ch,
uint16_t  vol 
)

Callback function called when speaker volume setting changed.

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

The callback function USBD_ADCn_SetSpeakerVolume is called when the speaker volume setting was changed by the USB Host.

The argument ch specifies the channel that is used.

The argument vol specifies the changed volume setting.

Code Example

void USBD_ADC0_SetSpeakerVolume (uint8_t ch, uint16_t vol) {
// store new volume and use it for scaling samples
}

◆ USBD_ADCn_RecordStart()

USBD_ADCn_RecordStart ( void  )

Callback function called when recording started.

Returns
none.

The callback function USBD_ADCn_RecordStart is called when a recording was started by the USB Host.

Code Example

void USBD_ADC0_RecordStart (void) {
Audio_Start (AUDIO_STREAM_IN); // Start recording audio stream
}

◆ USBD_ADCn_RecordStop()

USBD_ADCn_RecordStop ( void  )

Callback function called when recording stopped.

Returns
none.

The callback function USBD_ADCn_RecordStop is called when a recording was stopped by the USB Host.

Code Example

void USBD_ADC0_RecordStop (void) {
Audio_Stop (AUDIO_STREAM_IN); // Stop recording audio stream
}

◆ USBD_ADCn_SetMicrophoneMute()

USBD_ADCn_SetMicrophoneMute ( uint8_t  ch,
bool  on 
)

Callback function called when microphone mute setting changed.

Parameters
[in]chchannel index :
  • value 0 : master channel
  • value 1 : left microphone (in stereo mode)
  • value 2 : right microphone (in stereo mode)
[in]oncurrent mute setting
  • value false : mute off
  • value true : mute on
Returns
none.

The callback function USBD_ADCn_SetMicrophoneMute is called when the microphone mute setting was changed by the USB Host.

The argument ch specifies the channel that is used.

The argument on specifies the activated mute setting.

Code Example

void USBD_ADC0_SetMicrophoneMute (uint8_t ch, bool on) {
if (on) {
// start scaling samples to 0
} else {
// start scaling samples to volume
}
}

◆ USBD_ADCn_SetMicrophoneVolume()

USBD_ADCn_SetMicrophoneVolume ( uint8_t  ch,
uint16_t  vol 
)

Callback function called when microphone volume setting changed.

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

The callback function USBD_ADCn_SetMicrophoneVolume is called when the microphone volume setting was changed by the USB Host.

The argument ch specifies the channel that is used.

The argument vol specifies the changed volume setting.

Code Example

void USBD_ADC0_SetMicrophoneVolume (uint8_t ch, uint16_t vol) {
// store new volume and use it for scaling samples
}

◆ USBD_ADCn_ReceivedSamples()

USBD_ADCn_ReceivedSamples ( void  )

Callback function called when data in USB buffer for speaker samples reaches half-full from empty.

Returns
none.

The function USBD_ADCn_ReceivedSamples notifies that half of USB buffer for audio samples has been received starting from empty buffer.

Modify this function to the application needs.

◆ USBD_ADC_SetSpeakerVolumeRange()

usbStatus USBD_ADC_SetSpeakerVolumeRange ( 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_SetSpeakerVolumeRange 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_SetSpeakerVolumeRange (0, 0, 0, 100, 1, 50);

◆ USBD_ADC_SetMicrophoneVolumeRange()

usbStatus USBD_ADC_SetMicrophoneVolumeRange ( 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_SetMicrophoneVolumeRange 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_SetMicrophoneVolumeRange (0, 0, 0, 100, 1, 50);

◆ USBD_ADC_ReceivedSamplesAvailable()

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);
}

◆ USBD_ADC_WrittenSamplesPending()

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 were written and are pending. These signals must be still sent to the USB Host.

The argument instance specifies the instance of the ADC class.

See also
USBD_ADC_WriteSamples

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_ADC_ReadSamples()

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 (sample 0 = L0, sample 1 = R0, sample 2 = L1, sample 3 = R1, ...)

The argument num specifies the maximum number of samples to be read.

Code Example

uint16_t spkr_data[1024];
USBD_ADC_ReadSamples (0, (void *)spkr_data, 1024);

◆ USBD_ADC_WriteSamples()

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]numnumber 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 (sample 0 = L0, sample 1 = R0, sample 2 = L1, sample 3 = R1, ...)

The argument num specifies the maximum number of samples to be written.

Note
USB transferring will start when half of USB buffer for audio samples has been written.
See also
USBD_ADC_WrittenSamplesPending

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);
}