Skip to content

Commit a239080

Browse files
authored
Merge pull request #2282 from HiFiPhile/uac_interl
UAC IN transfer improvements
2 parents dd58822 + d3fa3cd commit a239080

File tree

5 files changed

+309
-86
lines changed

5 files changed

+309
-86
lines changed

examples/device/audio_4_channel_mic/src/main.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ uint8_t clkValid;
6969
audio_control_range_2_n_t(1) volumeRng[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX+1]; // Volume range state
7070
audio_control_range_4_n_t(1) sampleFreqRng; // Sample frequency range state
7171

72+
#if CFG_TUD_AUDIO_ENABLE_ENCODING
7273
// Audio test data, each buffer contains 2 channels, buffer[0] for CH0-1, buffer[1] for CH1-2
7374
uint16_t i2s_dummy_buffer[CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO][CFG_TUD_AUDIO_FUNC_1_TX_SUPP_SW_FIFO_SZ/2]; // Ensure half word aligned
75+
#else
76+
// Audio test data, 4 channels muxed together, buffer[0] for CH0, buffer[1] for CH1, buffer[2] for CH2, buffer[3] for CH3
77+
uint16_t i2s_dummy_buffer[CFG_TUD_AUDIO_EP_SZ_IN]; // Ensure half word aligned
78+
#endif
7479

7580
void led_blinking_task(void);
7681
void audio_task(void);
@@ -97,6 +102,7 @@ int main(void)
97102
sampleFreqRng.subrange[0].bRes = 0;
98103

99104
// Generate dummy data
105+
#if CFG_TUD_AUDIO_ENABLE_ENCODING
100106
uint16_t * p_buff = i2s_dummy_buffer[0];
101107
uint16_t dataVal = 1;
102108
for (uint16_t cnt = 0; cnt < AUDIO_SAMPLE_RATE/1000; cnt++)
@@ -116,6 +122,23 @@ int main(void)
116122
float t = 2*3.1415f * cnt / (AUDIO_SAMPLE_RATE/1000);
117123
*p_buff++ = (uint16_t)(sinf(t) * 25) + 200;
118124
}
125+
#else
126+
uint16_t * p_buff = i2s_dummy_buffer;
127+
uint16_t dataVal = 1;
128+
for (uint16_t cnt = 0; cnt < AUDIO_SAMPLE_RATE/1000; cnt++)
129+
{
130+
// CH0 saw wave
131+
*p_buff++ = dataVal;
132+
// CH1 inverted saw wave
133+
*p_buff++ = 60 + AUDIO_SAMPLE_RATE/1000 - dataVal;
134+
dataVal++;
135+
// CH3 square wave
136+
*p_buff++ = cnt < (AUDIO_SAMPLE_RATE/1000/2) ? 120:170;
137+
// CH4 sinus wave
138+
float t = 2*3.1415f * cnt / (AUDIO_SAMPLE_RATE/1000);
139+
*p_buff++ = (uint16_t)(sinf(t) * 25) + 200;
140+
}
141+
#endif
119142

120143
while (1)
121144
{
@@ -384,7 +407,8 @@ bool tud_audio_get_req_entity_cb(uint8_t rhport, tusb_control_request_t const *
384407
{
385408
case AUDIO_CS_REQ_CUR:
386409
TU_LOG2(" Get Sample Freq.\r\n");
387-
return tud_control_xfer(rhport, p_request, &sampFreq, sizeof(sampFreq));
410+
// Buffered control transfer is needed for IN flow control to work
411+
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &sampFreq, sizeof(sampFreq));
388412

389413
case AUDIO_CS_REQ_RANGE:
390414
TU_LOG2(" Get Sample Freq. range\r\n");
@@ -429,12 +453,15 @@ bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t itf, uint8_t ep_in, u
429453
// tud_audio_write_support_ff(channel, data, samples * N_BYTES_PER_SAMPLE * N_CHANNEL_PER_FIFO);
430454
// }
431455

456+
#if CFG_TUD_AUDIO_ENABLE_ENCODING
432457
// Write I2S buffer into FIFO
433458
for (uint8_t cnt=0; cnt < 2; cnt++)
434459
{
435460
tud_audio_write_support_ff(cnt, i2s_dummy_buffer[cnt], AUDIO_SAMPLE_RATE/1000 * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX * CFG_TUD_AUDIO_FUNC_1_CHANNEL_PER_FIFO_TX);
436461
}
437-
462+
#else
463+
tud_audio_write(i2s_dummy_buffer, AUDIO_SAMPLE_RATE/1000 * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX);
464+
#endif
438465
return true;
439466
}
440467

examples/device/audio_4_channel_mic/src/plot_audio_samples.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
# print(sd.query_devices())
1111

1212
fs = 48000 # Sample rate
13-
duration = 20e-3 # Duration of recording
13+
duration = 1 # Duration of recording
1414

1515
if platform.system() == 'Windows':
1616
# WDM-KS is needed since there are more than one MicNode device APIs (at least in Windows)
17-
device = 'Microphone (MicNode_4_Ch), Windows WDM-KS'
17+
device = 'Microphone (MicNode_4_Ch), Windows WASAPI'
1818
elif platform.system() == 'Darwin':
1919
device = 'MicNode_4_Ch'
2020
else:
@@ -28,8 +28,7 @@
2828

2929
time = np.arange(0, duration, 1 / fs) # time vector
3030
# strip starting zero
31-
myrecording = myrecording[100:]
32-
time = time[100:]
31+
3332
plt.plot(time, myrecording)
3433
plt.xlabel('Time [s]')
3534
plt.ylabel('Amplitude')

examples/device/audio_4_channel_mic/src/tusb_config.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,26 @@ extern "C" {
114114
#define CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX 2 // This value is not required by the driver, it parses this information from the descriptor once the alternate interface is set by the host - we use it for the setup
115115
#define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX 4 // This value is not required by the driver, it parses this information from the descriptor once the alternate interface is set by the host - we use it for the setup
116116
#define CFG_TUD_AUDIO_EP_SZ_IN TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX)
117+
118+
#define CFG_TUD_AUDIO_ENABLE_ENCODING 1
119+
#define CFG_TUD_AUDIO_EP_IN_FLOW_CONTROL 1
120+
121+
#if CFG_TUD_AUDIO_ENABLE_ENCODING
122+
117123
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX CFG_TUD_AUDIO_EP_SZ_IN
118124
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ CFG_TUD_AUDIO_EP_SZ_IN
119125

120-
#define CFG_TUD_AUDIO_ENABLE_ENCODING 1
121126
#define CFG_TUD_AUDIO_ENABLE_TYPE_I_ENCODING 1
122127
#define CFG_TUD_AUDIO_FUNC_1_CHANNEL_PER_FIFO_TX 2 // One I2S stream contains two channels, each stream is saved within one support FIFO - this value is currently fixed, the driver does not support a changing value
123128
#define CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO (CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX / CFG_TUD_AUDIO_FUNC_1_CHANNEL_PER_FIFO_TX)
124-
#define CFG_TUD_AUDIO_FUNC_1_TX_SUPP_SW_FIFO_SZ (CFG_TUD_AUDIO_EP_SZ_IN / CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO)
129+
#define CFG_TUD_AUDIO_FUNC_1_TX_SUPP_SW_FIFO_SZ 4 * (CFG_TUD_AUDIO_EP_SZ_IN / CFG_TUD_AUDIO_FUNC_1_N_TX_SUPP_SW_FIFO) // Minimum 4*EP size is needed for flow control
130+
131+
#else
132+
133+
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX CFG_TUD_AUDIO_EP_SZ_IN
134+
#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ 4 * CFG_TUD_AUDIO_EP_SZ_IN // Minimum 4*EP size is needed for flow control
135+
136+
#endif
125137

126138
#ifdef __cplusplus
127139
}

0 commit comments

Comments
 (0)