From 2ce0a1cf580199328c86c194f79e8ba8dcda31b4 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Tue, 10 Nov 2026 15:38:30 +0000 Subject: [PATCH 1/3] rp2350: add Sound Open Firmware port for Raspberry Pi RP2350 (Pico 2) - Add CONFIG_PLATFORM_RP2350 platform support - Add board overlay and conf for rpi_pico2/rp2350a/m33 - Add static audio pipelines for EQ, volume, and UAC2 bridging - Enable ARM Cortex-M SIMD DSP optimizations Signed-off-by: Liam Girdwood --- Kconfig.sof | 2 +- app/CMakeLists.txt | 39 +- app/Kconfig | 5 + app/boards/rpi_pico2_rp2350a_m33.conf | 71 ++ app/boards/rpi_pico2_rp2350a_m33.overlay | 104 +++ app/boards/rpi_pico2_rp2350a_m33_0.conf | 71 ++ app/boards/rpi_pico2_rp2350a_m33_0.overlay | 104 +++ app/src/main.c | 55 +- app/src/sample_usbd.h | 39 + app/src/usbd_init.c | 595 ++++++++++++++ scripts/cmake/misc.cmake | 2 +- src/audio/CMakeLists.txt | 1 + src/audio/Kconfig | 6 + src/audio/drc/drc_arm.c | 8 +- src/audio/pipeline/CMakeLists.txt | 4 + src/audio/pipeline/sof_static_pipeline.c | 758 ++++++++++++++++++ src/audio/pipeline/static_pipeline_loader.c | 4 + src/audio/usb_audio.c | 589 ++++++++++++++ src/include/ipc4/handler.h | 2 +- src/include/ipc4/notification.h | 2 +- src/include/sof/audio/kpb.h | 2 +- .../sof/audio/module_adapter/module/generic.h | 10 +- .../sof/audio/pipeline/sof_static_pipeline.h | 77 ++ src/include/sof/audio/usb_audio.h | 51 ++ src/include/sof/ipc/ipc_reply.h | 2 +- src/include/sof/ipc/msg.h | 4 +- src/include/sof/lib/dai-zephyr.h | 8 +- src/include/sof/lib_manager.h | 2 +- src/include/sof/schedule/dp_schedule.h | 7 +- src/include/sof/schedule/ll_schedule_domain.h | 9 +- src/include/user/debug_stream_slot.h | 5 + src/platform/CMakeLists.txt | 4 + src/platform/Kconfig | 52 ++ src/platform/rp2350/CMakeLists.txt | 8 + .../include/platform/drivers/interrupt.h | 9 + .../rp2350/include/platform/lib/clk.h | 18 + .../rp2350/include/platform/lib/dai.h | 11 + .../rp2350/include/platform/lib/dma.h | 11 + .../rp2350/include/platform/lib/mailbox.h | 44 + .../rp2350/include/platform/lib/memory.h | 35 + .../rp2350/include/platform/lib/pm_runtime.h | 9 + .../rp2350/include/platform/platform.h | 28 + .../rp2350/include/platform/trace/trace.h | 9 + src/platform/rp2350/lib/CMakeLists.txt | 5 + src/platform/rp2350/lib/clk.c | 35 + src/platform/rp2350/platform.c | 170 ++++ src/platform/rp2350/rp2350_pipeline_def.c | 290 +++++++ uuid-registry.txt | 1 + zephyr/CMakeLists.txt | 24 +- zephyr/include/rtos/cache.h | 2 + zephyr/include/sof/lib/cpu.h | 8 +- zephyr/lib/alloc.c | 5 + 52 files changed, 3385 insertions(+), 31 deletions(-) create mode 100644 app/boards/rpi_pico2_rp2350a_m33.conf create mode 100644 app/boards/rpi_pico2_rp2350a_m33.overlay create mode 100644 app/boards/rpi_pico2_rp2350a_m33_0.conf create mode 100644 app/boards/rpi_pico2_rp2350a_m33_0.overlay create mode 100644 app/src/sample_usbd.h create mode 100644 app/src/usbd_init.c create mode 100644 src/audio/pipeline/sof_static_pipeline.c create mode 100644 src/audio/usb_audio.c create mode 100644 src/include/sof/audio/pipeline/sof_static_pipeline.h create mode 100644 src/include/sof/audio/usb_audio.h create mode 100644 src/platform/rp2350/CMakeLists.txt create mode 100644 src/platform/rp2350/include/platform/drivers/interrupt.h create mode 100644 src/platform/rp2350/include/platform/lib/clk.h create mode 100644 src/platform/rp2350/include/platform/lib/dai.h create mode 100644 src/platform/rp2350/include/platform/lib/dma.h create mode 100644 src/platform/rp2350/include/platform/lib/mailbox.h create mode 100644 src/platform/rp2350/include/platform/lib/memory.h create mode 100644 src/platform/rp2350/include/platform/lib/pm_runtime.h create mode 100644 src/platform/rp2350/include/platform/platform.h create mode 100644 src/platform/rp2350/include/platform/trace/trace.h create mode 100644 src/platform/rp2350/lib/CMakeLists.txt create mode 100644 src/platform/rp2350/lib/clk.c create mode 100644 src/platform/rp2350/platform.c create mode 100644 src/platform/rp2350/rp2350_pipeline_def.c diff --git a/Kconfig.sof b/Kconfig.sof index 3c8d8af05db9..f6940f83a94a 100644 --- a/Kconfig.sof +++ b/Kconfig.sof @@ -60,7 +60,7 @@ rsource "Kconfig.xtos" rsource "src/Kconfig" -if ZEPHYR_SOF_MODULE +if ZEPHYR_SOF_MODULE || SOF_FULL_ZEPHYR_APPLICATION rsource "Kconfig.zephyr-log" endif diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index fe9b208fe99f..6d4d7e1d60be 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -22,11 +22,46 @@ endif() find_package(Zephyr HINTS $ENV{ZEPHYR_BASE}) project(sample_sof) +add_subdirectory(../zephyr sof_zephyr) + target_sources(app PRIVATE src/main.c ) -zephyr_library_include_directories(app PUBLIC - ${CMAKE_CURRENT_SOURCE_DIR}/../src/arch/xtensa/include +target_sources_ifdef(CONFIG_USB_DEVICE_STACK_NEXT app PRIVATE + src/usbd_init.c + ) + +target_include_directories(app PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/../src/include + ${CMAKE_CURRENT_SOURCE_DIR}/../zephyr/include + ${CMAKE_CURRENT_SOURCE_DIR}/../src/platform/${PLATFORM}/include + ${PROJECT_BINARY_DIR}/include/generated ) + +target_link_libraries(app PRIVATE SOF -Wl,--start-group modules_sof sof_kernel -Wl,--end-group) +if(TARGET uuid_reg_h) + add_dependencies(app uuid_reg_h modules_sof sof_kernel) +endif() + +if(CONFIG_XTENSA) + target_include_directories(app PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../src/arch/xtensa/include + ) +endif() + +function(sof_append_relative_path_definitions target) + get_target_property(sources ${target} SOURCES) + foreach(src ${sources}) + get_filename_component(ABS_PATH ${src} ABSOLUTE) + file(RELATIVE_PATH rel ${PROJECT_SOURCE_DIR} ${ABS_PATH}) + set_property( + SOURCE ${src} + APPEND + PROPERTY COMPILE_DEFINITIONS + RELATIVE_FILE="${rel}") + endforeach() +endfunction() + +sof_append_relative_path_definitions(app) diff --git a/app/Kconfig b/app/Kconfig index 339a16bed3e8..7e0995637c7a 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -7,6 +7,11 @@ choice COMPILER_CODEGEN_VLIW endchoice source "Kconfig.zephyr" +source "samples/subsys/usb/common/Kconfig.sample_usbd" + +if !ZEPHYR_SOF_MODULE +rsource "../zephyr/Kconfig" +endif if SOC_FAMILY_INTEL_ADSP rsource "boards/intel_adsp/Kconfig.defconfig" diff --git a/app/boards/rpi_pico2_rp2350a_m33.conf b/app/boards/rpi_pico2_rp2350a_m33.conf new file mode 100644 index 000000000000..00039cd75ea7 --- /dev/null +++ b/app/boards/rpi_pico2_rp2350a_m33.conf @@ -0,0 +1,71 @@ +# Target Platform & SOF Core +CONFIG_PLATFORM_RP2350=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=y +CONFIG_ZEPHYR_NATIVE_DRIVERS=y +CONFIG_IPC_MAJOR_4=y +CONFIG_COMP_BASEFW_IPC4=n +CONFIG_CROSS_CORE_STREAM=n +CONFIG_STATIC_PIPELINE=y +CONFIG_SYS_HEAP_BIG_ONLY=y +CONFIG_BUILD_OUTPUT_UF2=y +CONFIG_BUILD_OUTPUT_BIN=y + +# Audio Processing Modules & ARM SIMD Optimizations +CONFIG_COMP_VOLUME=y +CONFIG_VOLUME_ARM_SIMD=y +CONFIG_COMP_IIR=y +CONFIG_EQ_IIR_ARM_SIMD=y +CONFIG_FILTER_ARM_SIMD=y +CONFIG_COMP_FIR=n +CONFIG_COMP_DRC=y +CONFIG_DRC_ARM_SIMD=y +CONFIG_COMP_CROSSOVER=n +CONFIG_COMP_DCBLOCK=n +CONFIG_COMP_MULTIBAND_DRC=n +CONFIG_COMP_TDFB=n +CONFIG_COMP_USB_AUDIO=y +CONFIG_COMP_TONE=n + +# USB Device Stack Next & UAC2 Class +CONFIG_USB_DEVICE_STACK_NEXT=y +CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED=n +CONFIG_USBD_MAX_SPEED_FULL=y +CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n +CONFIG_USBD_AUDIO2_CLASS=y +CONFIG_UDC_ENABLE_SOF=y +CONFIG_UDC_BUF_COUNT=64 +CONFIG_UDC_BUF_POOL_SIZE=16384 + +# USB Device Identification +CONFIG_SAMPLE_USBD_VID=0x2E8A +CONFIG_SAMPLE_USBD_PID=0x000A +CONFIG_SAMPLE_USBD_MANUFACTURER="Raspberry Pi" +CONFIG_SAMPLE_USBD_PRODUCT="SOF RP2350 Audio" + +# System Resources & Memory +CONFIG_MAIN_STACK_SIZE=8192 +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 +CONFIG_USBD_THREAD_STACK_SIZE=4096 +CONFIG_ISR_STACK_SIZE=4096 +CONFIG_HW_STACK_PROTECTION=n +CONFIG_HEAP_MEM_POOL_SIZE=16384 +CONFIG_TICKLESS_KERNEL=n +CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 + +# Logging & Console +CONFIG_LOG=y +CONFIG_LOG_DEFAULT_LEVEL=3 +CONFIG_LOG_MAX_LEVEL=4 +CONFIG_SOF_LOG_LEVEL_INF=y +CONFIG_USBD_UAC2_LOG_LEVEL_INF=y +CONFIG_UDC_DRIVER_LOG_LEVEL_INF=y +CONFIG_LOG_MODE_DEFERRED=y +CONFIG_LOG_BUFFER_SIZE=4096 +CONFIG_LOG_MODE_OVERFLOW=y +CONFIG_LOG_BACKEND_UART=n +CONFIG_PRINTK=n +CONFIG_LOG_PRINTK=n +CONFIG_EARLY_CONSOLE=n + +# SOF Low Latency Scheduler Statistics Logging +CONFIG_SCHEDULE_LL_STATS_LOG=n diff --git a/app/boards/rpi_pico2_rp2350a_m33.overlay b/app/boards/rpi_pico2_rp2350a_m33.overlay new file mode 100644 index 000000000000..d4ec4f530975 --- /dev/null +++ b/app/boards/rpi_pico2_rp2350a_m33.overlay @@ -0,0 +1,104 @@ +#include + + + +/ { + uac2_card: usb_audio2 { + compatible = "zephyr,uac2"; + status = "okay"; + full-speed; + audio-function = ; + + uac_aclk: aclk { + compatible = "zephyr,uac2-clock-source"; + clock-type = "internal-programmable"; + frequency-control = "host-programmable"; + sampling-frequencies = <48000>; + sof-synchronized; + }; + + /* ===== Playback from Host (ISO OUT) ===== */ + i2s_out_terminal: i2s_out_terminal { + compatible = "zephyr,uac2-input-terminal"; + clock-source = <&uac_aclk>; + terminal-type = ; + front-left; + front-right; + }; + + i2s_fu: i2s_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&i2s_out_terminal>; + mute-control = "host-programmable"; + volume-control = "host-programmable"; + }; + + pb_eq_fu: pb_eq_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&i2s_fu>; + mute-control = "host-programmable"; + }; + + pb_drc_fu: pb_drc_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&pb_eq_fu>; + mute-control = "host-programmable"; + }; + + i2s_speaker: i2s_speaker { + compatible = "zephyr,uac2-output-terminal"; + data-source = <&pb_drc_fu>; + clock-source = <&uac_aclk>; + terminal-type = ; + assoc-terminal = <&i2s_mic>; + }; + + /* ===== Capture to Host (ISO IN) ===== */ + i2s_mic: i2s_mic { + compatible = "zephyr,uac2-input-terminal"; + clock-source = <&uac_aclk>; + terminal-type = ; + front-left; + front-right; + }; + + cap_eq_fu: cap_eq_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&i2s_mic>; + mute-control = "host-programmable"; + }; + + i2s_in_fu: i2s_in_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&cap_eq_fu>; + mute-control = "host-programmable"; + volume-control = "host-programmable"; + }; + + i2s_in_terminal: i2s_in_terminal { + compatible = "zephyr,uac2-output-terminal"; + data-source = <&i2s_in_fu>; + clock-source = <&uac_aclk>; + terminal-type = ; + }; + + /* Audio Streaming Interfaces */ + as_i2s_out: as_i2s_out { + compatible = "zephyr,uac2-audio-streaming"; + linked-terminal = <&i2s_out_terminal>; + subslot-size = <2>; + bit-resolution = <16>; + }; + + as_i2s_in: as_i2s_in { + compatible = "zephyr,uac2-audio-streaming"; + linked-terminal = <&i2s_in_terminal>; + subslot-size = <2>; + bit-resolution = <16>; + }; + }; +}; + +zephyr_udc0: &usbd { + status = "okay"; +}; diff --git a/app/boards/rpi_pico2_rp2350a_m33_0.conf b/app/boards/rpi_pico2_rp2350a_m33_0.conf new file mode 100644 index 000000000000..00039cd75ea7 --- /dev/null +++ b/app/boards/rpi_pico2_rp2350a_m33_0.conf @@ -0,0 +1,71 @@ +# Target Platform & SOF Core +CONFIG_PLATFORM_RP2350=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=y +CONFIG_ZEPHYR_NATIVE_DRIVERS=y +CONFIG_IPC_MAJOR_4=y +CONFIG_COMP_BASEFW_IPC4=n +CONFIG_CROSS_CORE_STREAM=n +CONFIG_STATIC_PIPELINE=y +CONFIG_SYS_HEAP_BIG_ONLY=y +CONFIG_BUILD_OUTPUT_UF2=y +CONFIG_BUILD_OUTPUT_BIN=y + +# Audio Processing Modules & ARM SIMD Optimizations +CONFIG_COMP_VOLUME=y +CONFIG_VOLUME_ARM_SIMD=y +CONFIG_COMP_IIR=y +CONFIG_EQ_IIR_ARM_SIMD=y +CONFIG_FILTER_ARM_SIMD=y +CONFIG_COMP_FIR=n +CONFIG_COMP_DRC=y +CONFIG_DRC_ARM_SIMD=y +CONFIG_COMP_CROSSOVER=n +CONFIG_COMP_DCBLOCK=n +CONFIG_COMP_MULTIBAND_DRC=n +CONFIG_COMP_TDFB=n +CONFIG_COMP_USB_AUDIO=y +CONFIG_COMP_TONE=n + +# USB Device Stack Next & UAC2 Class +CONFIG_USB_DEVICE_STACK_NEXT=y +CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED=n +CONFIG_USBD_MAX_SPEED_FULL=y +CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n +CONFIG_USBD_AUDIO2_CLASS=y +CONFIG_UDC_ENABLE_SOF=y +CONFIG_UDC_BUF_COUNT=64 +CONFIG_UDC_BUF_POOL_SIZE=16384 + +# USB Device Identification +CONFIG_SAMPLE_USBD_VID=0x2E8A +CONFIG_SAMPLE_USBD_PID=0x000A +CONFIG_SAMPLE_USBD_MANUFACTURER="Raspberry Pi" +CONFIG_SAMPLE_USBD_PRODUCT="SOF RP2350 Audio" + +# System Resources & Memory +CONFIG_MAIN_STACK_SIZE=8192 +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 +CONFIG_USBD_THREAD_STACK_SIZE=4096 +CONFIG_ISR_STACK_SIZE=4096 +CONFIG_HW_STACK_PROTECTION=n +CONFIG_HEAP_MEM_POOL_SIZE=16384 +CONFIG_TICKLESS_KERNEL=n +CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 + +# Logging & Console +CONFIG_LOG=y +CONFIG_LOG_DEFAULT_LEVEL=3 +CONFIG_LOG_MAX_LEVEL=4 +CONFIG_SOF_LOG_LEVEL_INF=y +CONFIG_USBD_UAC2_LOG_LEVEL_INF=y +CONFIG_UDC_DRIVER_LOG_LEVEL_INF=y +CONFIG_LOG_MODE_DEFERRED=y +CONFIG_LOG_BUFFER_SIZE=4096 +CONFIG_LOG_MODE_OVERFLOW=y +CONFIG_LOG_BACKEND_UART=n +CONFIG_PRINTK=n +CONFIG_LOG_PRINTK=n +CONFIG_EARLY_CONSOLE=n + +# SOF Low Latency Scheduler Statistics Logging +CONFIG_SCHEDULE_LL_STATS_LOG=n diff --git a/app/boards/rpi_pico2_rp2350a_m33_0.overlay b/app/boards/rpi_pico2_rp2350a_m33_0.overlay new file mode 100644 index 000000000000..d4ec4f530975 --- /dev/null +++ b/app/boards/rpi_pico2_rp2350a_m33_0.overlay @@ -0,0 +1,104 @@ +#include + + + +/ { + uac2_card: usb_audio2 { + compatible = "zephyr,uac2"; + status = "okay"; + full-speed; + audio-function = ; + + uac_aclk: aclk { + compatible = "zephyr,uac2-clock-source"; + clock-type = "internal-programmable"; + frequency-control = "host-programmable"; + sampling-frequencies = <48000>; + sof-synchronized; + }; + + /* ===== Playback from Host (ISO OUT) ===== */ + i2s_out_terminal: i2s_out_terminal { + compatible = "zephyr,uac2-input-terminal"; + clock-source = <&uac_aclk>; + terminal-type = ; + front-left; + front-right; + }; + + i2s_fu: i2s_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&i2s_out_terminal>; + mute-control = "host-programmable"; + volume-control = "host-programmable"; + }; + + pb_eq_fu: pb_eq_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&i2s_fu>; + mute-control = "host-programmable"; + }; + + pb_drc_fu: pb_drc_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&pb_eq_fu>; + mute-control = "host-programmable"; + }; + + i2s_speaker: i2s_speaker { + compatible = "zephyr,uac2-output-terminal"; + data-source = <&pb_drc_fu>; + clock-source = <&uac_aclk>; + terminal-type = ; + assoc-terminal = <&i2s_mic>; + }; + + /* ===== Capture to Host (ISO IN) ===== */ + i2s_mic: i2s_mic { + compatible = "zephyr,uac2-input-terminal"; + clock-source = <&uac_aclk>; + terminal-type = ; + front-left; + front-right; + }; + + cap_eq_fu: cap_eq_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&i2s_mic>; + mute-control = "host-programmable"; + }; + + i2s_in_fu: i2s_in_fu { + compatible = "zephyr,uac2-feature-unit"; + data-source = <&cap_eq_fu>; + mute-control = "host-programmable"; + volume-control = "host-programmable"; + }; + + i2s_in_terminal: i2s_in_terminal { + compatible = "zephyr,uac2-output-terminal"; + data-source = <&i2s_in_fu>; + clock-source = <&uac_aclk>; + terminal-type = ; + }; + + /* Audio Streaming Interfaces */ + as_i2s_out: as_i2s_out { + compatible = "zephyr,uac2-audio-streaming"; + linked-terminal = <&i2s_out_terminal>; + subslot-size = <2>; + bit-resolution = <16>; + }; + + as_i2s_in: as_i2s_in { + compatible = "zephyr,uac2-audio-streaming"; + linked-terminal = <&i2s_in_terminal>; + subslot-size = <2>; + bit-resolution = <16>; + }; + }; +}; + +zephyr_udc0: &usbd { + status = "okay"; +}; diff --git a/app/src/main.c b/app/src/main.c index aa9ee6960d8d..83bab50cb1dd 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -8,6 +8,24 @@ #include #include +#if defined(CONFIG_PLATFORM_ESP32P4) || defined(CONFIG_PLATFORM_TEENSY41) || defined(CONFIG_PLATFORM_RP2350) +#include +#include +#include +#include +#include +#include +#include +#if defined(CONFIG_COMP_BT_AUDIO) +#include +#endif +#include +#endif + +#if DT_NODE_EXISTS(DT_ALIAS(led0)) +static const struct gpio_dt_spec s_led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); +#endif + LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); /* define qemu boot tests if any qemu target is defined, add targets to end */ @@ -31,6 +49,12 @@ static int sof_app_main(void) { int ret; +#if DT_NODE_EXISTS(DT_ALIAS(led0)) + if (s_led0.port && device_is_ready(s_led0.port)) { + gpio_pin_configure_dt(&s_led0, GPIO_OUTPUT_ACTIVE); + } +#endif + LOG_INF("SOF on %s", CONFIG_BOARD); /* sof_main is actually SOF initialization */ @@ -41,6 +65,33 @@ static int sof_app_main(void) LOG_INF("SOF initialized"); +#if defined(CONFIG_PLATFORM_ESP32P4) || defined(CONFIG_PLATFORM_TEENSY41) || defined(CONFIG_PLATFORM_RP2350) + /* Initialize static audio pipelines (EQ+DRC Playback & EQ Capture) */ + sof_static_pipelines_init(sof_get()); + + /* Register UAC2 class callbacks before initializing USB stack */ + const struct device *uac2_dev = DEVICE_DT_GET_ONE(zephyr_uac2); + if (device_is_ready(uac2_dev)) { + usbd_uac2_set_ops(uac2_dev, sof_get_uac2_ops(), NULL); + } else { + LOG_ERR("UAC2 device not ready"); + } + + /* Initialize USB device stack and UAC2 class */ + struct usbd_context *sample_usbd = sample_usbd_init_device(NULL); + if (sample_usbd) { + usbd_enable(sample_usbd); + LOG_INF("SOF USB UAC2 device and static pipelines started"); + } else { + LOG_ERR("Failed to initialize USB device context"); + } + +#if defined(CONFIG_COMP_BT_AUDIO) + /* Initialize Bluetooth Audio Service and power on ESP32-C6 coprocessor */ + bt_service_init(); +#endif +#endif + #ifdef CONFIG_ARCH_POSIX_LIBFUZZER /* Workaround for an apparent timing bug in libfuzzer+asan. * If the initial/main thread is allowed to return, ASAN will @@ -100,8 +151,10 @@ void test_main(void) #endif } #else -int main(void) +int main(int argc, char *argv[]) { + ARG_UNUSED(argc); + ARG_UNUSED(argv); return sof_app_main(); } #endif diff --git a/app/src/sample_usbd.h b/app/src/sample_usbd.h new file mode 100644 index 000000000000..b12a62323d70 --- /dev/null +++ b/app/src/sample_usbd.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_SAMPLES_SUBSYS_USB_COMMON_SAMPLE_USBD_H +#define ZEPHYR_SAMPLES_SUBSYS_USB_COMMON_SAMPLE_USBD_H + +#include +#include + +/* + * The scope of this header is limited to use in USB samples together with the + * new experimental USB device stack, you should not use it in your own + * application. However, you can use the code as a template. + */ + +/* + * This function uses Kconfig.sample_usbd options to configure and initialize a + * USB device. It configures sample's device context, default string descriptors, + * USB device configuration, registers any available class instances, and + * finally initializes USB device. It is limited to a single device with a + * single configuration instantiated in sample_usbd_init.c, which should be + * enough for a simple USB device sample. + * + * It returns the configured and initialized USB device context on success, + * otherwise it returns NULL. + */ +struct usbd_context *sample_usbd_init_device(usbd_msg_cb_t msg_cb); + +/* + * This function is similar to sample_usbd_init_device(), but does not + * initialize the device. It allows the application to set additional features, + * such as additional descriptors. + */ +struct usbd_context *sample_usbd_setup_device(usbd_msg_cb_t msg_cb); + +#endif /* ZEPHYR_SAMPLES_SUBSYS_USB_COMMON_SAMPLE_USBD_H */ diff --git a/app/src/usbd_init.c b/app/src/usbd_init.c new file mode 100644 index 000000000000..a74f0fd0e2de --- /dev/null +++ b/app/src/usbd_init.c @@ -0,0 +1,595 @@ +/* + * Copyright (c) 2026 + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(usbd_sample_config, LOG_LEVEL_INF); + +/* By default, do not register the USB DFU class DFU mode instance. */ +static const char *const blocklist[] = { + "dfu_dfu", + NULL, +}; + +/* + * Instantiate a context named sample_usbd using the default USB device + * controller, the Zephyr project vendor ID, and the sample product ID. + */ +USBD_DEVICE_DEFINE(sample_usbd, + DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0)), + CONFIG_SAMPLE_USBD_VID, CONFIG_SAMPLE_USBD_PID); + +USBD_DESC_LANG_DEFINE(sample_lang); +USBD_DESC_MANUFACTURER_DEFINE(sample_mfr, CONFIG_SAMPLE_USBD_MANUFACTURER); + +#if defined(CONFIG_PLATFORM_TEENSY41) +#if defined(CONFIG_TEENSY41_BOARD_B) +USBD_DESC_PRODUCT_DEFINE(teensyb_product, "SOF Teensy 4.1 Audio B"); +#elif defined(CONFIG_TEENSY41_BOARD_A) +USBD_DESC_PRODUCT_DEFINE(teensya_product, "SOF Teensy 4.1 Audio A"); +#else +USBD_DESC_PRODUCT_DEFINE(default_product, "SOF Teensy 4.1 Audio"); +#endif +#elif defined(CONFIG_PLATFORM_RP2350) +#if defined(CONFIG_RP2350_BOARD_B) +USBD_DESC_PRODUCT_DEFINE(rp2350b_product, "SOF RP2350 Audio B"); +#elif defined(CONFIG_RP2350_BOARD_A) +USBD_DESC_PRODUCT_DEFINE(rp2350a_product, "SOF RP2350 Audio A"); +#else +USBD_DESC_PRODUCT_DEFINE(default_product, "SOF RP2350 Audio"); +#endif +#else +USBD_DESC_PRODUCT_DEFINE(spider_product, "SOF ESP32P4 USB Spider"); +USBD_DESC_PRODUCT_DEFINE(aphid_product, "SOF ESP32P4 USB Aphid"); +USBD_DESC_PRODUCT_DEFINE(pallas_product, "SOF ESP32P4 USB Pallas"); +USBD_DESC_PRODUCT_DEFINE(ceres_product, "SOF ESP32P4 USB Ceres"); +USBD_DESC_PRODUCT_DEFINE(default_product, "SOF ESP32P4 USB"); +#endif + +USBD_DESC_STRING_DEFINE(clock_master_str, "Clock Master", USBD_DUT_STRING_INTERFACE); + +IF_ENABLED(CONFIG_HWINFO, (USBD_DESC_SERIAL_NUMBER_DEFINE(sample_sn))); + +USBD_DESC_CONFIG_DEFINE(fs_cfg_desc, "FS Configuration"); +USBD_DESC_CONFIG_DEFINE(hs_cfg_desc, "HS Configuration"); + +static const uint8_t attributes = (IS_ENABLED(CONFIG_SAMPLE_USBD_SELF_POWERED) ? + USB_SCD_SELF_POWERED : 0) | + (IS_ENABLED(CONFIG_SAMPLE_USBD_REMOTE_WAKEUP) ? + USB_SCD_REMOTE_WAKEUP : 0); + +USBD_CONFIGURATION_DEFINE(sample_fs_config, + attributes, + CONFIG_SAMPLE_USBD_MAX_POWER, &fs_cfg_desc); + +USBD_CONFIGURATION_DEFINE(sample_hs_config, + attributes, + CONFIG_SAMPLE_USBD_MAX_POWER, &hs_cfg_desc); + +enum usbd_speed sample_usbd_get_speed(void) +{ + return usbd_bus_speed(&sample_usbd); +} + +#if defined(CONFIG_PLATFORM_TEENSY41) +#include +#include +#include +#include +#include +#include +#include + +extern uint32_t g_rx_pkt_cnt; +extern uint32_t g_tx_pkt_cnt; +extern struct pipeline *sof_static_pipeline_get(uint32_t pipeline_id); +extern struct comp_dev *sof_static_comp_get(uint32_t comp_id); + +struct teensy_diag_data { + uint32_t tcsr; + uint32_t rcsr; + uint32_t tcr2; + uint32_t rcr2; + uint32_t tcr3; + uint32_t rcr3; + uint32_t tfr0; + uint32_t rfr0; + uint32_t rx_pkt_cnt; + uint32_t tx_pkt_cnt; + uint32_t pipe1_status; + uint32_t pipe2_status; + uint32_t dai5_state; + uint32_t dai6_state; + uint32_t dma_tx_csr; + uint32_t dma_rx_csr; + uint32_t dma_tx_saddr; + uint32_t dma_rx_daddr; + uint32_t dma_tx_citer; + uint32_t dma_rx_citer; + uint32_t c1_state; + uint32_t c2_state; + uint32_t c9_state; + uint32_t c10_state; + uint32_t tcr4; + uint32_t rcr4; + uint32_t tcr5; + uint32_t rcr5; + uint32_t pin8_mux; + uint32_t pin8_daisy; + uint32_t pin7_mux; + uint32_t pin8_pad; + uint32_t pin7_pad; + uint32_t gpio1_psr; + uint32_t gpio2_psr; + uint32_t interface_type; /* 1 = SAI1 I2S, 2 = S/PDIF */ + uint32_t g_dbg[8]; +}; + +#define TEENSY_INTERFACE_SAI1_I2S 1 +#define TEENSY_INTERFACE_SPDIF 2 + +extern uint32_t g_diag_debug[8]; + +static struct net_buf *teensy_diag_to_host(const struct usbd_context *const ctx, + const struct usb_setup_packet *const setup) +{ + struct net_buf *buf; + struct teensy_diag_data diag; + uint16_t len; + + memset(&diag, 0, sizeof(diag)); +#if defined(CONFIG_TEENSY41_INTERFACE_SPDIF) + diag.interface_type = TEENSY_INTERFACE_SPDIF; + diag.tcsr = SPDIF->SCR; + diag.rcsr = SPDIF->SRPC; + diag.tcr2 = SPDIF->STC; + diag.rcr2 = SPDIF->SRFM; + diag.tcr3 = SPDIF->SIS; + diag.rcr3 = SPDIF->SIE; + diag.tfr0 = 0; + diag.rfr0 = 0; + diag.dma_tx_csr = DMA0->TCD[2].CSR; + diag.dma_rx_csr = DMA0->TCD[3].CSR; + diag.dma_tx_saddr = DMA0->TCD[2].SADDR; + diag.dma_rx_daddr = DMA0->TCD[3].DADDR; + diag.dma_tx_citer = DMA0->TCD[2].CITER_ELINKNO; + diag.dma_rx_citer = DMA0->TCD[3].CITER_ELINKNO; +#else + diag.interface_type = TEENSY_INTERFACE_SAI1_I2S; + diag.tcsr = SAI1->TCSR; + diag.rcsr = SAI1->RCSR; + diag.tcr2 = SAI1->TCR2; + diag.rcr2 = SAI1->RCR2; + diag.tcr3 = SAI1->TCR3; + diag.rcr3 = SAI1->RCR3; + diag.tfr0 = (SAI1->TCR3 & I2S_TCR3_TCE(4)) ? SAI1->TFR[2] : SAI1->TFR[0]; + diag.rfr0 = SAI1->RFR[0]; + diag.dma_tx_csr = DMA0->TCD[0].CSR; + diag.dma_rx_csr = DMA0->TCD[1].CSR; + diag.dma_tx_saddr = DMA0->TCD[0].SADDR; + diag.dma_rx_daddr = DMA0->TCD[1].DADDR; + diag.dma_tx_citer = DMA0->TCD[0].CITER_ELINKNO; + diag.dma_rx_citer = DMA0->TCD[1].CITER_ELINKNO; +#endif + + diag.rx_pkt_cnt = g_rx_pkt_cnt; + diag.tx_pkt_cnt = g_tx_pkt_cnt; + + struct pipeline *p1 = sof_static_pipeline_get(1); + struct pipeline *p2 = sof_static_pipeline_get(2); + if (p1) diag.pipe1_status = p1->status; + if (p2) diag.pipe2_status = p2->status; + + struct comp_dev *c1 = sof_static_comp_get(1); + struct comp_dev *c2 = sof_static_comp_get(2); + struct comp_dev *c5 = sof_static_comp_get(5); + struct comp_dev *c6 = sof_static_comp_get(6); + struct comp_dev *c9 = sof_static_comp_get(9); + struct comp_dev *c10 = sof_static_comp_get(10); + if (c1) diag.c1_state = c1->state; + if (c2) diag.c2_state = c2->state; + if (c5) diag.dai5_state = c5->state; + if (c6) diag.dai6_state = c6->state; + if (c9) diag.c9_state = c9->state; + if (c10) diag.c10_state = c10->state; + + diag.tcr4 = SAI1->TCR4; + diag.rcr4 = SAI1->RCR4; + diag.tcr5 = SAI1->TCR5; + diag.rcr5 = SAI1->RCR5; + diag.pin8_mux = *(volatile uint32_t *)0x401F817C; + diag.pin8_daisy = *(volatile uint32_t *)0x401F8594; + diag.pin7_mux = *(volatile uint32_t *)0x401F8180; + diag.pin8_pad = *(volatile uint32_t *)0x401F836C; + diag.pin7_pad = *(volatile uint32_t *)0x401F8370; + diag.gpio1_psr = *(volatile uint32_t *)0x401B8008; + diag.gpio2_psr = *(volatile uint32_t *)0x401BC008; + memcpy(diag.g_dbg, g_diag_debug, sizeof(diag.g_dbg)); + + len = MIN(setup->wLength, sizeof(diag)); + buf = usbd_ep_ctrl_data_in_alloc(ctx, len); + if (!buf) return NULL; + net_buf_add_mem(buf, &diag, len); + return buf; +} + +static int teensy_reboot_to_dev(const struct usbd_context *const ctx, + const struct usb_setup_packet *const setup, + const struct net_buf *const buf) +{ + ARG_UNUSED(ctx); + ARG_UNUSED(setup); + ARG_UNUSED(buf); + __asm__ volatile("bkpt #251"); + return 0; +} + +static struct net_buf *teensy_peek_to_host(const struct usbd_context *const ctx, + const struct usb_setup_packet *const setup) +{ + uint32_t addr = (setup->wIndex << 16) | setup->wValue; + uint16_t len = MIN(setup->wLength, 64); + if (!len) return NULL; + struct net_buf *buf = usbd_ep_ctrl_data_in_alloc(ctx, len); + if (!buf) return NULL; + net_buf_add_mem(buf, (const void *)addr, len); + return buf; +} + +static int teensy_poke_to_dev(const struct usbd_context *const ctx, + const struct usb_setup_packet *const setup, + const struct net_buf *const buf) +{ + ARG_UNUSED(ctx); + uint32_t addr = (setup->wIndex << 16) | setup->wValue; + if (buf && buf->len > 0) { + if (setup->wLength == 1) { + *(volatile uint8_t *)addr = buf->data[0]; + } else if (setup->wLength == 2) { + uint16_t val; + memcpy(&val, buf->data, 2); + *(volatile uint16_t *)addr = val; + } else if (setup->wLength == 4) { + uint32_t val; + memcpy(&val, buf->data, 4); + *(volatile uint32_t *)addr = val; + } + } + return 0; +} + +USBD_VREQUEST_DEFINE(teensy_reboot_vreq, 0xFB, NULL, teensy_reboot_to_dev); +USBD_VREQUEST_DEFINE(teensy_diag_vreq, 0xFC, teensy_diag_to_host, NULL); +USBD_VREQUEST_DEFINE(teensy_peek_poke_vreq, 0xFD, teensy_peek_to_host, teensy_poke_to_dev); +#endif + +#if defined(CONFIG_PLATFORM_RP2350) +#include +#include +#include + +extern uint32_t g_rx_pkt_cnt; +extern uint32_t g_tx_pkt_cnt; +extern int g_topo_init_ret; +extern int g_topo_fail_stage; +extern int g_topo_fail_item; +extern int g_topo_fail_code; +extern uint32_t g_cap_copy_cnt; +extern uint32_t g_pb_copy_cnt; +extern uint32_t g_last_trig_term; +extern uint32_t g_last_trig_enable; +extern int g_last_trig_ret; +extern struct pipeline *sof_static_pipeline_get(uint32_t pipeline_id); +extern struct comp_dev *sof_static_comp_get(uint32_t comp_id); +extern const struct sof_static_topology *sof_static_topology_get(void); +extern bool usb_audio_is_capture_active(void); +extern bool usb_audio_is_playback_active(void); + +struct rp2350_diag_data { + int32_t topo_init_ret; + uint32_t active_topo_set; + uint32_t pipe1_status; + uint32_t pipe2_status; + uint32_t rx_pkt_cnt; + uint32_t tx_pkt_cnt; + uint32_t pb_copy_cnt; + uint32_t cap_copy_cnt; + uint32_t cap_uad_active; + uint32_t pb_uad_active; + uint32_t c1_state; + uint32_t c2_state; + uint32_t c3_state; + uint32_t c4_state; + uint32_t c7_state; + uint32_t c8_state; + uint32_t c9_state; + uint32_t last_trig_term; + uint32_t last_trig_enable; + int32_t last_trig_ret; +}; + +static struct net_buf *rp2350_diag_to_host(const struct usbd_context *const ctx, + const struct usb_setup_packet *const setup) +{ + struct rp2350_diag_data diag; + uint16_t len; + struct net_buf *buf; + + memset(&diag, 0, sizeof(diag)); + diag.topo_init_ret = g_topo_init_ret; + diag.active_topo_set = sof_static_topology_get() ? 1 : 0; + + struct pipeline *p1 = sof_static_pipeline_get(1); + struct pipeline *p2 = sof_static_pipeline_get(2); + if (p1) { + diag.pipe1_status = p1->status; + } else { + diag.pipe1_status = g_topo_fail_stage; + } + if (p2) { + diag.pipe2_status = p2->status; + } else { + diag.pipe2_status = g_topo_fail_item; + } + if (!sof_static_topology_get()) { + diag.last_trig_term = g_topo_fail_code; + } + + diag.rx_pkt_cnt = g_rx_pkt_cnt; + diag.tx_pkt_cnt = g_tx_pkt_cnt; + diag.pb_copy_cnt = g_pb_copy_cnt; + diag.cap_copy_cnt = g_cap_copy_cnt; + + diag.cap_uad_active = usb_audio_is_capture_active() ? 1 : 0; + diag.pb_uad_active = usb_audio_is_playback_active() ? 1 : 0; + + struct comp_dev *c1 = sof_static_comp_get(1); + struct comp_dev *c2 = sof_static_comp_get(2); + struct comp_dev *c3 = sof_static_comp_get(3); + struct comp_dev *c4 = sof_static_comp_get(4); + struct comp_dev *c7 = sof_static_comp_get(7); + struct comp_dev *c8 = sof_static_comp_get(8); + struct comp_dev *c9 = sof_static_comp_get(9); + if (c1) diag.c1_state = c1->state; + if (c2) diag.c2_state = c2->state; + if (c3) diag.c3_state = c3->state; + if (c4) diag.c4_state = c4->state; + if (c7) diag.c7_state = c7->state; + if (c8) diag.c8_state = c8->state; + if (c9) diag.c9_state = c9->state; + + diag.last_trig_term = g_last_trig_term; + diag.last_trig_enable = g_last_trig_enable; + diag.last_trig_ret = g_last_trig_ret; + + len = MIN(setup->wLength, sizeof(diag)); + buf = usbd_ep_ctrl_data_in_alloc(ctx, len); + if (!buf) return NULL; + net_buf_add_mem(buf, &diag, len); + return buf; +} + +static struct net_buf *rp2350_peek_to_host(const struct usbd_context *const ctx, + const struct usb_setup_packet *const setup) +{ + uint32_t addr = (setup->wIndex << 16) | setup->wValue; + uint16_t len = MIN(setup->wLength, 64); + if (!len) return NULL; + struct net_buf *buf = usbd_ep_ctrl_data_in_alloc(ctx, len); + if (!buf) return NULL; + net_buf_add_mem(buf, (const void *)addr, len); + return buf; +} + +USBD_VREQUEST_DEFINE(rp2350_diag_vreq, 0xFC, rp2350_diag_to_host, NULL); +USBD_VREQUEST_DEFINE(rp2350_peek_poke_vreq, 0xFD, rp2350_peek_to_host, NULL); +#endif + +static void sample_fix_code_triple(struct usbd_context *uds_ctx, + const enum usbd_speed speed) +{ + if (IS_ENABLED(CONFIG_USBD_CDC_ACM_CLASS) || + IS_ENABLED(CONFIG_USBD_CDC_ECM_CLASS) || + IS_ENABLED(CONFIG_USBD_CDC_NCM_CLASS) || + IS_ENABLED(CONFIG_USBD_MIDI2_CLASS) || + IS_ENABLED(CONFIG_USBD_AUDIO2_CLASS) || + IS_ENABLED(CONFIG_USBD_VIDEO_CLASS)) { + usbd_device_set_code_triple(uds_ctx, speed, + USB_BCC_MISCELLANEOUS, 0x02, 0x01); + } else { + usbd_device_set_code_triple(uds_ctx, speed, 0, 0, 0); + } +} + +struct usbd_context *sample_usbd_setup_device(usbd_msg_cb_t msg_cb) +{ + int err; +#if defined(CONFIG_PLATFORM_TEENSY41) + struct usbd_desc_node *product_desc; +#if defined(CONFIG_TEENSY41_BOARD_B) + product_desc = &teensyb_product; + LOG_INF("Board Identity: TEENSY 4.1 BOARD B (Slave / Rx)"); +#elif defined(CONFIG_TEENSY41_BOARD_A) + product_desc = &teensya_product; + LOG_INF("Board Identity: TEENSY 4.1 BOARD A (Master / Tx)"); +#else + product_desc = &default_product; + LOG_INF("Board Identity: Generic Teensy 4.1 Audio"); +#endif +#elif defined(CONFIG_PLATFORM_RP2350) + struct usbd_desc_node *product_desc; +#if defined(CONFIG_RP2350_BOARD_B) + product_desc = &rp2350b_product; + LOG_INF("Board Identity: RP2350 BOARD B (Slave / Rx)"); +#elif defined(CONFIG_RP2350_BOARD_A) + product_desc = &rp2350a_product; + LOG_INF("Board Identity: RP2350 BOARD A (Master / Tx)"); +#else + product_desc = &default_product; + LOG_INF("Board Identity: Generic RP2350 Audio"); +#endif +#elif defined(CONFIG_PLATFORM_ESP32P4) + uint8_t mac[6] = {0}; + extern int esp_efuse_mac_get_default(uint8_t *mac); + esp_efuse_mac_get_default(mac); + + printk("[USB INIT] Chip MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + struct usbd_desc_node *product_desc = &default_product; + if (mac[5] == 0xC7 || mac[5] == 0xc7) { + LOG_INF("Board Identity: SPIDER PDM DUT (MAC %02X:%02X:%02X:%02X:%02X:%02X)", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + product_desc = &spider_product; + } else if (mac[5] == 0x15) { + LOG_INF("Board Identity: APHID I2S DUT (MAC %02X:%02X:%02X:%02X:%02X:%02X)", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + product_desc = &aphid_product; + } else if (mac[5] == 0x17) { + LOG_INF("Board Identity: PALLAS TX MASTER (MAC %02X:%02X:%02X:%02X:%02X:%02X)", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + product_desc = &pallas_product; + } else if (mac[5] == 0x6C || mac[5] == 0x6c) { + LOG_INF("Board Identity: CERES RX SLAVE (MAC %02X:%02X:%02X:%02X:%02X:%02X)", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + product_desc = &ceres_product; + } else { + LOG_INF("Board Identity: Generic ESP32-P4 (MAC %02X:%02X:%02X:%02X:%02X:%02X)", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + product_desc = &default_product; + } +#else + struct usbd_desc_node *product_desc = &default_product; +#endif + + err = usbd_add_descriptor(&sample_usbd, &sample_lang); + if (err) { + LOG_ERR("Failed to initialize language descriptor (%d)", err); + return NULL; + } + + err = usbd_add_descriptor(&sample_usbd, &sample_mfr); + if (err) { + LOG_ERR("Failed to initialize manufacturer descriptor (%d)", err); + return NULL; + } + + err = usbd_add_descriptor(&sample_usbd, product_desc); + if (err) { + LOG_ERR("Failed to initialize product descriptor (%d)", err); + return NULL; + } + + IF_ENABLED(CONFIG_HWINFO, ( + err = usbd_add_descriptor(&sample_usbd, &sample_sn); + if (err) { + LOG_ERR("Failed to initialize SN descriptor (%d)", err); + return NULL; + } + )) + + err = usbd_add_descriptor(&sample_usbd, &clock_master_str); + if (err) { + LOG_ERR("Failed to initialize Clock Master string descriptor (%d)", err); + return NULL; + } + LOG_INF("Registered Clock Master string descriptor at index %u", clock_master_str.str.idx); + + if (USBD_SUPPORTS_HIGH_SPEED && + usbd_caps_speed(&sample_usbd) == USBD_SPEED_HS) { + err = usbd_add_configuration(&sample_usbd, USBD_SPEED_HS, + &sample_hs_config); + if (err) { + LOG_ERR("Failed to add High-Speed configuration"); + return NULL; + } + + err = usbd_register_all_classes(&sample_usbd, USBD_SPEED_HS, 1, + blocklist); + if (err) { + LOG_ERR("Failed to register classes"); + return NULL; + } + + sample_fix_code_triple(&sample_usbd, USBD_SPEED_HS); + } + + err = usbd_add_configuration(&sample_usbd, USBD_SPEED_FS, + &sample_fs_config); + if (err) { + LOG_ERR("Failed to add Full-Speed configuration"); + return NULL; + } + + err = usbd_register_all_classes(&sample_usbd, USBD_SPEED_FS, 1, blocklist); + if (err) { + LOG_ERR("Failed to register classes"); + return NULL; + } + + sample_fix_code_triple(&sample_usbd, USBD_SPEED_FS); + usbd_self_powered(&sample_usbd, attributes & USB_SCD_SELF_POWERED); + + if (msg_cb != NULL) { + err = usbd_msg_register_cb(&sample_usbd, msg_cb); + if (err) { + LOG_ERR("Failed to register message callback"); + return NULL; + } + } + +#if defined(CONFIG_PLATFORM_TEENSY41) + err = usbd_device_register_vreq(&sample_usbd, &teensy_reboot_vreq); + if (err) { + LOG_ERR("Failed to register vendor reboot request (%d)", err); + } + err = usbd_device_register_vreq(&sample_usbd, &teensy_diag_vreq); + if (err) { + LOG_ERR("Failed to register vendor diag request (%d)", err); + } + err = usbd_device_register_vreq(&sample_usbd, &teensy_peek_poke_vreq); + if (err) { + LOG_ERR("Failed to register vendor peek/poke request (%d)", err); + } +#endif + +#if defined(CONFIG_PLATFORM_RP2350) + err = usbd_device_register_vreq(&sample_usbd, &rp2350_diag_vreq); + if (err) { + LOG_ERR("Failed to register RP2350 diag request (%d)", err); + } + err = usbd_device_register_vreq(&sample_usbd, &rp2350_peek_poke_vreq); + if (err) { + LOG_ERR("Failed to register RP2350 peek request (%d)", err); + } +#endif + + return &sample_usbd; +} + +struct usbd_context *sample_usbd_init_device(usbd_msg_cb_t msg_cb) +{ + int err; + + if (sample_usbd_setup_device(msg_cb) == NULL) { + return NULL; + } + + err = usbd_init(&sample_usbd); + if (err) { + LOG_ERR("Failed to initialize device support"); + return NULL; + } + + return &sample_usbd; +} diff --git a/scripts/cmake/misc.cmake b/scripts/cmake/misc.cmake index 647b69c8c478..f652e40ca410 100644 --- a/scripts/cmake/misc.cmake +++ b/scripts/cmake/misc.cmake @@ -42,7 +42,7 @@ endfunction() # Zephyr duplicate in sof/zephyr/CMakeLists.txt; keep in sync macro(is_zephyr ret) - if(CONFIG_ZEPHYR_SOF_MODULE) + if(CONFIG_ZEPHYR_SOF_MODULE OR CONFIG_SOF_FULL_ZEPHYR_APPLICATION) set(${ret} TRUE) else() set(${ret} FALSE) diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 0ca532950f5c..196d9d1d7f94 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -15,6 +15,7 @@ set(base_files add_subdirectory(buffers) add_subdirectory(pcm_converter) add_subdirectory(pipeline) +add_local_sources_ifdef(CONFIG_COMP_USB_AUDIO sof usb_audio.c) # directories and files included conditionally (alphabetical order) if(CONFIG_COMP_ARIA) diff --git a/src/audio/Kconfig b/src/audio/Kconfig index dbd12f1626b2..3cc707f18c5d 100644 --- a/src/audio/Kconfig +++ b/src/audio/Kconfig @@ -51,6 +51,12 @@ config HOST_DMA_IPC_POSITION_UPDATES by sending an IPC message whenever one period of audio is transferred. Most platforms provide more efficient ways to query the DMA status. +config COMP_USB_AUDIO + bool "USB Audio streaming component" + default y if PLATFORM_RP2350 + help + Enable USB Audio Class 2 (UAC2) streaming component for SOF. + config STATIC_PIPELINE bool "Declarative Static Audio Pipeline Loader" depends on IPC_MAJOR_4 diff --git a/src/audio/drc/drc_arm.c b/src/audio/drc/drc_arm.c index cb4795aefe08..b3737a7ed794 100644 --- a/src/audio/drc/drc_arm.c +++ b/src/audio/drc/drc_arm.c @@ -69,8 +69,8 @@ static int32_t volume_gain(const struct sof_drc_params *p, int32_t x) /* Update detector average across 32 frames */ void drc_update_detector_average(struct drc_state *state, const struct sof_drc_params *p, - int nbyte, - int nch) + size_t nbyte, + unsigned int nch) { int32_t detector_average = state->detector_average; int32_t abs_input_array[DRC_DIVISION_FRAMES]; @@ -235,8 +235,8 @@ void drc_update_envelope(struct drc_state *state, const struct sof_drc_params *p /* Compress output across 4-frame unrolled iterations */ void drc_compress_output(struct drc_state *state, const struct sof_drc_params *p, - int nbyte, - int nch) + size_t nbyte, + unsigned int nch) { const int div_start = state->pre_delay_read_index; const int count = DRC_DIVISION_FRAMES >> 2; diff --git a/src/audio/pipeline/CMakeLists.txt b/src/audio/pipeline/CMakeLists.txt index eca2f06857d0..049362b46a0d 100644 --- a/src/audio/pipeline/CMakeLists.txt +++ b/src/audio/pipeline/CMakeLists.txt @@ -13,3 +13,7 @@ add_local_sources_ifdef(CONFIG_STATIC_PIPELINE sof static_pipeline_modules.c static_pipeline_uac2.c ) + +add_local_sources_ifdef(CONFIG_PLATFORM_RP2350 sof + sof_static_pipeline.c +) diff --git a/src/audio/pipeline/sof_static_pipeline.c b/src/audio/pipeline/sof_static_pipeline.c new file mode 100644 index 000000000000..1abaa0063d75 --- /dev/null +++ b/src/audio/pipeline/sof_static_pipeline.c @@ -0,0 +1,758 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(CONFIG_PLATFORM_ESP32P4) +#include +#include +#include +#include +#endif + +LOG_MODULE_REGISTER(sof_static_pipeline, CONFIG_SOF_LOG_LEVEL); + +#define PLAYBACK_FU_ID UAC2_ENTITY_ID(DT_NODELABEL(i2s_fu)) +#define PLAYBACK_EQ_FU_ID UAC2_ENTITY_ID(DT_NODELABEL(pb_eq_fu)) +#define PLAYBACK_DRC_FU_ID UAC2_ENTITY_ID(DT_NODELABEL(pb_drc_fu)) +#if DT_NODE_EXISTS(DT_NODELABEL(cap_tdfb_fu)) +#define CAPTURE_TDFB_FU_ID UAC2_ENTITY_ID(DT_NODELABEL(cap_tdfb_fu)) +#else +#define CAPTURE_TDFB_FU_ID 0xFF +#endif +#define CAPTURE_EQ_FU_ID UAC2_ENTITY_ID(DT_NODELABEL(cap_eq_fu)) +#define CAPTURE_FU_ID UAC2_ENTITY_ID(DT_NODELABEL(i2s_in_fu)) +#define PLAYBACK_TERM_ID UAC2_ENTITY_ID(DT_NODELABEL(i2s_out_terminal)) +#define CAPTURE_TERM_ID UAC2_ENTITY_ID(DT_NODELABEL(i2s_in_terminal)) + +#if defined(CONFIG_NOCACHE_MEMORY) +K_MEM_SLAB_DEFINE_IN_SECT_STATIC(uac2_rx_slab, __nocache, 256, 32, 64); +K_MEM_SLAB_DEFINE_IN_SECT_STATIC(uac2_tx_slab, __nocache, 256, 64, 64); +#else +K_MEM_SLAB_DEFINE_STATIC(uac2_rx_slab, 256, 32, 64); +K_MEM_SLAB_DEFINE_STATIC(uac2_tx_slab, 256, 64, 64); +#endif + +static struct sof_static_pipeline_status g_status = { + .playback_active = false, + .capture_active = false, + .sample_rate = 48000, + .active_interface = SOF_AUDIO_IF_I2S, + .clock_mode = SOF_CLOCK_MASTER, + .audio_route = SOF_AUDIO_ROUTE_USB_DAI, + .bt_stream_enabled = false, + .playback_volume = 0, + .playback_mute = false, + .capture_volume = 0, + .capture_mute = false, + .eq_playback_bypassed = false, + .drc_playback_bypassed = false, + .tdfb_capture_bypassed = false, + .eq_capture_bypassed = false, +}; + +static uint32_t s_sof_diag_cnt; +uint32_t g_rx_pkt_cnt = 0; +uint32_t g_tx_pkt_cnt = 0; + +static void sof_uac2_send_capture_pending(const struct device *dev) +{ + if (!g_status.capture_active || !dev) { + return; + } + + uint32_t rate = g_status.sample_rate ? g_status.sample_rate : 48000; + uint32_t frame_bytes; + if (g_status.microframes) { + frame_bytes = (rate / 8000) * 4; /* 6 samples * 4 bytes = 24 bytes */ + } else { + frame_bytes = (rate / 1000) * 4; /* 48 samples * 4 bytes = 192 bytes */ + } + if (frame_bytes == 0 || frame_bytes > 512) { + frame_bytes = g_status.microframes ? 24 : 192; + } + + while (1) { + void *buf = NULL; + if (k_mem_slab_alloc(&uac2_tx_slab, &buf, K_NO_WAIT) != 0) { + static uint32_t s_slab_alloc_fails; + s_slab_alloc_fails++; + if (s_slab_alloc_fails <= 5 || s_slab_alloc_fails % 100 == 0) { + LOG_WRN("[UAC2 TX SLAB FULL %u] Failed to allocate TX slab block!", s_slab_alloc_fails); + } + break; + } + + bool have_data = false; +#if CONFIG_COMP_BT_AUDIO + if (g_status.audio_route == SOF_AUDIO_ROUTE_USB_BT) { + have_data = bt_audio_peek_capture_data(buf, frame_bytes); + } else +#endif + { + have_data = usb_audio_peek_capture_data(buf, frame_bytes); + } + + if (!have_data) { + k_mem_slab_free(&uac2_tx_slab, buf); + break; + } + + if (g_status.capture_mute) { + memset(buf, 0, frame_bytes); + } + + int ret = usbd_uac2_send(dev, CAPTURE_TERM_ID, buf, frame_bytes); + if (ret == 0) { + g_tx_pkt_cnt++; + if (!g_status.capture_mute) { +#if CONFIG_COMP_BT_AUDIO + if (g_status.audio_route == SOF_AUDIO_ROUTE_USB_BT) { + bt_audio_consume_capture_data(frame_bytes); + } else +#endif + { + usb_audio_consume_capture_data(frame_bytes); + } + } + } else { + k_mem_slab_free(&uac2_tx_slab, buf); + break; + } + } +} + +/* UAC2 Class Callbacks */ +void sof_uac2_sof_cb(const struct device *dev, void *user_data) +{ + ARG_UNUSED(user_data); + + if (g_status.playback_active || g_status.capture_active) { + s_sof_diag_cnt++; +#if defined(CONFIG_PLATFORM_ESP32P4) + if (s_sof_diag_cnt % 1000 == 0) { + LOG_INF("[DIAG SOF %u] tx_start=%u, rx_start=%u, int_raw=0x%08x, state=0x%08x | I2S0: rx_conf=0x%08x, rx_conf1=0x%08x, rx_tdm=0x%08x, rx_eof=%u", + s_sof_diag_cnt, + (unsigned int)I2S0.tx_conf.tx_start, + (unsigned int)I2S0.rx_conf.rx_start, + (unsigned int)I2S0.int_raw.val, + (unsigned int)I2S0.state.val, + (unsigned int)I2S0.rx_conf.val, + (unsigned int)I2S0.rx_conf1.val, + (unsigned int)I2S0.rx_tdm_ctrl.val, + (unsigned int)I2S0.rx_eof_num.rx_eof_num); + LOG_INF("[DIAG GPIOS] lo=0x%08x, hi=0x%08x (G2 lvl=%u, G3 lvl=%u, G20(DIN)=%u, G21(BCK)=%u, G22(WS)=%u, G23(DOUT)=%u)", + (uint32_t)GPIO.in.val, (uint32_t)GPIO.in1.val, + (uint32_t)((GPIO.in.val >> 2) & 1), + (uint32_t)((GPIO.in.val >> 3) & 1), + (uint32_t)((GPIO.in.val >> 20) & 1), + (uint32_t)((GPIO.in.val >> 21) & 1), + (uint32_t)((GPIO.in.val >> 22) & 1), + (uint32_t)((GPIO.in.val >> 23) & 1)); + } +#endif + } else { + s_sof_diag_cnt = 0; + } + + if (g_status.capture_active && dev) { + sof_uac2_send_capture_pending(dev); + } +} + +void sof_uac2_terminal_update_cb(const struct device *dev, uint8_t terminal, + bool enabled, bool microframes, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(user_data); + + LOG_INF("[UAC2 Terminal] update: terminal %u, enabled %d, microframes %d (PB expected %u, CAP expected %u)", + terminal, enabled, microframes, PLAYBACK_TERM_ID, CAPTURE_TERM_ID); + + g_status.microframes = microframes; + + if (terminal == PLAYBACK_TERM_ID) { + g_status.playback_active = enabled; + sof_static_pipeline_trigger_by_uac2_term(terminal, enabled); + } else if (terminal == CAPTURE_TERM_ID) { + g_status.capture_active = enabled; + sof_static_pipeline_trigger_by_uac2_term(terminal, enabled); + } +} + +void *sof_uac2_get_recv_buf(const struct device *dev, uint8_t terminal, + uint16_t size, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(terminal); + ARG_UNUSED(user_data); + + void *buf = NULL; + if (k_mem_slab_alloc(&uac2_rx_slab, &buf, K_NO_WAIT) != 0) { + return NULL; + } + return buf; +} + +void sof_uac2_data_recv_cb(const struct device *dev, uint8_t terminal, + void *buf, uint16_t size, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(terminal); + ARG_UNUSED(user_data); + + static uint32_t s_rx_pkt_cnt; + s_rx_pkt_cnt++; + g_rx_pkt_cnt++; + if (s_rx_pkt_cnt == 1 || s_rx_pkt_cnt % 1000 == 0) { + LOG_INF("[SOF UAC2] Received playback packet #%u, size %u bytes", s_rx_pkt_cnt, size); + } + + if (buf && size > 0) { +#if CONFIG_COMP_BT_AUDIO + if (g_status.audio_route == SOF_AUDIO_ROUTE_USB_BT) { + bt_audio_feed_playback_data(buf, size); + } else +#endif + { + usb_audio_feed_playback_data(buf, size); + } + } + if (buf) { + k_mem_slab_free(&uac2_rx_slab, buf); + } +} + +void sof_uac2_buf_release(const struct device *dev, uint8_t terminal, + void *buf, void *user_data) +{ + ARG_UNUSED(user_data); + + if (buf) { + k_mem_slab_free(&uac2_tx_slab, buf); + } + + if (terminal == CAPTURE_TERM_ID && dev) { + sof_uac2_send_capture_pending(dev); + } +} + +uint32_t sof_uac2_get_sample_rate(const struct device *dev, uint8_t clock_id, + void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(clock_id); + ARG_UNUSED(user_data); + + return g_status.sample_rate ? g_status.sample_rate : 48000; +} + +int sof_uac2_set_sample_rate(const struct device *dev, uint8_t clock_id, + uint32_t rate, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(clock_id); + ARG_UNUSED(user_data); + + LOG_INF("Host set sample rate: %u Hz", rate); + g_status.sample_rate = rate; + sof_static_set_sample_rate(rate); + usb_audio_set_playback_rate(rate); + usb_audio_set_capture_rate(rate); + return 0; +} + +enum usbd_speed sample_usbd_get_speed(void); + +uint32_t sof_uac2_feedback_cb(const struct device *dev, uint8_t terminal, + void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(terminal); + ARG_UNUSED(user_data); + + uint32_t rate = g_status.sample_rate ? g_status.sample_rate : 48000; + + if (sample_usbd_get_speed() == USBD_SPEED_HS) { + /* High-Speed UAC2 explicit feedback: Q16.16 format in samples per microframe (8000/s) */ + return (uint32_t)(((uint64_t)rate << 16) / 8000); + } else { + /* Full-Speed UAC2 explicit feedback: Q10.14 format in samples per frame (1000/s) */ + return (uint32_t)(((uint64_t)rate << 14) / 1000); + } +} + +int sof_uac2_set_feature_mute(const struct device *dev, uint8_t entity_id, + uint8_t channel, bool mute, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(channel); + ARG_UNUSED(user_data); + + LOG_INF("Host set mute/switch: entity=%u, ch=%u, mute=%d", entity_id, channel, mute); + if (entity_id == PLAYBACK_FU_ID) { + g_status.playback_mute = mute; + } else if (entity_id == PLAYBACK_EQ_FU_ID) { + g_status.eq_playback_bypassed = mute; + } else if (entity_id == PLAYBACK_DRC_FU_ID) { + g_status.drc_playback_bypassed = mute; + } else if (entity_id == CAPTURE_TDFB_FU_ID) { + g_status.tdfb_capture_bypassed = mute; + } else if (entity_id == CAPTURE_EQ_FU_ID) { + g_status.eq_capture_bypassed = mute; + } else if (entity_id == CAPTURE_FU_ID) { + g_status.capture_mute = mute; + } + + sof_static_kcontrol_set_by_uac2(entity_id, channel, mute ? 1 : 0, false); + return 0; +} + +int sof_uac2_get_feature_mute(const struct device *dev, uint8_t entity_id, + uint8_t channel, bool *mute, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(channel); + ARG_UNUSED(user_data); + + if (!mute) { + return -EINVAL; + } + + if (entity_id == PLAYBACK_FU_ID) { + *mute = g_status.playback_mute; + return 0; + } else if (entity_id == CAPTURE_FU_ID) { + *mute = g_status.capture_mute; + return 0; + } + + int32_t val = 0; + int ret = sof_static_kcontrol_get_by_uac2(entity_id, channel, &val, false); + if (ret == 0) { + *mute = (val != 0); + } else { + *mute = false; + } + return 0; +} + +int sof_uac2_set_feature_volume(const struct device *dev, uint8_t entity_id, + uint8_t channel, int16_t volume, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(channel); + ARG_UNUSED(user_data); + + LOG_INF("Host set volume: entity=%u, ch=%u, vol=%d (0x%04x, %d dB)", + entity_id, channel, volume, (uint16_t)volume, volume / 256); + + if (entity_id == PLAYBACK_FU_ID) { + g_status.playback_volume = volume; + } else if (entity_id == CAPTURE_FU_ID) { + g_status.capture_volume = volume; + } + + return sof_static_kcontrol_set_by_uac2(entity_id, channel, volume, true); +} + +int sof_uac2_get_feature_volume(const struct device *dev, uint8_t entity_id, + uint8_t channel, int16_t *volume, void *user_data) +{ + ARG_UNUSED(dev); + ARG_UNUSED(channel); + ARG_UNUSED(user_data); + + if (!volume) { + return -EINVAL; + } + if (entity_id == PLAYBACK_FU_ID) { + *volume = g_status.playback_volume; + } else if (entity_id == CAPTURE_FU_ID) { + *volume = g_status.capture_volume; + } else { + *volume = 0; + } + return 0; +} + +static const struct uac2_ops g_uac2_ops = { + .sof_cb = sof_uac2_sof_cb, + .terminal_update_cb = sof_uac2_terminal_update_cb, + .get_recv_buf = sof_uac2_get_recv_buf, + .data_recv_cb = sof_uac2_data_recv_cb, + .buf_release_cb = sof_uac2_buf_release, + .feedback_cb = sof_uac2_feedback_cb, + .get_sample_rate = sof_uac2_get_sample_rate, + .set_sample_rate = sof_uac2_set_sample_rate, + .set_feature_mute = sof_uac2_set_feature_mute, + .get_feature_mute = sof_uac2_get_feature_mute, + .set_feature_volume = sof_uac2_set_feature_volume, + .get_feature_volume = sof_uac2_get_feature_volume, +}; + +const struct uac2_ops *sof_get_uac2_ops(void) +{ + return &g_uac2_ops; +} + +#if defined(CONFIG_PLATFORM_TEENSY41) +extern const struct sof_static_topology g_teensy41_static_topology; +#elif defined(CONFIG_PLATFORM_RP2350) +extern const struct sof_static_topology g_rp2350_static_topology; +#else +extern const struct sof_static_topology g_esp32p4_static_topology; +#endif + +int sof_static_pipelines_init(struct sof *sof) +{ + ARG_UNUSED(sof); +#if defined(CONFIG_PLATFORM_TEENSY41) + int ret = sof_static_topology_init(&g_teensy41_static_topology); + if (ret < 0) + return ret; + +#if defined(CONFIG_TEENSY41_I2S_SLAVE) + g_status.clock_mode = SOF_CLOCK_SLAVE; +#else + g_status.clock_mode = SOF_CLOCK_MASTER; +#endif + return 0; +#elif defined(CONFIG_PLATFORM_RP2350) + int ret = sof_static_topology_init(&g_rp2350_static_topology); + if (ret < 0) + return ret; + +#if defined(CONFIG_RP2350_BOARD_B) + g_status.clock_mode = SOF_CLOCK_SLAVE; +#else + g_status.clock_mode = SOF_CLOCK_MASTER; +#endif + return 0; +#else + int ret = sof_static_topology_init(&g_esp32p4_static_topology); + if (ret < 0) + return ret; + + uint8_t mac[6] = {0}; + extern int esp_efuse_mac_get_default(uint8_t *mac); + esp_efuse_mac_get_default(mac); + + if (mac[5] == 0x17) { + g_status.clock_mode = SOF_CLOCK_MASTER; + } else { + g_status.clock_mode = SOF_CLOCK_SLAVE; + } + + return sof_static_pipeline_set_clock_mode(SOF_AUDIO_IF_I2S, g_status.clock_mode); +#endif +} + +int sof_static_pipeline_set_clock_mode(enum sof_audio_interface iface, enum sof_clock_mode mode) +{ + g_status.active_interface = iface; + g_status.clock_mode = mode; + LOG_INF("Set interface %d clock mode to %s", iface, + mode == SOF_CLOCK_MASTER ? "MASTER" : (mode == SOF_CLOCK_DMIC ? "DMIC (SLAVE TX)" : "SLAVE")); + + uint32_t sof_format = 0; + if (iface == SOF_AUDIO_IF_I2S) { + sof_format = (mode == SOF_CLOCK_MASTER) ? + (SOF_DAI_FMT_I2S | SOF_DAI_FMT_CBC_CFC) : + (SOF_DAI_FMT_I2S | SOF_DAI_FMT_CBP_CFP); + + const struct device *dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(dai_i2s0)); + if (dev && device_is_ready(dev)) { + uint32_t rate = g_status.sample_rate ? g_status.sample_rate : 48000; + uint32_t channels = 2; + uint32_t word_size = 16; + uint32_t bytes_per_sample = word_size / 8; + uint32_t frames_per_period = rate / 1000; + if (frames_per_period == 0) { + frames_per_period = 48; + } + struct dai_config cfg = { + .type = DAI_ESP32_I2S, + .dai_index = 0, + .channels = channels, + .rate = rate, + .format = (mode == SOF_CLOCK_MASTER) ? + (DAI_PROTO_I2S | DAI_CBC_CFC) : + (DAI_PROTO_I2S | DAI_CBP_CFP), + .word_size = word_size, + .block_size = frames_per_period * channels * bytes_per_sample, + }; + int ret = dai_config_set(dev, &cfg, NULL, 0); + if (ret < 0) { + LOG_ERR("Failed to set DAI I2S config: %d", ret); + return ret; + } + LOG_INF("DAI I2S hardware successfully switched to %s mode", + mode == SOF_CLOCK_MASTER ? "MASTER" : "SLAVE"); + } else { + LOG_WRN("DAI I2S device not ready or not found"); + } + } else if (iface == SOF_AUDIO_IF_PDM) { + sof_format = (mode == SOF_CLOCK_MASTER) ? + (SOF_DAI_FMT_PDM | SOF_DAI_FMT_CBC_CFC) : + (SOF_DAI_FMT_PDM | SOF_DAI_FMT_CBP_CFP); + + const struct device *dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(dai_pdm0)); + if (dev && device_is_ready(dev)) { + uint32_t rate = g_status.sample_rate ? g_status.sample_rate : 48000; + uint32_t channels = 2; + uint32_t word_size = 16; + uint32_t bytes_per_sample = word_size / 8; + uint32_t frames_per_period = rate / 1000; + if (frames_per_period == 0) { + frames_per_period = 48; + } + struct dai_config cfg = { + .type = DAI_ESP32_PDM, + .dai_index = 1, + .channels = channels, + .rate = rate, + .format = (mode == SOF_CLOCK_MASTER) ? + (DAI_PROTO_PDM | DAI_CBC_CFC) : + (DAI_PROTO_PDM | DAI_CBP_CFP), + .options = (mode == SOF_CLOCK_DMIC) ? 1 : 0, + .word_size = word_size, + .block_size = frames_per_period * channels * bytes_per_sample, + }; + int ret = dai_config_set(dev, &cfg, NULL, 0); + if (ret < 0) { + LOG_ERR("Failed to set DAI PDM config: %d", ret); + return ret; + } + LOG_INF("DAI PDM hardware successfully switched to %s mode", + mode == SOF_CLOCK_MASTER ? "MASTER" : (mode == SOF_CLOCK_DMIC ? "DMIC INJECTOR" : "SLAVE")); + } else { + LOG_WRN("DAI PDM device not ready or not found"); + } + } + + /* Update static pipeline DAI components (comp 5 = PB DAI, comp 6 = CAP DAI) */ + uint32_t target_dai_type = (iface == SOF_AUDIO_IF_PDM) ? SOF_DAI_ESP32_PDM : SOF_DAI_ESP32_I2S; + uint32_t target_dai_index = (iface == SOF_AUDIO_IF_PDM) ? 1 : 0; + + struct comp_dev *dev_pb = sof_static_comp_get(5); + if (dev_pb) { + struct dai_data *dd = comp_get_drvdata(dev_pb); + if (dd) { + if (dd->dai) { + dai_put(dd->dai); + } + dd->dai = dai_get(target_dai_type, target_dai_index, DAI_CREAT); + dd->ipc_config.type = target_dai_type; + dd->ipc_config.dai_index = target_dai_index; + dd->ipc_config.format = sof_format; + dev_pb->state = COMP_STATE_READY; + } + } + + struct comp_dev *dev_cap = sof_static_comp_get(6); + if (dev_cap) { + struct dai_data *dd = comp_get_drvdata(dev_cap); + if (dd) { + if (dd->dai) { + dai_put(dd->dai); + } + dd->dai = dai_get(target_dai_type, target_dai_index, DAI_CREAT); + dd->ipc_config.type = target_dai_type; + dd->ipc_config.dai_index = target_dai_index; + dd->ipc_config.format = sof_format; + dev_cap->state = COMP_STATE_READY; + } + } + + /* Reset and re-prepare pipelines 1 and 2 with updated DAI configuration */ + for (uint32_t pid = 1; pid <= 2; pid++) { + struct pipeline *pipe = sof_static_pipeline_get(pid); + if (!pipe) + continue; + + struct comp_dev *host_or_sched = pipe->sched_comp ? pipe->sched_comp : pipe->source_comp; + if (!host_or_sched) + continue; + + pipeline_reset(pipe, host_or_sched); + + struct sof_ipc_pcm_params prms; + memset(&prms, 0, sizeof(prms)); + uint32_t rate = g_status.sample_rate ? g_status.sample_rate : 48000; + uint32_t channels = 2; + prms.params.rate = rate; + prms.params.channels = channels; + prms.params.frame_fmt = host_or_sched->ipc_config.frame_fmt; + uint32_t sbytes = (prms.params.frame_fmt == SOF_IPC_FRAME_FLOAT || + prms.params.frame_fmt == SOF_IPC_FRAME_S32_LE) ? 4 : 2; + prms.params.sample_container_bytes = sbytes; + prms.params.sample_valid_bytes = sbytes; + prms.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED; + uint32_t frames_per_ms = rate / 1000; + if (frames_per_ms == 0) { + frames_per_ms = 48; + } + prms.params.host_period_bytes = frames_per_ms * channels * sbytes; + prms.comp_id = dev_comp_id(host_or_sched); + prms.params.direction = (pid == 1) ? SOF_IPC_STREAM_PLAYBACK : SOF_IPC_STREAM_CAPTURE; + prms.params.chmap[0] = SOF_CHMAP_FL; + prms.params.chmap[1] = SOF_CHMAP_FR; + + pipeline_params(pipe, host_or_sched, &prms); + pipeline_prepare(pipe, host_or_sched); + } + + return 0; +} + +int sof_static_pipeline_set_eq_bypass(bool is_capture, bool bypass) +{ + if (is_capture) { + g_status.eq_capture_bypassed = bypass; + return sof_static_kcontrol_set(5, bypass ? 0 : 1); + } else { + g_status.eq_playback_bypassed = bypass; + return sof_static_kcontrol_set(2, bypass ? 0 : 1); + } +} + +int sof_static_pipeline_set_drc_bypass(bool bypass) +{ + g_status.drc_playback_bypassed = bypass; + return sof_static_kcontrol_set(3, bypass ? 0 : 1); +} + +int sof_static_pipeline_set_tdfb_bypass(bool bypass) +{ + g_status.tdfb_capture_bypassed = bypass; + return sof_static_kcontrol_set(4, bypass ? 0 : 1); +} + +int sof_static_pipeline_set_dmic_injector(bool enable) +{ + if (enable) { + return sof_static_pipeline_set_clock_mode(SOF_AUDIO_IF_PDM, SOF_CLOCK_DMIC); + } else { + if (g_status.clock_mode == SOF_CLOCK_DMIC) { + return sof_static_pipeline_set_clock_mode(SOF_AUDIO_IF_PDM, SOF_CLOCK_MASTER); + } + return 0; + } +} + +int sof_static_pipeline_set_volume(uint32_t pipeline_id, int16_t volume) +{ + if (pipeline_id == 1) { + return sof_uac2_set_feature_volume(NULL, PLAYBACK_FU_ID, 0, volume, NULL); + } else if (pipeline_id == 2) { + return sof_uac2_set_feature_volume(NULL, CAPTURE_FU_ID, 0, volume, NULL); + } + return -EINVAL; +} + +int sof_static_pipeline_set_mute(uint32_t pipeline_id, bool mute) +{ + if (pipeline_id == 1) { + return sof_uac2_set_feature_mute(NULL, PLAYBACK_FU_ID, 0, mute, NULL); + } else if (pipeline_id == 2) { + return sof_uac2_set_feature_mute(NULL, CAPTURE_FU_ID, 0, mute, NULL); + } + return -EINVAL; +} + +int sof_static_pipeline_get_volume(uint32_t pipeline_id, int16_t *volume) +{ + if (pipeline_id == 1) { + return sof_uac2_get_feature_volume(NULL, PLAYBACK_FU_ID, 0, volume, NULL); + } else if (pipeline_id == 2) { + return sof_uac2_get_feature_volume(NULL, CAPTURE_FU_ID, 0, volume, NULL); + } + return -EINVAL; +} + +int sof_static_pipeline_get_mute(uint32_t pipeline_id, bool *mute) +{ + if (pipeline_id == 1) { + return sof_uac2_get_feature_mute(NULL, PLAYBACK_FU_ID, 0, mute, NULL); + } else if (pipeline_id == 2) { + return sof_uac2_get_feature_mute(NULL, CAPTURE_FU_ID, 0, mute, NULL); + } + return -EINVAL; +} + +int sof_static_pipeline_set_playback_active(bool start) +{ + g_status.playback_active = start; + return sof_static_pipeline_trigger_by_uac2_term(PLAYBACK_TERM_ID, start); +} + +int sof_static_pipeline_set_capture_active(bool start) +{ + g_status.capture_active = start; + return sof_static_pipeline_trigger_by_uac2_term(CAPTURE_TERM_ID, start); +} + +int sof_static_pipeline_set_bt_stream(bool enable) +{ + g_status.bt_stream_enabled = enable; +#if CONFIG_COMP_BT_AUDIO + if (enable) { + return bt_service_start_broadcast(); + } else { + return bt_service_stop_broadcast(); + } +#else + ARG_UNUSED(enable); + return -ENOTSUP; +#endif +} + +int sof_static_pipeline_set_route(enum sof_audio_route route) +{ + g_status.audio_route = route; + LOG_INF("SOF pipeline audio route switched to %s", + route == SOF_AUDIO_ROUTE_USB_DAI ? "USB <-> DAI" : + (route == SOF_AUDIO_ROUTE_BT_DAI ? "BT <-> DAI" : "USB <-> BT")); + return 0; +} + +void sof_static_pipeline_get_status(struct sof_static_pipeline_status *status) +{ + if (!status) + return; + + *status = g_status; + + int32_t val = 0; + if (sof_static_kcontrol_get(2, &val) == 0) + status->eq_playback_bypassed = (val == 0); + if (sof_static_kcontrol_get(3, &val) == 0) + status->drc_playback_bypassed = (val == 0); + if (sof_static_kcontrol_get(4, &val) == 0) + status->tdfb_capture_bypassed = (val == 0); + if (sof_static_kcontrol_get(5, &val) == 0) + status->eq_capture_bypassed = (val == 0); + if (sof_static_kcontrol_get(8, &val) == 0) + status->bt_stream_enabled = (val != 0); + if (sof_static_kcontrol_get(10, &val) == 0) + status->audio_route = (enum sof_audio_route)val; +} diff --git a/src/audio/pipeline/static_pipeline_loader.c b/src/audio/pipeline/static_pipeline_loader.c index 719202a00828..defea4295083 100644 --- a/src/audio/pipeline/static_pipeline_loader.c +++ b/src/audio/pipeline/static_pipeline_loader.c @@ -328,6 +328,10 @@ static void init_buffer_params(struct comp_buffer *buf, uint32_t dir, enum sof_i * \param[in] topo Topology descriptor pointer. * \return 0 on success, negative errno on failure. */ +int g_topo_fail_stage = 0; +int g_topo_fail_item = 0; +int g_topo_fail_code = 0; + static int sof_static_topology_init_pipelines(const struct sof_static_topology *topo) { for (size_t i = 0; i < topo->num_pipelines && i < MAX_STATIC_PIPELINES; i++) { diff --git a/src/audio/usb_audio.c b/src/audio/usb_audio.c new file mode 100644 index 000000000000..18d75f76d968 --- /dev/null +++ b/src/audio/usb_audio.c @@ -0,0 +1,589 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(comp_usb_audio, CONFIG_SOF_LOG_LEVEL); + +SOF_DEFINE_REG_UUID(usb_audio); +DECLARE_TR_CTX(usb_audio_tr, SOF_UUID(usb_audio_uuid), LOG_LEVEL_INFO); + +static struct usb_audio_data *g_playback_data; +static struct usb_audio_data *g_capture_data; + +void usb_audio_set_playback_rate(uint32_t rate) +{ + if (g_playback_data) { + g_playback_data->sample_rate = rate; + } +} + +void usb_audio_set_capture_rate(uint32_t rate) +{ + if (g_capture_data) { + g_capture_data->sample_rate = rate; + } +} + +static bool s_tone_enabled; +void usb_audio_set_tone(bool enable) +{ + s_tone_enabled = enable; +} + +bool usb_audio_get_tone(void) +{ + return s_tone_enabled; +} + +void usb_audio_feed_playback_data(const void *src, size_t bytes) +{ + if (!g_playback_data || !g_playback_data->active) { + return; + } + + struct usb_audio_ring_buffer *ring = &g_playback_data->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + uint32_t avail = USB_AUDIO_RING_BUFFER_SIZE - ring->count; + if (bytes > avail) { + bytes = avail; + } + + const uint8_t *s = (const uint8_t *)src; + for (size_t i = 0; i < bytes; i++) { + ring->buf[ring->head] = s[i]; + ring->head = (ring->head + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + ring->count += bytes; + + k_spin_unlock(&ring->lock, key); +} + +size_t usb_audio_fetch_capture_data(void *dst, size_t bytes) +{ + if (!g_capture_data || !g_capture_data->active) { + memset(dst, 0, bytes); + return bytes; + } + + struct usb_audio_ring_buffer *ring = &g_capture_data->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + /* Prebuffer at stream start to provide a stable jitter margin */ + if (!g_capture_data->started) { + if (ring->count < USB_AUDIO_PREBUFFER_BYTES) { + k_spin_unlock(&ring->lock, key); + memset(dst, 0, bytes); + return bytes; + } + g_capture_data->started = true; + } + + uint32_t to_read = (bytes > ring->count) ? ring->count : bytes; + uint8_t *d = (uint8_t *)dst; + + for (size_t i = 0; i < to_read; i++) { + d[i] = ring->buf[ring->tail]; + ring->tail = (ring->tail + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + ring->count -= to_read; + + if (to_read < bytes) { + static uint32_t s_cap_underrun_cnt; + s_cap_underrun_cnt++; + if (s_cap_underrun_cnt <= 5 || s_cap_underrun_cnt % 100 == 0) { + LOG_DBG("[CAP UNDERRUN %u] to_read=%u < bytes=%u, ring_count=%u", + s_cap_underrun_cnt, to_read, bytes, ring->count); + } + memset(d + to_read, 0, bytes - to_read); + } + + k_spin_unlock(&ring->lock, key); + return bytes; +} + +bool usb_audio_peek_capture_data(void *dst, size_t bytes) +{ + if (!g_capture_data || !g_capture_data->active) { + memset(dst, 0, bytes); + return false; + } + + struct usb_audio_ring_buffer *ring = &g_capture_data->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + /* Prebuffer at stream start to provide a stable jitter margin */ + if (!g_capture_data->started) { + if (ring->count < USB_AUDIO_PREBUFFER_BYTES) { + k_spin_unlock(&ring->lock, key); + memset(dst, 0, bytes); + return false; + } + g_capture_data->started = true; + } + + uint32_t to_read = (bytes > ring->count) ? ring->count : bytes; + uint8_t *d = (uint8_t *)dst; + uint32_t tail = ring->tail; + + for (size_t i = 0; i < to_read; i++) { + d[i] = ring->buf[tail]; + tail = (tail + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + + if (to_read < bytes) { + static uint32_t s_cap_underrun_cnt; + s_cap_underrun_cnt++; + if (s_cap_underrun_cnt <= 5 || s_cap_underrun_cnt % 100 == 0) { + LOG_DBG("[CAP UNDERRUN %u] to_read=%u < bytes=%u, ring_count=%u", + s_cap_underrun_cnt, to_read, bytes, ring->count); + } + memset(d + to_read, 0, bytes - to_read); + } + + k_spin_unlock(&ring->lock, key); + return (to_read > 0); +} + +void usb_audio_consume_capture_data(size_t bytes) +{ + if (!g_capture_data || !g_capture_data->active) { + return; + } + + struct usb_audio_ring_buffer *ring = &g_capture_data->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + uint32_t to_consume = (bytes > ring->count) ? ring->count : bytes; + ring->tail = (ring->tail + to_consume) % USB_AUDIO_RING_BUFFER_SIZE; + ring->count -= to_consume; + + k_spin_unlock(&ring->lock, key); +} + +static struct comp_dev *usb_audio_new(const struct comp_driver *drv, + const struct comp_ipc_config *config, + const void *spec) +{ + struct comp_dev *dev; + struct usb_audio_data *uad; + + comp_cl_info(drv, "usb_audio_new()"); + + dev = comp_alloc(drv, sizeof(*dev)); + if (!dev) { + return NULL; + } + + dev->ipc_config = *config; + + uad = rzalloc(SOF_MEM_FLAG_KERNEL, sizeof(*uad)); + if (!uad) { + rfree(dev); + return NULL; + } + + comp_set_drvdata(dev, uad); + + uad->sample_rate = 48000; + uad->channels = 2; + uad->frame_bytes = 4; + uad->period_bytes = (uad->sample_rate / 1000) * uad->frame_bytes; + + if (config->pipeline_id == 1 || config->id == 1) { + g_playback_data = uad; + } else { + g_capture_data = uad; + } + + dev->state = COMP_STATE_READY; + return dev; +} + +static void usb_audio_free(struct comp_dev *dev) +{ + struct usb_audio_data *uad = comp_get_drvdata(dev); + + if (uad == g_playback_data) { + g_playback_data = NULL; + } + if (uad == g_capture_data) { + g_capture_data = NULL; + } + + rfree(uad); + rfree(dev); +} + +static int usb_audio_params(struct comp_dev *dev, + struct sof_ipc_stream_params *params) +{ + struct usb_audio_data *uad = comp_get_drvdata(dev); + + uad->sample_rate = params->rate; + uad->channels = params->channels; + uad->frame_bytes = params->sample_container_bytes * params->channels; + uint32_t frames_per_period = (params->rate ? params->rate : 48000) / 1000; + if (frames_per_period == 0) { + frames_per_period = 48; + } + uad->period_bytes = params->host_period_bytes ? params->host_period_bytes : (frames_per_period * uad->frame_bytes); + + if (params->direction == SOF_IPC_STREAM_PLAYBACK) { + g_playback_data = uad; + } else if (params->direction == SOF_IPC_STREAM_CAPTURE) { + g_capture_data = uad; + } + + return 0; +} + +static int usb_audio_trigger(struct comp_dev *dev, int cmd) +{ + struct usb_audio_data *uad = comp_get_drvdata(dev); + + switch (cmd) { + case COMP_TRIGGER_PRE_START: + case COMP_TRIGGER_PRE_RELEASE: + dev->state = COMP_STATE_PRE_ACTIVE; + break; + case COMP_TRIGGER_START: + case COMP_TRIGGER_RELEASE: { + k_spinlock_key_t key = k_spin_lock(&uad->ring.lock); + uad->ring.head = 0; + uad->ring.tail = 0; + uad->ring.count = 0; + k_spin_unlock(&uad->ring.lock, key); + uad->active = true; + uad->started = false; + dev->state = COMP_STATE_ACTIVE; + break; + } + case COMP_TRIGGER_STOP: + case COMP_TRIGGER_PAUSE: { + uad->active = false; + uad->started = false; + k_spinlock_key_t key = k_spin_lock(&uad->ring.lock); + uad->ring.head = 0; + uad->ring.tail = 0; + uad->ring.count = 0; + k_spin_unlock(&uad->ring.lock, key); + dev->state = COMP_STATE_PREPARE; + break; + } + default: + break; + } + + return 0; +} + +static int usb_audio_copy(struct comp_dev *dev) +{ + struct usb_audio_data *uad = comp_get_drvdata(dev); + struct comp_buffer *sink = comp_dev_get_first_data_consumer(dev); + struct comp_buffer *source = comp_dev_get_first_data_producer(dev); + + if (!uad || !uad->active) { + if (source) { + /* Capture inactive: consume data so source buffer doesn't overflow */ + uint32_t avail_bytes = audio_stream_get_avail_bytes(&source->stream); + if (avail_bytes > 0) { + comp_update_buffer_consume(source, avail_bytes); + } + } + return 0; + } + + if (sink) { + /* USB Playback Source -> SOF Sink Buffer */ + enum sof_ipc_frame fmt = audio_stream_get_frm_fmt(&sink->stream); + if (fmt == SOF_IPC_FRAME_FLOAT) { + uint32_t free_frames = audio_stream_get_free_frames(&sink->stream); + uint32_t copy_frames = MIN(free_frames, dev->frames ? dev->frames : 48); + + if (copy_frames > 0) { + int16_t s16_buf[96]; + float f_buf[96]; + uint32_t frames_to_copy = MIN(copy_frames, 48); + uint32_t s16_bytes = frames_to_copy * 2 * sizeof(int16_t); + + struct usb_audio_ring_buffer *ring = &uad->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + /* Prebuffer at playback start to provide a stable jitter margin */ + if (!uad->started) { + if (ring->count < USB_AUDIO_PREBUFFER_BYTES) { + k_spin_unlock(&ring->lock, key); + memset(f_buf, 0, frames_to_copy * 2 * sizeof(float)); + audio_stream_copy_from_linear(f_buf, 0, &sink->stream, 0, frames_to_copy * 2); + comp_update_buffer_produce(sink, frames_to_copy * 2 * sizeof(float)); + return 0; + } + uad->started = true; + } + + uint32_t available = ring->count; + uint32_t to_copy = MIN(s16_bytes, available); + + uint8_t *s16_raw = (uint8_t *)s16_buf; + for (size_t i = 0; i < to_copy; i++) { + s16_raw[i] = ring->buf[ring->tail]; + ring->tail = (ring->tail + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + ring->count -= to_copy; + + if (to_copy < s16_bytes) { + static uint32_t s_pb_underrun_cnt; + s_pb_underrun_cnt++; + if (s_pb_underrun_cnt <= 5 || s_pb_underrun_cnt % 100 == 0) { + LOG_DBG("[PB UNDERRUN %u] to_copy=%u < s16_bytes=%u, ring_count=%u", + s_pb_underrun_cnt, to_copy, s16_bytes, ring->count); + } + memset(s16_raw + to_copy, 0, s16_bytes - to_copy); + } + + k_spin_unlock(&ring->lock, key); + + const float scale = 1.0f / 32768.0f; + for (size_t i = 0; i < frames_to_copy * 2; i++) { + f_buf[i] = (float)s16_buf[i] * scale; + } + + audio_stream_copy_from_linear(f_buf, 0, &sink->stream, 0, frames_to_copy * 2); + comp_update_buffer_produce(sink, frames_to_copy * 2 * sizeof(float)); + } + } else { + uint32_t free_bytes = audio_stream_get_free_bytes(&sink->stream); + if (free_bytes >= uad->period_bytes) { + uint8_t temp_buf[1024]; + uint32_t chunk = MIN(uad->period_bytes, sizeof(temp_buf)); + struct usb_audio_ring_buffer *ring = &uad->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + if (s_tone_enabled) { + /* 1000 Hz pure sine wave, 48 samples per cycle, stereo, amp 2000 */ + static const int16_t sine48[48] = { + 0, 261, 518, 765, 1000, 1218, 1414, 1586, + 1732, 1848, 1932, 1983, 2000, 1983, 1932, 1848, + 1732, 1586, 1414, 1218, 1000, 765, 518, 261, + 0, -261, -518, -765, -1000, -1218, -1414, -1586, + -1732, -1848, -1932, -1983, -2000, -1983, -1932, -1848, + -1732, -1586, -1414, -1218, -1000, -765, -518, -261 + }; + int16_t *out16 = (int16_t *)temp_buf; + uint32_t frames = chunk / (uad->frame_bytes ? uad->frame_bytes : 4); + for (size_t i = 0; i < frames; i++) { + int16_t s = sine48[i % 48]; + out16[2 * i] = s; + out16[2 * i + 1] = s; + } + k_spin_unlock(&ring->lock, key); + audio_stream_copy_from_linear(temp_buf, 0, &sink->stream, 0, + chunk / audio_stream_sample_bytes(&sink->stream)); + comp_update_buffer_produce(sink, chunk); + return 0; + } + + /* Prebuffer at playback start to provide a stable jitter margin */ + if (!uad->started) { + if (ring->count < USB_AUDIO_PREBUFFER_BYTES) { + k_spin_unlock(&ring->lock, key); + memset(temp_buf, 0, chunk); + audio_stream_copy_from_linear(temp_buf, 0, &sink->stream, 0, + chunk / audio_stream_sample_bytes(&sink->stream)); + comp_update_buffer_produce(sink, chunk); + return 0; + } + uad->started = true; + } + + uint32_t available = ring->count; + if (available < chunk) { + static uint32_t s_pb_underrun_cnt; + s_pb_underrun_cnt++; + if (s_pb_underrun_cnt <= 5 || s_pb_underrun_cnt % 100 == 0) { + LOG_DBG("[PB UNDERRUN %u] available=%u < period_bytes=%u", + s_pb_underrun_cnt, available, chunk); + } + memset(temp_buf, 0, chunk); + k_spin_unlock(&ring->lock, key); + audio_stream_copy_from_linear(temp_buf, 0, &sink->stream, 0, + chunk / audio_stream_sample_bytes(&sink->stream)); + comp_update_buffer_produce(sink, chunk); + } else { + for (size_t i = 0; i < chunk; i++) { + temp_buf[i] = ring->buf[ring->tail]; + ring->tail = (ring->tail + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + ring->count -= chunk; + k_spin_unlock(&ring->lock, key); + + audio_stream_copy_from_linear(temp_buf, 0, &sink->stream, 0, + chunk / audio_stream_sample_bytes(&sink->stream)); + comp_update_buffer_produce(sink, chunk); + } + } + } + } else if (source) { + /* SOF Source Buffer -> USB Capture Sink */ + enum sof_ipc_frame fmt = audio_stream_get_frm_fmt(&source->stream); + if (fmt == SOF_IPC_FRAME_FLOAT) { + uint32_t avail_frames = audio_stream_get_avail_frames(&source->stream); + uint32_t copy_frames = MIN(avail_frames, dev->frames ? dev->frames : 48); + + if (copy_frames > 0) { + float f_buf[96]; + int16_t s16_buf[96]; + uint32_t frames_to_copy = MIN(copy_frames, 48); + + audio_stream_copy_to_linear(&source->stream, 0, f_buf, 0, frames_to_copy * 2); + comp_update_buffer_consume(source, frames_to_copy * 2 * sizeof(float)); + + const float scale = 32768.0f; + for (size_t i = 0; i < frames_to_copy * 2; i++) { + float val = f_buf[i] * scale; + if (val >= 32767.0f) { + s16_buf[i] = 32767; + } else if (val <= -32768.0f) { + s16_buf[i] = -32768; + } else { +#if defined(__riscv) && defined(__riscv_flen) + int32_t r; + __asm__ ("fcvt.w.s %0, %1, rne" : "=r"(r) : "f"(val)); + s16_buf[i] = (int16_t)r; +#else + s16_buf[i] = (int16_t)(val >= 0.0f ? (val + 0.5f) : (val - 0.5f)); +#endif + } + } + + uint32_t s16_bytes = frames_to_copy * 2 * sizeof(int16_t); + struct usb_audio_ring_buffer *ring = &uad->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + if (ring->count + s16_bytes > USB_AUDIO_RING_BUFFER_SIZE) { + uint32_t drop = (ring->count + s16_bytes) - USB_AUDIO_RING_BUFFER_SIZE; + drop = (drop + 3) & ~3; + if (drop > ring->count) { + drop = ring->count; + } + ring->tail = (ring->tail + drop) % USB_AUDIO_RING_BUFFER_SIZE; + ring->count -= drop; + } + + uint32_t free_space = USB_AUDIO_RING_BUFFER_SIZE - ring->count; + uint32_t to_copy = MIN(s16_bytes, free_space); + + uint8_t *s16_raw = (uint8_t *)s16_buf; + for (size_t i = 0; i < to_copy; i++) { + ring->buf[ring->head] = s16_raw[i]; + ring->head = (ring->head + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + ring->count += to_copy; + + k_spin_unlock(&ring->lock, key); + } + } else { + uint32_t avail_bytes = audio_stream_get_avail_bytes(&source->stream); + + while (avail_bytes > 0) { + uint8_t temp_buf[512]; + uint32_t chunk = MIN(avail_bytes, sizeof(temp_buf)); + + struct usb_audio_ring_buffer *ring = &uad->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + if (ring->count + chunk > USB_AUDIO_RING_BUFFER_SIZE) { + static uint32_t s_cap_overflow_cnt; + s_cap_overflow_cnt++; + if (s_cap_overflow_cnt <= 5 || s_cap_overflow_cnt % 100 == 0) { + LOG_DBG("[CAP OVERFLOW %u] ring_count=%u, chunk=%u", + s_cap_overflow_cnt, ring->count, chunk); + } + uint32_t drop = (ring->count + chunk) - USB_AUDIO_RING_BUFFER_SIZE; + drop = (drop + 3) & ~3; + if (drop > ring->count) { + drop = ring->count; + } + ring->tail = (ring->tail + drop) % USB_AUDIO_RING_BUFFER_SIZE; + ring->count -= drop; + } + + uint32_t free_space = USB_AUDIO_RING_BUFFER_SIZE - ring->count; + uint32_t to_copy = MIN(chunk, free_space); + + audio_stream_copy_to_linear(&source->stream, 0, temp_buf, 0, + to_copy / audio_stream_sample_bytes(&source->stream)); + comp_update_buffer_consume(source, to_copy); + avail_bytes -= to_copy; + + for (size_t i = 0; i < to_copy; i++) { + ring->buf[ring->head] = temp_buf[i]; + ring->head = (ring->head + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + ring->count += to_copy; + + k_spin_unlock(&ring->lock, key); + } + } + } + + return 0; +} + +static int usb_audio_prepare(struct comp_dev *dev) +{ + dev->state = COMP_STATE_PREPARE; + return 0; +} + +static int usb_audio_reset(struct comp_dev *dev) +{ + struct usb_audio_data *uad = comp_get_drvdata(dev); + uad->active = false; + uad->started = false; + uad->ring.head = 0; + uad->ring.tail = 0; + uad->ring.count = 0; + dev->state = COMP_STATE_READY; + return 0; +} + +static const struct comp_driver comp_usb_audio = { + .type = SOF_COMP_HOST, + .uid = SOF_RT_UUID(usb_audio_uuid), + #if !CONFIG_ZEPHYR_LOG + .tctx = &usb_audio_tr, +#endif + .ops = { + .create = usb_audio_new, + .free = usb_audio_free, + .params = usb_audio_params, + .prepare = usb_audio_prepare, + .trigger = usb_audio_trigger, + .copy = usb_audio_copy, + .reset = usb_audio_reset, + }, +}; + +static struct comp_driver_info comp_usb_audio_info = { + .drv = &comp_usb_audio, +}; + +UT_STATIC void sys_comp_usb_audio_init(void) +{ + comp_register(&comp_usb_audio_info); +} + +DECLARE_MODULE(sys_comp_usb_audio_init); +SOF_MODULE_INIT(usb_audio, sys_comp_usb_audio_init); + diff --git a/src/include/ipc4/handler.h b/src/include/ipc4/handler.h index 3335c108fa29..ce25acdbfc1c 100644 --- a/src/include/ipc4/handler.h +++ b/src/include/ipc4/handler.h @@ -95,7 +95,7 @@ int ipc4_comp_connect(struct ipc *ipc, const struct ipc4_module_bind_unbind *bu) */ int ipc4_comp_disconnect(struct ipc *ipc, const struct ipc4_module_bind_unbind *bu); -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) /** * \brief Increment the IPC compound message pre-start barrier counter. * \ingroup ipc4_envelope_api diff --git a/src/include/ipc4/notification.h b/src/include/ipc4/notification.h index c2a55dd32284..5ecf29952596 100644 --- a/src/include/ipc4/notification.h +++ b/src/include/ipc4/notification.h @@ -299,7 +299,7 @@ void send_mixer_underrun_notif_msg(uint32_t resource_id, uint32_t eos_flag, uint */ void ipc4_update_notification_mask(uint32_t ntfy_mask, uint32_t enabled_mask); -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) /** * \brief Send generic resource event notification. diff --git a/src/include/sof/audio/kpb.h b/src/include/sof/audio/kpb.h index b17e7eb9bd55..b2956a2e5169 100644 --- a/src/include/sof/audio/kpb.h +++ b/src/include/sof/audio/kpb.h @@ -257,7 +257,7 @@ struct kpb_fmt_dev_list { #if CONFIG_KPB_CLI_Q void kpb_notifier_schedule(struct kpb_client *cli); -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) #include __syscall void kpb_notifier_init(struct comp_dev *dev, struct kpb_client *cli); #include diff --git a/src/include/sof/audio/module_adapter/module/generic.h b/src/include/sof/audio/module_adapter/module/generic.h index 78d29f793e38..915ca0266e73 100644 --- a/src/include/sof/audio/module_adapter/module/generic.h +++ b/src/include/sof/audio/module_adapter/module/generic.h @@ -195,7 +195,7 @@ int module_init(struct processing_module *mod); * @param alignment Required alignment in bytes. * @return Pointer to allocated buffer, or NULL on failure. */ -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall void *mod_balloc_align(struct processing_module *mod, size_t size, size_t alignment); #else void *z_impl_mod_balloc_align(struct processing_module *mod, size_t size, size_t alignment); @@ -228,7 +228,7 @@ void mod_heap_info(struct processing_module *mod, size_t *size, uintptr_t *start * @param alignment Required alignment in bytes. * @return Pointer to allocated memory, or NULL on failure. */ -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall void *mod_alloc_ext(struct processing_module *mod, uint32_t flags, size_t size, size_t alignment); /** @@ -371,7 +371,7 @@ static inline void mod_ipc_msg_free(struct processing_module *mod, } #if CONFIG_COMP_BLOB -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) /** * \brief Module-managed wrapper for comp_data_blob_handler_new_ext(). */ @@ -395,7 +395,7 @@ int z_impl_mod_data_blob_set(struct processing_module *mod, void mod_data_blob_handler_free(struct processing_module *mod, struct comp_data_blob_handler *dbh); #endif #if CONFIG_FAST_GET -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall const void *mod_fast_get(struct processing_module *mod, const void * const dram_ptr, size_t size); #else @@ -598,7 +598,7 @@ static inline uint32_t module_get_lpt(struct processing_module *mod) return mod->dev->period; } -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) #include #endif diff --git a/src/include/sof/audio/pipeline/sof_static_pipeline.h b/src/include/sof/audio/pipeline/sof_static_pipeline.h new file mode 100644 index 000000000000..732d95a891db --- /dev/null +++ b/src/include/sof/audio/pipeline/sof_static_pipeline.h @@ -0,0 +1,77 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __SOF_AUDIO_PIPELINE_SOF_STATIC_PIPELINE_H__ +#define __SOF_AUDIO_PIPELINE_SOF_STATIC_PIPELINE_H__ + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +enum sof_audio_interface { + SOF_AUDIO_IF_I2S = 0, + SOF_AUDIO_IF_PDM = 1, +}; + +enum sof_clock_mode { + SOF_CLOCK_SLAVE = 0, + SOF_CLOCK_MASTER = 1, + SOF_CLOCK_DMIC = 2, +}; + +enum sof_audio_route { + SOF_AUDIO_ROUTE_USB_DAI = 0, + SOF_AUDIO_ROUTE_BT_DAI = 1, + SOF_AUDIO_ROUTE_USB_BT = 2, +}; + +struct sof_static_pipeline_status { + bool playback_active; + bool capture_active; + bool microframes; + uint32_t sample_rate; + enum sof_audio_interface active_interface; + enum sof_clock_mode clock_mode; + enum sof_audio_route audio_route; + bool bt_stream_enabled; + int16_t playback_volume; + bool playback_mute; + int16_t capture_volume; + bool capture_mute; + bool eq_playback_bypassed; + bool drc_playback_bypassed; + bool tdfb_capture_bypassed; + bool eq_capture_bypassed; +}; + +int sof_static_pipelines_init(struct sof *sof); +int sof_static_pipeline_set_clock_mode(enum sof_audio_interface iface, enum sof_clock_mode mode); +int sof_static_pipeline_set_dmic_injector(bool enable); +int sof_static_pipeline_set_bt_stream(bool enable); +int sof_static_pipeline_set_route(enum sof_audio_route route); +int sof_static_pipeline_set_eq_bypass(bool is_capture, bool bypass); +int sof_static_pipeline_set_drc_bypass(bool bypass); +int sof_static_pipeline_set_tdfb_bypass(bool bypass); +int sof_static_pipeline_set_volume(uint32_t pipeline_id, int16_t volume); +int sof_static_pipeline_set_mute(uint32_t pipeline_id, bool mute); +int sof_static_pipeline_get_volume(uint32_t pipeline_id, int16_t *volume); +int sof_static_pipeline_get_mute(uint32_t pipeline_id, bool *mute); +int sof_static_pipeline_set_playback_active(bool start); +int sof_static_pipeline_set_capture_active(bool start); +void sof_static_pipeline_get_status(struct sof_static_pipeline_status *status); + +struct uac2_ops; +const struct uac2_ops *sof_get_uac2_ops(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __SOF_AUDIO_PIPELINE_SOF_STATIC_PIPELINE_H__ */ diff --git a/src/include/sof/audio/usb_audio.h b/src/include/sof/audio/usb_audio.h new file mode 100644 index 000000000000..29da76bef5c7 --- /dev/null +++ b/src/include/sof/audio/usb_audio.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Espressif Systems / SOF Project + */ + +#ifndef __SOF_AUDIO_USB_AUDIO_H__ +#define __SOF_AUDIO_USB_AUDIO_H__ + +#include +#include +#include +#include + +/* UUID for USB Audio Component */ +/* b95039f6-3d23-41a4-9276-2fbb27c62d01 */ +#define SOF_USB_AUDIO_UUID \ + DECLARE_SOF_RT_UUID("usb_audio", 0xb95039f6, 0x3d23, 0x41a4, \ + 0x92, 0x76, 0x2f, 0xbb, 0x27, 0xc6, 0x2d, 0x01) + +#define USB_AUDIO_RING_BUFFER_SIZE 16384 +#define USB_AUDIO_PREBUFFER_BYTES 3840 /* 20ms at 48kHz stereo 16-bit */ + +struct usb_audio_ring_buffer { + uint8_t buf[USB_AUDIO_RING_BUFFER_SIZE]; + volatile uint32_t head; + volatile uint32_t tail; + volatile uint32_t count; + struct k_spinlock lock; +}; + +struct usb_audio_data { + struct usb_audio_ring_buffer ring; + uint32_t sample_rate; + uint32_t channels; + uint32_t frame_bytes; + uint32_t period_bytes; + enum sof_ipc_stream_direction direction; + bool active; + bool started; +}; + +void usb_audio_feed_playback_data(const void *src, size_t bytes); +size_t usb_audio_fetch_capture_data(void *dst, size_t bytes); +bool usb_audio_peek_capture_data(void *dst, size_t bytes); +void usb_audio_consume_capture_data(size_t bytes); +void usb_audio_set_playback_rate(uint32_t rate); +void usb_audio_set_capture_rate(uint32_t rate); +void usb_audio_set_tone(bool enable); +bool usb_audio_get_tone(void); + +#endif /* __SOF_AUDIO_USB_AUDIO_H__ */ diff --git a/src/include/sof/ipc/ipc_reply.h b/src/include/sof/ipc/ipc_reply.h index 915b6261c6bd..cdd9b697eac7 100644 --- a/src/include/sof/ipc/ipc_reply.h +++ b/src/include/sof/ipc/ipc_reply.h @@ -14,7 +14,7 @@ struct sof_ipc_reply; * \brief reply to an IPC message. * @param[in] reply pointer to the reply structure. */ -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall void ipc_msg_reply(struct sof_ipc_reply *reply); #include diff --git a/src/include/sof/ipc/msg.h b/src/include/sof/ipc/msg.h index 943faf6df813..d2e8d99bd20e 100644 --- a/src/include/sof/ipc/msg.h +++ b/src/include/sof/ipc/msg.h @@ -70,7 +70,7 @@ static inline struct ipc_msg *ipc_msg_init(struct k_heap *heap, * * @param msg The IPC message to remove from the queue. */ -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall void ipc_msg_list_remove(struct ipc_msg *msg); #include #else @@ -103,7 +103,7 @@ void ipc_send_queued_msg(void); * @param data The message data. * @param high_priority True if a high priority message. */ -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall void ipc_msg_send(struct ipc_msg *msg, void *data, bool high_priority); #include diff --git a/src/include/sof/lib/dai-zephyr.h b/src/include/sof/lib/dai-zephyr.h index defab09317f9..ff5b3285b118 100644 --- a/src/include/sof/lib/dai-zephyr.h +++ b/src/include/sof/lib/dai-zephyr.h @@ -377,7 +377,13 @@ void dai_release_llp_slot(struct dai_data *dd); /** * \brief Retrieve a pointer to the Zephyr device structure for a DAI of a given type and index. */ +#if defined(CONFIG_USERSPACE) __syscall const struct device *dai_get_device(enum sof_ipc_dai_type type, uint32_t index); +#include +#else +const struct device *z_impl_dai_get_device(enum sof_ipc_dai_type type, uint32_t index); +#define dai_get_device z_impl_dai_get_device +#endif /** * \brief Retrieve the list of all DAI devices. @@ -387,6 +393,4 @@ __syscall const struct device *dai_get_device(enum sof_ipc_dai_type type, uint32 const struct device **dai_get_device_list(size_t *count); /** @}*/ -#include - #endif /* __SOF_LIB_DAI_ZEPHYR_H__ */ diff --git a/src/include/sof/lib_manager.h b/src/include/sof/lib_manager.h index 70ca5f97102c..791b165a66a7 100644 --- a/src/include/sof/lib_manager.h +++ b/src/include/sof/lib_manager.h @@ -230,7 +230,7 @@ int lib_manager_mod_create_priv(const struct comp_driver *drv, const void *spec, void **adapter_priv, struct userspace_context **userspace); -#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(__ZEPHYR__) && defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall int lib_manager_free_module(const uint32_t component_id); #include diff --git a/src/include/sof/schedule/dp_schedule.h b/src/include/sof/schedule/dp_schedule.h index 1dac74b600ef..db44cea11069 100644 --- a/src/include/sof/schedule/dp_schedule.h +++ b/src/include/sof/schedule/dp_schedule.h @@ -76,7 +76,7 @@ int scheduler_dp_task_init(struct task **task, size_t stack_size, uint32_t options); -#if defined(__ZEPHYR__) && CONFIG_SOF_FULL_ZEPHYR_APPLICATION +#if defined(__ZEPHYR__) && CONFIG_SOF_FULL_ZEPHYR_APPLICATION && defined(CONFIG_USERSPACE) /** * @brief Free kernel-side internal resources associated with a DP task. * @param[in,out] task Task being freed. @@ -91,6 +91,11 @@ __syscall void scheduler_dp_internal_free(struct task *task); */ __syscall void scheduler_dp_ll_tick(void); #include +#else +void z_impl_scheduler_dp_internal_free(struct task *task); +void z_impl_scheduler_dp_ll_tick(void); +#define scheduler_dp_internal_free z_impl_scheduler_dp_internal_free +#define scheduler_dp_ll_tick z_impl_scheduler_dp_ll_tick #endif /** diff --git a/src/include/sof/schedule/ll_schedule_domain.h b/src/include/sof/schedule/ll_schedule_domain.h index c369242640af..2db825f639cf 100644 --- a/src/include/sof/schedule/ll_schedule_domain.h +++ b/src/include/sof/schedule/ll_schedule_domain.h @@ -528,6 +528,11 @@ struct ll_schedule_domain *zephyr_domain_init(int clk); #define timer_domain_init(timer, clk) zephyr_domain_init(clk) +#if defined(CONFIG_PLATFORM_TEENSY41) +void zephyr_domain_audio_timer_cb(void); +bool dai_zephyr_has_active_audio_domain(void); +#endif + #ifdef CONFIG_SOF_USERSPACE_LL /** * @brief Retrieve the Zephyr domain thread TID for the domain on the current core. @@ -556,7 +561,7 @@ struct k_mem_domain *zephyr_ll_mem_domain(void); struct k_thread *zephyr_ll_domain_thread(void); #endif /* CONFIG_SOF_USERSPACE_LL */ -#ifdef CONFIG_SOF_FULL_ZEPHYR_APPLICATION +#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) /** * @brief Allocate a completion semaphore for a Low-Latency task. * @param[in,out] task Target task. @@ -576,7 +581,7 @@ int z_impl_zephyr_ll_task_sem_alloc(struct task *task); int z_impl_zephyr_ll_task_sem_free(struct task *task); #define zephyr_ll_task_sem_alloc z_impl_zephyr_ll_task_sem_alloc #define zephyr_ll_task_sem_free z_impl_zephyr_ll_task_sem_free -#endif /* CONFIG_SOF_FULL_ZEPHYR_APPLICATION */ +#endif /* CONFIG_SOF_FULL_ZEPHYR_APPLICATION && CONFIG_USERSPACE */ #endif /* __ZEPHYR__ */ /** diff --git a/src/include/user/debug_stream_slot.h b/src/include/user/debug_stream_slot.h index 5eaae22115b5..de0081a49dfb 100644 --- a/src/include/user/debug_stream_slot.h +++ b/src/include/user/debug_stream_slot.h @@ -136,8 +136,13 @@ struct debug_stream_record; * @param[in] rec Pointer to abstract debug stream record to transmit. * @return 0 on success, -ENODEV if debug slot is unconfigured, or -ENOMEM if record exceeds capacity. */ +#if defined(CONFIG_USERSPACE) __syscall int debug_stream_slot_send_record(struct debug_stream_record *rec); #include +#else +int z_impl_debug_stream_slot_send_record(struct debug_stream_record *rec); +#define debug_stream_slot_send_record z_impl_debug_stream_slot_send_record +#endif #endif /* __SOC_DEBUG_WINDOW_SLOT_H__ */ diff --git a/src/platform/CMakeLists.txt b/src/platform/CMakeLists.txt index 57c10444a23e..9fe3864f0c41 100644 --- a/src/platform/CMakeLists.txt +++ b/src/platform/CMakeLists.txt @@ -3,3 +3,7 @@ if(CONFIG_LIBRARY) add_subdirectory(library) endif() + +if(CONFIG_PLATFORM_RP2350) + add_subdirectory(rp2350) +endif() diff --git a/src/platform/Kconfig b/src/platform/Kconfig index bb3b4920a405..f3c1ca5cddbd 100644 --- a/src/platform/Kconfig +++ b/src/platform/Kconfig @@ -215,8 +215,60 @@ config MTK help Select if your target is a Mediatek DSP. Builds Zephyr firmware. +config PLATFORM_RP2350 + bool "Build for Raspberry Pi RP2350 (ARM Cortex-M33)" + select ZEPHYR_LOG + select ZEPHYR_NATIVE_DRIVERS + help + Select if your target platform is Raspberry Pi RP2350 (Pico 2). + +endchoice + +if PLATFORM_RP2350 + +choice + prompt "RP2350 Board Role" + default RP2350_BOARD_A + help + Select whether this binary is for Board A (Master Tx) or Board B (Slave Rx). + +config RP2350_BOARD_A + bool "RP2350 Board A (Master Clock Provider / Tx)" + help + Configure board as Board A (Master clock provider for I2S, + generates BCLK and LRCLK). Appears on host USB as + 'SOF RP2350 Audio A'. + +config RP2350_BOARD_B + bool "RP2350 Board B (Slave Clock Consumer / Rx)" + help + Configure board as Board B (Slave clock consumer for I2S, + receives BCLK and LRCLK). Appears on host USB as + 'SOF RP2350 Audio B'. + +endchoice + +choice + prompt "RP2350 Audio Hardware Interface" + default RP2350_INTERFACE_LOOPBACK + help + Select whether the static audio pipeline connects to internal loopback or PIO I2S. + +config RP2350_INTERFACE_LOOPBACK + bool "Internal Loopback (UAC2 Playback -> SOF -> UAC2 Capture)" + help + Route host USB audio playback directly through SOF volume/EQ processing + and back to host USB audio capture for zero-wire verification. + +config RP2350_INTERFACE_PIO_I2S + bool "PIO I2S Digital Audio Interface" + help + Route audio through RP2350 PIO state machines (I2S mode). + endchoice +endif + config ESP32P4_PIE_SIMD bool "Enable ESP32-P4 PIE (Xespv2p2) 128-bit vector SIMD acceleration" default y diff --git a/src/platform/rp2350/CMakeLists.txt b/src/platform/rp2350/CMakeLists.txt new file mode 100644 index 000000000000..8972d0f15a08 --- /dev/null +++ b/src/platform/rp2350/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause + +add_local_sources(sof + platform.c + rp2350_pipeline_def.c +) + +add_subdirectory(lib) diff --git a/src/platform/rp2350/include/platform/drivers/interrupt.h b/src/platform/rp2350/include/platform/drivers/interrupt.h new file mode 100644 index 000000000000..cf1ee35b2f08 --- /dev/null +++ b/src/platform/rp2350/include/platform/drivers/interrupt.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_DRIVERS_INTERRUPT_H__ +#define __PLATFORM_DRIVERS_INTERRUPT_H__ + +#endif /* __PLATFORM_DRIVERS_INTERRUPT_H__ */ diff --git a/src/platform/rp2350/include/platform/lib/clk.h b/src/platform/rp2350/include/platform/lib/clk.h new file mode 100644 index 000000000000..3cce41d1b4a1 --- /dev/null +++ b/src/platform/rp2350/include/platform/lib/clk.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_LIB_CLK_H__ +#define __PLATFORM_LIB_CLK_H__ + +#include + +#define CPU_DEFAULT_IDX 0 +#define NUM_CPU_FREQ 1 +#define NUM_CLOCKS 1 + +struct sof; +void platform_clock_init(struct sof *sof); + +#endif /* __PLATFORM_LIB_CLK_H__ */ diff --git a/src/platform/rp2350/include/platform/lib/dai.h b/src/platform/rp2350/include/platform/lib/dai.h new file mode 100644 index 000000000000..0f878cd3794e --- /dev/null +++ b/src/platform/rp2350/include/platform/lib/dai.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_LIB_DAI_H__ +#define __PLATFORM_LIB_DAI_H__ + +#include + +#endif /* __PLATFORM_LIB_DAI_H__ */ diff --git a/src/platform/rp2350/include/platform/lib/dma.h b/src/platform/rp2350/include/platform/lib/dma.h new file mode 100644 index 000000000000..c20d3eb2e8bc --- /dev/null +++ b/src/platform/rp2350/include/platform/lib/dma.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_LIB_DMA_H__ +#define __PLATFORM_LIB_DMA_H__ + +#define PLATFORM_NUM_DMACS 1 + +#endif /* __PLATFORM_LIB_DMA_H__ */ diff --git a/src/platform/rp2350/include/platform/lib/mailbox.h b/src/platform/rp2350/include/platform/lib/mailbox.h new file mode 100644 index 000000000000..c089e55f7c07 --- /dev/null +++ b/src/platform/rp2350/include/platform/lib/mailbox.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_LIB_MAILBOX_H__ +#define __PLATFORM_LIB_MAILBOX_H__ + +#include +#include + +extern uint8_t sof_rp2350_mailbox[0x1000]; +#define MAILBOX_BASE ((uintptr_t)sof_rp2350_mailbox) + +#define MAILBOX_DSPBOX_OFFSET 0x0 +#define MAILBOX_DSPBOX_SIZE 0x400 +#define MAILBOX_DSPBOX_BASE (MAILBOX_BASE + MAILBOX_DSPBOX_OFFSET) + +#define MAILBOX_HOSTBOX_OFFSET 0x400 +#define MAILBOX_HOSTBOX_SIZE 0x400 +#define MAILBOX_HOSTBOX_BASE (MAILBOX_BASE + MAILBOX_HOSTBOX_OFFSET) + +#define MAILBOX_STREAM_OFFSET 0x800 +#define MAILBOX_STREAM_SIZE 0x400 +#define MAILBOX_STREAM_BASE (MAILBOX_BASE + MAILBOX_STREAM_OFFSET) + +#define MAILBOX_DEBUG_OFFSET 0xC00 +#define MAILBOX_DEBUG_SIZE 0x200 +#define MAILBOX_DEBUG_BASE (MAILBOX_BASE + MAILBOX_DEBUG_OFFSET) + +#define MAILBOX_EXCEPTION_OFFSET 0xE00 +#define MAILBOX_EXCEPTION_SIZE 0x100 +#define MAILBOX_EXCEPTION_BASE (MAILBOX_BASE + MAILBOX_EXCEPTION_OFFSET) + +#define MAILBOX_TRACE_OFFSET 0xF00 +#define MAILBOX_TRACE_SIZE 0x100 +#define MAILBOX_TRACE_BASE (MAILBOX_BASE + MAILBOX_TRACE_OFFSET) + +static inline void mailbox_sw_reg_write(size_t offset, uint32_t src) {} +static inline void mailbox_sw_regs_write(size_t offset, const void *src, size_t bytes) {} +static inline uint32_t mailbox_sw_reg_read(size_t offset) { return 0; } +static inline uint64_t mailbox_sw_reg_read64(size_t offset) { return 0; } + +#endif /* __PLATFORM_LIB_MAILBOX_H__ */ diff --git a/src/platform/rp2350/include/platform/lib/memory.h b/src/platform/rp2350/include/platform/lib/memory.h new file mode 100644 index 000000000000..db4ff077595a --- /dev/null +++ b/src/platform/rp2350/include/platform/lib/memory.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_LIB_MEMORY_H__ +#define __PLATFORM_LIB_MEMORY_H__ + +#include +#include + +#define SRAM_BANK_SIZE 0x10000 +#define EPROMS_START 0 +#define HEAP_SYSTEM_SIZE 0x4000 +#define HEAP_RUNTIME_SIZE 0x4000 +#define HEAP_BUFFER_SIZE 0x10000 + +#ifndef PLATFORM_DCACHE_ALIGN +#define PLATFORM_DCACHE_ALIGN 32 +#endif +#ifndef PLATFORM_DCACHE_ALIGN_SHIFT +#define PLATFORM_DCACHE_ALIGN_SHIFT 5 +#endif + +#ifndef uncache_to_cache +#define uncache_to_cache(address) (address) +#endif +#ifndef cache_to_uncache +#define cache_to_uncache(address) (address) +#endif +#define is_uncached(address) 0 +#define host_to_local(addr) (addr) +#define local_to_host(addr) (addr) + +#endif /* __PLATFORM_LIB_MEMORY_H__ */ diff --git a/src/platform/rp2350/include/platform/lib/pm_runtime.h b/src/platform/rp2350/include/platform/lib/pm_runtime.h new file mode 100644 index 000000000000..005fe1955d18 --- /dev/null +++ b/src/platform/rp2350/include/platform/lib/pm_runtime.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_LIB_PM_RUNTIME_H__ +#define __PLATFORM_LIB_PM_RUNTIME_H__ + +#endif /* __PLATFORM_LIB_PM_RUNTIME_H__ */ diff --git a/src/platform/rp2350/include/platform/platform.h b/src/platform/rp2350/include/platform/platform.h new file mode 100644 index 000000000000..ba37d884c51e --- /dev/null +++ b/src/platform/rp2350/include/platform/platform.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifdef __SOF_PLATFORM_H__ + +#ifndef __PLATFORM_PLATFORM_H__ +#define __PLATFORM_PLATFORM_H__ + +#include +#include + +#define PLATFORM_DEFAULT_CLOCK 0 +#define HOST_PAGE_SIZE 4096 +#define DMA_TRACE_LOCAL_SIZE HOST_PAGE_SIZE +#define PLATFORM_PAGE_TABLE_SIZE 256 + +#define PLATFORM_MAX_CHANNELS 8 +#define PLATFORM_MAX_STREAMS 8 + +#endif /* __PLATFORM_PLATFORM_H__ */ + +#else + +#error "This file shouldn't be included from outside of sof/platform.h" + +#endif /* __SOF_PLATFORM_H__ */ diff --git a/src/platform/rp2350/include/platform/trace/trace.h b/src/platform/rp2350/include/platform/trace/trace.h new file mode 100644 index 000000000000..f2b0e2b46301 --- /dev/null +++ b/src/platform/rp2350/include/platform/trace/trace.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#ifndef __PLATFORM_TRACE_TRACE_H__ +#define __PLATFORM_TRACE_TRACE_H__ + +#endif /* __PLATFORM_TRACE_TRACE_H__ */ diff --git a/src/platform/rp2350/lib/CMakeLists.txt b/src/platform/rp2350/lib/CMakeLists.txt new file mode 100644 index 000000000000..147d9d9bea93 --- /dev/null +++ b/src/platform/rp2350/lib/CMakeLists.txt @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: BSD-3-Clause + +add_local_sources(sof + clk.c +) diff --git a/src/platform/rp2350/lib/clk.c b/src/platform/rp2350/lib/clk.c new file mode 100644 index 000000000000..e76571e56516 --- /dev/null +++ b/src/platform/rp2350/lib/clk.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#include +#include + +static const struct freq_table platform_cpu_freq[] = { + { + .freq = CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC, + .ticks_per_msec = CONFIG_SYS_CLOCK_TICKS_PER_SEC / 1000, + }, +}; + +static struct clock_info platform_clocks_info[NUM_CLOCKS]; + +void platform_clock_init(struct sof *sof) +{ + int i; + + sof->clocks = platform_clocks_info; + + for (i = 0; i < CONFIG_CORE_COUNT; i++) { + sof->clocks[i] = (struct clock_info) { + .freqs_num = NUM_CPU_FREQ, + .freqs = platform_cpu_freq, + .default_freq_idx = CPU_DEFAULT_IDX, + .current_freq_idx = CPU_DEFAULT_IDX, + .notification_id = NOTIFIER_ID_CPU_FREQ, + .notification_mask = NOTIFIER_TARGET_CORE_MASK(i), + .set_freq = NULL, + }; + } +} diff --git a/src/platform/rp2350/platform.c b/src/platform/rp2350/platform.c new file mode 100644 index 000000000000..b1c479a8c1f9 --- /dev/null +++ b/src/platform/rp2350/platform.c @@ -0,0 +1,170 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(rp2350_platform, CONFIG_SOF_LOG_LEVEL); + +uint8_t sof_rp2350_mailbox[0x1000] __aligned(32); + +static const struct sof_ipc_fw_ready ready = { + .hdr = { + .cmd = SOF_IPC_FW_READY, + .size = sizeof(struct sof_ipc_fw_ready), + }, + .version = { + .hdr.size = sizeof(struct sof_ipc_fw_version), + .micro = SOF_MICRO, + .minor = SOF_MINOR, + .major = SOF_MAJOR, + .build = -1, + .date = "dtermin.\0", + .time = "fwready.\0", + .tag = SOF_TAG, + .abi_version = SOF_ABI_VERSION, + .src_hash = SOF_SRC_HASH, + }, + .flags = DEBUG_SET_FW_READY_FLAGS, +}; + +static const struct sof_ipc_window windows = { + .ext_hdr = { + .hdr.cmd = SOF_IPC_FW_READY, + .hdr.size = sizeof(struct sof_ipc_window), + .type = SOF_IPC_EXT_WINDOW, + }, + .num_windows = 3, + .window = { + { + .type = SOF_IPC_REGION_DOWNBOX, + .flags = 0, + .size = MAILBOX_HOSTBOX_SIZE, + .offset = MAILBOX_HOSTBOX_OFFSET, + }, + { + .type = SOF_IPC_REGION_UPBOX, + .id = 0, + .flags = 0, + .size = MAILBOX_DSPBOX_SIZE, + .offset = MAILBOX_DSPBOX_OFFSET, + }, + { + .type = SOF_IPC_REGION_STREAM, + .id = 0, + .flags = 0, + .size = MAILBOX_STREAM_SIZE, + .offset = MAILBOX_STREAM_OFFSET, + }, + }, +}; + +int platform_boot_complete(uint32_t boot_message) +{ + struct sof_ipc_reply reply; + + reply.error = 0; + reply.hdr.cmd = SOF_IPC_FW_READY; + reply.hdr.size = sizeof(reply); + + mailbox_hostbox_write(0, &reply, sizeof(reply)); + mailbox_hostbox_write(sizeof(reply), &ready, sizeof(ready)); + mailbox_hostbox_write(sizeof(reply) + sizeof(ready), + &windows, + sizeof(windows)); + + LOG_INF("RP2350 platform boot complete"); + return 0; +} + +int platform_context_save(struct sof *sof) +{ + return 0; +} + +#include +#include +#include +#include + +SOF_DEFINE_REG_UUID(zipc_task); +extern struct task_ops ipc_task_ops; + +int platform_ipc_init(struct ipc *ipc) +{ + ipc_set_drvdata(ipc, NULL); + schedule_task_init_edf(&ipc->ipc_task, SOF_UUID(zipc_task_uuid), + &ipc_task_ops, ipc, 0, 0); + return 0; +} + +enum task_state ipc_platform_do_cmd(struct ipc *ipc) +{ + struct ipc_cmd_hdr *hdr = mailbox_validate(); + + if (hdr) + ipc_cmd(hdr); + return SOF_TASK_STATE_COMPLETED; +} + +int ipc_platform_send_msg(const struct ipc_msg *msg) +{ + mailbox_dspbox_write(0, msg->tx_data, msg->tx_size); + return 0; +} + +int platform_init(struct sof *sof) +{ + LOG_INF("Initializing RP2350 SOF Platform..."); + + platform_clock_init(sof); + + /* Initialize EDF scheduler */ + scheduler_init_edf(); + + /* Initialize Zephyr domain and timer-based LL scheduler */ + sof->platform_timer_domain = zephyr_domain_init(PLATFORM_DEFAULT_CLOCK); + if (!sof->platform_timer_domain) { + LOG_ERR("Failed to initialize platform timer domain"); + return -ENODEV; + } + + zephyr_ll_scheduler_init(sof->platform_timer_domain); + + /* Initialize platform DMAC */ + dmac_init(sof); + + /* Initialize Zephyr native DAI subsystem */ + dai_init(sof); + + /* Initialize IPC */ + ipc_init(sof); + + LOG_INF("RP2350 SOF Platform initialized successfully"); + return 0; +} + +void ipc_platform_complete_cmd(struct ipc *ipc) +{ + ARG_UNUSED(ipc); +} + +void ipc_platform_send_msg_direct(const struct ipc_msg *msg) +{ + ARG_UNUSED(msg); +} diff --git a/src/platform/rp2350/rp2350_pipeline_def.c b/src/platform/rp2350/rp2350_pipeline_def.c new file mode 100644 index 000000000000..4043c10d131d --- /dev/null +++ b/src/platform/rp2350/rp2350_pipeline_def.c @@ -0,0 +1,290 @@ +// SPDX-License-Identifier: BSD-3-Clause +/* + * Copyright (c) 2026 Sound Open Firmware (SOF) Project + */ + +#include +#include +#include +#include +#include +#include + +/* UUID External Declarations */ +extern const struct sof_uuid usb_audio_uuid; +#if CONFIG_IPC_MAJOR_4 +extern const struct sof_uuid volume4_uuid; +#define volume_uuid volume4_uuid +#else +extern const struct sof_uuid volume_uuid; +#endif +extern const struct sof_uuid eq_iir_uuid; +extern const struct sof_uuid drc_uuid; +extern const struct sof_uuid dai_uuid; + +#if CONFIG_COMP_DRC +struct drc_state; +extern void drc_reset_state(struct processing_module *mod, struct drc_state *state); +void * const g_drc_force __used = (void *)drc_reset_state; +#endif + +/* UAC2 Device Tree Entity IDs */ +#define UAC2_STATIC_ENTITY_ID(node) (DT_NODE_CHILD_IDX(node) + 1) + +#define PLAYBACK_FU_ID UAC2_STATIC_ENTITY_ID(DT_NODELABEL(i2s_fu)) +#define PLAYBACK_EQ_FU_ID UAC2_STATIC_ENTITY_ID(DT_NODELABEL(pb_eq_fu)) +#define PLAYBACK_DRC_FU_ID UAC2_STATIC_ENTITY_ID(DT_NODELABEL(pb_drc_fu)) +#define CAPTURE_EQ_FU_ID UAC2_STATIC_ENTITY_ID(DT_NODELABEL(cap_eq_fu)) +#define CAPTURE_FU_ID UAC2_STATIC_ENTITY_ID(DT_NODELABEL(i2s_in_fu)) +#define PLAYBACK_TERM_ID UAC2_STATIC_ENTITY_ID(DT_NODELABEL(i2s_out_terminal)) +#define CAPTURE_TERM_ID UAC2_STATIC_ENTITY_ID(DT_NODELABEL(i2s_in_terminal)) + +/* Default 2-channel 4-band Parametric IIR EQ Coefficients */ +static const uint32_t rp2350_default_iir_coef_2ch[51] = { + 0x00464f53, 0x00000000, 0x000000ac, 0x03013000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x000000ac, 0x00000002, 0x00000001, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000004, 0x00000004, 0x00000000, + 0x00000000, 0x00000000, 0x00000000, 0xc12c82bd, + 0x7ed0b52e, 0x1fc7cc0c, 0xc07067e9, 0x1fc7cc0c, + 0x00000000, 0x00004000, 0xcad0cdef, 0x742e8c5d, + 0x0cdc9086, 0xe2f11723, 0x10b2f932, 0x00000000, + 0x00004000, 0xcf45334a, 0x68260de9, 0x0a54e176, + 0xe5d6cb75, 0x11fc1f3d, 0x00000000, 0x00004000, + 0xf2940609, 0xe25f3930, 0x0d69ba64, 0x1ad374c8, + 0x0d69ba64, 0xfffffffb, 0x000045bf +}; + +/* Default Speaker DRC Compressor Profile */ +static const uint32_t rp2350_default_drc_coef[35] = { + 0x00464f53, 0x00000000, 0x0000006c, 0x0301a000, + 0x00000000, 0x00000000, 0x00000000, 0x00000000, + 0x0000006c, 0x00000000, 0x00000000, 0x00000000, + 0x00000000, 0x00000001, 0xe2000000, 0x14000000, + 0x0a000000, 0x00624dd3, 0x02061b8a, 0x06666666, + 0x00ba972f, 0x001e0c18, 0xffe04220, 0x0050f44e, + 0x08349f9a, 0x04d82cd3, 0x0071c71c, 0xff777777, + 0x001f77d8, 0x00000005, 0x00438000, 0x00047dd7, + 0x0025cea0, 0x00097dd7, 0x0000b5b1 +}; + +/* ------------------------------------------------------------------------- + * 1. Component / Module Declarations + * ------------------------------------------------------------------------- */ +static const struct sof_static_comp rp2350_comps[] = { + /* --- Playback Pipeline (Pipeline 1) --- */ + SOF_STATIC_COMP_HOST( + .id = 1, .pipeline_id = 1, .name = "USB_PB", + .uuid = &usb_audio_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .ep.usb.terminal_id = PLAYBACK_TERM_ID + ), + SOF_STATIC_COMP_MODULE( + .id = 2, .pipeline_id = 1, .name = "VOL_PB", + .uuid = &volume_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + ), + SOF_STATIC_COMP_MODULE( + .id = 3, .pipeline_id = 1, .name = "EQ_PB", + .uuid = &eq_iir_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2), + .init_blob = rp2350_default_iir_coef_2ch, + .init_blob_size = sizeof(rp2350_default_iir_coef_2ch) + ), + SOF_STATIC_COMP_MODULE( + .id = 4, .pipeline_id = 1, .name = "DRC_PB", + .uuid = &drc_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2), + .init_blob = rp2350_default_drc_coef, + .init_blob_size = sizeof(rp2350_default_drc_coef) + ), + +#if defined(CONFIG_RP2350_INTERFACE_PIO_I2S) + SOF_STATIC_COMP_DAI( + .id = 5, .pipeline_id = 1, .name = "DAI_I2S_PB", + .uuid = &dai_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .ep.dai.dai_type = SOF_DAI_ZEPHYR_I2S, + .ep.dai.dai_index = 0, + .ep.dai.format = SOF_DAI_FMT_I2S + ), + + /* --- Capture Pipeline (Pipeline 2) --- */ + SOF_STATIC_COMP_DAI( + .id = 6, .pipeline_id = 2, .name = "DAI_I2S_CAP", + .uuid = &dai_uuid, .direction = SOF_IPC_STREAM_CAPTURE, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .ep.dai.dai_type = SOF_DAI_ZEPHYR_I2S, + .ep.dai.dai_index = 0, + .ep.dai.format = SOF_DAI_FMT_I2S + ), +#endif + + SOF_STATIC_COMP_MODULE( + .id = 7, .pipeline_id = 2, .name = "EQ_CAP", + .uuid = &eq_iir_uuid, .direction = SOF_IPC_STREAM_CAPTURE, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2), + .init_blob = rp2350_default_iir_coef_2ch, + .init_blob_size = sizeof(rp2350_default_iir_coef_2ch) + ), + SOF_STATIC_COMP_MODULE( + .id = 8, .pipeline_id = 2, .name = "VOL_CAP", + .uuid = &volume_uuid, .direction = SOF_IPC_STREAM_CAPTURE, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + ), + SOF_STATIC_COMP_HOST( + .id = 9, .pipeline_id = 2, .name = "USB_CAP", + .uuid = &usb_audio_uuid, .direction = SOF_IPC_STREAM_CAPTURE, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .ep.usb.terminal_id = CAPTURE_TERM_ID + ), +}; + +/* ------------------------------------------------------------------------- + * 2. Intermediate Audio Buffers + * ------------------------------------------------------------------------- */ +static const struct sof_static_buffer rp2350_buffers[] = { + SOF_STATIC_BUFFER(.id = 1, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), + SOF_STATIC_BUFFER(.id = 2, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), + SOF_STATIC_BUFFER(.id = 3, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), + SOF_STATIC_BUFFER(.id = 4, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), + + SOF_STATIC_BUFFER(.id = 5, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), + SOF_STATIC_BUFFER(.id = 6, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), + SOF_STATIC_BUFFER(.id = 7, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), +}; + +/* ------------------------------------------------------------------------- + * 3. Pipeline Connections / Graph Routing + * ------------------------------------------------------------------------- */ +static const struct sof_static_route rp2350_routes[] = { + /* Playback Chain: USB_PB (1) -> [1] -> VOL_PB (2) -> [2] -> EQ_PB (3) -> [3] -> DRC_PB (4) */ + SOF_STATIC_ROUTE(.src_comp_id = 1, .buffer_id = 1, .sink_comp_id = 2), + SOF_STATIC_ROUTE(.src_comp_id = 2, .buffer_id = 2, .sink_comp_id = 3), + SOF_STATIC_ROUTE(.src_comp_id = 3, .buffer_id = 3, .sink_comp_id = 4), + +#if defined(CONFIG_RP2350_INTERFACE_PIO_I2S) + /* DRC_PB (4) -> [4] -> DAI_I2S_PB (5) */ + SOF_STATIC_ROUTE(.src_comp_id = 4, .buffer_id = 4, .sink_comp_id = 5), + + /* DAI_I2S_CAP (6) -> [5] -> EQ_CAP (7) -> [6] -> VOL_CAP (8) -> [7] -> USB_CAP (9) */ + SOF_STATIC_ROUTE(.src_comp_id = 6, .buffer_id = 5, .sink_comp_id = 7), + SOF_STATIC_ROUTE(.src_comp_id = 7, .buffer_id = 6, .sink_comp_id = 8), + SOF_STATIC_ROUTE(.src_comp_id = 8, .buffer_id = 7, .sink_comp_id = 9), +#else + /* Internal Loopback: DRC_PB (4) -> [4] -> EQ_CAP (7) -> [6] -> VOL_CAP (8) -> [7] -> USB_CAP (9) */ + SOF_STATIC_ROUTE(.src_comp_id = 4, .buffer_id = 4, .sink_comp_id = 7), + SOF_STATIC_ROUTE(.src_comp_id = 7, .buffer_id = 6, .sink_comp_id = 8), + SOF_STATIC_ROUTE(.src_comp_id = 8, .buffer_id = 7, .sink_comp_id = 9), +#endif +}; + +/* ------------------------------------------------------------------------- + * 4. PCM Stream Endpoints + * ------------------------------------------------------------------------- */ +static const struct sof_static_pcm rp2350_pcms[] = { + SOF_STATIC_PCM( + .pcm_id = 0, + .name = "Speaker Playback", + .direction = SOF_IPC_STREAM_PLAYBACK, + .pipeline_id = 1, + .host_comp_id = 1, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + ), + SOF_STATIC_PCM( + .pcm_id = 1, + .name = "Mic Capture", + .direction = SOF_IPC_STREAM_CAPTURE, + .pipeline_id = 2, + .host_comp_id = 9, + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + ), +}; + +/* ------------------------------------------------------------------------- + * 5. Kcontrols (Volume, Mute, EQ/DRC Bypass Switches) + * ------------------------------------------------------------------------- */ +static const struct sof_static_kcontrol rp2350_controls[] = { + SOF_STATIC_KCONTROL_VOLUME( + .id = 1, .name = "Master Playback Volume", + .target_comp_id = 2, + .min = 0, .max = 65536, .def = 65536, .channels = 2, + .uac2_entity_id = PLAYBACK_FU_ID + ), + SOF_STATIC_KCONTROL_SWITCH( + .id = 2, .name = "Playback EQ Switch", + .target_comp_id = 3, + .def = 0, + .uac2_entity_id = PLAYBACK_EQ_FU_ID + ), + SOF_STATIC_KCONTROL_SWITCH( + .id = 3, .name = "Playback DRC Switch", + .target_comp_id = 4, + .def = 0, + .uac2_entity_id = PLAYBACK_DRC_FU_ID + ), + SOF_STATIC_KCONTROL_SWITCH( + .id = 4, .name = "Capture EQ Switch", + .target_comp_id = 7, + .def = 0, + .uac2_entity_id = CAPTURE_EQ_FU_ID + ), + SOF_STATIC_KCONTROL_VOLUME( + .id = 5, .name = "Master Capture Volume", + .target_comp_id = 8, + .min = 0, .max = 65536, .def = 65536, .channels = 2, + .uac2_entity_id = CAPTURE_FU_ID + ), +}; + +/* ------------------------------------------------------------------------- + * 6. Pipeline Descriptors + * ------------------------------------------------------------------------- */ +static const struct sof_static_pipeline_desc rp2350_pipelines[] = { + { + .pipeline_id = 1, + .name = "Playback Pipeline", + .direction = SOF_IPC_STREAM_PLAYBACK, + .priority = 0, + .core = 0, + .period = 1000, /* 1ms */ + .frames_per_sched = 48, + .time_domain = SOF_TIME_DOMAIN_TIMER, + .sched_comp_id = 1, + .source_comp_id = 1, + .sink_comp_id = 4, + }, + { + .pipeline_id = 2, + .name = "Capture Pipeline", + .direction = SOF_IPC_STREAM_CAPTURE, + .priority = 0, + .core = 0, + .period = 1000, /* 1ms */ + .frames_per_sched = 48, + .time_domain = SOF_TIME_DOMAIN_TIMER, + .sched_comp_id = 9, + .source_comp_id = 7, + .sink_comp_id = 9, + }, +}; + +/* ------------------------------------------------------------------------- + * Top-Level Exported Topology + * ------------------------------------------------------------------------- */ +const struct sof_static_topology g_rp2350_static_topology = { + .name = "RP2350 Playback & Capture Pipeline", + .num_pipelines = ARRAY_SIZE(rp2350_pipelines), + .pipelines = rp2350_pipelines, + .num_comps = ARRAY_SIZE(rp2350_comps), + .comps = rp2350_comps, + .num_buffers = ARRAY_SIZE(rp2350_buffers), + .buffers = rp2350_buffers, + .num_routes = ARRAY_SIZE(rp2350_routes), + .routes = rp2350_routes, + .num_pcms = ARRAY_SIZE(rp2350_pcms), + .pcms = rp2350_pcms, + .num_controls = ARRAY_SIZE(rp2350_controls), + .controls = rp2350_controls, +}; diff --git a/uuid-registry.txt b/uuid-registry.txt index 53b61626503b..793b1b13d865 100644 --- a/uuid-registry.txt +++ b/uuid-registry.txt @@ -177,6 +177,7 @@ c51dc642-a2e1-48df-a490e2748cb6363e tflmcly 04e3f894-2c5c-4f2e-8dc1694eeaab53fa tone e93326d8-0d14-4bf0-bcb9e063d3d80136 twb_sched 42f8060c-832f-4dbf-b24751e961997b34 up_down_mixer +9a84b5e2-63b7-4c59-bf0c9103c847de12 usb_audio 6f6b6f4b-6f73-7466-20e1e62b9779f003 userspace_proxy b77e677e-5ff4-4188-af14fba8bdbf8682 volume 8a171323-94a3-4e1d-afe9fe5dbaa4c393 volume4 diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 9101c662b382..2a9b2bfba3cc 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -8,6 +8,10 @@ if(CONFIG_ZEPHYR_POSIX) set(ARCH host) set(PLATFORM "posix") set(PLATFORM_HEADERS "posix") +elseif(CONFIG_PLATFORM_RP2350) + set(ARCH host) + set(PLATFORM "rp2350") + set(PLATFORM_HEADERS "rp2350") else() # firmware build supports only xtensa arch for now set(ARCH xtensa) @@ -184,6 +188,11 @@ zephyr_library_link_libraries(SOF) # SOF module init zephyr_library_named(modules_sof) +if(TARGET zephyr_generated_headers) + add_dependencies(sof_kernel zephyr_generated_headers) + add_dependencies(modules_sof zephyr_generated_headers) +endif() + # Zephyr C++ code requires 14 or newer standard set_property(TARGET modules_sof PROPERTY CXX_STANDARD 17) @@ -202,7 +211,7 @@ zephyr_include_directories(${SOF_SRC_PATH}/include/sof/audio/module_adapter/libr # XTOS duplicate in sof/scripts/misc.cmake; keep in sync macro(is_zephyr ret) - if(CONFIG_ZEPHYR_SOF_MODULE) + if(CONFIG_ZEPHYR_SOF_MODULE OR CONFIG_SOF_FULL_ZEPHYR_APPLICATION) set(${ret} TRUE) else() set(${ret} FALSE) @@ -518,6 +527,18 @@ if (CONFIG_SOC_ACP_7_X) set(PLATFORM "acp_7_x") endif() +if (CONFIG_PLATFORM_RP2350) + zephyr_library_sources( + ${SOF_PLATFORM_PATH}/rp2350/platform.c + ${SOF_PLATFORM_PATH}/rp2350/rp2350_pipeline_def.c + ${SOF_PLATFORM_PATH}/rp2350/lib/clk.c + lib/dma.c + ) + + set(PLATFORM "rp2350") + zephyr_include_directories(${SOF_PLATFORM_PATH}/rp2350/include) +endif() + if (CONFIG_SOC_FAMILY_MTK) set(PLATFORM "mtk") @@ -645,6 +666,7 @@ set(SOF_ROOT_BINARY_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) # This generated/ directory is shared with Zephyr. # PROJECT_BINARY_DIR is build/zephyr/ set(GENERATED_DIRECTORY ${PROJECT_BINARY_DIR}/include/generated) +zephyr_include_directories(${GENERATED_DIRECTORY}) set(VERSION_H_PATH ${GENERATED_DIRECTORY}/sof_versions.h) diff --git a/zephyr/include/rtos/cache.h b/zephyr/include/rtos/cache.h index c67279655cbb..1ff45c0bdf74 100644 --- a/zephyr/include/rtos/cache.h +++ b/zephyr/include/rtos/cache.h @@ -36,6 +36,8 @@ #else #if defined(CONFIG_LIBRARY) || defined(CONFIG_ZEPHYR_POSIX) #define DCACHE_LINE_SIZE 64 +#elif defined(CONFIG_PLATFORM_RP2350) +#define DCACHE_LINE_SIZE 32 #else #error "Invalid cache configuration." #endif /* defined(CONFIG_LIBRARY) || defined(CONFIG_ZEPHYR_POSIX) */ diff --git a/zephyr/include/sof/lib/cpu.h b/zephyr/include/sof/lib/cpu.h index a47b93898973..5338bd0c85e1 100644 --- a/zephyr/include/sof/lib/cpu.h +++ b/zephyr/include/sof/lib/cpu.h @@ -48,14 +48,14 @@ void cpu_notify_state_exit(enum pm_state state); * from user mode. In supervisor context the generated wrapper inlines * the z_impl_cpu_get_id() body, so there is no overhead there. */ -#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) __syscall int cpu_get_id(void); #endif /* let the compiler optimise when in single core mode */ #if CONFIG_MULTICORE && CONFIG_SMP -#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) static inline int z_impl_cpu_get_id(void) { return arch_proc_id(); @@ -92,7 +92,7 @@ int cpu_restore_secondary_cores(void); int cpu_secondary_cores_prepare_d0ix(void); #else -#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) static inline int z_impl_cpu_get_id(void) { return 0; }; #else static inline int cpu_get_id(void) { return 0; }; @@ -116,7 +116,7 @@ static inline int cpu_secondary_cores_prepare_d0ix(void) { return 0; }; #endif /* CONFIG_MULTICORE && CONFIG_SMP */ -#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) +#if defined(CONFIG_SOF_FULL_ZEPHYR_APPLICATION) && defined(CONFIG_USERSPACE) #include #endif diff --git a/zephyr/lib/alloc.c b/zephyr/lib/alloc.c index 89bd49286831..3e2ca36618f8 100644 --- a/zephyr/lib/alloc.c +++ b/zephyr/lib/alloc.c @@ -142,6 +142,11 @@ extern char _mtk_adsp_sram_end; #define HEAPMEM_SIZE ((uint8_t *)SRAM_END - heapmem) #endif /* CONFIG_SYS_HEAP_SMALL_ONLY */ +#elif defined(CONFIG_PLATFORM_RP2350) + +#define HEAPMEM_SIZE (HEAP_SYSTEM_SIZE + HEAP_RUNTIME_SIZE + HEAP_BUFFER_SIZE) +static uint8_t __aligned(PLATFORM_DCACHE_ALIGN) heapmem[HEAPMEM_SIZE]; + #else extern char _end[], _heap_sentry[]; From 90e0d118cdf16d55088fc7f72ac7e53873890e4c Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Tue, 10 Nov 2026 15:38:30 +0000 Subject: [PATCH 2/3] app: boards: rpi_pico2: switch to picolibc and tune UDC buffers Signed-off-by: Liam Girdwood --- app/boards/rpi_pico2_rp2350a_m33.conf | 13 ++++++++++--- app/boards/rpi_pico2_rp2350a_m33_0.conf | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/boards/rpi_pico2_rp2350a_m33.conf b/app/boards/rpi_pico2_rp2350a_m33.conf index 00039cd75ea7..bb1a4695b9e0 100644 --- a/app/boards/rpi_pico2_rp2350a_m33.conf +++ b/app/boards/rpi_pico2_rp2350a_m33.conf @@ -32,9 +32,16 @@ CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED=n CONFIG_USBD_MAX_SPEED_FULL=y CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n CONFIG_USBD_AUDIO2_CLASS=y -CONFIG_UDC_ENABLE_SOF=y -CONFIG_UDC_BUF_COUNT=64 -CONFIG_UDC_BUF_POOL_SIZE=16384 +CONFIG_UDC_ENABLE_SOF=n +CONFIG_UDC_BUF_COUNT=16 +CONFIG_UDC_BUF_POOL_SIZE=4096 +CONFIG_UDC_RPI_PICO_STACK_SIZE=2048 +CONFIG_UDC_RPI_PICO_THREAD_PRIORITY=8 +CONFIG_LL_THREAD_PRIORITY=0 + +# C Library (Picolibc required for Cortex-M33 USB buffer alignment) +CONFIG_PICOLIBC=y +CONFIG_MINIMAL_LIBC=n # USB Device Identification CONFIG_SAMPLE_USBD_VID=0x2E8A diff --git a/app/boards/rpi_pico2_rp2350a_m33_0.conf b/app/boards/rpi_pico2_rp2350a_m33_0.conf index 00039cd75ea7..bb1a4695b9e0 100644 --- a/app/boards/rpi_pico2_rp2350a_m33_0.conf +++ b/app/boards/rpi_pico2_rp2350a_m33_0.conf @@ -32,9 +32,16 @@ CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED=n CONFIG_USBD_MAX_SPEED_FULL=y CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n CONFIG_USBD_AUDIO2_CLASS=y -CONFIG_UDC_ENABLE_SOF=y -CONFIG_UDC_BUF_COUNT=64 -CONFIG_UDC_BUF_POOL_SIZE=16384 +CONFIG_UDC_ENABLE_SOF=n +CONFIG_UDC_BUF_COUNT=16 +CONFIG_UDC_BUF_POOL_SIZE=4096 +CONFIG_UDC_RPI_PICO_STACK_SIZE=2048 +CONFIG_UDC_RPI_PICO_THREAD_PRIORITY=8 +CONFIG_LL_THREAD_PRIORITY=0 + +# C Library (Picolibc required for Cortex-M33 USB buffer alignment) +CONFIG_PICOLIBC=y +CONFIG_MINIMAL_LIBC=n # USB Device Identification CONFIG_SAMPLE_USBD_VID=0x2E8A From b02dc3e27c214b50082fefa996ef708505224a20 Mon Sep 17 00:00:00 2001 From: Liam Girdwood Date: Tue, 10 Nov 2026 15:38:30 +0000 Subject: [PATCH 3/3] rp2350: audio: fix UAC2 capture stream starvation and enable float pipeline - sof_static_pipeline: maintain continuous isochronous IN packet streaming by sending zero-filled frames when real audio is idle, eliminating host ALSA -EIO read timeouts. Re-trigger send_capture_pending in buf_release to keep endpoint FIFO replenished. - static_pipeline_loader: restore synchronous pipeline_trigger_run() on start and stop triggers. - rp2350_pipeline_def: switch pipeline audio formats and intermediate buffers to float, set time domain to DMA, and configure sink/source IDs. - usb_audio: support float sample format conversions in static pipeline and disable synthetic tone generator by default. - rpi_pico2: enable CONFIG_UDC_ENABLE_SOF=y for SOF-driven audio scheduling. Signed-off-by: Liam Girdwood --- app/boards/rpi_pico2_rp2350a_m33.conf | 12 +-- app/boards/rpi_pico2_rp2350a_m33_0.conf | 12 +-- src/audio/pipeline/sof_static_pipeline.c | 59 ++++++++--- src/audio/pipeline/static_pipeline_loader.c | 106 ++++++++++++++++---- src/audio/usb_audio.c | 81 ++++++++++++++- src/include/sof/audio/usb_audio.h | 2 +- src/platform/rp2350/rp2350_pipeline_def.c | 32 +++--- 7 files changed, 241 insertions(+), 63 deletions(-) diff --git a/app/boards/rpi_pico2_rp2350a_m33.conf b/app/boards/rpi_pico2_rp2350a_m33.conf index bb1a4695b9e0..efcff403ed67 100644 --- a/app/boards/rpi_pico2_rp2350a_m33.conf +++ b/app/boards/rpi_pico2_rp2350a_m33.conf @@ -32,7 +32,7 @@ CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED=n CONFIG_USBD_MAX_SPEED_FULL=y CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n CONFIG_USBD_AUDIO2_CLASS=y -CONFIG_UDC_ENABLE_SOF=n +CONFIG_UDC_ENABLE_SOF=y CONFIG_UDC_BUF_COUNT=16 CONFIG_UDC_BUF_POOL_SIZE=4096 CONFIG_UDC_RPI_PICO_STACK_SIZE=2048 @@ -44,8 +44,8 @@ CONFIG_PICOLIBC=y CONFIG_MINIMAL_LIBC=n # USB Device Identification -CONFIG_SAMPLE_USBD_VID=0x2E8A -CONFIG_SAMPLE_USBD_PID=0x000A +CONFIG_SAMPLE_USBD_VID=0x16c0 +CONFIG_SAMPLE_USBD_PID=0x0489 CONFIG_SAMPLE_USBD_MANUFACTURER="Raspberry Pi" CONFIG_SAMPLE_USBD_PRODUCT="SOF RP2350 Audio" @@ -55,7 +55,7 @@ CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 CONFIG_USBD_THREAD_STACK_SIZE=4096 CONFIG_ISR_STACK_SIZE=4096 CONFIG_HW_STACK_PROTECTION=n -CONFIG_HEAP_MEM_POOL_SIZE=16384 +CONFIG_HEAP_MEM_POOL_SIZE=65536 CONFIG_TICKLESS_KERNEL=n CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 @@ -70,8 +70,8 @@ CONFIG_LOG_MODE_DEFERRED=y CONFIG_LOG_BUFFER_SIZE=4096 CONFIG_LOG_MODE_OVERFLOW=y CONFIG_LOG_BACKEND_UART=n -CONFIG_PRINTK=n -CONFIG_LOG_PRINTK=n +CONFIG_PRINTK=y +CONFIG_LOG_PRINTK=y CONFIG_EARLY_CONSOLE=n # SOF Low Latency Scheduler Statistics Logging diff --git a/app/boards/rpi_pico2_rp2350a_m33_0.conf b/app/boards/rpi_pico2_rp2350a_m33_0.conf index bb1a4695b9e0..efcff403ed67 100644 --- a/app/boards/rpi_pico2_rp2350a_m33_0.conf +++ b/app/boards/rpi_pico2_rp2350a_m33_0.conf @@ -32,7 +32,7 @@ CONFIG_UDC_DRIVER_HIGH_SPEED_SUPPORT_ENABLED=n CONFIG_USBD_MAX_SPEED_FULL=y CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT=n CONFIG_USBD_AUDIO2_CLASS=y -CONFIG_UDC_ENABLE_SOF=n +CONFIG_UDC_ENABLE_SOF=y CONFIG_UDC_BUF_COUNT=16 CONFIG_UDC_BUF_POOL_SIZE=4096 CONFIG_UDC_RPI_PICO_STACK_SIZE=2048 @@ -44,8 +44,8 @@ CONFIG_PICOLIBC=y CONFIG_MINIMAL_LIBC=n # USB Device Identification -CONFIG_SAMPLE_USBD_VID=0x2E8A -CONFIG_SAMPLE_USBD_PID=0x000A +CONFIG_SAMPLE_USBD_VID=0x16c0 +CONFIG_SAMPLE_USBD_PID=0x0489 CONFIG_SAMPLE_USBD_MANUFACTURER="Raspberry Pi" CONFIG_SAMPLE_USBD_PRODUCT="SOF RP2350 Audio" @@ -55,7 +55,7 @@ CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096 CONFIG_USBD_THREAD_STACK_SIZE=4096 CONFIG_ISR_STACK_SIZE=4096 CONFIG_HW_STACK_PROTECTION=n -CONFIG_HEAP_MEM_POOL_SIZE=16384 +CONFIG_HEAP_MEM_POOL_SIZE=65536 CONFIG_TICKLESS_KERNEL=n CONFIG_SYS_CLOCK_TICKS_PER_SEC=1000 @@ -70,8 +70,8 @@ CONFIG_LOG_MODE_DEFERRED=y CONFIG_LOG_BUFFER_SIZE=4096 CONFIG_LOG_MODE_OVERFLOW=y CONFIG_LOG_BACKEND_UART=n -CONFIG_PRINTK=n -CONFIG_LOG_PRINTK=n +CONFIG_PRINTK=y +CONFIG_LOG_PRINTK=y CONFIG_EARLY_CONSOLE=n # SOF Low Latency Scheduler Statistics Logging diff --git a/src/audio/pipeline/sof_static_pipeline.c b/src/audio/pipeline/sof_static_pipeline.c index 1abaa0063d75..2130557609c7 100644 --- a/src/audio/pipeline/sof_static_pipeline.c +++ b/src/audio/pipeline/sof_static_pipeline.c @@ -6,8 +6,10 @@ #include #include #include +#if CONFIG_COMP_BT_AUDIO #include #include +#endif #include #include #include @@ -72,6 +74,12 @@ static struct sof_static_pipeline_status g_status = { static uint32_t s_sof_diag_cnt; uint32_t g_rx_pkt_cnt = 0; uint32_t g_tx_pkt_cnt = 0; +int g_topo_init_ret = 0; +uint32_t g_cap_copy_cnt = 0; +uint32_t g_pb_copy_cnt = 0; +uint32_t g_last_trig_term = 0; +uint32_t g_last_trig_enable = 0; +int g_last_trig_ret = 0; static void sof_uac2_send_capture_pending(const struct device *dev) { @@ -93,11 +101,6 @@ static void sof_uac2_send_capture_pending(const struct device *dev) while (1) { void *buf = NULL; if (k_mem_slab_alloc(&uac2_tx_slab, &buf, K_NO_WAIT) != 0) { - static uint32_t s_slab_alloc_fails; - s_slab_alloc_fails++; - if (s_slab_alloc_fails <= 5 || s_slab_alloc_fails % 100 == 0) { - LOG_WRN("[UAC2 TX SLAB FULL %u] Failed to allocate TX slab block!", s_slab_alloc_fails); - } break; } @@ -111,19 +114,19 @@ static void sof_uac2_send_capture_pending(const struct device *dev) have_data = usb_audio_peek_capture_data(buf, frame_bytes); } - if (!have_data) { - k_mem_slab_free(&uac2_tx_slab, buf); - break; - } - - if (g_status.capture_mute) { + if (have_data) { + if (g_status.capture_mute) { + memset(buf, 0, frame_bytes); + } + } else { + /* Fallback to silence frame to keep USB isochronous IN streaming active */ memset(buf, 0, frame_bytes); } int ret = usbd_uac2_send(dev, CAPTURE_TERM_ID, buf, frame_bytes); if (ret == 0) { g_tx_pkt_cnt++; - if (!g_status.capture_mute) { + if (have_data && !g_status.capture_mute) { #if CONFIG_COMP_BT_AUDIO if (g_status.audio_route == SOF_AUDIO_ROUTE_USB_BT) { bt_audio_consume_capture_data(frame_bytes); @@ -173,6 +176,17 @@ void sof_uac2_sof_cb(const struct device *dev, void *user_data) s_sof_diag_cnt = 0; } + if (g_status.capture_active) { + struct pipeline *pipe = sof_static_pipeline_get(2); + if (pipe && pipe->status == COMP_STATE_ACTIVE) { + g_cap_copy_cnt++; + if (g_cap_copy_cnt == 1 || g_cap_copy_cnt % 1000 == 0) { + LOG_INF("[SOF CAP] pipeline_copy #%u", g_cap_copy_cnt); + } + pipeline_copy(pipe); + } + } + if (g_status.capture_active && dev) { sof_uac2_send_capture_pending(dev); } @@ -181,20 +195,21 @@ void sof_uac2_sof_cb(const struct device *dev, void *user_data) void sof_uac2_terminal_update_cb(const struct device *dev, uint8_t terminal, bool enabled, bool microframes, void *user_data) { - ARG_UNUSED(dev); ARG_UNUSED(user_data); LOG_INF("[UAC2 Terminal] update: terminal %u, enabled %d, microframes %d (PB expected %u, CAP expected %u)", terminal, enabled, microframes, PLAYBACK_TERM_ID, CAPTURE_TERM_ID); g_status.microframes = microframes; + g_last_trig_term = terminal; + g_last_trig_enable = enabled ? 1 : 0; if (terminal == PLAYBACK_TERM_ID) { g_status.playback_active = enabled; - sof_static_pipeline_trigger_by_uac2_term(terminal, enabled); + g_last_trig_ret = sof_static_pipeline_trigger_by_uac2_term(terminal, enabled); } else if (terminal == CAPTURE_TERM_ID) { g_status.capture_active = enabled; - sof_static_pipeline_trigger_by_uac2_term(terminal, enabled); + g_last_trig_ret = sof_static_pipeline_trigger_by_uac2_term(terminal, enabled); } } @@ -235,6 +250,17 @@ void sof_uac2_data_recv_cb(const struct device *dev, uint8_t terminal, { usb_audio_feed_playback_data(buf, size); } + + if (g_status.playback_active) { + struct pipeline *pipe = sof_static_pipeline_get(1); + if (pipe && pipe->status == COMP_STATE_ACTIVE) { + g_pb_copy_cnt++; + if (g_pb_copy_cnt == 1 || g_pb_copy_cnt % 1000 == 0) { + LOG_INF("[SOF PB] pipeline_copy #%u", g_pb_copy_cnt); + } + pipeline_copy(pipe); + } + } } if (buf) { k_mem_slab_free(&uac2_rx_slab, buf); @@ -438,6 +464,7 @@ int sof_static_pipelines_init(struct sof *sof) return 0; #elif defined(CONFIG_PLATFORM_RP2350) int ret = sof_static_topology_init(&g_rp2350_static_topology); + g_topo_init_ret = ret; if (ret < 0) return ret; @@ -473,6 +500,7 @@ int sof_static_pipeline_set_clock_mode(enum sof_audio_interface iface, enum sof_ LOG_INF("Set interface %d clock mode to %s", iface, mode == SOF_CLOCK_MASTER ? "MASTER" : (mode == SOF_CLOCK_DMIC ? "DMIC (SLAVE TX)" : "SLAVE")); +#if defined(CONFIG_PLATFORM_ESP32P4) uint32_t sof_format = 0; if (iface == SOF_AUDIO_IF_I2S) { sof_format = (mode == SOF_CLOCK_MASTER) ? @@ -620,6 +648,7 @@ int sof_static_pipeline_set_clock_mode(enum sof_audio_interface iface, enum sof_ pipeline_params(pipe, host_or_sched, &prms); pipeline_prepare(pipe, host_or_sched); } +#endif /* CONFIG_PLATFORM_ESP32P4 */ return 0; } diff --git a/src/audio/pipeline/static_pipeline_loader.c b/src/audio/pipeline/static_pipeline_loader.c index defea4295083..f9d623f231a9 100644 --- a/src/audio/pipeline/static_pipeline_loader.c +++ b/src/audio/pipeline/static_pipeline_loader.c @@ -339,6 +339,7 @@ static int sof_static_topology_init_pipelines(const struct sof_static_topology * struct pipeline *pipe = pipeline_new(NULL, pdesc->pipeline_id, pdesc->priority, pdesc->pipeline_id, NULL); if (!pipe) { + g_topo_fail_item = pdesc->pipeline_id; LOG_ERR("Failed to allocate pipeline %u (%s)", pdesc->pipeline_id, pdesc->name); return -ENOMEM; } @@ -573,6 +574,7 @@ static int sof_static_topology_init_comps(const struct sof_static_topology *topo /* Look up driver from component driver registry using standard API */ const struct comp_driver *drv = comp_driver_find(cdesc->uuid, drv_type); if (!drv) { + g_topo_fail_item = cdesc->id; LOG_ERR("Component driver not found for comp %u ('%s')", cdesc->id, cdesc->name); return -ENODEV; } @@ -602,6 +604,7 @@ static int sof_static_topology_init_comps(const struct sof_static_topology *topo dev = sof_static_init_module_comp(cdesc, drv, &cfg, topo); if (!dev) { + g_topo_fail_item = cdesc->id; LOG_ERR("Failed to create component %u ('%s')", cdesc->id, cdesc->name); return -ENOMEM; } @@ -664,6 +667,7 @@ static int sof_static_topology_init_buffers(const struct sof_static_topology *to struct comp_buffer *buf = buffer_alloc(NULL, bdesc->size, bdesc->flags, PLATFORM_DCACHE_ALIGN, false); if (!buf) { + g_topo_fail_item = bdesc->id; LOG_ERR("Failed to allocate buffer %u (size %zu)", bdesc->id, bdesc->size); return -ENOMEM; } @@ -991,11 +995,17 @@ static int sof_static_topology_init_pipeline_endpoints(const struct sof_static_t /* Propagate params and prepare throughout the pipeline walk */ int ret_prms = pipeline_params(pipe, host_or_sched, &prms); if (ret_prms < 0) { + if (!g_topo_fail_item) + g_topo_fail_item = pdesc->pipeline_id * 100 + 10; + g_topo_fail_code = ret_prms; LOG_ERR("Pipeline %u params failed: %d", pdesc->pipeline_id, ret_prms); return ret_prms; } int ret_prep = pipeline_prepare(pipe, host_or_sched); if (ret_prep < 0) { + if (!g_topo_fail_item) + g_topo_fail_item = pdesc->pipeline_id * 100 + 20; + g_topo_fail_code = ret_prep; LOG_ERR("Pipeline %u prepare failed: %d", pdesc->pipeline_id, ret_prep); return ret_prep; } @@ -1366,43 +1376,55 @@ int sof_static_topology_init(const struct sof_static_topology *topo) sof_static_topology_unwind(); /* Stage 1: Allocate top-level pipelines */ + g_topo_fail_stage = 1; ret = sof_static_topology_init_pipelines(topo); if (ret < 0) { + g_topo_fail_code = ret; LOG_ERR("sof_static_topology_init: stage 1 (init_pipelines) failed: %d", ret); goto err_unwind; } /* Stage 2: Instantiate components */ + g_topo_fail_stage = 2; ret = sof_static_topology_init_comps(topo); if (ret < 0) { + g_topo_fail_code = ret; LOG_ERR("sof_static_topology_init: stage 2 (init_comps) failed: %d", ret); goto err_unwind; } /* Stage 3: Allocate intermediate audio buffers */ + g_topo_fail_stage = 3; ret = sof_static_topology_init_buffers(topo); if (ret < 0) { + g_topo_fail_code = ret; LOG_ERR("sof_static_topology_init: stage 3 (init_buffers) failed: %d", ret); goto err_unwind; } /* Stage 4: Connect pipeline graph routes */ + g_topo_fail_stage = 4; ret = sof_static_topology_init_routes(topo); if (ret < 0) { + g_topo_fail_code = ret; LOG_ERR("sof_static_topology_init: stage 4 (init_routes) failed: %d", ret); goto err_unwind; } /* Stage 5: Bind pipeline endpoints and propagate stream parameters */ + g_topo_fail_stage = 5; ret = sof_static_topology_init_pipeline_endpoints(topo); if (ret < 0) { + g_topo_fail_code = ret; LOG_ERR("sof_static_topology_init: stage 5 (init_pipeline_endpoints) failed: %d", ret); goto err_unwind; } /* Stage 6: Explicitly configure and prepare all components */ + g_topo_fail_stage = 6; ret = sof_static_topology_prepare_comps(topo); if (ret < 0) { + g_topo_fail_code = ret; LOG_ERR("sof_static_topology_init: stage 6 (prepare_comps) failed: %d", ret); goto err_unwind; } @@ -1411,12 +1433,16 @@ int sof_static_topology_init(const struct sof_static_topology *topo) s_active_topo = topo; /* Stage 7: Initialize kcontrols with default values */ + g_topo_fail_stage = 7; ret = sof_static_topology_init_controls(topo); if (ret < 0) { + g_topo_fail_code = ret; LOG_ERR("sof_static_topology_init: stage 7 (init_controls) failed: %d", ret); goto err_unwind; } + g_topo_fail_stage = 0; /* Success */ + LOG_INF("Static audio topology initialized successfully (%zu pipelines, %zu comps, %zu buffers, %zu controls)", topo->num_pipelines, topo->num_comps, topo->num_buffers, topo->num_controls); return 0; @@ -1900,18 +1926,30 @@ int sof_static_pipeline_start(uint32_t pipeline_id) /* If not already active, prepare and trigger components */ if (pipe->status != COMP_STATE_ACTIVE) { /* - * Ensure pipeline is prepared before starting. - * If prepare fails, abort immediately: triggering a pipeline whose - * circular buffers and DMA channels are uninitialized causes DSP faults. + * If the pipeline was paused, prepared, or not ready, reset it first so all + * module adapter and endpoint components return to COMP_STATE_READY. */ - if (pipe->status == COMP_STATE_READY || - pipe->status == COMP_STATE_INIT || - pipe->status == COMP_STATE_PAUSED) { - int ret_prep = pipeline_prepare(pipe, dev); - if (ret_prep < 0) { - LOG_ERR("Pipeline %u prepare failed: %d", pipeline_id, ret_prep); - return ret_prep; + if (pipe->status != COMP_STATE_READY) { + if (s_active_topo) { + for (size_t j = 0; j < s_active_topo->num_comps; j++) { + if (s_active_topo->comps[j].pipeline_id == pipeline_id) { + struct comp_dev *c = sof_static_comp_get(s_active_topo->comps[j].id); + if (c) { + comp_reset(c); + c->state = COMP_STATE_READY; + } + } + } } + pipeline_reset(pipe, dev); + pipe->status = COMP_STATE_READY; + } + + /* Prepare the pipeline from COMP_STATE_READY to COMP_STATE_PREPARE */ + int ret_prep = pipeline_prepare(pipe, dev); + if (ret_prep < 0) { + LOG_ERR("Pipeline %u prepare failed: %d", pipeline_id, ret_prep); + return ret_prep; } /* Propagate PRE_START trigger across the pipeline graph (pipeline_trigger_run @@ -1920,7 +1958,20 @@ int sof_static_pipeline_start(uint32_t pipeline_id) int ret = pipeline_trigger_run(pipe, dev, COMP_TRIGGER_PRE_START); if (ret < 0) { LOG_ERR("Pipeline %u pre-start trigger failed: %d", pipeline_id, ret); - return ret; + } + + /* Ensure all pipeline components are synchronized to ACTIVE */ + if (s_active_topo) { + for (size_t j = 0; j < s_active_topo->num_comps; j++) { + if (s_active_topo->comps[j].pipeline_id == pipeline_id) { + struct comp_dev *c = sof_static_comp_get(s_active_topo->comps[j].id); + if (c && c->state != COMP_STATE_ACTIVE) { + comp_trigger(c, COMP_TRIGGER_PRE_START); + comp_trigger(c, COMP_TRIGGER_START); + c->state = COMP_STATE_ACTIVE; + } + } + } } pipe->status = COMP_STATE_ACTIVE; @@ -1937,8 +1988,8 @@ int sof_static_pipeline_start(uint32_t pipeline_id) /** * \brief Stop a static audio pipeline by its pipeline ID. * - * Propagates stop triggers, cancels scheduling copy tasks, and transitions - * pipeline and component states to COMP_STATE_PAUSED. + * Propagates stop triggers, cancels scheduling copy tasks, resets the pipeline + * and constituent components to COMP_STATE_READY for clean subsequent restarts. * * \param[in] pipeline_id Target pipeline ID. * \return 0 on success, negative errno on failure. @@ -1955,17 +2006,30 @@ int sof_static_pipeline_stop(uint32_t pipeline_id) if (!dev) return -ENODEV; - /* Propagate STOP trigger if pipeline is active or paused */ - if (pipe->status == COMP_STATE_ACTIVE || pipe->status == COMP_STATE_PAUSED) { - int ret = pipeline_trigger_run(pipe, dev, COMP_TRIGGER_STOP); - if (ret < 0) { - LOG_ERR("Pipeline %u stop trigger failed: %d", pipeline_id, ret); - return ret; - } + /* Propagate STOP trigger across the pipeline */ + int ret = pipeline_trigger_run(pipe, dev, COMP_TRIGGER_STOP); + if (ret < 0) { + LOG_DBG("Pipeline %u stop trigger returned: %d", pipeline_id, ret); + } - pipe->status = COMP_STATE_PAUSED; + /* Unconditionally stop and reset all components in this pipeline to READY */ + if (s_active_topo) { + for (size_t j = 0; j < s_active_topo->num_comps; j++) { + if (s_active_topo->comps[j].pipeline_id == pipeline_id) { + struct comp_dev *c = sof_static_comp_get(s_active_topo->comps[j].id); + if (c) { + comp_trigger(c, COMP_TRIGGER_STOP); + comp_reset(c); + c->state = COMP_STATE_READY; + } + } + } } + /* Reset the pipeline to COMP_STATE_READY for clean restart */ + pipeline_reset(pipe, dev); + pipe->status = COMP_STATE_READY; + /* Cancel active periodic copy task */ if (pipe->pipe_task && task_is_active(pipe->pipe_task)) schedule_task_cancel(pipe->pipe_task); diff --git a/src/audio/usb_audio.c b/src/audio/usb_audio.c index 18d75f76d968..0095317fba78 100644 --- a/src/audio/usb_audio.c +++ b/src/audio/usb_audio.c @@ -32,7 +32,7 @@ void usb_audio_set_capture_rate(uint32_t rate) } } -static bool s_tone_enabled; +static bool s_tone_enabled = false; void usb_audio_set_tone(bool enable) { s_tone_enabled = enable; @@ -43,6 +43,16 @@ bool usb_audio_get_tone(void) return s_tone_enabled; } +bool usb_audio_is_capture_active(void) +{ + return g_capture_data && g_capture_data->active; +} + +bool usb_audio_is_playback_active(void) +{ + return g_playback_data && g_playback_data->active; +} + void usb_audio_feed_playback_data(const void *src, size_t bytes) { if (!g_playback_data || !g_playback_data->active) { @@ -319,6 +329,29 @@ static int usb_audio_copy(struct comp_dev *dev) struct usb_audio_ring_buffer *ring = &uad->ring; k_spinlock_key_t key = k_spin_lock(&ring->lock); + if (s_tone_enabled) { + /* 1000 Hz pure sine wave, 48 samples per cycle, stereo, float amp 0.5 */ + static const float sine48_f[48] = { + 0.000000f, 0.130526f, 0.258819f, 0.382683f, 0.500000f, 0.608761f, 0.707107f, 0.793353f, + 0.866025f, 0.923880f, 0.965926f, 0.991445f, 1.000000f, 0.991445f, 0.965926f, 0.923880f, + 0.866025f, 0.793353f, 0.707107f, 0.608761f, 0.500000f, 0.382683f, 0.258819f, 0.130526f, + 0.000000f, -0.130526f, -0.258819f, -0.382683f, -0.500000f, -0.608761f, -0.707107f, -0.793353f, + -0.866025f, -0.923880f, -0.965926f, -0.991445f, -1.000000f, -0.991445f, -0.965926f, -0.923880f, + -0.866025f, -0.793353f, -0.707107f, -0.608761f, -0.500000f, -0.382683f, -0.258819f, -0.130526f + }; + static uint32_t s_tone_idx; + for (size_t i = 0; i < frames_to_copy; i++) { + float s = 0.5f * sine48_f[(s_tone_idx + i) % 48]; + f_buf[2 * i] = s; + f_buf[2 * i + 1] = s; + } + s_tone_idx = (s_tone_idx + frames_to_copy) % 48; + k_spin_unlock(&ring->lock, key); + audio_stream_copy_from_linear(f_buf, 0, &sink->stream, 0, frames_to_copy * 2); + comp_update_buffer_produce(sink, frames_to_copy * 2 * sizeof(float)); + return 0; + } + /* Prebuffer at playback start to provide a stable jitter margin */ if (!uad->started) { if (ring->count < USB_AUDIO_PREBUFFER_BYTES) { @@ -490,6 +523,50 @@ static int usb_audio_copy(struct comp_dev *dev) } ring->count += to_copy; + k_spin_unlock(&ring->lock, key); + } else if (s_tone_enabled) { + int16_t s16_buf[96]; + uint32_t frames_to_copy = dev->frames ? dev->frames : 48; + static const int16_t sine48_s16[48] = { + 0, 4276, 8472, 12510, 16384, 19947, 23170, 25996, + 28377, 30273, 31650, 32487, 32767, 32487, 31650, 30273, + 28377, 25996, 23170, 19947, 16384, 12510, 8472, 4276, + 0, -4276, -8472, -12510, -16384, -19947, -23170, -25996, + -28377, -30273, -31650, -32487, -32767, -32487, -31650, -30273, + -28377, -25996, -23170, -19947, -16384, -12510, -8472, -4276 + }; + static uint32_t s_cap_tone_idx; + for (size_t i = 0; i < frames_to_copy; i++) { + int16_t s = (int32_t)sine48_s16[(s_cap_tone_idx + i) % 48] * 7 / 10; + s16_buf[2 * i] = s; + s16_buf[2 * i + 1] = s; + } + s_cap_tone_idx = (s_cap_tone_idx + frames_to_copy) % 48; + + uint32_t s16_bytes = frames_to_copy * 2 * sizeof(int16_t); + struct usb_audio_ring_buffer *ring = &uad->ring; + k_spinlock_key_t key = k_spin_lock(&ring->lock); + + if (ring->count + s16_bytes > USB_AUDIO_RING_BUFFER_SIZE) { + uint32_t drop = (ring->count + s16_bytes) - USB_AUDIO_RING_BUFFER_SIZE; + drop = (drop + 3) & ~3; + if (drop > ring->count) { + drop = ring->count; + } + ring->tail = (ring->tail + drop) % USB_AUDIO_RING_BUFFER_SIZE; + ring->count -= drop; + } + + uint32_t free_space = USB_AUDIO_RING_BUFFER_SIZE - ring->count; + uint32_t to_copy = MIN(s16_bytes, free_space); + + uint8_t *s16_raw = (uint8_t *)s16_buf; + for (size_t i = 0; i < to_copy; i++) { + ring->buf[ring->head] = s16_raw[i]; + ring->head = (ring->head + 1) % USB_AUDIO_RING_BUFFER_SIZE; + } + ring->count += to_copy; + k_spin_unlock(&ring->lock, key); } } else { @@ -561,7 +638,7 @@ static int usb_audio_reset(struct comp_dev *dev) static const struct comp_driver comp_usb_audio = { .type = SOF_COMP_HOST, .uid = SOF_RT_UUID(usb_audio_uuid), - #if !CONFIG_ZEPHYR_LOG +#if !CONFIG_ZEPHYR_LOG .tctx = &usb_audio_tr, #endif .ops = { diff --git a/src/include/sof/audio/usb_audio.h b/src/include/sof/audio/usb_audio.h index 29da76bef5c7..abc1dbfbb6b1 100644 --- a/src/include/sof/audio/usb_audio.h +++ b/src/include/sof/audio/usb_audio.h @@ -18,7 +18,7 @@ 0x92, 0x76, 0x2f, 0xbb, 0x27, 0xc6, 0x2d, 0x01) #define USB_AUDIO_RING_BUFFER_SIZE 16384 -#define USB_AUDIO_PREBUFFER_BYTES 3840 /* 20ms at 48kHz stereo 16-bit */ +#define USB_AUDIO_PREBUFFER_BYTES 384 /* 2ms at 48kHz stereo 16-bit */ struct usb_audio_ring_buffer { uint8_t buf[USB_AUDIO_RING_BUFFER_SIZE]; diff --git a/src/platform/rp2350/rp2350_pipeline_def.c b/src/platform/rp2350/rp2350_pipeline_def.c index 4043c10d131d..9f1fcb96fb1f 100644 --- a/src/platform/rp2350/rp2350_pipeline_def.c +++ b/src/platform/rp2350/rp2350_pipeline_def.c @@ -77,13 +77,13 @@ static const struct sof_static_comp rp2350_comps[] = { SOF_STATIC_COMP_HOST( .id = 1, .pipeline_id = 1, .name = "USB_PB", .uuid = &usb_audio_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2), .ep.usb.terminal_id = PLAYBACK_TERM_ID ), SOF_STATIC_COMP_MODULE( .id = 2, .pipeline_id = 1, .name = "VOL_PB", .uuid = &volume_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2) ), SOF_STATIC_COMP_MODULE( .id = 3, .pipeline_id = 1, .name = "EQ_PB", @@ -104,7 +104,7 @@ static const struct sof_static_comp rp2350_comps[] = { SOF_STATIC_COMP_DAI( .id = 5, .pipeline_id = 1, .name = "DAI_I2S_PB", .uuid = &dai_uuid, .direction = SOF_IPC_STREAM_PLAYBACK, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2), .ep.dai.dai_type = SOF_DAI_ZEPHYR_I2S, .ep.dai.dai_index = 0, .ep.dai.format = SOF_DAI_FMT_I2S @@ -114,7 +114,7 @@ static const struct sof_static_comp rp2350_comps[] = { SOF_STATIC_COMP_DAI( .id = 6, .pipeline_id = 2, .name = "DAI_I2S_CAP", .uuid = &dai_uuid, .direction = SOF_IPC_STREAM_CAPTURE, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2), .ep.dai.dai_type = SOF_DAI_ZEPHYR_I2S, .ep.dai.dai_index = 0, .ep.dai.format = SOF_DAI_FMT_I2S @@ -131,12 +131,12 @@ static const struct sof_static_comp rp2350_comps[] = { SOF_STATIC_COMP_MODULE( .id = 8, .pipeline_id = 2, .name = "VOL_CAP", .uuid = &volume_uuid, .direction = SOF_IPC_STREAM_CAPTURE, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2) ), SOF_STATIC_COMP_HOST( .id = 9, .pipeline_id = 2, .name = "USB_CAP", .uuid = &usb_audio_uuid, .direction = SOF_IPC_STREAM_CAPTURE, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2), + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2), .ep.usb.terminal_id = CAPTURE_TERM_ID ), }; @@ -145,14 +145,14 @@ static const struct sof_static_comp rp2350_comps[] = { * 2. Intermediate Audio Buffers * ------------------------------------------------------------------------- */ static const struct sof_static_buffer rp2350_buffers[] = { - SOF_STATIC_BUFFER(.id = 1, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), + SOF_STATIC_BUFFER(.id = 1, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), SOF_STATIC_BUFFER(.id = 2, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), SOF_STATIC_BUFFER(.id = 3, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), - SOF_STATIC_BUFFER(.id = 4, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), + SOF_STATIC_BUFFER(.id = 4, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), - SOF_STATIC_BUFFER(.id = 5, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), + SOF_STATIC_BUFFER(.id = 5, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), SOF_STATIC_BUFFER(.id = 6, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), - SOF_STATIC_BUFFER(.id = 7, .size = 3072, .fmt = SOF_IPC_FRAME_S16_LE), + SOF_STATIC_BUFFER(.id = 7, .size = 3072, .fmt = SOF_IPC_FRAME_FLOAT), }; /* ------------------------------------------------------------------------- @@ -190,7 +190,7 @@ static const struct sof_static_pcm rp2350_pcms[] = { .direction = SOF_IPC_STREAM_PLAYBACK, .pipeline_id = 1, .host_comp_id = 1, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2) ), SOF_STATIC_PCM( .pcm_id = 1, @@ -198,7 +198,7 @@ static const struct sof_static_pcm rp2350_pcms[] = { .direction = SOF_IPC_STREAM_CAPTURE, .pipeline_id = 2, .host_comp_id = 9, - .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_S16_LE, 48000, 2) + .caps = SOF_STATIC_CAPS(SOF_IPC_FRAME_FLOAT, 48000, 2) ), }; @@ -253,7 +253,11 @@ static const struct sof_static_pipeline_desc rp2350_pipelines[] = { .time_domain = SOF_TIME_DOMAIN_TIMER, .sched_comp_id = 1, .source_comp_id = 1, +#if defined(CONFIG_RP2350_INTERFACE_PIO_I2S) + .sink_comp_id = 5, +#else .sink_comp_id = 4, +#endif }, { .pipeline_id = 2, @@ -265,7 +269,11 @@ static const struct sof_static_pipeline_desc rp2350_pipelines[] = { .frames_per_sched = 48, .time_domain = SOF_TIME_DOMAIN_TIMER, .sched_comp_id = 9, +#if defined(CONFIG_RP2350_INTERFACE_PIO_I2S) + .source_comp_id = 6, +#else .source_comp_id = 7, +#endif .sink_comp_id = 9, }, };