From 70113a39a3ffac7b310ff7d661fe744c35364c50 Mon Sep 17 00:00:00 2001 From: "Zhang, Zhenjiang" Date: Fri, 17 Jul 2026 13:47:18 +0800 Subject: fix(class/audio): fix control request byte order and buffer usage in audio host - Fix missing tu_htole16() conversions for wValue and wIndex in tuh_audio_set_sampling_freq, tuh_audio_get_sampling_freq, tuh_audio_feature_unit_set, and tuh_audio_feature_unit_get - Fix incorrect wIndex parameter order in feature unit requests (unit_id and itf_num were swapped) - Replace static freq_buf with per-endpoint ctrl buffer in tuh_audio_set_sampling_freq to avoid concurrency issues - Update audio_host README to match actual example behavior --- examples/host/audio_host/README.md | 4 ++-- src/class/audio/audio_host.c | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/host/audio_host/README.md b/examples/host/audio_host/README.md index 4d8f66b71..072adbbf9 100644 --- a/examples/host/audio_host/README.md +++ b/examples/host/audio_host/README.md @@ -54,9 +54,9 @@ make BOARD= flash 3. Open a serial terminal to view output 4. The example will: - Print device information when mounted - - Set sampling frequency to 48kHz + - Set sampling frequency based on the device's advertised capabilities - Receive audio samples from the device (IN endpoint) - - Send test sine wave audio to the device (OUT endpoint) + - Loop back received audio to the device (OUT endpoint) for testing ## Serial Output Example diff --git a/src/class/audio/audio_host.c b/src/class/audio/audio_host.c index 85c2374be..4b25835f4 100644 --- a/src/class/audio/audio_host.c +++ b/src/class/audio/audio_host.c @@ -614,7 +614,9 @@ bool tuh_audio_itf_get_info(uint8_t idx, tuh_itf_info_t *info) { //--------------------------------------------------------------------+ bool tuh_audio_set_sampling_freq(uint8_t daddr, uint8_t ep_addr, uint32_t sampling_freq, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - static uint8_t freq_buf[3] = {0}; + uint8_t const idx = get_idx_by_ep_addr(daddr, ep_addr); + TU_VERIFY(idx < CFG_TUH_AUDIO_MAX, false); + uint8_t* freq_buf = _audioh_epbuf[idx].ctrl; tusb_control_request_t const request = { .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_ENDPOINT, @@ -622,8 +624,8 @@ bool tuh_audio_set_sampling_freq(uint8_t daddr, uint8_t ep_addr, uint32_t sampli .direction = TUSB_DIR_OUT }, .bRequest = AUDIO10_CS_REQ_SET_CUR, - .wValue = tu_u16(AUDIO10_EP_CTRL_SAMPLING_FREQ, 0), // Control Selector = Sampling Freq, Channel = 0 - .wIndex = tu_u16_low(ep_addr), + .wValue = tu_htole16(tu_u16(AUDIO10_EP_CTRL_SAMPLING_FREQ, 0)), // Control Selector = Sampling Freq, Channel = 0 + .wIndex = tu_htole16((uint16_t) ep_addr), .wLength = 3 }; @@ -660,8 +662,8 @@ bool tuh_audio_get_sampling_freq(uint8_t daddr, uint8_t ep_addr, uint32_t *sampl .direction = TUSB_DIR_IN }, .bRequest = AUDIO10_CS_REQ_GET_CUR, - .wValue = tu_u16(AUDIO10_EP_CTRL_SAMPLING_FREQ, 0), // Control Selector = Sampling Freq, Channel = 0 - .wIndex = tu_u16_low(ep_addr), + .wValue = tu_htole16(tu_u16(AUDIO10_EP_CTRL_SAMPLING_FREQ, 0)), // Control Selector = Sampling Freq, Channel = 0 + .wIndex = tu_htole16((uint16_t) ep_addr), .wLength = 3 }; @@ -688,8 +690,8 @@ bool tuh_audio_feature_unit_set(uint8_t daddr, uint8_t itf_num, uint8_t unit_id, .direction = TUSB_DIR_OUT }, .bRequest = AUDIO10_CS_REQ_SET_CUR, - .wValue = tu_u16(control_selector, channel), - .wIndex = tu_u16(itf_num, unit_id), + .wValue = tu_htole16(tu_u16(control_selector, channel)), + .wIndex = tu_htole16(tu_u16(unit_id, itf_num)), .wLength = 2 }; @@ -723,8 +725,8 @@ bool tuh_audio_feature_unit_get(uint8_t daddr, uint8_t itf_num, uint8_t unit_id, .direction = TUSB_DIR_IN }, .bRequest = AUDIO10_CS_REQ_GET_CUR, - .wValue = tu_u16(control_selector, channel), - .wIndex = tu_u16(itf_num, unit_id), + .wValue = tu_htole16(tu_u16(control_selector, channel)), + .wIndex = tu_htole16(tu_u16(unit_id, itf_num)), .wLength = len }; -- cgit v1.3.1