341 lines
9.3 KiB
C
341 lines
9.3 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include "bsp/board.h"
|
|
#include "tusb.h"
|
|
#include "usb_descriptors.h"
|
|
#include "class/audio/audio_device.h"
|
|
|
|
#ifdef HAVE_AUDIO
|
|
|
|
/* could also be 16kHz for AMR-WB calls? */
|
|
#define AUDIO_SAMPLE_RATE 8000
|
|
|
|
#undef TU_LOG2
|
|
#define TU_LOG2 printf
|
|
|
|
static uint32_t sampFreq;
|
|
static uint8_t clkValid;
|
|
|
|
static audio_control_range_4_n_t(1) sampleFreqRng; // Sample frequency range state
|
|
|
|
// Audio test data
|
|
static uint16_t test_buffer_audio[(CFG_TUD_AUDIO_EP_SZ - 2) / 2];
|
|
static uint16_t startVal = 0;
|
|
|
|
int audio_init(void)
|
|
{
|
|
TU_LOG2("%s()\r\n", __func__);
|
|
|
|
sampFreq = AUDIO_SAMPLE_RATE;
|
|
clkValid = 1;
|
|
|
|
sampleFreqRng.wNumSubRanges = 1;
|
|
sampleFreqRng.subrange[0].bMin = AUDIO_SAMPLE_RATE;
|
|
sampleFreqRng.subrange[0].bMax = AUDIO_SAMPLE_RATE;
|
|
sampleFreqRng.subrange[0].bRes = 0;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Invoked when audio class specific set request received for an EP
|
|
bool tud_audio_set_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff)
|
|
{
|
|
(void) rhport;
|
|
(void) pBuff;
|
|
|
|
// We do not support any set range requests here, only current value requests
|
|
TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR);
|
|
|
|
// Page 91 in UAC2 specification
|
|
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
|
|
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
|
|
uint8_t ep = TU_U16_LOW(p_request->wIndex);
|
|
|
|
(void) channelNum; (void) ctrlSel; (void) ep;
|
|
|
|
TU_LOG2("%s(chan=%u, sel=%u, ep=0x%02x)\r\n", __func__, channelNum, ctrlSel, ep);
|
|
|
|
return false; // Yet not implemented
|
|
}
|
|
|
|
// Invoked when audio class specific set request received for an interface
|
|
bool tud_audio_set_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff)
|
|
{
|
|
(void) rhport;
|
|
(void) pBuff;
|
|
|
|
// We do not support any set range requests here, only current value requests
|
|
TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR);
|
|
|
|
// Page 91 in UAC2 specification
|
|
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
|
|
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
|
|
uint8_t itf = TU_U16_LOW(p_request->wIndex);
|
|
|
|
(void) channelNum; (void) ctrlSel; (void) itf;
|
|
|
|
TU_LOG2("%s(chan=%u, sel=%u, itf=%u)\r\n", __func__, channelNum, ctrlSel, itf);
|
|
|
|
return false; // Yet not implemented
|
|
}
|
|
|
|
// Invoked when audio class specific set request received for an entity
|
|
bool tud_audio_set_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request, uint8_t *pBuff)
|
|
{
|
|
(void) rhport;
|
|
|
|
// Page 91 in UAC2 specification
|
|
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
|
|
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
|
|
uint8_t itf = TU_U16_LOW(p_request->wIndex);
|
|
uint8_t entityID = TU_U16_HIGH(p_request->wIndex);
|
|
|
|
(void) itf;
|
|
|
|
TU_LOG2("%s(chan=%u, sel=%u, itf=%u, entity=%u)\r\n", __func__, channelNum, ctrlSel, itf, entityID);
|
|
|
|
// We do not support any set range requests here, only current value requests
|
|
TU_VERIFY(p_request->bRequest == AUDIO_CS_REQ_CUR);
|
|
|
|
#if 0
|
|
// If request is for our feature unit
|
|
if ( entityID == 2 )
|
|
{
|
|
switch ( ctrlSel )
|
|
{
|
|
case AUDIO_FU_CTRL_MUTE:
|
|
// Request uses format layout 1
|
|
TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_1_t));
|
|
|
|
mute[channelNum] = ((audio_control_cur_1_t*) pBuff)->bCur;
|
|
|
|
TU_LOG2(" Set Mute: %d of channel: %u\r\n", mute[channelNum], channelNum);
|
|
return true;
|
|
|
|
case AUDIO_FU_CTRL_VOLUME:
|
|
// Request uses format layout 2
|
|
TU_VERIFY(p_request->wLength == sizeof(audio_control_cur_2_t));
|
|
|
|
volume[channelNum] = (uint16_t) ((audio_control_cur_2_t*) pBuff)->bCur;
|
|
|
|
TU_LOG2(" Set Volume: %d dB of channel: %u\r\n", volume[channelNum], channelNum);
|
|
return true;
|
|
|
|
// Unknown/Unsupported control
|
|
default:
|
|
TU_BREAKPOINT();
|
|
return false;
|
|
}
|
|
}
|
|
#endif
|
|
return false; // Yet not implemented
|
|
}
|
|
|
|
// Invoked when audio class specific get request received for an EP
|
|
bool tud_audio_get_req_ep_cb(uint8_t rhport, tusb_control_request_t const * p_request)
|
|
{
|
|
(void) rhport;
|
|
|
|
// Page 91 in UAC2 specification
|
|
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
|
|
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
|
|
uint8_t ep = TU_U16_LOW(p_request->wIndex);
|
|
|
|
(void) channelNum; (void) ctrlSel; (void) ep;
|
|
|
|
// return tud_control_xfer(rhport, p_request, &tmp, 1);
|
|
|
|
TU_LOG2("%s(chan=%u, sel=%u, ep=0x%02x)\r\n", __func__, channelNum, ctrlSel, ep);
|
|
|
|
return false; // Yet not implemented
|
|
}
|
|
|
|
// Invoked when audio class specific get request received for an interface
|
|
bool tud_audio_get_req_itf_cb(uint8_t rhport, tusb_control_request_t const * p_request)
|
|
{
|
|
(void) rhport;
|
|
|
|
// Page 91 in UAC2 specification
|
|
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
|
|
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
|
|
uint8_t itf = TU_U16_LOW(p_request->wIndex);
|
|
|
|
(void) channelNum; (void) ctrlSel; (void) itf;
|
|
|
|
TU_LOG2("%s(chan=%u, sel=%u, itf=%u)\r\n", __func__, channelNum, ctrlSel, itf);
|
|
|
|
return false; // Yet not implemented
|
|
}
|
|
|
|
// Invoked when audio class specific get request received for an entity
|
|
bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const * p_request)
|
|
{
|
|
(void) rhport;
|
|
|
|
// Page 91 in UAC2 specification
|
|
uint8_t channelNum = TU_U16_LOW(p_request->wValue);
|
|
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
|
|
uint8_t itf = TU_U16_LOW(p_request->wIndex);
|
|
uint8_t entityID = TU_U16_HIGH(p_request->wIndex);
|
|
|
|
TU_LOG2("%s(chan=%u, sel=%u, itf=%u, entity=%u)\r\n", __func__, channelNum, ctrlSel, itf, entityID);
|
|
|
|
// Input terminal (Microphone input)
|
|
if (entityID == 1)
|
|
{
|
|
switch ( ctrlSel )
|
|
{
|
|
case AUDIO_TE_CTRL_CONNECTOR:
|
|
{
|
|
// The terminal connector control only has a get request with only the CUR attribute.
|
|
audio_desc_channel_cluster_t ret;
|
|
|
|
// Those are dummy values for now
|
|
ret.bNrChannels = 1;
|
|
ret.bmChannelConfig = (audio_channel_config_t) 0;
|
|
ret.iChannelNames = 0;
|
|
|
|
TU_LOG2(" Get terminal connector\r\n");
|
|
|
|
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, (void*) &ret, sizeof(ret));
|
|
}
|
|
break;
|
|
|
|
// Unknown/Unsupported control selector
|
|
default:
|
|
TU_BREAKPOINT();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Clock Source unit
|
|
if ( entityID == 4 )
|
|
{
|
|
switch ( ctrlSel )
|
|
{
|
|
case AUDIO_CS_CTRL_SAM_FREQ:
|
|
// channelNum is always zero in this case
|
|
switch ( p_request->bRequest )
|
|
{
|
|
case AUDIO_CS_REQ_CUR:
|
|
TU_LOG2(" Get Sample Freq.\r\n");
|
|
return tud_control_xfer(rhport, p_request, &sampFreq, sizeof(sampFreq));
|
|
|
|
case AUDIO_CS_REQ_RANGE:
|
|
TU_LOG2(" Get Sample Freq. range\r\n");
|
|
return tud_control_xfer(rhport, p_request, &sampleFreqRng, sizeof(sampleFreqRng));
|
|
|
|
// Unknown/Unsupported control
|
|
default:
|
|
TU_BREAKPOINT();
|
|
return false;
|
|
}
|
|
break;
|
|
|
|
case AUDIO_CS_CTRL_CLK_VALID:
|
|
// Only cur attribute exists for this request
|
|
TU_LOG2(" Get Sample Freq. valid\r\n");
|
|
return tud_control_xfer(rhport, p_request, &clkValid, sizeof(clkValid));
|
|
|
|
// Unknown/Unsupported control
|
|
default:
|
|
TU_BREAKPOINT();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
TU_LOG2(" Unsupported entity: %d\r\n", entityID);
|
|
return false; // Yet not implemented
|
|
}
|
|
|
|
bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t itf, uint8_t ep_in, uint8_t cur_alt_setting)
|
|
{
|
|
(void) rhport;
|
|
(void) itf;
|
|
(void) ep_in;
|
|
(void) cur_alt_setting;
|
|
|
|
/* tud_audio core tells us that the previous Tx was completed and we need new data to transmit */
|
|
//printf("%s()\r\n", __func__);
|
|
|
|
tud_audio_write((uint8_t *)test_buffer_audio, CFG_TUD_AUDIO_EP_SZ - 2);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool tud_audio_tx_done_post_load_cb(uint8_t rhport, uint16_t n_bytes_copied, uint8_t itf, uint8_t ep_in, uint8_t cur_alt_setting)
|
|
{
|
|
(void) rhport;
|
|
(void) n_bytes_copied;
|
|
(void) itf;
|
|
(void) ep_in;
|
|
(void) cur_alt_setting;
|
|
|
|
/* tud_audio core tells us that the previous Tx was completed (at n_bytes_copied) */
|
|
//printf("%s(%u)\r\n", __func__, n_bytes_copied);
|
|
|
|
for (size_t cnt = 0; cnt < (CFG_TUD_AUDIO_EP_SZ - 2) / 2; cnt++) {
|
|
test_buffer_audio[cnt] = startVal++;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool tud_audio_rx_done_pre_read_cb(uint8_t rhport, uint16_t n_bytes_received, uint8_t func_id, uint8_t ep_out, uint8_t cur_alt_setting)
|
|
{
|
|
(void) rhport;
|
|
(void) func_id;
|
|
(void) ep_out;
|
|
(void) cur_alt_setting;
|
|
|
|
//printf("%s(%u)\r\n", __func__, n_bytes_received);
|
|
|
|
uint8_t buf[n_bytes_received];
|
|
|
|
/* read whatever data was received from the host */
|
|
tud_audio_read(buf, n_bytes_received);
|
|
|
|
return true;
|
|
}
|
|
|
|
#if 0
|
|
bool tud_audio_rx_done_post_read_cb(uint8_t rhport, uint16_t n_bytes_received, uint8_t func_id, uint8_t ep_out, uint8_t cur_alt_setting)
|
|
{
|
|
(void) rhport;
|
|
(void) n_bytes_received;
|
|
(void) func_id;
|
|
(void) ep_out;
|
|
(void) cur_alt_setting;
|
|
}
|
|
#endif
|
|
|
|
bool tud_audio_set_itf_close_EP_cb(uint8_t rhport, tusb_control_request_t const * p_request)
|
|
{
|
|
(void) rhport;
|
|
uint8_t const itf = tu_u16_low(p_request->wIndex);
|
|
uint8_t const alt = tu_u16_low(p_request->wValue);
|
|
|
|
printf("%s(itf=%u, alt=%u)\r\n", __func__, itf, alt);
|
|
|
|
switch (itf) {
|
|
case ITF_NUM_AUDIO_STREAM_OUT:
|
|
break;
|
|
case ITF_NUM_AUDIO_STREAM_IN:
|
|
startVal = 0;
|
|
break;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
#else /* HAVE_AUDIO */
|
|
|
|
int audio_init(void)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
#endif /* HAVE_AUDIO */
|