summaryrefslogtreecommitdiff
path: root/examples/host/audio_host/src/main.c
diff options
context:
space:
mode:
authorZhang, Zhenjiang <[email protected]>2026-08-14 11:10:23 +0800
committerZhang, Zhenjiang <[email protected]>2026-08-14 11:17:00 +0800
commitc950109bcef6404971c3609a6e4caeb0421120b8 (patch)
treedb2c23a460ff6887395140d458c5f552a5322204 /examples/host/audio_host/src/main.c
parente9578eb103a90d5ab059a067b2e17ca1ddcedee5 (diff)
feat(class/audio): rework TUH_AUDIO into a WASAPI/ALSA-like stream API
Provide a high-level audio streaming API over UAC 1.0 devices while keeping the USB topology private: applications select supported {format, sample_rate, channels} configurations per logical stream, and the driver owns the mapping to AS interface, alternate setting, and endpoint. - One logical stream per direction per instance; multiple AS interfaces and alternate settings in a direction are merged into the stream's configuration list (discrete tuples; continuous ranges exposed as a single configuration at the top rate) - Asynchronous tuh_audio_configure(): SET_INTERFACE to the selected alternate setting, open/reconfigure the endpoint, set the sampling frequency when supported, initialize the FIFO and packet scheduler, then invoke the completion callback - Frame-based FIFO streaming: tuh_audio_read()/tuh_audio_write() queue whole frames; the driver owns transfer replenishment and fractional packet scheduling (44.1 kHz pays back the 0.1 frame/ms remainder via an accumulator for exact average pacing) - tuh_audio_start()/tuh_audio_stop() activate/deactivate the stream interface through SET_INTERFACE (alt n / alt 0) Driver correctness fixes: - Parse only the AC header's interface collection; MIDI Streaming and other subclasses are skipped - Keep every discrete format as a separate configuration; endpoints are opened only for the alternate setting selected by tuh_audio_configure() - Check tuh_interface_set() return values and SET_INTERFACE transfer results instead of ignoring failures - Validate instance state, direction, buffers, and frame counts in every transfer API - Feature Unit requests use the control's real width (mute/AGC/loudness 1 byte, others 2 bytes) and convert multibyte values to host order - Failed/stalled/aborted isochronous transfers reach only the error callback, never the capture/playback callbacks The audio_host example uses the new API: 48 kHz stereo by default, automatic stream restart on error callbacks, a sine test tone on the playback stream, and periodic mic-only / spk-only / echo phase switching.
Diffstat (limited to 'examples/host/audio_host/src/main.c')
-rw-r--r--examples/host/audio_host/src/main.c32
1 files changed, 5 insertions, 27 deletions
diff --git a/examples/host/audio_host/src/main.c b/examples/host/audio_host/src/main.c
index b80cd2938..77c28cf41 100644
--- a/examples/host/audio_host/src/main.c
+++ b/examples/host/audio_host/src/main.c
@@ -35,39 +35,17 @@ int main(void) {
printf("Connect a USB Audio Device (UAC 1.0) to test\r\n");
// init host stack on configured roothub port
- tusb_rhport_init_t host_init = {
- .role = TUSB_ROLE_HOST,
- .speed = TUSB_SPEED_AUTO
- };
+ tusb_rhport_init_t host_init = {.role = TUSB_ROLE_HOST, .speed = TUSB_SPEED_AUTO};
tusb_init(BOARD_TUH_RHPORT, &host_init);
board_init_after_tusb();
-
+ uint32_t last_ms = tusb_time_millis_api();
while (1) {
// tinyusb host task
tuh_task();
led_blinking_task();
- audio_app_task();
+ audio_app_task_read();
+ audio_app_task_write();
+ defer_queue_task();
}
}
-
-//--------------------------------------------------------------------+
-// TinyUSB Callbacks
-//--------------------------------------------------------------------+
-
-//--------------------------------------------------------------------+
-// Blinking Task
-//--------------------------------------------------------------------+
-void led_blinking_task(void) {
- const uint32_t interval_ms = 1000;
- static uint32_t start_ms = 0;
-
- static bool led_state = false;
-
- // Blink every interval ms
- if ( tusb_time_millis_api() - start_ms < interval_ms) return; // not enough time
- start_ms += interval_ms;
-
- board_led_write(led_state);
- led_state = 1 - led_state; // toggle
-}