Skip to content

Commit b109e97

Browse files
alexsvenrlubos
authored andcommitted
applications: nrf5340_audio: Switch to new usb stack
- This new stack uses the new USB driver API introduced in Zephyr 3.4.0 - Update audio_usb module to use the new USB stack API - Update Kconfig and CMakeLists.txt to include new USB stack - Update board overlay files to enable new USB audio device - Update prj.conf to include necessary USB stack configurations - Remove old USB stack references - OCT-3442 Signed-off-by: Alexander Svensen <[email protected]>
1 parent d6c88a7 commit b109e97

File tree

17 files changed

+613
-132
lines changed

17 files changed

+613
-132
lines changed

applications/nrf5340_audio/boards/nrf5340_audio_dk_nrf5340_cpuapp.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66

77
CONFIG_NRF5340_AUDIO_CS47L63_DRIVER=y
88
CONFIG_NRF5340_AUDIO_SD_CARD_MODULE=y
9+
CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED=n

applications/nrf5340_audio/boards/nrf5340_audio_dk_nrf5340_cpuapp.overlay

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <dt-bindings/led/led.h>
8+
#include <dt-bindings/usb/audio.h>
89

910
/ {
1011
audioleds: audioleds {
@@ -41,3 +42,104 @@
4142
};
4243
};
4344
};
45+
46+
/ {
47+
uac2_headset: usb_audio2 {
48+
compatible = "zephyr,uac2";
49+
status = "okay";
50+
full-speed;
51+
audio-function = <AUDIO_FUNCTION_HEADSET>;
52+
53+
uac_aclk: aclk {
54+
compatible = "zephyr,uac2-clock-source";
55+
clock-type = "internal-programmable";
56+
frequency-control = "read-only";
57+
sampling-frequencies = <48000>;
58+
sof-synchronized;
59+
};
60+
61+
out_terminal: out_terminal {
62+
compatible = "zephyr,uac2-input-terminal";
63+
clock-source = <&uac_aclk>;
64+
terminal-type = <USB_TERMINAL_STREAMING>;
65+
front-left;
66+
front-right;
67+
};
68+
69+
headphones_output: headphones {
70+
compatible = "zephyr,uac2-output-terminal";
71+
data-source = <&out_terminal>;
72+
clock-source = <&uac_aclk>;
73+
terminal-type = <BIDIRECTIONAL_TERMINAL_HEADSET>;
74+
assoc-terminal = <&mic_input>;
75+
};
76+
77+
in_terminal: in_terminal {
78+
compatible = "zephyr,uac2-output-terminal";
79+
data-source = <&mic_input>;
80+
clock-source = <&uac_aclk>;
81+
terminal-type = <USB_TERMINAL_STREAMING>;
82+
};
83+
84+
mic_input: microphone {
85+
compatible = "zephyr,uac2-input-terminal";
86+
clock-source = <&uac_aclk>;
87+
terminal-type = <BIDIRECTIONAL_TERMINAL_HEADSET>;
88+
front-left;
89+
front-right;
90+
};
91+
92+
as_iso_out: out_interface {
93+
compatible = "zephyr,uac2-audio-streaming";
94+
linked-terminal = <&out_terminal>;
95+
subslot-size = <2>;
96+
bit-resolution = <16>;
97+
};
98+
99+
as_iso_in: in_interface {
100+
compatible = "zephyr,uac2-audio-streaming";
101+
linked-terminal = <&in_terminal>;
102+
subslot-size = <2>;
103+
bit-resolution = <16>;
104+
};
105+
};
106+
};
107+
108+
/ {
109+
uac2_headphones: usb_audio2 {
110+
compatible = "zephyr,uac2";
111+
status = "okay";
112+
full-speed;
113+
audio-function = <AUDIO_FUNCTION_OTHER>;
114+
115+
hp_uac_aclk: hp_aclk {
116+
compatible = "zephyr,uac2-clock-source";
117+
clock-type = "internal-programmable";
118+
frequency-control = "read-only";
119+
sampling-frequencies = <48000>;
120+
sof-synchronized;
121+
};
122+
123+
hp_out_terminal: out_terminal {
124+
compatible = "zephyr,uac2-input-terminal";
125+
clock-source = <&hp_uac_aclk>;
126+
terminal-type = <USB_TERMINAL_STREAMING>;
127+
front-left;
128+
front-right;
129+
};
130+
131+
hp_headphones_output: hp_headphones_output {
132+
compatible = "zephyr,uac2-output-terminal";
133+
data-source = <&hp_out_terminal>;
134+
clock-source = <&hp_uac_aclk>;
135+
terminal-type = <OUTPUT_TERMINAL_HEADPHONES>;
136+
};
137+
138+
hp_as_iso_out: out_interface {
139+
compatible = "zephyr,uac2-audio-streaming";
140+
linked-terminal = <&hp_out_terminal>;
141+
subslot-size = <2>;
142+
bit-resolution = <16>;
143+
};
144+
};
145+
};

applications/nrf5340_audio/boards/nrf5340dk_nrf5340_cpuapp.overlay

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
#include <dt-bindings/usb/audio.h>
8+
79
/ {
810
audioleds: audioleds {
911
compatible = "gpio-leds";
@@ -33,16 +35,104 @@
3335
};
3436
};
3537

36-
&usbd {
37-
hs_0: hs_0 {
38-
compatible = "usb-audio-hs";
39-
mic-feature-mute;
40-
mic-channel-l;
41-
mic-channel-r;
38+
/ {
39+
uac2_headset: usb_audio2 {
40+
compatible = "zephyr,uac2";
41+
status = "okay";
42+
full-speed;
43+
audio-function = <AUDIO_FUNCTION_HEADSET>;
44+
45+
uac_aclk: aclk {
46+
compatible = "zephyr,uac2-clock-source";
47+
clock-type = "internal-programmable";
48+
frequency-control = "read-only";
49+
sampling-frequencies = <48000>;
50+
sof-synchronized;
51+
};
52+
53+
out_terminal: out_terminal {
54+
compatible = "zephyr,uac2-input-terminal";
55+
clock-source = <&uac_aclk>;
56+
terminal-type = <USB_TERMINAL_STREAMING>;
57+
front-left;
58+
front-right;
59+
};
60+
61+
headphones_output: headphones {
62+
compatible = "zephyr,uac2-output-terminal";
63+
data-source = <&out_terminal>;
64+
clock-source = <&uac_aclk>;
65+
terminal-type = <BIDIRECTIONAL_TERMINAL_HEADSET>;
66+
assoc-terminal = <&mic_input>;
67+
};
68+
69+
in_terminal: in_terminal {
70+
compatible = "zephyr,uac2-output-terminal";
71+
data-source = <&mic_input>;
72+
clock-source = <&uac_aclk>;
73+
terminal-type = <USB_TERMINAL_STREAMING>;
74+
};
75+
76+
mic_input: microphone {
77+
compatible = "zephyr,uac2-input-terminal";
78+
clock-source = <&uac_aclk>;
79+
terminal-type = <BIDIRECTIONAL_TERMINAL_HEADSET>;
80+
front-left;
81+
front-right;
82+
};
83+
84+
as_iso_out: out_interface {
85+
compatible = "zephyr,uac2-audio-streaming";
86+
linked-terminal = <&out_terminal>;
87+
subslot-size = <2>;
88+
bit-resolution = <16>;
89+
};
4290

43-
hp-feature-mute;
44-
hp-channel-l;
45-
hp-channel-r;
91+
as_iso_in: in_interface {
92+
compatible = "zephyr,uac2-audio-streaming";
93+
linked-terminal = <&in_terminal>;
94+
subslot-size = <2>;
95+
bit-resolution = <16>;
96+
};
97+
};
98+
};
99+
100+
/ {
101+
uac2_headphones: usb_audio2 {
102+
compatible = "zephyr,uac2";
103+
status = "okay";
104+
full-speed;
105+
audio-function = <AUDIO_FUNCTION_OTHER>;
106+
107+
hp_uac_aclk: hp_aclk {
108+
compatible = "zephyr,uac2-clock-source";
109+
clock-type = "internal-programmable";
110+
frequency-control = "read-only";
111+
sampling-frequencies = <48000>;
112+
sof-synchronized;
113+
};
114+
115+
hp_out_terminal: out_terminal {
116+
compatible = "zephyr,uac2-input-terminal";
117+
clock-source = <&hp_uac_aclk>;
118+
terminal-type = <USB_TERMINAL_STREAMING>;
119+
front-left;
120+
front-right;
121+
};
122+
123+
hp_headphones_output: hp_headphones_output {
124+
compatible = "zephyr,uac2-output-terminal";
125+
data-source = <&hp_out_terminal>;
126+
clock-source = <&hp_uac_aclk>;
127+
terminal-type = <OUTPUT_TERMINAL_HEADPHONES>;
128+
};
129+
130+
hp_as_iso_out: out_interface {
131+
compatible = "zephyr,uac2-audio-streaming";
132+
linked-terminal = <&hp_out_terminal>;
133+
subslot-size = <2>;
134+
bit-resolution = <16>;
135+
};
46136
};
47137
};
48138

applications/nrf5340_audio/prj_release.conf

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,3 @@ CONFIG_BT_ATT_TX_COUNT=12
3333
# BT_PRIVACY is default turned off to ease the development.
3434
# Should be turned on before production.
3535
# CONFIG_BT_PRIVACY=y
36-
37-
# USB
38-
# Default values from src/modules/Kconfig.defaults.
39-
# USB VID and PID must be changed before production.
40-
# CONFIG_USB_DEVICE_VID=0x1915
41-
# CONFIG_USB_DEVICE_PID=0x530A
42-
43-
44-
# Suppress the USB deprecation warning
45-
CONFIG_DEPRECATION_TEST=y

applications/nrf5340_audio/src/audio/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ choice AUDIO_SOURCE_GATEWAY
155155

156156
config AUDIO_SOURCE_USB
157157
bool "Use USB as audio source"
158+
select USB_DEVICE_STACK_NEXT
159+
select USBD_AUDIO2_CLASS
158160
help
159161
Set USB as audio source. Note that this forces the
160162
stream to be unidirectional because of CPU load.

applications/nrf5340_audio/src/audio/audio_system.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ static struct audio_metadata decoder_meta = {.data_coding = PCM,
7272
.bits_per_sample = CONFIG_AUDIO_BIT_DEPTH_BITS,
7373
.carried_bits_per_sample = CONFIG_AUDIO_BIT_DEPTH_BITS,
7474
.bytes_per_location = PCM_NUM_BYTES_MONO,
75-
.interleaved = true,
76-
.locations = BT_AUDIO_LOCATION_FRONT_LEFT |
77-
BT_AUDIO_LOCATION_FRONT_RIGHT,
75+
.interleaved = false,
76+
.locations = BT_AUDIO_LOCATION_MONO_AUDIO,
7877
.bad_data = 0};
7978

8079
bool sample_rate_valid(uint32_t sample_rate_hz)
@@ -423,12 +422,15 @@ int audio_system_decode(struct net_buf *audio_frame_in)
423422
struct audio_metadata *meta_blk = net_buf_user_data(audio_block);
424423

425424
net_buf_add_mem(audio_block,
426-
(char *)audio_frame_out->data + (i * (BLOCK_SIZE_BYTES)),
427-
BLOCK_SIZE_BYTES);
425+
(char *)audio_frame_out->data +
426+
(i * (audio_frame_out->len / CONFIG_FIFO_FRAME_SPLIT_NUM)),
427+
(audio_frame_out->len / CONFIG_FIFO_FRAME_SPLIT_NUM));
428428
net_buf_user_data_copy(audio_block, audio_frame_out);
429429

430430
meta_blk->data_len_us = meta_out->data_len_us / CONFIG_FIFO_FRAME_SPLIT_NUM;
431-
meta_blk->bytes_per_location = BLOCK_SIZE_BYTES;
431+
meta_blk->bytes_per_location =
432+
(audio_frame_out->len / CONFIG_FIFO_FRAME_SPLIT_NUM) /
433+
audio_metadata_num_loc_get(meta_out);
432434

433435
ret = k_msgq_put(&audio_q_tx, (void *)&audio_block, K_NO_WAIT);
434436
if (ret) {
@@ -580,7 +582,13 @@ int audio_system_init(void)
580582
int ret;
581583

582584
#if ((CONFIG_AUDIO_DEV == GATEWAY) && (CONFIG_AUDIO_SOURCE_USB))
583-
ret = audio_usb_init();
585+
bool host_in = IS_ENABLED(CONFIG_STREAM_BIDIRECTIONAL);
586+
/* TODO: OCT-3116 Change when microphone-only support (headset with only an audio source)
587+
* is added
588+
*/
589+
bool host_out = true;
590+
591+
ret = audio_usb_init(host_in, host_out);
584592
if (ret) {
585593
LOG_ERR("Failed to initialize USB: %d", ret);
586594
return ret;

applications/nrf5340_audio/src/audio/sw_codec_select.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,16 @@ int sw_codec_decode(struct net_buf const *const audio_frame_in,
288288
return -EINVAL;
289289
}
290290

291-
if (meta_out->locations == BT_AUDIO_LOCATION_MONO_AUDIO &&
292-
meta_in->locations == BT_AUDIO_LOCATION_MONO_AUDIO) {
291+
/* If input is only one channel and not mono_audio and the output
292+
* is mono_audio we will set loc_out to 0xFFFF to accept any location
293+
*/
294+
if (meta_in->locations != BT_AUDIO_LOCATION_MONO_AUDIO &&
295+
meta_out->locations == BT_AUDIO_LOCATION_MONO_AUDIO &&
296+
audio_metadata_num_loc_get(meta_in) == 1) {
297+
loc_out = 0xFFFF;
298+
loc_in = meta_in->locations;
299+
} else if (meta_out->locations == BT_AUDIO_LOCATION_MONO_AUDIO &&
300+
meta_in->locations == BT_AUDIO_LOCATION_MONO_AUDIO) {
293301
loc_in = 1;
294302
loc_out = 1;
295303
} else if (meta_out->locations & meta_in->locations) {

applications/nrf5340_audio/src/modules/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
#
66

77
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/audio_i2s.c)
8-
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/audio_usb.c)
98
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/button_handler.c)
109
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/led.c)
1110
target_sources(app PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/audio_sync_timer.c)
1211

12+
zephyr_library_include_directories(
13+
${ZEPHYR_BASE}/subsys/usb/device_next/class
14+
)
15+
16+
target_sources_ifdef(CONFIG_AUDIO_SOURCE_USB app PRIVATE
17+
${CMAKE_CURRENT_SOURCE_DIR}/audio_usb.c)
18+
target_sources_ifdef(CONFIG_AUDIO_SOURCE_USB app PRIVATE
19+
${CMAKE_CURRENT_SOURCE_DIR}/audio_usb_init.c)
1320
target_sources_ifdef(CONFIG_NRF5340_AUDIO_CS47L63_DRIVER app PRIVATE
1421
${CMAKE_CURRENT_SOURCE_DIR}/hw_codec.c)
1522
target_sources_ifdef(CONFIG_NRF5340_AUDIO_POWER_MEASUREMENT app PRIVATE

applications/nrf5340_audio/src/modules/Kconfig

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,41 @@ config AUDIO_SYNC_TIMER_USES_RTC
1717
default y
1818
select NRFX_RTC0
1919

20+
config USBD_PID
21+
hex
22+
default 0x530A
23+
24+
config USBD_VID
25+
hex
26+
default 0x1915
27+
28+
config USBD_PRODUCT
29+
string
30+
default "nRF5340 USB Audio"
31+
32+
config USBD_MANUFACTURER
33+
string
34+
default "Nordic Semiconductor AS"
35+
36+
config USBD_SELF_POWERED
37+
bool "USB device Self-powered attribute"
38+
default y
39+
help
40+
Set the Self-powered attribute in the configuration.
41+
42+
config USBD_MAX_POWER
43+
int
44+
default 50 if USBD_SELF_POWERED
45+
default 250 # if !USBD_SELF_POWERED
46+
help
47+
The maximum power consumption of the USB device in units of 2 mA
48+
(i.e., the value 50 represents 100 mA, the value 250 represents 500 mA).
49+
50+
config USBD_REMOTE_WAKEUP
51+
bool "USB device Remote Wakeup attribute"
52+
help
53+
Set the Remote Wakeup attribute in the configuration.
54+
2055
#----------------------------------------------------------------------------#
2156
menuconfig NRF5340_AUDIO_POWER_MEASUREMENT
2257
bool "Power measurement"

0 commit comments

Comments
 (0)