Skip to content

Commit dce2ad4

Browse files
committed
adding feedback fifo count (WIP)
1 parent 15aa593 commit dce2ad4

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

src/class/audio/audio_device.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,10 @@ typedef struct
323323
uint32_t mclk_freq;
324324
}fixed;
325325

326-
// struct {
327-
//
328-
// }fifo_count;
326+
struct {
327+
uint32_t nominal_value;
328+
uint32_t threshold_bytes;
329+
}fifo_count;
329330
}compute;
330331

331332
} feedback;
@@ -1715,17 +1716,28 @@ static bool audiod_set_interface(uint8_t rhport, tusb_control_request_t const *
17151716
tud_audio_feedback_params_cb(func_id, alt, &fb_param);
17161717
audio->feedback.compute_method = fb_param.method;
17171718

1719+
// Minimal/Maximum value in 16.16 format for full speed (1ms per frame) or high speed (125 us per frame)
1720+
uint32_t const frame_div = (TUSB_SPEED_FULL == tud_speed_get()) ? 1000 : 8000;
1721+
audio->feedback.min_value = (fb_param.sample_freq/frame_div - 1) << 16;
1722+
audio->feedback.max_value = (fb_param.sample_freq/frame_div + 1) << 16;
1723+
17181724
switch(fb_param.method)
17191725
{
17201726
case AUDIO_FEEDBACK_METHOD_FREQUENCY_FIXED:
17211727
case AUDIO_FEEDBACK_METHOD_FREQUENCY_FLOAT:
17221728
case AUDIO_FEEDBACK_METHOD_FREQUENCY_POWER_OF_2:
1723-
set_fb_params_freq(audio, fb_param.frequency.sample_freq, fb_param.frequency.mclk_freq);
1729+
set_fb_params_freq(audio, fb_param.sample_freq, fb_param.frequency.mclk_freq);
17241730
break;
17251731

17261732
case AUDIO_FEEDBACK_METHOD_FIFO_COUNT_FIXED:
17271733
case AUDIO_FEEDBACK_METHOD_FIFO_COUNT_FLOAT:
1728-
// TODO feedback by fifo count
1734+
{
1735+
uint64_t fb64 = ((uint64_t) fb_param.sample_freq) << 16;
1736+
audio->feedback.compute.fifo_count.nominal_value = (uint32_t) (fb64 / frame_div);
1737+
audio->feedback.compute.fifo_count.threshold_bytes = fb_param.fifo_count.threshold_bytes;
1738+
1739+
tud_audio_fb_set(audio->feedback.compute.fifo_count.nominal_value);
1740+
}
17291741
break;
17301742

17311743
// nothing to do
@@ -1972,14 +1984,14 @@ bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
19721984
(void) xferred_bytes;
19731985

19741986
// Search for interface belonging to given end point address and proceed as required
1975-
uint8_t func_id;
1976-
for (func_id = 0; func_id < CFG_TUD_AUDIO; func_id++)
1987+
for (uint8_t func_id = 0; func_id < CFG_TUD_AUDIO; func_id++)
19771988
{
1989+
audiod_function_t* audio = &_audiod_fct[func_id];
19781990

19791991
#if CFG_TUD_AUDIO_INT_CTR_EPSIZE_IN
19801992

19811993
// Data transmission of control interrupt finished
1982-
if (_audiod_fct[func_id].ep_int_ctr == ep_addr)
1994+
if (audio->ep_int_ctr == ep_addr)
19831995
{
19841996
// According to USB2 specification, maximum payload of interrupt EP is 8 bytes on low speed, 64 bytes on full speed, and 1024 bytes on high speed (but only if an alternate interface other than 0 is used - see specification p. 49)
19851997
// In case there is nothing to send we have to return a NAK - this is taken care of by PHY ???
@@ -1996,7 +2008,7 @@ bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
19962008
#if CFG_TUD_AUDIO_ENABLE_EP_IN
19972009

19982010
// Data transmission of audio packet finished
1999-
if (_audiod_fct[func_id].ep_in == ep_addr && _audiod_fct[func_id].alt_setting != 0)
2011+
if (audio->ep_in == ep_addr && audio->alt_setting != 0)
20002012
{
20012013
// USB 2.0, section 5.6.4, third paragraph, states "An isochronous endpoint must specify its required bus access period. However, an isochronous endpoint must be prepared to handle poll rates faster than the one specified."
20022014
// That paragraph goes on to say "An isochronous IN endpoint must return a zero-length packet whenever data is requested at a faster interval than the specified interval and data is not available."
@@ -2007,7 +2019,7 @@ bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
20072019
// This is the only place where we can fill something into the EPs buffer!
20082020

20092021
// Load new data
2010-
TU_VERIFY(audiod_tx_done_cb(rhport, &_audiod_fct[func_id]));
2022+
TU_VERIFY(audiod_tx_done_cb(rhport, audio));
20112023

20122024
// Transmission of ZLP is done by audiod_tx_done_cb()
20132025
return true;
@@ -2017,24 +2029,24 @@ bool audiod_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint3
20172029
#if CFG_TUD_AUDIO_ENABLE_EP_OUT
20182030

20192031
// New audio packet received
2020-
if (_audiod_fct[func_id].ep_out == ep_addr)
2032+
if (audio->ep_out == ep_addr)
20212033
{
2022-
TU_VERIFY(audiod_rx_done_cb(rhport, &_audiod_fct[func_id], (uint16_t) xferred_bytes));
2034+
TU_VERIFY(audiod_rx_done_cb(rhport, audio, (uint16_t) xferred_bytes));
20232035
return true;
20242036
}
20252037

20262038

20272039
#if CFG_TUD_AUDIO_ENABLE_FEEDBACK_EP
20282040
// Transmission of feedback EP finished
2029-
if (_audiod_fct[func_id].ep_fb == ep_addr)
2041+
if (audio->ep_fb == ep_addr)
20302042
{
20312043
if (tud_audio_fb_done_cb) tud_audio_fb_done_cb(func_id);
20322044

20332045
// Schedule a transmit with the new value if EP is not busy
2034-
if (!usbd_edpt_busy(rhport, _audiod_fct[func_id].ep_fb))
2046+
if (!usbd_edpt_busy(rhport, audio->ep_fb))
20352047
{
20362048
// Schedule next transmission - value is changed bytud_audio_n_fb_set() in the meantime or the old value gets sent
2037-
return audiod_fb_send(rhport, &_audiod_fct[func_id]);
2049+
return audiod_fb_send(rhport, audio);
20382050
}
20392051
}
20402052
#endif
@@ -2077,11 +2089,6 @@ static bool set_fb_params_freq(audiod_function_t* audio, uint32_t sample_freq, u
20772089
audio->feedback.compute.fixed.mclk_freq = mclk_freq;
20782090
}
20792091

2080-
// Minimal/Maximum value in 16.16 format for full speed (1ms per frame) or high speed (125 us per frame)
2081-
uint32_t const frame_div = (TUSB_SPEED_FULL == tud_speed_get()) ? 1000 : 8000;
2082-
audio->feedback.min_value = (sample_freq/frame_div - 1) << 16;
2083-
audio->feedback.max_value = (sample_freq/frame_div + 1) << 16;
2084-
20852092
return true;
20862093
}
20872094

src/class/audio/audio_device.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,15 @@ enum {
504504

505505
typedef struct {
506506
uint8_t method;
507+
uint32_t sample_freq; // sample frequency in Hz
508+
507509
union {
508510
struct {
509-
uint32_t sample_freq; // sample frequency in Hz
510-
uint32_t mclk_freq; // Main clock frequency in Hz i.e. master clock to which sample clock is based on
511+
uint32_t mclk_freq; // Main clock frequency in Hz i.e. master clock to which sample clock is based on
511512
}frequency;
512513

513514
struct {
514-
uint32_t nominal;
515-
uint32_t threshold;
516-
// variation etc ..
515+
uint32_t threshold_bytes; // minimum number of bytes received to be considered as filled/ready
517516
}fifo_count;
518517
};
519518
}audio_feedback_params_t;

0 commit comments

Comments
 (0)