Skip to content

Commit a7d1888

Browse files
authored
Merge pull request #2328 from HiFiPhile/rx_fb
UAC2: Implement feedback by fifo counting.
2 parents 4232642 + 6a67bac commit a7d1888

File tree

17 files changed

+1868
-284
lines changed

17 files changed

+1868
-284
lines changed

examples/device/cdc_uac2/src/uac2_app.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ static bool tud_audio_feature_unit_get_request(uint8_t rhport, audio_control_req
141141
TU_LOG1("Get channel %u mute %d\r\n", request->bChannelNumber, mute1.bCur);
142142
return tud_audio_buffer_and_schedule_control_xfer(rhport, (tusb_control_request_t const *)request, &mute1, sizeof(mute1));
143143
}
144-
else if (UAC2_ENTITY_SPK_FEATURE_UNIT && request->bControlSelector == AUDIO_FU_CTRL_VOLUME)
144+
else if (request->bControlSelector == AUDIO_FU_CTRL_VOLUME)
145145
{
146146
if (request->bRequest == AUDIO_CS_REQ_RANGE)
147147
{

examples/device/uac2_headset/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static bool tud_audio_feature_unit_get_request(uint8_t rhport, audio_control_req
229229
TU_LOG1("Get channel %u mute %d\r\n", request->bChannelNumber, mute1.bCur);
230230
return tud_audio_buffer_and_schedule_control_xfer(rhport, (tusb_control_request_t const *)request, &mute1, sizeof(mute1));
231231
}
232-
else if (UAC2_ENTITY_SPK_FEATURE_UNIT && request->bControlSelector == AUDIO_FU_CTRL_VOLUME)
232+
else if (request->bControlSelector == AUDIO_FU_CTRL_VOLUME)
233233
{
234234
if (request->bRequest == AUDIO_CS_REQ_RANGE)
235235
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.17)
2+
3+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
4+
5+
# gets PROJECT name for the example (e.g. <BOARD>-<DIR_NAME>)
6+
family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
7+
8+
project(${PROJECT} C CXX ASM)
9+
10+
# Checks this example is valid for the family and initializes the project
11+
family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
12+
13+
# Espressif has its own cmake build system
14+
if(FAMILY STREQUAL "espressif")
15+
return()
16+
endif()
17+
18+
add_executable(${PROJECT})
19+
20+
# Example source
21+
target_sources(${PROJECT} PUBLIC
22+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
23+
${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
24+
)
25+
26+
# Example include
27+
target_include_directories(${PROJECT} PUBLIC
28+
${CMAKE_CURRENT_SOURCE_DIR}/src
29+
)
30+
31+
# Configure compilation flags and libraries for the example without RTOS.
32+
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
33+
family_configure_device_example(${PROJECT} noos)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
include ../../build_system/make/make.mk
2+
3+
INC += \
4+
src \
5+
$(TOP)/hw \
6+
7+
# Example source
8+
EXAMPLE_SOURCE += $(wildcard src/*.c)
9+
SRC_C += $(addprefix $(CURRENT_PATH)/, $(EXAMPLE_SOURCE))
10+
11+
include ../../build_system/make/rules.mk
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mcu:LPC11UXX
2+
mcu:LPC13XX
3+
mcu:NUC121
4+
mcu:SAMD11
5+
mcu:SAME5X
6+
mcu:SAMG
7+
board:stm32l052dap52
8+
family:broadcom_64bit
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Install python3 HID package https://pypi.org/project/hid/
2+
# Install python3 matplotlib package https://pypi.org/project/matplotlib/
3+
4+
from ctypes import *
5+
try:
6+
import hid
7+
import matplotlib.pyplot as plt
8+
import matplotlib.animation as animation
9+
except:
10+
print("Missing import, please try 'pip install hid matplotlib' or consult your OS's python package manager.")
11+
12+
# Example must be compiled with CFG_AUDIO_DEBUG=1
13+
VID = 0xcafe
14+
PID = 0x4014
15+
16+
CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX = 2
17+
18+
class audio_debug_info_t (Structure):
19+
_fields_ = [("sample_rate", c_uint32),
20+
("alt_settings", c_uint8),
21+
("mute", (CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1) * c_int8),
22+
("volume", (CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1) * c_int16),
23+
("fifo_size", c_uint16),
24+
("fifo_count", c_uint16),
25+
("fifo_count_avg", c_uint16)
26+
]
27+
28+
dev = hid.Device(VID, PID)
29+
30+
if dev:
31+
# Create figure for plotting
32+
fig = plt.figure()
33+
ax = fig.add_subplot(1, 1, 1)
34+
fifo_avg = []
35+
fifo_cnt = []
36+
# This function is called periodically from FuncAnimation
37+
def animate(i):
38+
info = None
39+
for i in range(30):
40+
try:
41+
str_in = dev.read(64, 50)
42+
info = audio_debug_info_t.from_buffer_copy(str_in)
43+
44+
global fifo_avg
45+
global fifo_cnt
46+
fifo_avg.append(info.fifo_count_avg)
47+
fifo_cnt.append(info.fifo_count)
48+
except:
49+
exit(1)
50+
# Limit to 1000 items
51+
fifo_avg = fifo_avg[-1000:]
52+
fifo_cnt = fifo_cnt[-1000:]
53+
54+
if info is not None:
55+
# Draw x and y lists
56+
ax.clear()
57+
ax.plot(fifo_cnt, label='FIFO count')
58+
ax.plot(fifo_avg, label='FIFO average')
59+
ax.legend()
60+
ax.set_ylim(bottom=0, top=info.fifo_size)
61+
62+
# Format plot
63+
plt.title('FIFO information')
64+
plt.grid()
65+
66+
print(f'Sample rate:{info.sample_rate} | Alt settings:{info.alt_settings} | Volume:{info.volume[:]}')
67+
68+
ani = animation.FuncAnimation(fig, animate, interval=10)
69+
plt.show()
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2023 HiFiPhile
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
#ifndef _COMMON_TYPES_H_
27+
#define _COMMON_TYPES_H_
28+
29+
enum
30+
{
31+
ITF_NUM_AUDIO_CONTROL = 0,
32+
ITF_NUM_AUDIO_STREAMING,
33+
#if CFG_AUDIO_DEBUG
34+
ITF_NUM_DEBUG,
35+
#endif
36+
ITF_NUM_TOTAL
37+
};
38+
39+
#if CFG_AUDIO_DEBUG
40+
typedef struct
41+
{
42+
uint32_t sample_rate;
43+
uint8_t alt_settings;
44+
int8_t mute[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1];
45+
int16_t volume[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX + 1];
46+
uint16_t fifo_size;
47+
uint16_t fifo_count;
48+
uint16_t fifo_count_avg;
49+
} audio_debug_info_t;
50+
#endif
51+
52+
#endif

0 commit comments

Comments
 (0)