diff --git a/.gitlab/ci/build.yml b/.gitlab/ci/build.yml index 14042bd..6845b8e 100644 --- a/.gitlab/ci/build.yml +++ b/.gitlab/ci/build.yml @@ -150,8 +150,7 @@ build_example_usb_headset: - .rules:build:example_usb_headset parallel: matrix: - - IMAGE: espressif/idf:release-v5.0 - - IMAGE: espressif/idf:release-v5.1 + - IMAGE: espressif/idf:release-v5.2 - IMAGE: espressif/idf:latest variables: EXAMPLE_DIR: examples/usb_headset diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5c94829..26f9bb5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,12 +1,12 @@ repos: - repo: https://github.com/igrr/astyle_py.git - rev: master + rev: v1.0.5 hooks: - id: astyle_py args: ['--style=otbs', '--attach-namespaces', '--attach-classes', '--indent=spaces=4', '--convert-tabs', '--align-pointer=name', '--align-reference=name', '--keep-one-line-statements', '--pad-header', '--pad-oper'] - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.3.0 + rev: v4.5.0 hooks: - id: trailing-whitespace types_or: [c, c++] diff --git a/components/bsp/src/boards/esp32_bsp_board.c b/components/bsp/src/boards/esp32_bsp_board.c index 2c5da56..43a9ade 100644 --- a/components/bsp/src/boards/esp32_bsp_board.c +++ b/components/bsp/src/boards/esp32_bsp_board.c @@ -157,7 +157,7 @@ esp_err_t bsp_codec_set_fs(uint32_t rate, uint32_t bits_cfg, i2s_slot_mode_t ch) ret |= esp_codec_dev_set_in_gain(record_dev_handle, CODEC_DEFAULT_ADC_VOLUME); } - if (record_dev_handle) { + if (play_dev_handle) { ret |= esp_codec_dev_open(play_dev_handle, &fs); } if (record_dev_handle) { diff --git a/examples/usb_headset/bootloader_components/boot_hooks/CMakeLists.txt b/examples/usb_headset/bootloader_components/boot_hooks/CMakeLists.txt new file mode 100644 index 0000000..8b5501b --- /dev/null +++ b/examples/usb_headset/bootloader_components/boot_hooks/CMakeLists.txt @@ -0,0 +1,10 @@ +if("$ENV{IDF_TARGET}" STREQUAL "esp32s3") +idf_component_register(SRCS "boot_hooks.c") +endif() + +# We need to force GCC to integrate this static library into the +# bootloader link. Indeed, by default, as the hooks in the bootloader are weak, +# the linker would just ignore the symbols in the extra. (i.e. not strictly +# required) +# To do so, we need to define the symbol (function) `bootloader_hooks_include` +# within hooks.c source file. diff --git a/examples/usb_headset/bootloader_components/boot_hooks/boot_hooks.c b/examples/usb_headset/bootloader_components/boot_hooks/boot_hooks.c new file mode 100644 index 0000000..4e10c47 --- /dev/null +++ b/examples/usb_headset/bootloader_components/boot_hooks/boot_hooks.c @@ -0,0 +1,23 @@ +/* + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "esp_log.h" +#include "soc/rtc_cntl_struct.h" +#include "soc/usb_serial_jtag_reg.h" + +/* Function used to tell the linker to include this file + * with all its symbols. + */ +void bootloader_hooks_include(void) +{ +} + +void bootloader_before_init(void) +{ + // Disable D+ pullup, to prevent the USB host from retrieving USB-Serial-JTAG's descriptor. + SET_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_PAD_PULL_OVERRIDE); + CLEAR_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_DP_PULLUP); + CLEAR_PERI_REG_MASK(USB_SERIAL_JTAG_CONF0_REG, USB_SERIAL_JTAG_USB_PAD_ENABLE); +} diff --git a/examples/usb_headset/main/idf_component.yml b/examples/usb_headset/main/idf_component.yml index 7a475f4..1a7afb1 100644 --- a/examples/usb_headset/main/idf_component.yml +++ b/examples/usb_headset/main/idf_component.yml @@ -1,6 +1,5 @@ ## IDF Component Manager Manifest File dependencies: idf: ">=5.0" - #espressif/es7210: "^1.0.1" espressif/esp-dsp: "^1.2.1" - espressif/tinyusb: "^0.14.2" \ No newline at end of file + espressif/tinyusb: "^0.15.0" \ No newline at end of file diff --git a/examples/usb_headset/main/include/tusb_config.h b/examples/usb_headset/main/include/tusb_config.h index 5fe5b9a..2325e0e 100644 --- a/examples/usb_headset/main/include/tusb_config.h +++ b/examples/usb_headset/main/include/tusb_config.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -11,9 +11,8 @@ extern "C" { #endif -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" #include "usb_descriptors.h" +#include "usb_headset.h" //--------------------------------------------------------------------+ // Board Specific Configuration @@ -39,7 +38,11 @@ extern "C" { #endif #ifndef CFG_TUSB_OS -#define CFG_TUSB_OS OPT_OS_NONE +#define CFG_TUSB_OS OPT_OS_FREERTOS +#endif + +#ifndef ESP_PLATFORM +#define ESP_PLATFORM 1 #endif #ifndef CFG_TUSB_DEBUG @@ -95,58 +98,35 @@ extern "C" { #define CFG_TUD_AUDIO_FUNC_1_DESC_LEN TUD_AUDIO_HEADSET_STEREO_DESC_LEN // How many formats are used, need to adjust USB descriptor if changed -#define CFG_TUD_AUDIO_FUNC_1_N_FORMATS 2 +#define CFG_TUD_AUDIO_FUNC_1_N_FORMATS 1 -#define __RX__ 1 -// Audio format type I specifications -#if defined(__RX__) -#define CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE 48000 // 16bit/48kHz is the best quality for Renesas RX -#else -#define CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE 96000 // 24bit/96kHz is the best quality for full-speed, high-speed is needed beyond this -#endif -#define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX 1 -#define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX 2 +#define CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE DEFAULT_SAMPLE_RATE // 24bit/48kHz is the best quality for full-speed, high-speed is needed beyond this +#define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX DEFAULT_RECORDER_CHANNEL +#define CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX DEFAULT_PLAYER_CHANNEL // 16bit in 16bit slots -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_TX 2 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_TX 16 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX 2 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_RX 16 +#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_TX (DEFAULT_RECORDER_WIDTH/8) +#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_TX DEFAULT_RECORDER_WIDTH +#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX (DEFAULT_PLAYER_WIDTH/8) +#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_RX DEFAULT_PLAYER_WIDTH -#if defined(__RX__) -// 8bit in 8bit slots -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_TX 1 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_TX 8 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_RX 1 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_RX 8 -#else -// 24bit in 32bit slots -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_TX 4 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_TX 24 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_RX 4 -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_RX 24 -#endif // EP and buffer size - for isochronous EP´s, the buffer and EP size are equal (different sizes would not make sense) #define CFG_TUD_AUDIO_ENABLE_EP_IN 1 - +#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_MS 11 // using a bigger buffer size than the packet size is recommended to avoid packet loss #define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_IN TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_TX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX) -#define CFG_TUD_AUDIO_FUNC_1_FORMAT_2_EP_SZ_IN TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_TX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX) - -#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ TU_MAX(CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_IN, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_EP_SZ_IN)*2 -#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX TU_MAX(CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_IN, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_EP_SZ_IN) // Maximum EP IN size for all AS alternate settings used +#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ (CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_IN * CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_MS) +#define CFG_TUD_AUDIO_FUNC_1_EP_IN_SZ_MAX CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_IN // Maximum EP IN size for all AS alternate settings used // EP and buffer size - for isochronous EP´s, the buffer and EP size are equal (different sizes would not make sense) #define CFG_TUD_AUDIO_ENABLE_EP_OUT 1 - -#define CFG_TUD_AUDIO_UNC_1_FORMAT_1_EP_SZ_OUT TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX) -#define CFG_TUD_AUDIO_UNC_1_FORMAT_2_EP_SZ_OUT TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX) - -#define CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_SZ TU_MAX(CFG_TUD_AUDIO_UNC_1_FORMAT_1_EP_SZ_OUT, CFG_TUD_AUDIO_UNC_1_FORMAT_2_EP_SZ_OUT)*2 -#define CFG_TUD_AUDIO_FUNC_1_EP_OUT_SZ_MAX TU_MAX(CFG_TUD_AUDIO_UNC_1_FORMAT_1_EP_SZ_OUT, CFG_TUD_AUDIO_UNC_1_FORMAT_2_EP_SZ_OUT) // Maximum EP IN size for all AS alternate settings used +#define CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_MS 11 // using a bigger buffer size than the packet size is recommended to avoid packet loss +#define CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_OUT TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX) +#define CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_SZ (CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_OUT * CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_MS) +#define CFG_TUD_AUDIO_FUNC_1_EP_OUT_SZ_MAX CFG_TUD_AUDIO_FUNC_1_FORMAT_1_EP_SZ_OUT // Maximum EP IN size for all AS alternate settings used // Number of Standard AS Interface Descriptors (4.9.1) defined per audio function - this is required to be able to remember the current alternate settings of these interfaces - We restrict us here to have a constant number for all audio functions (which means this has to be the maximum number of AS interfaces an audio function has and a second audio function with less AS interfaces just wastes a few bytes) -#define CFG_TUD_AUDIO_FUNC_1_N_AS_INT 2 +#define CFG_TUD_AUDIO_FUNC_1_N_AS_INT 1 // Size of control request buffer #define CFG_TUD_AUDIO_FUNC_1_CTRL_BUF_SZ 64 diff --git a/examples/usb_headset/main/include/usb_descriptors.h b/examples/usb_headset/main/include/usb_descriptors.h index 2598527..d09af82 100644 --- a/examples/usb_headset/main/include/usb_descriptors.h +++ b/examples/usb_headset/main/include/usb_descriptors.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -7,7 +7,14 @@ #ifndef _USB_DESCRIPTORS_H_ #define _USB_DESCRIPTORS_H_ -// #include "tusb.h" +#include "usb_headset.h" + +// VID +#define USB_VID 0x303A // Espressif VID 0x303A +// This PID is used for testing, please apply for PID through https://github.com/espressif/usb-pids +//#define USB_PID 0x7100 +// BCD_DEVICE = 0x0200, 0x02: version 2, 0x00: sub version 2.0 +#define BCD_DEVICE 0x0200 // Unit numbers are arbitrary selected #define UAC2_ENTITY_CLOCK 0x04 @@ -26,24 +33,38 @@ enum { ITF_NUM_TOTAL }; +#if DEFAULT_PLAYER_CHANNEL == 1 +#define TUD_AUDIO_DESC_FEATURE_UNIT_LEN TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL_LEN +#define TUD_AUDIO_DESC_FEATURE_UNIT() TUD_AUDIO_DESC_FEATURE_UNIT_ONE_CHANNEL(/*_unitid*/ UAC2_ENTITY_SPK_FEATURE_UNIT, /*_srcid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_ctrlch0master*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_ctrlch1*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_stridx*/ 0x00) +#define SPK_CHANNEL_CONFIG (AUDIO_CHANNEL_CONFIG_FRONT_CENTER) +#elif DEFAULT_PLAYER_CHANNEL == 2 +#define TUD_AUDIO_DESC_FEATURE_UNIT_LEN TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL_LEN +#define TUD_AUDIO_DESC_FEATURE_UNIT() TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL(/*_unitid*/ UAC2_ENTITY_SPK_FEATURE_UNIT, /*_srcid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_ctrlch0master*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_ctrlch1*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_ctrlch2*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_stridx*/ 0x00) +#define SPK_CHANNEL_CONFIG (AUDIO_CHANNEL_CONFIG_FRONT_RIGHT | AUDIO_CHANNEL_CONFIG_FRONT_LEFT) +#else +#error "Unsupported player channel count" +#endif + +#if DEFAULT_RECORDER_CHANNEL == 1 +#define MIC_CHANNEL_CONFIG (AUDIO_CHANNEL_CONFIG_FRONT_CENTER) +#elif DEFAULT_RECORDER_CHANNEL == 2 +#define MIC_CHANNEL_CONFIG (AUDIO_CHANNEL_CONFIG_FRONT_RIGHT | AUDIO_CHANNEL_CONFIG_FRONT_LEFT) +#else +#error "Unsupported recorder channel count" +#endif + #define TUD_AUDIO_HEADSET_STEREO_DESC_LEN (TUD_AUDIO_DESC_IAD_LEN\ + TUD_AUDIO_DESC_STD_AC_LEN\ + TUD_AUDIO_DESC_CS_AC_LEN\ + TUD_AUDIO_DESC_CLK_SRC_LEN\ + TUD_AUDIO_DESC_INPUT_TERM_LEN\ - + TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL_LEN\ + + TUD_AUDIO_DESC_FEATURE_UNIT_LEN\ + TUD_AUDIO_DESC_OUTPUT_TERM_LEN\ + TUD_AUDIO_DESC_INPUT_TERM_LEN\ + TUD_AUDIO_DESC_OUTPUT_TERM_LEN\ /* Interface 1, Alternate 0 */\ + TUD_AUDIO_DESC_STD_AS_INT_LEN\ - /* Interface 1, Alternate 0 */\ - + TUD_AUDIO_DESC_STD_AS_INT_LEN\ - + TUD_AUDIO_DESC_CS_AS_INT_LEN\ - + TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN\ - + TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN\ - + TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN\ - /* Interface 1, Alternate 2 */\ + /* Interface 1, Alternate 1 */\ + TUD_AUDIO_DESC_STD_AS_INT_LEN\ + TUD_AUDIO_DESC_CS_AS_INT_LEN\ + TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN\ @@ -57,12 +78,7 @@ enum { + TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN\ + TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN\ + TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN\ - /* Interface 2, Alternate 2 */\ - + TUD_AUDIO_DESC_STD_AS_INT_LEN\ - + TUD_AUDIO_DESC_CS_AS_INT_LEN\ - + TUD_AUDIO_DESC_TYPE_I_FORMAT_LEN\ - + TUD_AUDIO_DESC_STD_AS_ISO_EP_LEN\ - + TUD_AUDIO_DESC_CS_AS_ISO_EP_LEN) +) #define TUD_AUDIO_HEADSET_STEREO_DESCRIPTOR(_stridx, _epout, _epin) \ /* Standard Interface Association Descriptor (IAD) */\ @@ -70,66 +86,45 @@ enum { /* Standard AC Interface Descriptor(4.7.1) */\ TUD_AUDIO_DESC_STD_AC(/*_itfnum*/ ITF_NUM_AUDIO_CONTROL, /*_nEPs*/ 0x00, /*_stridx*/ _stridx),\ /* Class-Specific AC Interface Header Descriptor(4.7.2) */\ - TUD_AUDIO_DESC_CS_AC(/*_bcdADC*/ 0x0200, /*_category*/ AUDIO_FUNC_HEADSET, /*_totallen*/ TUD_AUDIO_DESC_CLK_SRC_LEN+TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL_LEN+TUD_AUDIO_DESC_INPUT_TERM_LEN+TUD_AUDIO_DESC_OUTPUT_TERM_LEN+TUD_AUDIO_DESC_INPUT_TERM_LEN+TUD_AUDIO_DESC_OUTPUT_TERM_LEN, /*_ctrl*/ AUDIO_CS_AS_INTERFACE_CTRL_LATENCY_POS),\ + TUD_AUDIO_DESC_CS_AC(/*_bcdADC*/ 0x0200, /*_category*/ AUDIO_FUNC_HEADSET, /*_totallen*/ TUD_AUDIO_DESC_CLK_SRC_LEN+TUD_AUDIO_DESC_FEATURE_UNIT_LEN+TUD_AUDIO_DESC_INPUT_TERM_LEN+TUD_AUDIO_DESC_OUTPUT_TERM_LEN+TUD_AUDIO_DESC_INPUT_TERM_LEN+TUD_AUDIO_DESC_OUTPUT_TERM_LEN, /*_ctrl*/ AUDIO_CS_AS_INTERFACE_CTRL_LATENCY_POS),\ /* Clock Source Descriptor(4.7.2.1) */\ TUD_AUDIO_DESC_CLK_SRC(/*_clkid*/ UAC2_ENTITY_CLOCK, /*_attr*/ 3, /*_ctrl*/ 7, /*_assocTerm*/ 0x00, /*_stridx*/ 0x00), \ /* Input Terminal Descriptor(4.7.2.4) */\ - TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ 0x00, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_nchannelslogical*/ 0x02, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ 0x00, /*_ctrl*/ 0 * (AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS), /*_stridx*/ 0x00),\ + TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ 0x00, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_nchannelslogical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX, /*_channelcfg*/ SPK_CHANNEL_CONFIG, /*_idxchannelnames*/ 0x00, /*_ctrl*/ 0 * (AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS), /*_stridx*/ 0x00),\ /* Feature Unit Descriptor(4.7.2.8) */\ - TUD_AUDIO_DESC_FEATURE_UNIT_TWO_CHANNEL(/*_unitid*/ UAC2_ENTITY_SPK_FEATURE_UNIT, /*_srcid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_ctrlch0master*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_ctrlch1*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_ctrlch2*/ (AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_MUTE_POS | AUDIO_CTRL_RW << AUDIO_FEATURE_UNIT_CTRL_VOLUME_POS), /*_stridx*/ 0x00),\ + TUD_AUDIO_DESC_FEATURE_UNIT(),\ /* Output Terminal Descriptor(4.7.2.5) */\ - TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ UAC2_ENTITY_SPK_OUTPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_OUT_HEADPHONES, /*_assocTerm*/ 0x00, /*_srcid*/ UAC2_ENTITY_SPK_FEATURE_UNIT, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\ + TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ UAC2_ENTITY_SPK_OUTPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_OUT_GENERIC_SPEAKER, /*_assocTerm*/ 0x00, /*_srcid*/ UAC2_ENTITY_SPK_FEATURE_UNIT, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\ /* Input Terminal Descriptor(4.7.2.4) */\ - TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ UAC2_ENTITY_MIC_INPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_IN_GENERIC_MIC, /*_assocTerm*/ 0x00, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_nchannelslogical*/ 0x01, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_idxchannelnames*/ 0x00, /*_ctrl*/ 0 * (AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS), /*_stridx*/ 0x00),\ + TUD_AUDIO_DESC_INPUT_TERM(/*_termid*/ UAC2_ENTITY_MIC_INPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_IN_GENERIC_MIC, /*_assocTerm*/ 0x00, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_nchannelslogical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX, /*_channelcfg*/ MIC_CHANNEL_CONFIG, /*_idxchannelnames*/ 0x00, /*_ctrl*/ 0 * (AUDIO_CTRL_R << AUDIO_IN_TERM_CTRL_CONNECTOR_POS), /*_stridx*/ 0x00),\ /* Output Terminal Descriptor(4.7.2.5) */\ TUD_AUDIO_DESC_OUTPUT_TERM(/*_termid*/ UAC2_ENTITY_MIC_OUTPUT_TERMINAL, /*_termtype*/ AUDIO_TERM_TYPE_USB_STREAMING, /*_assocTerm*/ 0x00, /*_srcid*/ UAC2_ENTITY_MIC_INPUT_TERMINAL, /*_clkid*/ UAC2_ENTITY_CLOCK, /*_ctrl*/ 0x0000, /*_stridx*/ 0x00),\ /* Standard AS Interface Descriptor(4.9.1) */\ /* Interface 1, Alternate 0 - default alternate setting with 0 bandwidth */\ - TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_SPK), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x05),\ + TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_SPK), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x04),\ /* Standard AS Interface Descriptor(4.9.1) */\ /* Interface 1, Alternate 1 - alternate interface for data streaming */\ - TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_SPK), /*_altset*/ 0x01, /*_nEPs*/ 0x01, /*_stridx*/ 0x05),\ + TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_SPK), /*_altset*/ 0x01, /*_nEPs*/ 0x01, /*_stridx*/ 0x04),\ /* Class-Specific AS Interface Descriptor(4.9.2) */\ - TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_stridx*/ 0x00),\ + TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX, /*_channelcfg*/ SPK_CHANNEL_CONFIG, /*_stridx*/ 0x00),\ /* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\ TUD_AUDIO_DESC_TYPE_I_FORMAT(CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_RX),\ /* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\ - TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epout, /*_attr*/ (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ADAPTIVE | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX), /*_interval*/ 0x01),\ + TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epout, /*_attr*/ (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_SYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX), /*_interval*/ 0x01),\ /* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\ TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_MILLISEC, /*_lockdelay*/ 0x0001),\ - /* Interface 1, Alternate 2 - alternate interface for data streaming */\ - TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_SPK), /*_altset*/ 0x02, /*_nEPs*/ 0x01, /*_stridx*/ 0x05),\ - /* Class-Specific AS Interface Descriptor(4.9.2) */\ - TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ UAC2_ENTITY_SPK_INPUT_TERMINAL, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_stridx*/ 0x00),\ - /* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\ - TUD_AUDIO_DESC_TYPE_I_FORMAT(CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_RX),\ - /* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\ - TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epout, /*_attr*/ (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ADAPTIVE | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_RX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX), /*_interval*/ 0x01),\ - /* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\ - TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_MILLISEC, /*_lockdelay*/ 0x0001),\ - /* Standard AS Interface Descriptor(4.9.1) */\ /* Interface 2, Alternate 0 - default alternate setting with 0 bandwidth */\ - TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_MIC), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x04),\ + TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_MIC), /*_altset*/ 0x00, /*_nEPs*/ 0x00, /*_stridx*/ 0x05),\ /* Standard AS Interface Descriptor(4.9.1) */\ /* Interface 2, Alternate 1 - alternate interface for data streaming */\ - TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_MIC), /*_altset*/ 0x01, /*_nEPs*/ 0x01, /*_stridx*/ 0x04),\ + TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_MIC), /*_altset*/ 0x01, /*_nEPs*/ 0x01, /*_stridx*/ 0x05),\ /* Class-Specific AS Interface Descriptor(4.9.2) */\ - TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ UAC2_ENTITY_MIC_OUTPUT_TERMINAL, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_stridx*/ 0x00),\ + TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ UAC2_ENTITY_MIC_OUTPUT_TERMINAL, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX, /*_channelcfg*/ MIC_CHANNEL_CONFIG, /*_stridx*/ 0x00),\ /* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\ TUD_AUDIO_DESC_TYPE_I_FORMAT(CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_TX, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_TX),\ /* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\ TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_1_N_BYTES_PER_SAMPLE_TX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX), /*_interval*/ 0x01),\ /* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\ - TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000),\ - /* Interface 2, Alternate 2 - alternate interface for data streaming */\ - TUD_AUDIO_DESC_STD_AS_INT(/*_itfnum*/ (uint8_t)(ITF_NUM_AUDIO_STREAMING_MIC), /*_altset*/ 0x02, /*_nEPs*/ 0x01, /*_stridx*/ 0x04),\ - /* Class-Specific AS Interface Descriptor(4.9.2) */\ - TUD_AUDIO_DESC_CS_AS_INT(/*_termid*/ UAC2_ENTITY_MIC_OUTPUT_TERMINAL, /*_ctrl*/ AUDIO_CTRL_NONE, /*_formattype*/ AUDIO_FORMAT_TYPE_I, /*_formats*/ AUDIO_DATA_FORMAT_TYPE_I_PCM, /*_nchannelsphysical*/ CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX, /*_channelcfg*/ AUDIO_CHANNEL_CONFIG_NON_PREDEFINED, /*_stridx*/ 0x00),\ - /* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\ - TUD_AUDIO_DESC_TYPE_I_FORMAT(CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_TX, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_TX),\ - /* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\ - TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ TUD_AUDIO_EP_SIZE(CFG_TUD_AUDIO_FUNC_1_MAX_SAMPLE_RATE, CFG_TUD_AUDIO_FUNC_1_FORMAT_2_N_BYTES_PER_SAMPLE_TX, CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX), /*_interval*/ 0x01),\ - /* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\ TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000) #endif diff --git a/examples/usb_headset/main/include/usb_headset.h b/examples/usb_headset/main/include/usb_headset.h index 21b64c5..9c0f38c 100644 --- a/examples/usb_headset/main/include/usb_headset.h +++ b/examples/usb_headset/main/include/usb_headset.h @@ -12,11 +12,15 @@ extern "C" { #include "esp_err.h" -#define SAMPLE_RATE (48000) -#define DEFAULT_VOLUME (80) -#define CHANNEL (2) -#define WIDTH (16) - +#define DEFAULT_SAMPLE_RATE (48000) +#define DEFAULT_VOLUME (99) +// Currently the player and recorder should use the same channel and width +#define DEFAULT_RECORDER_CHANNEL (1) +#define DEFAULT_RECORDER_WIDTH (16) +#define DEFAULT_PLAYER_CHANNEL (1) +#define DEFAULT_PLAYER_WIDTH (16) +#define DEBUG_USB_HEADSET (0) +#define DEBUG_SYSTEM_VIEW (0) /** * @brief Initialize the usb headset function * diff --git a/examples/usb_headset/main/include/usb_headset_debug.h b/examples/usb_headset/main/include/usb_headset_debug.h new file mode 100644 index 0000000..34acc5a --- /dev/null +++ b/examples/usb_headset/main/include/usb_headset_debug.h @@ -0,0 +1,107 @@ +/* + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#pragma once + +#if DEBUG_USB_HEADSET +#define bsp_codec_player_set_fs bsp_codec_player_set_fs_debug +#define bsp_codec_recorder_set_fs bsp_codec_recorder_set_fs_debug +#define bsp_codec_volume_set bsp_codec_volume_set_debug +#define bsp_codec_mute_set bsp_codec_mute_set_debug +#define bsp_i2s_read bsp_i2s_read_debug +#define bsp_i2s_write bsp_i2s_write_debug +#include "esp_timer.h" + +#ifdef __cplusplus +extern "C" { +#endif + +void bsp_codec_player_set_fs_debug(uint32_t sample_rate, uint8_t resolution, uint8_t channel) +{ + TU_LOG1("bsp_codec_player_set_fs: sample_rate %" PRIu32 ", resolution %d, channel %d", sample_rate, resolution, channel); +} + +void bsp_codec_recorder_set_fs_debug(uint32_t sample_rate, uint8_t resolution, uint8_t channel) +{ + TU_LOG1("bsp_codec_recorder_set_fs: sample_rate %" PRIu32 ", resolution %d, channel %d", sample_rate, resolution, channel); +} + +void bsp_codec_volume_set_debug(int volume, int *volume_db) +{ + TU_LOG1("bsp_codec_volume_set: volume %d, volume_db %p", volume, volume_db); +} + +void bsp_codec_mute_set_debug(int mute) +{ + TU_LOG1("bsp_codec_mute_set: mute %d", mute); +} + +esp_err_t bsp_i2s_read_debug(void *buf, size_t size, size_t *bytes_read, TickType_t ticks_to_wait) +{ + static uint64_t last_time = 0; + uint64_t current_time = esp_timer_get_time(); + size_t data_read_size = (current_time - last_time) / 1000 * sample_rate * mic_resolution / 8; + if (data_read_size > size) { + data_read_size = size; + } + +#if DEFAULT_RECORDER_CHANNEL == 1 + int16_t *data_buf = (int16_t *)buf; + for (int i = 0; i < data_read_size / 2; i++) { + data_buf[i] = (int16_t)(32767 * sin(2 * M_PI * 1000 * i / sample_rate)); + } + *bytes_read = data_read_size; +#elif DEFAULT_RECORDER_CHANNEL == 2 + int16_t *data_buf = (int16_t *)buf; + for (int i = 0; i < data_read_size / 4; i++) { + data_buf[2 * i] = (int16_t)(32767 * sin(2 * M_PI * 1000 * i / sample_rate)); + data_buf[2 * i + 1] = (int16_t)(32767 * sin(2 * M_PI * 1000 * i / sample_rate)); + } + *bytes_read = data_read_size; +#else +#error "Not support in debug mode" +#endif + + TU_LOG2("bsp_i2s_read: buf %p, size %d, bytes_read %d, ticks_to_wait %ld", buf, size, *bytes_read, ticks_to_wait); + return ESP_OK; +} + +esp_err_t bsp_i2s_write_debug(void *buf, size_t size, size_t *bytes_written, TickType_t ticks_to_wait) +{ + TU_LOG2("bsp_i2s_write: buf %p, size %d, bytes_written %d, ticks_to_wait %ld", buf, size, *bytes_written, ticks_to_wait); + return ESP_OK; +} + +#ifdef __cplusplus +} +#endif +#endif + +#if DEBUG_SYSTEM_VIEW +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#define SYSVIEW_SPK_WAIT_EVENT_ID 0 +#define SYSVIEW_SPK_SEND_EVENT_ID 1 +#define SYSVIEW_MIC_WAIT_EVENT_ID 2 +#define SYSVIEW_MIC_READ_EVENT_ID 3 + +#define SYSVIEW_SPK_WAIT_EVENT_START() SEGGER_SYSVIEW_OnUserStart(SYSVIEW_SPK_WAIT_EVENT_ID) +#define SYSVIEW_SPK_WAIT_EVENT_END() SEGGER_SYSVIEW_OnUserStop(SYSVIEW_SPK_WAIT_EVENT_ID) +#define SYSVIEW_SPK_SEND_EVENT_START() SEGGER_SYSVIEW_OnUserStart(SYSVIEW_SPK_SEND_EVENT_ID) +#define SYSVIEW_SPK_SEND_EVENT_END() SEGGER_SYSVIEW_OnUserStop(SYSVIEW_SPK_SEND_EVENT_ID) +#define SYSVIEW_MIC_WAIT_EVENT_START() SEGGER_SYSVIEW_OnUserStart(SYSVIEW_MIC_WAIT_EVENT_ID) +#define SYSVIEW_MIC_WAIT_EVENT_END() SEGGER_SYSVIEW_OnUserStop(SYSVIEW_MIC_WAIT_EVENT_ID) +#define SYSVIEW_MIC_READ_EVENT_START() SEGGER_SYSVIEW_OnUserStart(SYSVIEW_MIC_READ_EVENT_ID) +#define SYSVIEW_MIC_READ_EVENT_END() SEGGER_SYSVIEW_OnUserStop(SYSVIEW_MIC_READ_EVENT_ID) +#else +#define SYSVIEW_SPK_WAIT_EVENT_START() +#define SYSVIEW_SPK_WAIT_EVENT_END() +#define SYSVIEW_SPK_SEND_EVENT_START() +#define SYSVIEW_SPK_SEND_EVENT_END() +#define SYSVIEW_MIC_WAIT_EVENT_START() +#define SYSVIEW_MIC_WAIT_EVENT_END() +#define SYSVIEW_MIC_READ_EVENT_START() +#define SYSVIEW_MIC_READ_EVENT_END() +#endif diff --git a/examples/usb_headset/main/main.c b/examples/usb_headset/main/main.c index 328b9d4..a836d52 100644 --- a/examples/usb_headset/main/main.c +++ b/examples/usb_headset/main/main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -11,8 +11,32 @@ #include "fft_convert.h" #include "usb_headset.h" +/* Can be used for i2s_std_gpio_config_t and/or i2s_std_config_t initialization */ +#define BSP_I2S_GPIO_CFG \ + { \ + .mclk = BSP_I2S_MCLK, \ + .bclk = BSP_I2S_SCLK, \ + .ws = BSP_I2S_LCLK, \ + .dout = BSP_I2S_DOUT, \ + .din = BSP_I2S_DSIN, \ + .invert_flags = { \ + .mclk_inv = false, \ + .bclk_inv = false, \ + .ws_inv = false, \ + }, \ + } + +/* This configuration is used by default in bsp_audio_init() */ +#define BSP_I2S_DUPLEX_MONO_CFG(_sample_rate) \ + { \ + .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(_sample_rate), \ + .slot_cfg = I2S_STD_PHILIP_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO), \ + .gpio_cfg = BSP_I2S_GPIO_CFG, \ + } + void app_main(void) { +#if !DEBUG_USB_HEADSET /* Initialize I2C (for touch and audio) */ bsp_i2c_init(); @@ -22,11 +46,19 @@ void app_main(void) /* Init fft */ fft_convert_init(); + /* Initialize audio i2s */ + i2s_std_config_t i2s_config = BSP_I2S_DUPLEX_MONO_CFG(DEFAULT_SAMPLE_RATE); + i2s_config.clk_cfg.mclk_multiple = I2S_MCLK_MULTIPLE_384; + bsp_audio_init(&i2s_config); + + /* Initialize bsp board */ bsp_board_init(); - bsp_codec_set_fs(SAMPLE_RATE, WIDTH, CHANNEL); - bsp_codec_volume_set(80, NULL); + /* Initialize codec with defaults */ + bsp_codec_set_fs(DEFAULT_SAMPLE_RATE, DEFAULT_PLAYER_WIDTH, DEFAULT_PLAYER_CHANNEL); + bsp_codec_volume_set(DEFAULT_VOLUME, NULL); bsp_codec_mute_set(false); +#endif usb_headset_init(); } diff --git a/examples/usb_headset/main/main.h b/examples/usb_headset/main/main.h index f3e2f29..8230662 100644 --- a/examples/usb_headset/main/main.h +++ b/examples/usb_headset/main/main.h @@ -9,7 +9,7 @@ /** Major version number (X.x.x) */ #define USB_HEADSET_VERSION_MAJOR 0 /** Minor version number (x.X.x) */ -#define USB_HEADSET_VERSION_MINOR 0 +#define USB_HEADSET_VERSION_MINOR 1 /** Patch version number (x.x.X) */ #define USB_HEADSET_VERSION_PATCH 1 diff --git a/examples/usb_headset/main/src/fft_convert.c b/examples/usb_headset/main/src/fft_convert.c index 3c19b38..a148069 100644 --- a/examples/usb_headset/main/src/fft_convert.c +++ b/examples/usb_headset/main/src/fft_convert.c @@ -47,12 +47,12 @@ esp_err_t fft_init(void) return ESP_OK; } -IRAM_ATTR void rb_write(int16_t *buf, size_t size) +void rb_write(int16_t *buf, size_t size) { if (buf == NULL) { return; } - xRingbufferSendFromISR(rb_handle, (void *)buf, size, (TickType_t)0); + xRingbufferSend(rb_handle, (void *)buf, size, 0); } static void rb_init(void) @@ -103,6 +103,6 @@ esp_err_t fft_convert_init(void) { rb_init(); fft_init(); - xTaskCreate(fft_convert_task, "fft_convert_task", 1024 * 8, NULL, 10, NULL); + xTaskCreate(fft_convert_task, "fft_convert_task", 1024 * 8, NULL, 1, NULL); return ESP_OK; } diff --git a/examples/usb_headset/main/src/usb_descriptors.c b/examples/usb_headset/main/src/usb_descriptors.c index f33a7d3..03d4535 100644 --- a/examples/usb_headset/main/src/usb_descriptors.c +++ b/examples/usb_headset/main/src/usb_descriptors.c @@ -13,9 +13,11 @@ * Auto ProductID layout's Bitmap: * [MSB] AUDIO | MIDI | HID | MSC | CDC [LSB] */ +#ifndef USB_PID #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ _PID_MAP(MIDI, 3) | _PID_MAP(AUDIO, 4) | _PID_MAP(VENDOR, 5) ) +#endif //--------------------------------------------------------------------+ // Device Descriptors @@ -32,9 +34,9 @@ tusb_desc_device_t const desc_device = { .bDeviceProtocol = MISC_PROTOCOL_IAD, .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, - .idVendor = 0x303A, + .idVendor = USB_VID, .idProduct = USB_PID, - .bcdDevice = 0x0100, + .bcdDevice = BCD_DEVICE, .iManufacturer = 0x01, .iProduct = 0x02, @@ -54,35 +56,15 @@ uint8_t const *tud_descriptor_device_cb(void) // Configuration Descriptor //--------------------------------------------------------------------+ #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_AUDIO * TUD_AUDIO_HEADSET_STEREO_DESC_LEN) - -#if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX -// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number -// 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ... -#define EPNUM_AUDIO_IN 0x03 -#define EPNUM_AUDIO_OUT 0x03 - -#elif CFG_TUSB_MCU == OPT_MCU_NRF5X -// ISO endpoints for NRF5x are fixed to 0x08 (0x88) -#define EPNUM_AUDIO_IN 0x08 -#define EPNUM_AUDIO_OUT 0x08 - -#elif CFG_TUSB_MCU == OPT_MCU_SAMG || CFG_TUSB_MCU == OPT_MCU_SAMX7X -// SAMG & SAME70 don't support a same endpoint number with different direction IN and OUT -// e.g EP1 OUT & EP1 IN cannot exist together -#define EPNUM_AUDIO_IN 0x01 -#define EPNUM_AUDIO_OUT 0x02 - -#else -#define EPNUM_AUDIO_IN 0x01 +#define EPNUM_AUDIO_IN 0x81 #define EPNUM_AUDIO_OUT 0x01 -#endif uint8_t const desc_configuration[] = { // Interface count, string index, total length, attribute, power in mA - TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100), + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 500), - // Interface number, string index, EP Out & EP In address, EP size - TUD_AUDIO_HEADSET_STEREO_DESCRIPTOR(2, EPNUM_AUDIO_OUT, EPNUM_AUDIO_IN | 0x80) + // String index, EP Out & EP In address + TUD_AUDIO_HEADSET_STEREO_DESCRIPTOR(2, EPNUM_AUDIO_OUT, EPNUM_AUDIO_IN) }; // Invoked when received GET CONFIGURATION DESCRIPTOR diff --git a/examples/usb_headset/main/src/usb_headset.c b/examples/usb_headset/main/src/usb_headset.c index 31e5bb6..3bd200f 100644 --- a/examples/usb_headset/main/src/usb_headset.c +++ b/examples/usb_headset/main/src/usb_headset.c @@ -1,46 +1,27 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ -#include "bsp_board.h" -#include "bsp/esp-bsp.h" - +#include +#include +#include "freertos/FreeRTOS.h" #include "esp_private/usb_phy.h" -#include "fft_convert.h" +#include "esp_log.h" +#include "bsp/esp-bsp.h" +#include "bsp_board.h" #include "tusb.h" #include "usb_descriptors.h" #include "usb_headset.h" +#include "fft_convert.h" + +const static char *TAG = "usb_headset"; + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTOTYPES //--------------------------------------------------------------------+ -// List of supported sample rates -#if defined(__RX__) -const uint32_t sample_rates[] = {44100, 48000}; -#else -const uint32_t sample_rates[] = {44100, 48000, 88200, 96000}; -#endif - -uint32_t current_sample_rate = 48000; - -#define N_SAMPLE_RATES TU_ARRAY_SIZE(sample_rates) - -#define AUDIO_LENGTH 96 - -/* Blink pattern - * - 25 ms : streaming data - * - 250 ms : device not mounted - * - 1000 ms : device mounted - * - 2500 ms : device is suspended - */ -enum { - BLINK_STREAMING = 25, - BLINK_NOT_MOUNTED = 250, - BLINK_MOUNTED = 1000, - BLINK_SUSPENDED = 2500, -}; - +// Volume control range enum { VOLUME_CTRL_0_DB = 0, VOLUME_CTRL_10_DB = 2560, @@ -55,34 +36,55 @@ enum { VOLUME_CTRL_100_DB = 25600, VOLUME_CTRL_SILENCE = 0x8000, }; +// List of supported sample rates +// we just support one sample rate, due to the limitation of the codec +const uint32_t sample_rates[] = {DEFAULT_SAMPLE_RATE}; +static uint32_t s_sample_rate = DEFAULT_SAMPLE_RATE; +#define N_SAMPLE_RATES TU_ARRAY_SIZE(sample_rates) -static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; - -// Audio controls -// Current states -int8_t mute[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX + 1]; // +1 for master channel 0 -int16_t volume[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX + 1]; // +1 for master channel 0 +// Audio controls, current states +static int8_t spk_mute[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX + 1]; // +1 for master channel 0 +static int16_t spk_volume[CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX + 1]; // +1 for master channel 0 +static audio_control_range_2_n_t(1) range_vol = { + .wNumSubRanges = tu_htole16(1), + .subrange[0] = { .bMin = tu_htole16(-VOLUME_CTRL_50_DB), tu_htole16(VOLUME_CTRL_0_DB), tu_htole16(256) } +}; // Buffer for microphone data -int16_t mic_buf[CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ / 2]; +static int16_t s_mic_buf1[CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ / 2] = {0}; +static int16_t s_mic_buf2[CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_SZ / 2] = {0}; +static int16_t *s_mic_write_buf = s_mic_buf1; +static int16_t *s_mic_read_buf = s_mic_buf2; +volatile static size_t s_mic_read_buf_len = 0; +static TaskHandle_t mic_task_handle; // Buffer for speaker data -int16_t spk_buf[CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_SZ / 4]; -// Speaker data size received in the last frame -int spk_data_size; -// Resolution per format -const uint8_t resolutions_per_format[CFG_TUD_AUDIO_FUNC_1_N_FORMATS] = {CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_RX, - CFG_TUD_AUDIO_FUNC_1_FORMAT_2_RESOLUTION_RX - }; +static int16_t s_spk_buf[CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_SZ / 2] = {0}; +volatile static size_t s_spk_buf_len = 0; +static TaskHandle_t spk_task_handle; +// Speaker and microphone status +volatile static bool s_spk_active; +volatile static bool s_mic_active; +// Resolution per format, Note: due to the limitation of the codec, we currently just support one resolution +const uint8_t spk_resolutions_per_format[CFG_TUD_AUDIO_FUNC_1_N_FORMATS] = {CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_RX + }; +const uint8_t mic_resolutions_per_format[CFG_TUD_AUDIO_FUNC_1_N_FORMATS] = {CFG_TUD_AUDIO_FUNC_1_FORMAT_1_RESOLUTION_TX + }; // Current resolution, update on format change -uint8_t current_resolution; +static uint8_t s_spk_resolution = spk_resolutions_per_format[0]; +static uint8_t s_mic_resolution = mic_resolutions_per_format[0]; +static size_t s_spk_bytes_ms = 0; +static size_t s_mic_bytes_ms = 0; +static portMUX_TYPE s_mux = portMUX_INITIALIZER_UNLOCKED; +#define UAC_ENTER_CRITICAL() portENTER_CRITICAL(&s_mux) +#define UAC_EXIT_CRITICAL() portEXIT_CRITICAL(&s_mux) -// void led_blinking_task(void); -void audio_task(void); +// for debug purpose +#include "usb_headset_debug.h" -static usb_phy_handle_t phy_hdl; static void usb_phy_init(void) { // Configure USB PHY + usb_phy_handle_t phy_hdl; usb_phy_config_t phy_conf = { .controller = USB_PHY_CTRL_OTG, .otg_mode = USB_OTG_MODE_DEVICE, @@ -94,36 +96,114 @@ static void usb_phy_init(void) static void usb_task(void *pvParam) { (void) pvParam; + usb_phy_init(); + if (tud_init(BOARD_TUD_RHPORT) == false) { + ESP_LOGE(TAG, "Failed to initialize TinyUSB"); + goto error; + } do { - // TinyUSB device task + // TinyUSB Core task tud_task(); } while (true); +error: vTaskDelete(NULL); } -/*------------- MAIN -------------*/ +static void usb_headset_spk(void *pvParam) +{ + while (1) { + SYSVIEW_SPK_WAIT_EVENT_START(); + if (s_spk_active == false) { + ulTaskNotifyTake(pdFAIL, portMAX_DELAY); + continue; + } + // clear the notification + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); + if (s_spk_buf_len == 0) { + continue; + } + // playback the data from the ring buffer chunk by chunk + SYSVIEW_SPK_WAIT_EVENT_END(); + SYSVIEW_SPK_SEND_EVENT_START(); + size_t bytes_written = 0; + bsp_i2s_write(s_spk_buf, s_spk_buf_len, &bytes_written, 0); + for (int i = 0; i < bytes_written / 2; i ++) { + rb_write(s_spk_buf + i, 2); + } + s_spk_buf_len = 0; + SYSVIEW_SPK_SEND_EVENT_END(); + } +} + +static void usb_headset_mic(void *pvParam) +{ + while (1) { + if (s_mic_active == false) { + ulTaskNotifyTake(pdFAIL, portMAX_DELAY); + continue; + } + // clear the notification + ulTaskNotifyTake(pdTRUE, 0); + // read data from the microphone chunk by chunk + SYSVIEW_MIC_READ_EVENT_START(); + size_t bytes_read = 0; + size_t bytes_require = s_mic_bytes_ms * (CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_MS - 1); + esp_err_t ret = bsp_i2s_read(s_mic_write_buf, bytes_require, &bytes_read, 0); + if (ret != ESP_OK) { + ESP_LOGD(TAG, "Failed to read data from I2S, ret = %d", ret); + SYSVIEW_MIC_READ_EVENT_END(); + continue; + } + // swap the buffer + int16_t *tmp_buf = s_mic_read_buf; + UAC_ENTER_CRITICAL(); + s_mic_read_buf = s_mic_write_buf; + s_mic_read_buf_len = bytes_read; + s_mic_write_buf = tmp_buf; + UAC_EXIT_CRITICAL(); + SYSVIEW_MIC_READ_EVENT_END(); + } +} + esp_err_t usb_headset_init(void) { - usb_phy_init(); - // init device stack on configured roothub port - tud_init(BOARD_TUD_RHPORT); - TU_LOG1("Headset running\r\n"); - BaseType_t ret_val = xTaskCreatePinnedToCore(usb_task, "usb_task", 8 * 1024, NULL, 9, NULL, 1); - return (pdPASS == ret_val) ? ESP_OK : ESP_FAIL; + s_spk_bytes_ms = s_sample_rate / 1000 * s_spk_resolution * DEFAULT_PLAYER_CHANNEL / 8; + s_mic_bytes_ms = s_sample_rate / 1000 * s_mic_resolution * DEFAULT_RECORDER_CHANNEL / 8; + // we give the higher priority to playback task, to avoid the data pending in the ring buffer + BaseType_t ret_val = xTaskCreate(usb_headset_spk, "usb_headset_spk", 4 * 1024, NULL, 8, &spk_task_handle); + if (ret_val != pdPASS) { + ESP_LOGE(TAG, "Failed to create usb_headset_spk task"); + return ESP_FAIL; + } + // we give the lower priority to record task, to avoid the data pending in the ring buffer + ret_val = xTaskCreate(usb_headset_mic, "usb_headset_mic", 4 * 1024, NULL, 8, &mic_task_handle); + if (ret_val != pdPASS) { + ESP_LOGE(TAG, "Failed to create usb_headset_mic task"); + return ESP_FAIL; + } + ret_val = xTaskCreatePinnedToCore(usb_task, "usb_task", 4 * 1024, NULL, 10, NULL, 1); + if (ret_val != pdPASS) { + ESP_LOGE(TAG, "Failed to create usb_task"); + return ESP_FAIL; + } + ESP_LOGI(TAG, "TinyUSB initialized"); + return ESP_OK; } // Invoked when device is mounted void tud_mount_cb(void) { - blink_interval_ms = BLINK_MOUNTED; + s_spk_active = false; + s_mic_active = false; + ESP_LOGI(TAG, "USB mounted"); } // Invoked when device is unmounted void tud_umount_cb(void) { - blink_interval_ms = BLINK_NOT_MOUNTED; + ESP_LOGI(TAG, "USB unmounted"); } // Invoked when usb bus is suspended @@ -131,14 +211,16 @@ void tud_umount_cb(void) // Within 7ms, device must draw an average of current less than 2.5 mA from bus void tud_suspend_cb(bool remote_wakeup_en) { + s_spk_active = false; + s_mic_active = false; (void)remote_wakeup_en; - blink_interval_ms = BLINK_SUSPENDED; + ESP_LOGI(TAG, "USB suspended"); } // Invoked when usb bus is resumed void tud_resume_cb(void) { - blink_interval_ms = BLINK_MOUNTED; + ESP_LOGI(TAG, "USB resumed"); } // Helper for clock get requests @@ -148,9 +230,9 @@ static bool tud_audio_clock_get_request(uint8_t rhport, audio_control_request_t if (request->bControlSelector == AUDIO_CS_CTRL_SAM_FREQ) { if (request->bRequest == AUDIO_CS_REQ_CUR) { - TU_LOG1("Clock get current freq %lu\r\n", current_sample_rate); + TU_LOG1("Clock get current freq %lu\r\n", s_sample_rate); - audio_control_cur_4_t curf = { (int32_t) tu_htole32(current_sample_rate) }; + audio_control_cur_4_t curf = { (int32_t) tu_htole32(s_sample_rate) }; return tud_audio_buffer_and_schedule_control_xfer(rhport, (tusb_control_request_t const *)request, &curf, sizeof(curf)); } else if (request->bRequest == AUDIO_CS_REQ_RANGE) { audio_control_range_4_n_t(N_SAMPLE_RATES) rangef = { @@ -188,10 +270,30 @@ static bool tud_audio_clock_set_request(uint8_t rhport, audio_control_request_t if (request->bControlSelector == AUDIO_CS_CTRL_SAM_FREQ) { TU_VERIFY(request->wLength == sizeof(audio_control_cur_4_t)); - current_sample_rate = (uint32_t) ((audio_control_cur_4_t const *)buf)->bCur; - - TU_LOG1("Clock set current freq: %ld\r\n", current_sample_rate); + uint32_t target_sample_rate = (uint32_t) ((audio_control_cur_4_t const *)buf)->bCur; + TU_LOG1("Clock set current freq: %ld\r\n", target_sample_rate); + if (target_sample_rate != s_sample_rate) { + bool target_exists = false; + for (int i = 0; i < N_SAMPLE_RATES; i++) { + if (target_sample_rate == sample_rates[i]) { + target_exists = true; + break; + } + } + if (target_exists == false) { + TU_LOG1("Unsupported sample rate %ld", target_sample_rate); + return false; + } + // Currently the bsp_codec_set_fs() can not support different sample rate for mic and speaker + // the dynamic sample rate change is not supported + // TODO: bsp_codec_set_fs(target_sample_rate, s_spk_resolution, DEFAULT_PLAYER_CHANNEL); + s_sample_rate = target_sample_rate; + s_spk_bytes_ms = s_sample_rate / 1000 * s_spk_resolution * DEFAULT_PLAYER_CHANNEL / 8; + s_mic_bytes_ms = s_sample_rate / 1000 * s_mic_resolution * DEFAULT_RECORDER_CHANNEL / 8; + TU_LOG1("Mic/Speaker frequency %" PRIu32 ", resolution %d, ch %d", target_sample_rate, s_spk_resolution, DEFAULT_PLAYER_CHANNEL); + return true; + } return true; } else { TU_LOG1("Clock set request not supported, entity = %u, selector = %u, request = %u\r\n", @@ -206,21 +308,17 @@ static bool tud_audio_feature_unit_get_request(uint8_t rhport, audio_control_req TU_ASSERT(request->bEntityID == UAC2_ENTITY_SPK_FEATURE_UNIT); if (request->bControlSelector == AUDIO_FU_CTRL_MUTE && request->bRequest == AUDIO_CS_REQ_CUR) { - audio_control_cur_1_t mute1 = { .bCur = mute[request->bChannelNumber] }; - TU_LOG1("Get channel %u mute %d\r\n", request->bChannelNumber, mute1.bCur); + audio_control_cur_1_t mute1 = { .bCur = spk_mute[request->bChannelNumber] }; + TU_LOG1("Get channel %u spk_mute %d\r\n", request->bChannelNumber, mute1.bCur); return tud_audio_buffer_and_schedule_control_xfer(rhport, (tusb_control_request_t const *)request, &mute1, sizeof(mute1)); } else if (UAC2_ENTITY_SPK_FEATURE_UNIT && request->bControlSelector == AUDIO_FU_CTRL_VOLUME) { if (request->bRequest == AUDIO_CS_REQ_RANGE) { - audio_control_range_2_n_t(1) range_vol = { - .wNumSubRanges = tu_htole16(1), - .subrange[0] = { .bMin = tu_htole16(-VOLUME_CTRL_50_DB), tu_htole16(VOLUME_CTRL_0_DB), tu_htole16(256) } - }; - TU_LOG1("Get channel %u volume range (%d, %d, %u) dB\r\n", request->bChannelNumber, + TU_LOG1("Get channel %u spk_volume range (%d, %d, %u) dB\r\n", request->bChannelNumber, range_vol.subrange[0].bMin / 256, range_vol.subrange[0].bMax / 256, range_vol.subrange[0].bRes / 256); return tud_audio_buffer_and_schedule_control_xfer(rhport, (tusb_control_request_t const *)request, &range_vol, sizeof(range_vol)); } else if (request->bRequest == AUDIO_CS_REQ_CUR) { - audio_control_cur_2_t cur_vol = { .bCur = tu_htole16(volume[request->bChannelNumber]) }; - TU_LOG1("Get channel %u volume %d dB\r\n", request->bChannelNumber, cur_vol.bCur / 256); + audio_control_cur_2_t cur_vol = { .bCur = tu_htole16(spk_volume[request->bChannelNumber]) }; + TU_LOG1("Get channel %u spk_volume %d dB\r\n", request->bChannelNumber, cur_vol.bCur / 256); return tud_audio_buffer_and_schedule_control_xfer(rhport, (tusb_control_request_t const *)request, &cur_vol, sizeof(cur_vol)); } } @@ -240,19 +338,17 @@ static bool tud_audio_feature_unit_set_request(uint8_t rhport, audio_control_req if (request->bControlSelector == AUDIO_FU_CTRL_MUTE) { TU_VERIFY(request->wLength == sizeof(audio_control_cur_1_t)); - - mute[request->bChannelNumber] = ((audio_control_cur_1_t const *)buf)->bCur; - - TU_LOG1("Set channel %d Mute: %d\r\n", request->bChannelNumber, mute[request->bChannelNumber]); - + spk_mute[request->bChannelNumber] = ((audio_control_cur_1_t const *)buf)->bCur; + TU_LOG1("Set speaker channel %d Mute: %d\r\n", request->bChannelNumber, spk_mute[request->bChannelNumber]); + bsp_codec_mute_set(spk_mute[request->bChannelNumber]); return true; } else if (request->bControlSelector == AUDIO_FU_CTRL_VOLUME) { TU_VERIFY(request->wLength == sizeof(audio_control_cur_2_t)); - - volume[request->bChannelNumber] = ((audio_control_cur_2_t const *)buf)->bCur; - - TU_LOG1("Set channel %d volume: %d dB\r\n", request->bChannelNumber, volume[request->bChannelNumber] / 256); - + spk_volume[request->bChannelNumber] = ((audio_control_cur_2_t const *)buf)->bCur; + int spk_volume_db = spk_volume[request->bChannelNumber] / 256; // Convert to dB + int volume = (spk_volume_db + 50) * 2; // Map to range 0 to 100 + TU_LOG1("Set speaker channel %d volume: %d dB (%d)\r\n", request->bChannelNumber, spk_volume_db, volume); + bsp_codec_volume_set(volume, NULL); return true; } else { TU_LOG1("Feature unit set request not supported, entity = %u, selector = %u, request = %u\r\n", @@ -307,7 +403,11 @@ bool tud_audio_set_itf_close_EP_cb(uint8_t rhport, tusb_control_request_t const uint8_t const alt = tu_u16_low(tu_le16toh(p_request->wValue)); if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt == 0) { - blink_interval_ms = BLINK_MOUNTED; + TU_LOG2("Speaker interface closed"); + s_spk_active = false; + } else if (ITF_NUM_AUDIO_STREAMING_MIC == itf && alt == 0) { + TU_LOG2("Microphone interface closed"); + s_mic_active = false; } return true; @@ -321,26 +421,27 @@ bool tud_audio_set_itf_cb(uint8_t rhport, tusb_control_request_t const *p_reques TU_LOG2("Set interface %d alt %d\r\n", itf, alt); if (ITF_NUM_AUDIO_STREAMING_SPK == itf && alt != 0) { - blink_interval_ms = BLINK_STREAMING; + // due to the limitation of the codec, we just support one resolution + // TODO: configuration of the resolution of the speaker + uint8_t spk_resolution = spk_resolutions_per_format[alt - 1]; + s_spk_resolution = spk_resolution; + s_spk_bytes_ms = s_sample_rate / 1000 * s_spk_resolution * DEFAULT_PLAYER_CHANNEL / 8; + s_spk_active = true; + s_spk_buf_len = 0; + xTaskNotifyGive(spk_task_handle); + TU_LOG1("Speaker interface %d-%d opened\n", itf, alt); + } else if (ITF_NUM_AUDIO_STREAMING_MIC == itf && alt != 0) { + // due to the limitation of the codec, we just support one resolution + // TODO: configuration of the resolution of the microphone + uint8_t mic_resolution = mic_resolutions_per_format[alt - 1]; + s_mic_resolution = mic_resolution; + s_mic_bytes_ms = s_sample_rate / 1000 * s_mic_resolution * DEFAULT_RECORDER_CHANNEL / 8; + s_mic_active = true; + s_mic_read_buf_len = 0; + xTaskNotifyGive(mic_task_handle); + TU_LOG1("Microphone interface %d-%d opened\n", itf, alt); } - // Clear buffer when streaming format is changed - spk_data_size = 0; - if (alt != 0) { - current_resolution = resolutions_per_format[alt - 1]; - } - - return true; -} - -bool tud_audio_rx_done_pre_read_cb(uint8_t rhport, uint16_t n_bytes_received, uint8_t func_id, uint8_t ep_out, uint8_t cur_alt_setting) -{ - (void)rhport; - (void)func_id; - (void)ep_out; - (void)cur_alt_setting; - - spk_data_size = tud_audio_read(spk_buf, n_bytes_received); return true; } @@ -351,14 +452,17 @@ bool tud_audio_rx_done_post_read_cb(uint8_t rhport, uint16_t n_bytes_received, u (void)ep_out; (void)cur_alt_setting; - - size_t bytes_written = 0; - esp_err_t ret = bsp_i2s_write(&spk_buf, spk_data_size, &bytes_written, 0); - - for (int i = 0; i < AUDIO_LENGTH ; i += 2) { - rb_write(spk_buf + i, 2); + int bytes_remained = tud_audio_available(); + size_t bytes_require = (CFG_TUD_AUDIO_FUNC_1_EP_OUT_SW_BUF_MS - 1) * s_spk_bytes_ms; + if (bytes_remained < bytes_require) { + return true; } - return ret == ESP_OK ? true : false; + // read data chunk by chunk + size_t bytes_frame = s_spk_resolution * DEFAULT_PLAYER_CHANNEL / 8; + bytes_require = bytes_remained - bytes_remained % bytes_frame; + s_spk_buf_len = tud_audio_read(s_spk_buf, bytes_require); + xTaskNotifyGive(spk_task_handle); + return true; } bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t itf, uint8_t ep_in, uint8_t cur_alt_setting) @@ -368,25 +472,18 @@ bool tud_audio_tx_done_pre_load_cb(uint8_t rhport, uint8_t itf, uint8_t ep_in, u (void)ep_in; (void)cur_alt_setting; - tud_audio_write(mic_buf, 96); - // This callback could be used to fill microphone data separately + size_t bytes_require = (CFG_TUD_AUDIO_FUNC_1_EP_IN_SW_BUF_MS - 1) * s_mic_bytes_ms; + tu_fifo_t *sw_in_fifo = tud_audio_get_ep_in_ff(); + uint16_t fifo_remained = tu_fifo_remaining(sw_in_fifo); + if (fifo_remained < bytes_require) { + return true; + } + // load data chunk by chunk + UAC_ENTER_CRITICAL(); + if (s_mic_read_buf_len > 0) { + tud_audio_write(s_mic_read_buf, s_mic_read_buf_len); + s_mic_read_buf_len = 0; + } + UAC_EXIT_CRITICAL(); return true; } - -bool tud_audio_tx_done_post_load_cb(uint8_t rhport, uint16_t n_bytes_copied, uint8_t itf, uint8_t ep_in, uint8_t cur_alt_setting) -{ - (void) rhport; - (void) n_bytes_copied; - (void) itf; - (void) ep_in; - (void) cur_alt_setting; - - /*** Here to fill audio buffer, only use in audio transmission begin ***/ - size_t bytes_read = 0; - - esp_err_t ret = bsp_i2s_read(&mic_buf, AUDIO_LENGTH * 2, &bytes_read, 0); - for (int i = 0; i < AUDIO_LENGTH / 2 ; i++) { - mic_buf[i + 1] = mic_buf[2 * (i + 1)]; - } - return ret == ESP_OK ? true : false; -}