Skip to content

Commit 7706e6f

Browse files
authored
Merge pull request #2725
UAC2: Fix memclr on driver reset.
2 parents a615b0a + ffe1d06 commit 7706e6f

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

src/class/audio/audio_device.c

+50-44
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,6 @@ typedef struct
304304

305305
uint16_t desc_length; // Length of audio function descriptor
306306

307-
// Buffer for control requests
308-
uint8_t * ctrl_buf;
309-
uint8_t ctrl_buf_sz;
310-
311-
// Current active alternate settings
312-
uint8_t * alt_setting; // We need to save the current alternate setting this way, because it is possible that there are AS interfaces which do not have an EP!
313-
314-
// EP Transfer buffers and FIFOs
315-
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
316-
#if !CFG_TUD_AUDIO_ENABLE_DECODING
317-
tu_fifo_t ep_out_ff;
318-
#endif
319-
320307
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
321308
struct {
322309
CFG_TUSB_MEM_ALIGN uint32_t value; // Feedback value for asynchronous mode (in 16.16 format).
@@ -346,17 +333,6 @@ typedef struct
346333
} feedback;
347334
#endif // CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
348335

349-
#endif // CFG_TUD_AUDIO_ENABLE_EP_OUT
350-
351-
#if CFG_TUD_AUDIO_ENABLE_EP_IN && !CFG_TUD_AUDIO_ENABLE_ENCODING
352-
tu_fifo_t ep_in_ff;
353-
#endif
354-
355-
// Audio control interrupt buffer - no FIFO - 6 Bytes according to UAC 2 specification (p. 74)
356-
#if CFG_TUD_AUDIO_ENABLE_INTERRUPT_EP
357-
CFG_TUSB_MEM_ALIGN uint8_t ep_int_buf[6];
358-
#endif
359-
360336
// Decoding parameters - parameters are set when alternate AS interface is set by host
361337
// Coding is currently only supported for EP. Software coding corresponding to AS interfaces without EPs are not supported currently.
362338
#if CFG_TUD_AUDIO_ENABLE_EP_OUT && CFG_TUD_AUDIO_ENABLE_DECODING
@@ -365,8 +341,7 @@ typedef struct
365341

366342
#if CFG_TUD_AUDIO_ENABLE_TYPE_I_DECODING
367343
audio_data_format_type_I_t format_type_I_rx;
368-
uint8_t n_bytes_per_sampe_rx;
369-
uint8_t n_channels_per_ff_rx;
344+
uint8_t n_bytes_per_sample_rx;
370345
uint8_t n_ff_used_rx;
371346
#endif
372347
#endif
@@ -382,26 +357,57 @@ typedef struct
382357
#if CFG_TUD_AUDIO_ENABLE_EP_IN && (CFG_TUD_AUDIO_ENABLE_ENCODING || CFG_TUD_AUDIO_EP_IN_FLOW_CONTROL)
383358
audio_format_type_t format_type_tx;
384359
uint8_t n_channels_tx;
385-
uint8_t n_bytes_per_sampe_tx;
360+
uint8_t n_bytes_per_sample_tx;
386361

387362
#if CFG_TUD_AUDIO_ENABLE_TYPE_I_ENCODING
388363
audio_data_format_type_I_t format_type_I_tx;
389-
uint8_t n_channels_per_ff_tx;
390364
uint8_t n_ff_used_tx;
391365
#endif
392366
#endif
393367

368+
// Buffer for control requests
369+
uint8_t * ctrl_buf;
370+
uint8_t ctrl_buf_sz;
371+
372+
// Current active alternate settings
373+
uint8_t * alt_setting; // We need to save the current alternate setting this way, because it is possible that there are AS interfaces which do not have an EP!
374+
375+
// EP Transfer buffers and FIFOs
376+
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
377+
#if !CFG_TUD_AUDIO_ENABLE_DECODING
378+
tu_fifo_t ep_out_ff;
379+
#endif
380+
381+
382+
#endif // CFG_TUD_AUDIO_ENABLE_EP_OUT
383+
384+
#if CFG_TUD_AUDIO_ENABLE_EP_IN && !CFG_TUD_AUDIO_ENABLE_ENCODING
385+
tu_fifo_t ep_in_ff;
386+
#endif
387+
388+
// Audio control interrupt buffer - no FIFO - 6 Bytes according to UAC 2 specification (p. 74)
389+
#if CFG_TUD_AUDIO_ENABLE_INTERRUPT_EP
390+
CFG_TUSB_MEM_ALIGN uint8_t ep_int_buf[6];
391+
#endif
392+
393+
394394
// Support FIFOs for software encoding and decoding
395395
#if CFG_TUD_AUDIO_ENABLE_EP_OUT && CFG_TUD_AUDIO_ENABLE_DECODING
396396
tu_fifo_t * rx_supp_ff;
397397
uint8_t n_rx_supp_ff;
398398
uint16_t rx_supp_ff_sz_max;
399+
#if CFG_TUD_AUDIO_ENABLE_TYPE_I_DECODING
400+
uint8_t n_channels_per_ff_rx;
401+
#endif
399402
#endif
400403

401404
#if CFG_TUD_AUDIO_ENABLE_EP_IN && CFG_TUD_AUDIO_ENABLE_ENCODING
402405
tu_fifo_t * tx_supp_ff;
403406
uint8_t n_tx_supp_ff;
404407
uint16_t tx_supp_ff_sz_max;
408+
#if CFG_TUD_AUDIO_ENABLE_TYPE_I_ENCODING
409+
uint8_t n_channels_per_ff_tx;
410+
#endif
405411
#endif
406412

407413
// Linear buffer in case target MCU is not capable of handling a ring buffer FIFO e.g. no hardware buffer is available or driver is would need to be changed dramatically OR the support FIFOs are used
@@ -707,16 +713,16 @@ static bool audiod_decode_type_I_pcm(uint8_t rhport, audiod_function_t* audio, u
707713
if (info.len_lin != 0)
708714
{
709715
info.len_lin = tu_min16(nBytesPerFFToRead, info.len_lin);
710-
src = &audio->lin_buf_out[cnt_ff*audio->n_channels_per_ff_rx * audio->n_bytes_per_sampe_rx];
716+
src = &audio->lin_buf_out[cnt_ff*audio->n_channels_per_ff_rx * audio->n_bytes_per_sample_rx];
711717
dst_end = info.ptr_lin + info.len_lin;
712-
src = audiod_interleaved_copy_bytes_fast_decode(audio->n_bytes_per_sampe_rx, info.ptr_lin, dst_end, src, n_ff_used);
718+
src = audiod_interleaved_copy_bytes_fast_decode(audio->n_bytes_per_sample_rx, info.ptr_lin, dst_end, src, n_ff_used);
713719

714720
// Handle wrapped part of FIFO
715721
info.len_wrap = tu_min16(nBytesPerFFToRead - info.len_lin, info.len_wrap);
716722
if (info.len_wrap != 0)
717723
{
718724
dst_end = info.ptr_wrap + info.len_wrap;
719-
audiod_interleaved_copy_bytes_fast_decode(audio->n_bytes_per_sampe_rx, info.ptr_wrap, dst_end, src, n_ff_used);
725+
audiod_interleaved_copy_bytes_fast_decode(audio->n_bytes_per_sample_rx, info.ptr_wrap, dst_end, src, n_ff_used);
720726
}
721727
tu_fifo_advance_write_pointer(&audio->rx_supp_ff[cnt_ff], info.len_lin + info.len_wrap);
722728
}
@@ -1024,7 +1030,7 @@ static uint16_t audiod_encode_type_I_pcm(uint8_t rhport, audiod_function_t* audi
10241030
// Limit to maximum sample number - THIS IS A POSSIBLE ERROR SOURCE IF TOO MANY SAMPLE WOULD NEED TO BE SENT BUT CAN NOT!
10251031
nBytesPerFFToSend = tu_min16(nBytesPerFFToSend, audio->ep_in_sz / n_ff_used);
10261032
// Round to full number of samples (flooring)
1027-
uint16_t const nSlotSize = audio->n_channels_per_ff_tx * audio->n_bytes_per_sampe_tx;
1033+
uint16_t const nSlotSize = audio->n_channels_per_ff_tx * audio->n_bytes_per_sample_tx;
10281034
nBytesPerFFToSend = (nBytesPerFFToSend / nSlotSize) * nSlotSize;
10291035
#endif
10301036

@@ -1036,15 +1042,15 @@ static uint16_t audiod_encode_type_I_pcm(uint8_t rhport, audiod_function_t* audi
10361042

10371043
for (cnt_ff = 0; cnt_ff < n_ff_used; cnt_ff++)
10381044
{
1039-
dst = &audio->lin_buf_in[cnt_ff*audio->n_channels_per_ff_tx*audio->n_bytes_per_sampe_tx];
1045+
dst = &audio->lin_buf_in[cnt_ff*audio->n_channels_per_ff_tx*audio->n_bytes_per_sample_tx];
10401046

10411047
tu_fifo_get_read_info(&audio->tx_supp_ff[cnt_ff], &info);
10421048

10431049
if (info.len_lin != 0)
10441050
{
10451051
info.len_lin = tu_min16(nBytesPerFFToSend, info.len_lin); // Limit up to desired length
10461052
src_end = (uint8_t *)info.ptr_lin + info.len_lin;
1047-
dst = audiod_interleaved_copy_bytes_fast_encode(audio->n_bytes_per_sampe_tx, info.ptr_lin, src_end, dst, n_ff_used);
1053+
dst = audiod_interleaved_copy_bytes_fast_encode(audio->n_bytes_per_sample_tx, info.ptr_lin, src_end, dst, n_ff_used);
10481054

10491055
// Limit up to desired length
10501056
info.len_wrap = tu_min16(nBytesPerFFToSend - info.len_lin, info.len_wrap);
@@ -1053,7 +1059,7 @@ static uint16_t audiod_encode_type_I_pcm(uint8_t rhport, audiod_function_t* audi
10531059
if (info.len_wrap != 0)
10541060
{
10551061
src_end = (uint8_t *)info.ptr_wrap + info.len_wrap;
1056-
audiod_interleaved_copy_bytes_fast_encode(audio->n_bytes_per_sampe_tx, info.ptr_wrap, src_end, dst, n_ff_used);
1062+
audiod_interleaved_copy_bytes_fast_encode(audio->n_bytes_per_sample_tx, info.ptr_wrap, src_end, dst, n_ff_used);
10571063
}
10581064

10591065
tu_fifo_advance_read_pointer(&audio->tx_supp_ff[cnt_ff], info.len_lin + info.len_wrap);
@@ -1782,8 +1788,8 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
17821788

17831789
// Reconfigure size of support FIFOs - this is necessary to avoid samples to get split in case of a wrap
17841790
#if CFG_TUD_AUDIO_ENABLE_ENCODING && CFG_TUD_AUDIO_ENABLE_TYPE_I_ENCODING
1785-
const uint16_t active_fifo_depth = (uint16_t) ((audio->tx_supp_ff_sz_max / (audio->n_channels_per_ff_tx * audio->n_bytes_per_sampe_tx))
1786-
* (audio->n_channels_per_ff_tx * audio->n_bytes_per_sampe_tx));
1791+
const uint16_t active_fifo_depth = (uint16_t) ((audio->tx_supp_ff_sz_max / (audio->n_channels_per_ff_tx * audio->n_bytes_per_sample_tx))
1792+
* (audio->n_channels_per_ff_tx * audio->n_bytes_per_sample_tx));
17871793
for (uint8_t cnt = 0; cnt < audio->n_tx_supp_ff; cnt++)
17881794
{
17891795
tu_fifo_config(&audio->tx_supp_ff[cnt], audio->tx_supp_ff[cnt].buffer, active_fifo_depth, 1, true);
@@ -1813,7 +1819,7 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
18131819

18141820
// Reconfigure size of support FIFOs - this is necessary to avoid samples to get split in case of a wrap
18151821
#if CFG_TUD_AUDIO_ENABLE_TYPE_I_DECODING
1816-
const uint16_t active_fifo_depth = (audio->rx_supp_ff_sz_max / audio->n_bytes_per_sampe_rx) * audio->n_bytes_per_sampe_rx;
1822+
const uint16_t active_fifo_depth = (audio->rx_supp_ff_sz_max / audio->n_bytes_per_sample_rx) * audio->n_bytes_per_sample_rx;
18171823
for (uint8_t cnt = 0; cnt < audio->n_rx_supp_ff; cnt++)
18181824
{
18191825
tu_fifo_config(&audio->rx_supp_ff[cnt], audio->rx_supp_ff[cnt].buffer, active_fifo_depth, 1, true);
@@ -2587,14 +2593,14 @@ static void audiod_parse_for_AS_params(audiod_function_t* audio, uint8_t const *
25872593
#if CFG_TUD_AUDIO_ENABLE_EP_IN
25882594
if (as_itf == audio->ep_in_as_intf_num)
25892595
{
2590-
audio->n_bytes_per_sampe_tx = ((audio_desc_type_I_format_t const * )p_desc)->bSubslotSize;
2596+
audio->n_bytes_per_sample_tx = ((audio_desc_type_I_format_t const * )p_desc)->bSubslotSize;
25912597
}
25922598
#endif
25932599

25942600
#if CFG_TUD_AUDIO_ENABLE_EP_OUT && CFG_TUD_AUDIO_ENABLE_DECODING
25952601
if (as_itf == audio->ep_out_as_intf_num)
25962602
{
2597-
audio->n_bytes_per_sampe_rx = ((audio_desc_type_I_format_t const * )p_desc)->bSubslotSize;
2603+
audio->n_bytes_per_sample_rx = ((audio_desc_type_I_format_t const * )p_desc)->bSubslotSize;
25982604
}
25992605
#endif
26002606
}
@@ -2613,7 +2619,7 @@ static bool audiod_calc_tx_packet_sz(audiod_function_t* audio)
26132619
{
26142620
TU_VERIFY(audio->format_type_tx == AUDIO_FORMAT_TYPE_I);
26152621
TU_VERIFY(audio->n_channels_tx);
2616-
TU_VERIFY(audio->n_bytes_per_sampe_tx);
2622+
TU_VERIFY(audio->n_bytes_per_sample_tx);
26172623
TU_VERIFY(audio->interval_tx);
26182624
TU_VERIFY(audio->sample_rate_tx);
26192625

@@ -2622,9 +2628,9 @@ static bool audiod_calc_tx_packet_sz(audiod_function_t* audio)
26222628
const uint16_t sample_normimal = (uint16_t)(audio->sample_rate_tx * interval / ((tud_speed_get() == TUSB_SPEED_FULL) ? 1000 : 8000));
26232629
const uint16_t sample_reminder = (uint16_t)(audio->sample_rate_tx * interval % ((tud_speed_get() == TUSB_SPEED_FULL) ? 1000 : 8000));
26242630

2625-
const uint16_t packet_sz_tx_min = (uint16_t)((sample_normimal - 1) * audio->n_channels_tx * audio->n_bytes_per_sampe_tx);
2626-
const uint16_t packet_sz_tx_norm = (uint16_t)(sample_normimal * audio->n_channels_tx * audio->n_bytes_per_sampe_tx);
2627-
const uint16_t packet_sz_tx_max = (uint16_t)((sample_normimal + 1) * audio->n_channels_tx * audio->n_bytes_per_sampe_tx);
2631+
const uint16_t packet_sz_tx_min = (uint16_t)((sample_normimal - 1) * audio->n_channels_tx * audio->n_bytes_per_sample_tx);
2632+
const uint16_t packet_sz_tx_norm = (uint16_t)(sample_normimal * audio->n_channels_tx * audio->n_bytes_per_sample_tx);
2633+
const uint16_t packet_sz_tx_max = (uint16_t)((sample_normimal + 1) * audio->n_channels_tx * audio->n_bytes_per_sample_tx);
26282634

26292635
// Endpoint size must larger than packet size
26302636
TU_ASSERT(packet_sz_tx_max <= audio->ep_in_sz);

0 commit comments

Comments
 (0)