Skip to content

USB host iso ring for espressif port - #11918

Closed
FoamyGuy wants to merge 1 commit into
adafruit:mainfrom
FoamyGuy:espressif_usb_host_buffer
Closed

FoamyGuy wants to merge 1 commit into
adafruit:mainfrom
FoamyGuy:espressif_usb_host_buffer

Conversation

@FoamyGuy

Copy link
Copy Markdown
Collaborator

This depends on: hathach/tinyusb#4579

It adds support for ring buffer with isochronous USB In in espressif port, similar to the way that Pico-PIO-USB handles it in the raspberry pi port.

The goal of this work is to get an example like this one that records audio from USB mic directly to an SD card working on Metro ESP32-P4

import time

import adafruit_usb_host_descriptors
import board
import digitalio
import sdioio
import storage
import usb_host
import usb_host_audio
from audiofilewriter import AudioFileWriter

RECORD_SECONDS = 60

# Mount the SD card and start the USB host port.
sd_card = sdioio.SDCard(
    clock=board.SD_CLK,
    command=board.SD_CMD,
    data=(board.SD_DATA0, board.SD_DATA1, board.SD_DATA2, board.SD_DATA3),
    frequency=40_000_000,
)
storage.mount(storage.VfsFat(sd_card), "/sd")
usb_power = digitalio.DigitalInOut(board.USB_HOST_POWER)
usb_power.switch_to_output(value=True)
usb_port = usb_host.Port(board.USB_HOST_DP, board.USB_HOST_DM)

# Find the microphone and start its stream at 48 kHz.
device = None
while device is None:
    device = adafruit_usb_host_descriptors.find_microphone()
    time.sleep(0.1)
formats = adafruit_usb_host_descriptors.find_microphone_formats(device)
mic_format = adafruit_usb_host_descriptors.choose_microphone_format(formats, sample_rate=48000)
rate = mic_format.nearest_rate(48000)
device.set_configuration()
# SET_INTERFACE starts the stream, then SET_CUR picks the sample rate.
device.ctrl_transfer(0x01, 11, mic_format.alternate, mic_format.interface, None)
if mic_format.rate_control:
    device.ctrl_transfer(0x22, 0x01, 0x0100, mic_format.endpoint, rate.to_bytes(3, "little"))
mic = usb_host_audio.USBIn(
    device,
    mic_format.endpoint,
    sample_rate=rate,
    channel_count=mic_format.channel_count,
    bit_depth=8 * mic_format.bytes_per_sample,
    samples_signed=mic_format.samples_signed,
    max_packet_size=mic_format.max_packet_size,
)
# Some microphones send silence for a second or two after they are powered.
time.sleep(2)

# The point: AudioFileWriter writing straight to a file on the card.
print("Recording", RECORD_SECONDS, "s to /sd/recording.wav")
dropouts = 0
mic.overflow  # reading it clears it
with open("/sd/recording.wav", "wb") as file:
    writer = AudioFileWriter(file)
    writer.play(mic)
    start = time.monotonic()
    while time.monotonic() - start < RECORD_SECONDS:
        # The rest of your program runs here while the recording goes on.
        if mic.overflow:  # audio from the microphone was lost
            dropouts += 1
        time.sleep(0.01)
    writer.stop()  # writes what's left and fills in the WAV header
print("Done:", dropouts, "dropouts")

There are still a few tweaks that will be needed in audiofilewriter on top of this PR and the tinyusb to get things working fully.

@FoamyGuy
FoamyGuy marked this pull request as draft November 19, 2026 16:34
@FoamyGuy

Copy link
Copy Markdown
Collaborator Author

tinyusb PR is reviewed and proposed alternative approach. Will close this and re-work based on that feedback.

@FoamyGuy FoamyGuy closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant