arduino-audio-tools
Loading...
Searching...
No Matches
USBAudioConfig.h
Go to the documentation of this file.
1#pragma once
2#include <stdint.h>
3
4#include <cstring>
5#include <vector>
6
8
9namespace audio_tools {
10
11// ── Platform-specific default USB endpoint numbers ──────────────────────────
12// Some MCUs' USB peripherals restrict isochronous transfers to one hardwired
13// endpoint number instead of allowing the stack to allocate any free one.
14//
15// nRF52833/nRF52840 caveats:
16// - The USBD peripheral has exactly one isochronous endpoint pair, hardwired
17// to endpoint number 8 (tinyusb dcd_nrf5x.c: EP_ISO_NUM == 8). Opening an
18// isochronous endpoint at any other number hits
19// TU_ASSERT(epnum == EP_ISO_NUM) and the device stops responding -- this
20// is why ep_in/ep_out/ep_fb are pinned here instead of using
21// TinyUSBDevice.allocEndpoint() like on RP2040/SAMD/STM32.
22// - ep_in and ep_fb both resolve to 0x88 (the same physical IN endpoint),
23// which is safe because they're mutually exclusive: USB_AUDIO_MAX_ISO_EP
24// is 2, so enableFeedbackEp() (see USBAudio2DescriptorBuilder) is false
25// in RXTX mode and enable_feedback_ep_rxtx has no effect here, matching
26// the hardware's single ISO IN + single ISO OUT pair exactly.
27// - ep_int (the AC status/change interrupt endpoint) is NOT restricted --
28// endpoints 1-7 (control/bulk/interrupt) are freely assignable on this
29// chip -- so it keeps the generic -1 default below and is still allocated
30// dynamically even on nRF52.
31// - Because the fixed 0x88/0x08 numbers bypass allocEndpoint() entirely,
32// TinyUSBDevice's internal endpoint counters don't know endpoint 8 is
33// taken. Audio + a single CDC Serial (the common composite case) is safe
34// since CDC only needs a handful of low-numbered endpoints, but stacking
35// many other USB classes alongside Audio could in theory let one of them
36// also land on endpoint 8 and collide -- an edge case, not a concern for
37// a simple Audio+CDC composite device.
38#if defined(NRF52840_XXAA) || defined(NRF52833_XXAA)
39#ifndef USB_AUDIO_EP_IN
40#define USB_AUDIO_EP_IN 0x88
41#endif
42#ifndef USB_AUDIO_EP_OUT
43#define USB_AUDIO_EP_OUT 0x08
44#endif
45#ifndef USB_AUDIO_EP_FB
46#define USB_AUDIO_EP_FB 0x88
47#endif
48#ifndef USB_AUDIO_MAX_ISO_EP
49#define USB_AUDIO_MAX_ISO_EP 2
50#endif
51#endif
52
53// USBAudioDeviceESP32 (arduino-esp32) previously pinned 0x83/0x03/0x84/0x85
54// here under the assumption that CDC uses 0x81/0x82/0x02. That assumption is
55// wrong: arduino-esp32's own USBCDC::load_cdc_descriptor() (USBCDC.cpp)
56// hardcodes notification=0x85, OUT=0x03, IN=0x84 -- i.e. exactly the "safe"
57// defaults this block used to define -- so a composite CDC + Audio device
58// silently collided on three endpoints (confirmed independently: Adafruit's
59// own TinyUSBDevice::allocEndpoint() carries a matching ESP32 workaround
60// that explicitly steers around 0x03/0x84/0x85 for the same reason). Rather
61// than guess another set of "safe" fixed numbers, ESP32 now falls through to
62// the generic -1 ("allocate dynamically") default below: USBAudioDeviceESP32
63// resolves it via arduino-esp32's own tinyusb_get_free_in_endpoint() /
64// tinyusb_get_free_out_endpoint(), which already know about CDC's reserved
65// endpoints. See https://github.com/pschatzmann/arduino-audio-tools/issues/2396.
66
67// Arduino UNO R4 / Nano R4 (Renesas RA4M1, arduino:renesas_uno core) caveats:
68// - This core's USB.cpp builds one fixed composite descriptor (CDC + HID +
69// MSD) at runtime and has no dynamic endpoint allocator API (no
70// TinyUSBDevice.allocEndpoint(), no tinyusb_get_free_in_endpoint()), so
71// endpoint numbers must be fixed constants here, same as nRF52 above.
72// - Values must avoid the ones the core's own USB.cpp hardcodes for its
73// built-in interfaces: CDC uses 0x81 (notification), 0x02/0x82 (data);
74// HID uses 0x83; MSD uses 0x04/0x84 (see __USBGetCustomInterfaceDescriptor
75// in USB.h for the authoritative list). EP5-EP7 are free on every
76// variant since none of the built-ins use them.
77// - The RA4M1's RUSB2 USB peripheral (dcd_rusb2.c) only has 2 isochronous-
78// capable pipes, so at most 2 ISO endpoints can be open at once
79// (USB_AUDIO_MAX_ISO_EP). TX-only, RX-only-with-feedback, and
80// RXTX-without-feedback each need 2 or fewer ISO endpoints; RXTX with
81// feedback needs 3, so enable_feedback_ep_rxtx has no effect here.
82#if defined(ARDUINO_ARCH_RENESAS)
83#ifndef USB_AUDIO_EP_IN
84#define USB_AUDIO_EP_IN 0x85
85#endif
86#ifndef USB_AUDIO_EP_OUT
87#define USB_AUDIO_EP_OUT 0x05
88#endif
89#ifndef USB_AUDIO_EP_FB
90#define USB_AUDIO_EP_FB 0x86
91#endif
92#ifndef USB_AUDIO_EP_INT
93#define USB_AUDIO_EP_INT 0x87
94#endif
95#ifndef USB_AUDIO_MAX_ISO_EP
96#define USB_AUDIO_MAX_ISO_EP 2
97#endif
98#endif
99
100// Everywhere else (generic TinyUSB: RP2040, SAMD, STM32, and now ESP32) -1
101// tells the platform subclass to allocate the endpoint dynamically instead
102// of using a fixed number. Define any of these yourself (e.g. via a build
103// flag) to support additional boards/cores, or to pin a fixed address,
104// without touching the struct or driver.
105#ifndef USB_AUDIO_EP_IN
106#define USB_AUDIO_EP_IN -1
107#endif
108#ifndef USB_AUDIO_EP_OUT
109#define USB_AUDIO_EP_OUT -1
110#endif
111#ifndef USB_AUDIO_EP_FB
112#define USB_AUDIO_EP_FB -1
113#endif
114#ifndef USB_AUDIO_EP_INT
115#define USB_AUDIO_EP_INT -1
116#endif
117// How many isochronous endpoints the USB peripheral can have open at once,
118// counting the feedback endpoint. Platforms with fewer than 3 (see above)
119// can't have the feedback endpoint in RXTX mode (enable_feedback_ep_rxtx).
120#ifndef USB_AUDIO_MAX_ISO_EP
121#define USB_AUDIO_MAX_ISO_EP 255
122#endif
123
128struct USBAudioConfig : public AudioInfo {
129
130 // ── Direction ─────────────────────────────────────────────────────────────
131 bool enable_ep_in = true;
132 bool enable_ep_out = true;
133
134 // ── USB endpoint addresses ────────────────────────────────────────────────
147
148 // ── Interface numbering ───────────────────────────────────────────────────
152
153 // ── Buffering ─────────────────────────────────────────────────────────────
160
167
168 // ── Device identity ───────────────────────────────────────────────────────
169 uint16_t vid = 0xCafe;
170 uint16_t pid = 0x4002;
171 const char* manufacturer = "Audio Tools";
172 const char* product = "USB Audio";
173 const char* serial = "000001";
174 bool self_powered = true;
176
177 // ── Advanced ──────────────────────────────────────────────────────────────
182
193
202
215 std::vector<uint32_t> supported_sample_rates = {8000, 11025, 16000, 22050,
216 24000, 32000, 44100, 48000};
217
222
229
240
241#if defined(FREERTOS_H) || defined(INC_FREERTOS_H)
242 // ── FreeRTOS USB task (RP2040 + FreeRTOS only) ───────────────────────────
248 bool enable_usb_task = true;
251 int usb_task_stack_size = 1024;
254 int usb_task_priority = configMAX_PRIORITIES - 1;
258 int usb_task_write_wait_ms = 5;
259
260#endif
261 // ── Volume Handling ────────────────────────────────────────────────────────
265 bool volume_active = false;
266
267 // ── ESP32-specific ────────────────────────────────────────────────────────
271 bool begin_usb = false;
272
273 // ── Host-specific (USBAudioHostStream only, ignored by devices) ───────────
275 uint16_t host_vid = 0;
277 uint16_t host_pid = 0;
281 uint16_t host_ctrl_timeout_ms = 500;
288
289 // ── Zephyr-specific ───────────────────────────────────────────────────────
295
296 bool operator==(const USBAudioConfig& other) {
297 return sample_rate == other.sample_rate && channels == other.channels &&
299
300 enable_ep_in == other.enable_ep_in &&
301 enable_ep_out == other.enable_ep_out &&
302
303 ep_in == other.ep_in && ep_out == other.ep_out &&
304 ep_fb == other.ep_fb &&
305
306 itf_num_ac == other.itf_num_ac &&
307 fifo_packets == other.fifo_packets &&
308
309 vid == other.vid && pid == other.pid &&
310
311 std::strcmp(manufacturer ? manufacturer : "",
312 other.manufacturer ? other.manufacturer : "") == 0 &&
313
314 std::strcmp(product ? product : "",
315 other.product ? other.product : "") == 0 &&
316
317 std::strcmp(serial ? serial : "",
318 other.serial ? other.serial : "") == 0 &&
319
320 self_powered == other.self_powered &&
321 max_power_ma == other.max_power_ma &&
322
327
328 begin_usb == other.begin_usb && terminal_id == other.terminal_id &&
329
330 host_vid == other.host_vid && host_pid == other.host_pid &&
331 host_rhport == other.host_rhport &&
333 }
334
335 bool operator!=(const USBAudioConfig& other) { return !(*this == other); }
336};
337
346
347} // namespace audio_tools
#define USB_AUDIO_EP_IN
Definition USBAudioConfig.h:84
#define USB_AUDIO_EP_FB
Definition USBAudioConfig.h:90
#define USB_AUDIO_EP_INT
Definition USBAudioConfig.h:93
#define USB_AUDIO_EP_OUT
Definition USBAudioConfig.h:87
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
uint32_t highestSupportedSampleRate(const USBAudioConfig &cfg)
Definition USBAudioConfig.h:342
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:56
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:58
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:60
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:62
Configuration for USB Audio (inherits sample_rate / channels / bits_per_sample from AudioInfo).
Definition USBAudioConfig.h:128
bool enable_interrupt_ep
Definition USBAudioConfig.h:221
uint16_t host_vid
Only accept an attached audio device with this vendor id (0 = any).
Definition USBAudioConfig.h:275
bool enable_feedback_ep_rxtx
Definition USBAudioConfig.h:192
bool operator==(const USBAudioConfig &other)
Definition USBAudioConfig.h:296
int16_t ep_fb
ISO IN (explicit feedback, see enable_feedback_ep)
Definition USBAudioConfig.h:145
bool self_powered
Definition USBAudioConfig.h:174
const char * serial
Definition USBAudioConfig.h:173
int16_t ep_in
ISO IN (device → host, capture/microphone)
Definition USBAudioConfig.h:143
bool enable_multi_sample_rate
Definition USBAudioConfig.h:201
std::vector< uint32_t > supported_sample_rates
Definition USBAudioConfig.h:215
bool host_apply_device_format
Definition USBAudioConfig.h:287
const char * product
Definition USBAudioConfig.h:172
int16_t ep_int
INT IN (AC status/change notifications)
Definition USBAudioConfig.h:146
uint8_t max_power_ma
Definition USBAudioConfig.h:175
bool enable_feedback_ep
Definition USBAudioConfig.h:181
const char * manufacturer
Definition USBAudioConfig.h:171
uint16_t host_pid
Only accept an attached audio device with this product id (0 = any).
Definition USBAudioConfig.h:277
uint16_t pid
Definition USBAudioConfig.h:170
uint8_t rx_start_fill_percent
Definition USBAudioConfig.h:166
uint8_t host_rhport
TinyUSB root hub port used for host mode (e.g. 1 for Pico-PIO-USB).
Definition USBAudioConfig.h:279
uint16_t vid
Definition USBAudioConfig.h:169
bool enable_ep_in_rate_tracking
Definition USBAudioConfig.h:239
int16_t ep_out
ISO OUT (host → device, playback/speaker)
Definition USBAudioConfig.h:144
bool enable_ep_in
device → host (capture / microphone)
Definition USBAudioConfig.h:131
uint8_t itf_num_ac
Definition USBAudioConfig.h:151
uint16_t host_ctrl_timeout_ms
Timeout for class specific control requests in ms.
Definition USBAudioConfig.h:281
uint8_t fifo_packets
Definition USBAudioConfig.h:159
uint8_t terminal_id
Definition USBAudioConfig.h:294
bool enable_ep_in_flow_control
Definition USBAudioConfig.h:228
bool volume_active
Definition USBAudioConfig.h:265
bool operator!=(const USBAudioConfig &other)
Definition USBAudioConfig.h:335
bool enable_ep_out
host → device (playback / speaker)
Definition USBAudioConfig.h:132
bool begin_usb
Definition USBAudioConfig.h:271