How to Implement an USB 2.0 / USB 3.0 Gadget Audio

This article provides how to use the USB Gadget Audio on SP7350 board which makes the board as an audio card.

Table of Contents

Kernel Setup

Enter the kernel configuration by running the following command in the top directory of project.

# make kconfig

 

  1. Select submenus for PCM : Device Drivers > Sound card support > Advances Linux Sound Architecture. Select [*] for “PCM timer interface“.

image-20240126-104828.png

 

  1. Select submenus for supporting USB Audio/MIDI devices : Device Drivers > Sound card support > Advances Linux Sound Architecture > USB sound devices. Select <*> for “USB Audio/MIDI driver“.

image-20240126-104907.png

 

  1. Select submenus for USB 2.0 controller : Device Drivers > USB support > USB Gadget Support > USB Peripheral Controller. Select <*> for “Sunplus USB 2.0 Device Controller“.

image-20240126-110509.png

 

  1. Select submenus for USB 3.0 controller : Device Drivers > USB support. Select <*> for “DesignWare USB3 DRD Core Support“ and select “Dual Role mode“ for “Deware USB3 DRD Core Support“.

image-20240130-021958.png

 

  1. Select submenus for USB Gadget configfs : Device Drivers > USB support > USB Gadget Support. Select <*> for “USB Gadget functions configurable through configfs“ and [*] for “Audio Class 1.0“ and “Audio Class 2.0“.

image-20240814-025211.png

 

ConfigFS

Configure the UAC gadget through configfs saved as a file named setup_uac. It configures USB 2.0 as an UAC 1.0 gadget by default.

#!/bin/sh # USB2 or USB3 AUDIO_EN=USB2 # 1.0 or 2.0 AUDIO_CLASS_VERSION=2.0 CONFIGFS_HOME=/sys/kernel/config VID=0xabcd PID=0x1234 serial_number="myserial" manufacturer="mymfg" product="myproduct" name="c" number="1" maxpower="500" if [ $AUDIO_CLASS_VERSION = 1.0 ] then configuration="uac1" elif [ $AUDIO_CLASS_VERSION = 2.0 ] then configuration="uac2" fi CONFIG="configs/$name.$number" if [ $AUDIO_CLASS_VERSION = 1.0 ] then FUNCTION="uac1.0" elif [ $AUDIO_CLASS_VERSION = 2.0 ] then FUNCTION="uac2.0" fi if [ $AUDIO_EN = USB2 ] then UDC_NAME="f8102800.usb" # disable debug message echo 0 > /sys/module/sunplus_udc/parameters/dmsg elif [ $AUDIO_EN = USB3 ] then UDC_NAME="f80a1000.dwc3" fi modprobe libcomposite mount -t configfs none $CONFIGFS_HOME mkdir -p $CONFIGFS_HOME/usb_gadget/g1 cd $CONFIGFS_HOME/usb_gadget/g1 echo $VID > idVendor echo $PID > idProduct mkdir -p strings/0x409 echo $serial_number > strings/0x409/serialnumber echo $manufacturer > strings/0x409/manufacturer echo $product > strings/0x409/product mkdir -p configs/$name.$number echo $maxpower > configs/$name.$number/MaxPower mkdir -p configs/$name.$number/strings/0x409 echo $configuration > configs/$name.$number/strings/0x409/configuration mkdir -p functions/$FUNCTION echo 0x3 > functions/$FUNCTION/p_chmask echo 48000 > functions/$FUNCTION/p_srate echo 2 > functions/$FUNCTION/p_ssize echo 0x3 > functions/$FUNCTION/c_chmask echo 48000 > functions/$FUNCTION/c_srate echo 2 > functions/$FUNCTION/c_ssize ln -s functions/$FUNCTION configs/$name.$number echo $UDC_NAME > UDC

 

Test Configurations for USB 2.0 Gadget Audio

  1. Use an USB-A to Micro-USB cable to connect the Ubuntu PC and the SP7350 platform.

  2. Plug an USB sound card to the USB 3.0 Type C port of the SP7350 platform and plug a earphone to the sound card.

image-20240814-053205.png

 

  1. Use sudo sh setup_uac command to configure UDC (USB 2.0) as an UAC 1.0 gadget.

sunplus@ubuntu:~$ sudo sh setup_uac [sudo] password for sunplus: sunplus@ubuntu:~$

 

  1. Use cat /proc/asound/cards command to show all sound cards of the SP7350 platform. The sound cards of 1 [UAC1Gadget] and 2 [Device] will be used for the test.

sunplus@ubuntu:~$ cat /proc/asound/cards 0 [spaud ]: sp-aud - sp-aud Q645/Q654, Sunplus Technology Inc. 1 [UAC1Gadget ]: UAC1_Gadget - UAC1_Gadget UAC1_Gadget 0 2 [Device ]: USB-Audio - USB Audio Device C-Media Electronics Inc. USB Audio Device at usb-xhci-hcd.1.auto-1, full speed sunplus@ubuntu:~$

 

In the Ubuntu PC, “Digital Output (S/PDIF) - UDisk flash drive“ and “Analog Output - UDisk flash drive“ will show up in “Settings“ → “Sound” → “Output” → “Output Device”. “Digital Input (S/PDIF) - UDisk flash drive“ and “Analog Input - UDisk flash drive“ will show up in “Settings“ → “Sound” → “Input” → “Input Device”.

image-20240814-064953.png
image-20240814-065724.png

 

Test for USB 2.0 Gadget Audio

The command-line audio player (aplay) and soundfile recorder (arecord) are used for the test.

Record and Play

image-20240814-072307.png

 

Play music and choose “Digital Output (S/PDIF) - UDisk flash drive“ or “Analog Output - UDisk flash drive“ in the Ubuntu PC. Then use arecord -D hw:1,0 -c 2 -r 48000 -f S16_LE | aplay -D hw:2,0 -c 2 -r 48000 -f S16_LE command to record the music from the UAC gadget and play it to the earphone of the USB sound card in USB 3.0 type C port in the SP7350 platform.

sunplus@ubuntu:~$ arecord -D hw:1,0 -c 2 -r 48000 -f S16_LE | aplay -D hw:2,0 -c 2 -r 48000 -f S16_LE Recording WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo Playing WAVE 'stdin' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo

 

Record Channel

image-20240814-074832.png

 

Play music and choose “Digital Output (S/PDIF) - UDisk flash drive“ or “Analog Output - UDisk flash drive“ in the Ubuntu PC. Then use arecord -D hw:1,0 -c 2 -r 48000 -f S16_LE pc_music.wav command to record it as pc_music.wav in the UAC gadget.

sunplus@ubuntu:~$ arecord -D hw:1,0 -c 2 -r 48000 -f S16_LE pc_music.wav Recording WAVE 'pc_music.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo

 

Play Channel

image-20240814-075034.png

 

Use aplay -D hw:1,0 -c 2 -r 48000 -f S16_LE pc_music.wav command to play pc_music.wav in the SP7350 platform. Then choose “Digital Input (S/PDIF) - UDisk flash drive“ or “Analog Input - UDisk flash drive“ and record it in the Ubuntu PC.

sunplus@ubuntu:~$ aplay -D hw:1,0 -c 2 -r 48000 -f S16_LE pc_music.wav Playing WAVE 'pc_music.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo