Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions examples/host/audio_host/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.20)

include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)

project(audio_host C CXX ASM)

# Checks this example is valid for the family and initializes the project
family_initialize_project(${PROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR})

# Espressif has its own cmake build system
if(FAMILY STREQUAL "espressif")
return()
endif()

add_executable(${PROJECT_NAME})

# Example source
target_sources(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src/audio_app.c
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
)

# Example include
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
)

# Configure compilation flags and libraries for the example without RTOS.
# See the corresponding function in hw/bsp/FAMILY/family.cmake for details.
family_configure_host_example(${PROJECT_NAME} noos)
14 changes: 14 additions & 0 deletions examples/host/audio_host/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
include ../../../hw/bsp/family_support.mk

INC += \
src \


# Example source
EXAMPLE_SOURCE += \
src/audio_app.c \
src/main.c

SRC_C += $(addprefix $(EXAMPLE_PATH)/, $(EXAMPLE_SOURCE))

include ../../../hw/bsp/family_rules.mk
98 changes: 98 additions & 0 deletions examples/host/audio_host/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# USB Audio Host Example

This example demonstrates how to use TinyUSB's USB Audio Host driver (TUH_AUDIO) to communicate with a UAC 1.0 compatible USB Audio Device.

## Features

- Enumerates and mounts USB Audio Class 1.0 devices
- Receives audio data from IN endpoint (e.g., microphone)
- Sends audio data to OUT endpoint (e.g., speaker)
- Sets sampling frequency via control requests
- Demonstrates isochronous transfer handling

## Supported Devices

This example supports any UAC 1.0 compliant USB Audio device, such as:
- USB microphones
- USB speakers/headphones
- USB audio interfaces

## Building

### Using CMake (recommended)

```bash
cd examples/host/audio_host
mkdir -p build && cd build
cmake -DBOARD=<your_board> -G Ninja ..
cmake --build .
```

Replace `<your_board>` with your target board name (e.g., `raspberry_pi_pico`, `stm32f407disco`, etc.)

### Using Make

```bash
cd examples/host/audio_host
make BOARD=<your_board> all
```

## Flashing

```bash
# Using CMake
ninja flash

# Using Make
make BOARD=<your_board> flash
```

## Usage

1. Build and flash the example to your board
2. Connect a USB Audio device (UAC 1.0) to the USB host port
3. Open a serial terminal to view output
4. The example will:
- Print device information when mounted
- Set sampling frequency based on the device's advertised capabilities
- Receive audio samples from the device (IN endpoint)
- Loop back received audio to the device (OUT endpoint) for testing

## Serial Output Example

```
TinyUSB Host USB Audio Example
Connect a USB Audio Device (UAC 1.0) to test
Audio device mounted: idx=0, daddr=1
--- Microphone ---
IN EP: 0x81 (max size: 192)
Input Terminal: ID=1, Type=0x0201, Channels=1
Format Type: 1, Channels: 1, SubFrameSize: 2, BitResolution: 16
Sampling Freq: Discrete, count=4
Freq[0]: 44100 Hz
Freq[1]: 48000 Hz
Freq[2]: 96000 Hz
Freq[3]: 192000 Hz
--- Speaker ---
OUT EP: 0x02 (max size: 192)
Output Terminal: ID=2, Type=0x0301
Format Type: 1, Channels: 2, SubFrameSize: 2, BitResolution: 16
Sampling Freq: Continuous range 8000 Hz - 48000 Hz
Feature Unit: ID=3, SourceID=1
Setting IN sampling frequency to 48000 Hz
Setting OUT sampling frequency to 48000 Hz
Sampling frequency set OK, ready for isochronous transfer
```

## Configuration

Edit `src/tusb_config.h` to modify:
- `CFG_TUH_AUDIO_MAX`: Maximum number of audio devices supported
- `CFG_TUH_AUDIO_EPIN_BUFSIZE`: IN endpoint buffer size
- `CFG_TUH_AUDIO_EPOUT_BUFSIZE`: OUT endpoint buffer size

## Notes

- This example uses isochronous transfers which require precise timing
- For production applications, synchronize audio transfers with the device's audio clock
- The example sends a simple sine wave for testing; replace with actual audio data in real applications
32 changes: 32 additions & 0 deletions examples/host/audio_host/only.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
family:hpmicro
family:samd21
family:samd5x_e5x
mcu:CH32V20X
mcu:KINETIS_KL
mcu:LPC175X_6X
mcu:LPC177X_8X
mcu:LPC18XX
mcu:LPC40XX
mcu:LPC43XX
mcu:LPC54
mcu:LPC55
mcu:MAX3421
mcu:MIMXRT10XX
mcu:MIMXRT11XX
mcu:MIMXRT1XXX
mcu:MSP432E4
mcu:RAXXX
mcu:RP2040
mcu:RW61X
mcu:RX65X
mcu:STM32C0
mcu:STM32C5
mcu:STM32F4
mcu:STM32F7
mcu:STM32G0
mcu:STM32H5
mcu:STM32H7
mcu:STM32H7RS
mcu:STM32N6
mcu:STM32U3
mcu:STM32U5
26 changes: 26 additions & 0 deletions examples/host/audio_host/src/app.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2025 TinyUSB contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/

#ifndef TUSB_TINYUSB_EXAMPLES_APP_H
#define TUSB_TINYUSB_EXAMPLES_APP_H

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

void audio_app_task(void);

#endif
Loading
Loading