Audio clocking?
#2031
Replies: 2 comments
-
Short answer is no. Due to complicity of audio clocking, there is no way to managing it. Longer answer is you can refer to Here is a stripped-down of my implementation: // Helper for clock set requests
static bool tud_audio_clock_set_request(uint8_t rhport, audio_control_request_t const* request, uint8_t const* buf)
{
(void)rhport;
TU_VERIFY(request->bEntityID == UAC2_ENTITY_USB2IIS_CLOCK);
TU_VERIFY(request->bRequest == AUDIO_CS_REQ_CUR);
if(request->bControlSelector == AUDIO_CS_CTRL_SAM_FREQ)
{
TU_VERIFY(request->wLength == sizeof(audio_control_cur_4_t));
current_sample_rate = ((audio_control_cur_4_t*)buf)->bCur;
TU_LOG1("Clock set current freq: %d\r\n", current_sample_rate);
return true;
}
else
{
TU_LOG1("Clock set request not supported, entity = %u, selector = %u, request = %u\r\n",
request->bEntityID, request->bControlSelector, request->bRequest);
return false;
}
}
bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const* p_request)
{
(void)rhport;
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
// Clear buffer when streaming format is changed
if(itf == ITF_NUM_AUDIO_STREAMING_IIS)
{
if(alt != 0)
{
current_resolution = resolutions_per_format[alt - 1];
current_bytes_per_sample = bytes_per_sample[alt - 1];
// Set I2S params
UAC_Config_Cb(0, current_sample_rate, current_bytes_per_sample, current_resolution);
}
else
{
UAC_Config_Cb(0, 0, 0, 0);
}
}
return true;
}
/* Called when streaming format is changed */
void UAC_Config_Cb(uint8_t Func, uint32_t SampleRate, uint8_t BytesPerSample, uint8_t Resolution)
{
if(Func == 0)
{
if(current_SampleRate == SampleRate && current_BytesPerSample == BytesPerSample && current_Resolution == Resolution)
{
return;
}
current_SampleRate = SampleRate;
current_BytesPerSample = BytesPerSample;
current_Resolution = Resolution;
bPlayback_En = current_SampleRate != 0;
if(SampleRate)
{
if((SampleRate % 44100) == 0)
{
// For 44.1kHz, 88.2kHz, etc.
MCLK_Set(MCLK_45_1584M);
// Set I2S clock divider, DIV = 1 for 352.8k ... DIV = 8 for 44.1k
I2S_Tx_Init(44100 * 8 / SampleRate,);
}
else
{
// For 48kHz, 96kHz, etc.
MCLK_Set(MCLK_49_152M);
// Set I2S clock divider, DIV = 1 for 384k ... DIV = 8 for 48k
I2S_Tx_Init(48000 * 8 / SampleRate,);
}
}
}
}
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Excellent, thank you!!
That was about what I thought, and I just discovered that sample too.
Clocking in our product should be interesting, then. We're kicking around
ideas now. :)
Ray Rischpater, KF6GPE | kf6gpe.org | @kf6gpe
<http://www.twitter.com/kf6gpe>
…On Mon, Apr 17, 2023 at 2:40 AM HiFiPhile ***@***.***> wrote:
but how, if at all, does TinyUSB manage audio clocking?
Short answer is no. Due to complicity of audio clocking, there is no way
to managing it.
Longer answer is you can refer to uac2_headset example, in the
tud_audio_clock_set_request() callback you have the current sample rate,
you need to configurate I2S clock accordingly.
Here is a stripped-down of my implementation:
// Helper for clock set requestsstatic bool tud_audio_clock_set_request(uint8_t rhport, audio_control_request_t const* request, uint8_t const* buf)
{
(void)rhport;
TU_VERIFY(request->bEntityID == UAC2_ENTITY_USB2IIS_CLOCK);
TU_VERIFY(request->bRequest == AUDIO_CS_REQ_CUR);
if(request->bControlSelector == AUDIO_CS_CTRL_SAM_FREQ)
{
TU_VERIFY(request->wLength == sizeof(audio_control_cur_4_t));
current_sample_rate = ((audio_control_cur_4_t*)buf)->bCur;
TU_LOG1("Clock set current freq: %d\r\n", current_sample_rate);
return true;
}
else
{
TU_LOG1("Clock set request not supported, entity = %u, selector = %u, request = %u\r\n",
request->bEntityID, request->bControlSelector, request->bRequest);
return false;
}
}
bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const* p_request)
{
(void)rhport;
uint8_t const itf = tu_u16_low(tu_le16toh(p_request->wIndex));
uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue));
// Clear buffer when streaming format is changed
if(itf == ITF_NUM_AUDIO_STREAMING_IIS)
{
if(alt != 0)
{
current_resolution = resolutions_per_format[alt - 1];
current_bytes_per_sample = bytes_per_sample[alt - 1];
// Set I2S params
UAC_Config_Cb(0, current_sample_rate, current_bytes_per_sample, current_resolution);
}
else
{
UAC_Config_Cb(0, 0, 0, 0);
}
}
return true;
}
/* Called when streaming format is changed */void UAC_Config_Cb(uint8_t Func, uint32_t SampleRate, uint8_t BytesPerSample, uint8_t Resolution)
{
if(Func == 0)
{
if(current_SampleRate == SampleRate && current_BytesPerSample == BytesPerSample && current_Resolution == Resolution)
{
return;
}
current_SampleRate = SampleRate;
current_BytesPerSample = BytesPerSample;
current_Resolution = Resolution;
bPlayback_En = current_SampleRate != 0;
if(SampleRate)
{
bool spdif_En = SampleRate < 192000;
if((SampleRate % 44100) == 0)
{
MCLK_Set(MCLK_45_1584M);
I2S_Tx_Init(1, 44100 * 8 / SampleRate, spdif_En);
}
else
{
MCLK_Set(MCLK_49_152M);
I2S_Tx_Init(1, 48000 * 8 / SampleRate, spdif_En);
}
bSPDIF_En = spdif_En;
SPDIF_ENC_RESET(current_Resolution > 16, SampleRate);
}
}
}
—
Reply to this email directly, view it on GitHub
<#2031 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAK6QRAK3QBQ3TZCKJ5YK3DXBUFZJANCNFSM6AAAAAAW7TIV5I>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Maybe I'm missing something obvious -- it wouldn't be the first time --- but how, if at all, does TinyUSB manage audio clocking?
I see that the SOF isn't supported, and I don't see a lot of information in
audio_driver.c
. Does anyone have any light to share?Thanks in advance...
PS: Once I got over my chip-level integration problems that I posted a while ago and got done futzing with some descriptors, integration into our custom project wasn't bad at all! Very cool. Appreciate all of the work done by folks here.
Beta Was this translation helpful? Give feedback.
All reactions