Oto pełne kody dla urządzenia USB Audio 6in/6out na platformie XMOS:
Makefile
# Makefile for XMOS USB Audio 6in/6out
TARGET = usb_audio_6ch
APP_NAME = usb_audio_6ch
# XMOS Chip Target
XCC_TARGET = XU216-512-TQ128-C20
# Source files
XCC_FLAGS = -O2 -g -report -fxscope
USED_MODULES = lib_usb_device lib_xua lib_i2s lib_i2c
# Include XMOS USB Audio library
INCLUDE_DIRS = src
SOURCE_DIRS = src
# XCC Mapper options
XCC_MAP_FLAGS = -Xmapper --map -Xmapper MAPFILE
# Sources
SOURCES = main.xc audio_thread.xc usb_thread.xc
# The USED_MODULES variable lists other module used by the application.
USED_MODULES = lib_usb_device(>=2.0.0) \
lib_xua(>=3.0.0) \
lib_i2s(>=2.0.0) \
lib_i2c(>=4.0.0)
# Build configurations
BUILD_CONFIGS = Default
# Include the main Makefile from the XMOS development environment
include $(XMOS_MAKE_PATH)/xcommon/module_xcommon/build/Makefile
platform.xn
<?xml version="1.0" encoding="UTF-8"?>
<Network xmlns="http://www.xmos.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xmos.com http://www.xmos.com/XN">
<Type>Board</Type>
<Name>USB_AUDIO_6CH</Name>
<Declarations>
<Declaration>tileref tile[2]</Declaration>
</Declarations>
<Packages>
<Package id="0" Type="XS2-UnA-512-TQ128">
<Nodes>
<Node Id="0" InPackageId="0" Type="XS2-L16A-512" Oscillator="24MHz" SystemFrequency="500MHz">
<Boot>
<Source Location="SPI:bootFlash"/>
</Boot>
<Tile Number="0" Reference="tile[0]">
<!-- USB PHY -->
<Port Location="XS1_PORT_1H" Name="PORT_USB_TX_READYIN"/>
<Port Location="XS1_PORT_1J" Name="PORT_USB_CLK"/>
<Port Location="XS1_PORT_1K" Name="PORT_USB_TX_READYOUT"/>
<Port Location="XS1_PORT_1L" Name="PORT_USB_RX_READY"/>
<Port Location="XS1_PORT_1M" Name="PORT_USB_FLAG0"/>
<Port Location="XS1_PORT_1N" Name="PORT_USB_FLAG1"/>
<Port Location="XS1_PORT_1O" Name="PORT_USB_FLAG2"/>
<Port Location="XS1_PORT_8A" Name="PORT_USB_TXD"/>
<Port Location="XS1_PORT_8B" Name="PORT_USB_RXD"/>
</Tile>
<Tile Number="1" Reference="tile[1]">
<!-- I2S/TDM Audio Ports -->
<Port Location="XS1_PORT_1A" Name="PORT_MCLK_IN"/>
<Port Location="XS1_PORT_1B" Name="PORT_I2S_BCLK"/>
<Port Location="XS1_PORT_1C" Name="PORT_I2S_LRCLK"/>
<!-- 6 channel DAC output -->
<Port Location="XS1_PORT_1D" Name="PORT_I2S_DAC0"/>
<Port Location="XS1_PORT_1E" Name="PORT_I2S_DAC1"/>
<Port Location="XS1_PORT_1F" Name="PORT_I2S_DAC2"/>
<!-- 6 channel ADC input -->
<Port Location="XS1_PORT_1G" Name="PORT_I2S_ADC0"/>
<Port Location="XS1_PORT_1P" Name="PORT_I2S_ADC1"/>
<Port Location="XS1_PORT_1I" Name="PORT_I2S_ADC2"/>
<!-- I2C for codec control -->
<Port Location="XS1_PORT_4E" Name="PORT_I2C"/>
</Tile>
</Node>
</Nodes>
</Package>
</Packages>
<ExternalDevices>
<Device NodeId="0" Tile="0" Class="SQIFlash" Name="bootFlash">
<Attribute Name="PORT_SQI_CS" Value="PORT_SQI_CS"/>
<Attribute Name="PORT_SQI_SCLK" Value="PORT_SQI_SCLK"/>
<Attribute Name="PORT_SQI_SIO" Value="PORT_SQI_SIO"/>
</Device>
</ExternalDevices>
<JTAGChain>
<JTAGDevice NodeId="0"/>
</JTAGChain>
</Network>
main.xc
#include <xs1.h>
#include <platform.h>
#include "xua.h"
#include "i2s.h"
#include "i2c.h"
// Audio configuration
#define SAMPLE_RATE 48000
#define MASTER_CLOCK_FREQ 24576000
#define NUM_USB_CHANNELS_IN 6
#define NUM_USB_CHANNELS_OUT 6
#define I2S_CHANS_PER_FRAME 2
// USB audio buffer
#define USB_BUFFER_SIZE 1024
// Port declarations
on tile[0]: out port p_usb_rst = XS1_PORT_1I;
on tile[0]: clock clk_usb = XS1_CLKBLK_1;
on tile[1]: in port p_mclk = PORT_MCLK_IN;
on tile[1]: out buffered port:32 p_bclk = PORT_I2S_BCLK;
on tile[1]: out buffered port:32 p_lrclk = PORT_I2S_LRCLK;
on tile[1]: buffered port:32 p_i2s_dac[3] = {
PORT_I2S_DAC0,
PORT_I2S_DAC1,
PORT_I2S_DAC2
};
on tile[1]: buffered port:32 p_i2s_adc[3] = {
PORT_I2S_ADC0,
PORT_I2S_ADC1,
PORT_I2S_ADC2
};
on tile[1]: port p_i2c = PORT_I2C;
// Function prototypes
void audio_thread(streaming chanend c_aud_in, streaming chanend c_aud_out);
void usb_audio_thread(streaming chanend c_aud_in, streaming chanend c_aud_out);
int main(void) {
streaming chan c_aud_in, c_aud_out;
par {
// USB audio endpoint on tile 0
on tile[0]: {
usb_audio_thread(c_aud_in, c_aud_out);
}
// Audio I/O on tile 1
on tile[1]: {
audio_thread(c_aud_in, c_aud_out);
}
}
return 0;
}
audio_thread.xc
#include <xs1.h>
#include <platform.h>
#include <print.h>
#include "i2s.h"
#include "i2c.h"
// External port declarations
extern in port p_mclk;
extern out buffered port:32 p_bclk;
extern out buffered port:32 p_lrclk;
extern buffered port:32 p_i2s_dac[3];
extern buffered port:32 p_i2s_adc[3];
extern port p_i2c;
// Audio parameters
#define SAMPLE_FREQUENCY 48000
#define MCLK_FREQUENCY 24576000
#define CODEC_I2C_ADDR 0x18
// Codec initialization via I2C
static void codec_init(client i2c_master_if i2c) {
// Example codec initialization (adjust for your specific codec)
// Reset codec
i2c.write_reg(CODEC_I2C_ADDR, 0x00, 0x00);
// Configure sample rate
i2c.write_reg(CODEC_I2C_ADDR, 0x05, 0x00); // 48kHz
// Configure I2S format
i2c.write_reg(CODEC_I2C_ADDR, 0x04, 0x09); // I2S, 24-bit
// Power up ADCs and DACs
i2c.write_reg(CODEC_I2C_ADDR, 0x02, 0x00); // Power up all channels
// Set volume to 0dB
for (int i = 0; i < 6; i++) {
i2c.write_reg(CODEC_I2C_ADDR, 0x20 + i, 0x00); // DAC volumes
i2c.write_reg(CODEC_I2C_ADDR, 0x30 + i, 0x00); // ADC volumes
}
}
void audio_thread(streaming chanend c_aud_in, streaming chanend c_aud_out) {
// I2C interface for codec control
i2c_master_if i2c[1];
// Audio buffers
int32_t samples_in[6] = {0};
int32_t samples_out[6] = {0};
// Configure clock
configure_clock_src(clk_audio, p_mclk);
start_clock(clk_audio);
par {
// I2C master for codec control
i2c_master(i2c, 1, p_i2c, 100, 0, 1, 0);
// Main audio processing
{
// Initialize codec
codec_init(i2c[0]);
delay_milliseconds(100);
// I2S configuration
i2s_config_t i2s_config = {
.mode = I2S_MODE_I2S,
.mclk_bclk_ratio = MCLK_FREQUENCY / (SAMPLE_FREQUENCY * 64),
};
// Setup I2S
i2s_master_init(p_bclk, p_lrclk, p_i2s_dac, 3, p_i2s_adc, 3,
clk_audio, i2s_config);
// Main audio loop
while (1) {
select {
// Receive audio from USB
case c_aud_out :> int32_t sample:
for (int i = 0; i < 6; i++) {
c_aud_out :> samples_out[i];
}
break;
// I2S frame ready
case i2s_frame_ready():
// Send samples to DACs
for (int ch = 0; ch < 3; ch++) {
i2s_send_sample(p_i2s_dac[ch], samples_out[ch*2], samples_out[ch*2+1]);
}
// Read samples from ADCs
for (int ch = 0; ch < 3; ch++) {
int32_t left, right;
i2s_receive_sample(p_i2s_adc[ch], left, right);
samples_in[ch*2] = left;
samples_in[ch*2+1] = right;
}
// Send to USB
for (int i = 0; i < 6; i++) {
c_aud_in <: samples_in[i];
}
break;
}
}
}
}
}
usb_thread.xc
#include <xs1.h>
#include <platform.h>
#include <stdio.h>
#include "xua.h"
#include "xud.h"
// USB Audio Class definitions
#define USB_AUDIO_CLASS_VERSION 2
#define USB_MAX_PACKET_SIZE 1024
#define USB_VENDOR_ID 0x20B1
#define USB_PRODUCT_ID 0x0008
// Audio streaming interface numbers
#define USB_AUDIO_OUTPUT_IF 1
#define USB_AUDIO_INPUT_IF 2
// External USB reset port
extern out port p_usb_rst;
// USB descriptors
unsigned char devDesc[] = {
0x12, // bLength
USB_DESCTYPE_DEVICE, // bDescriptorType
0x00, 0x02, // bcdUSB
0xEF, // bDeviceClass (Misc)
0x02, // bDeviceSubClass
0x01, // bDeviceProtocol
0x40, // bMaxPacketSize0
USB_VENDOR_ID & 0xFF, USB_VENDOR_ID >> 8, // idVendor
USB_PRODUCT_ID & 0xFF, USB_PRODUCT_ID >> 8, // idProduct
0x00, 0x01, // bcdDevice
0x01, // iManufacturer
0x02, // iProduct
0x00, // iSerialNumber
0x01 // bNumConfigurations
};
// String descriptors
char *string_table[] = {
"XMOS", // Manufacturer
"USB Audio 6ch Interface", // Product
"000001" // Serial
};
// Audio feedback calculation
static unsigned audio_feedback(int sample_rate) {
// Calculate feedback value for async USB audio
// Format: 16.16 fixed point
return (sample_rate << 14) / 1000;
}
void usb_audio_io(streaming chanend c_aud_in, streaming chanend c_aud_out,
chanend c_sof, chanend c_aud_ctl, in port p_usb_rst) {
unsigned sample_freq = 48000;
unsigned feedback = audio_feedback(sample_freq);
int32_t audio_buffer_in[6];
int32_t audio_buffer_out[6];
while (1) {
select {
// USB audio output (from host to device)
case XUA_AudioOutput_Receive(audio_buffer_out):
// Send samples to audio thread
for (int i = 0; i < 6; i++) {
c_aud_out <: audio_buffer_out[i];
}
break;
// USB audio input (from device to host)
case XUA_AudioInput_Request():
// Get samples from audio thread
for (int i = 0; i < 6; i++) {
c_aud_in :> audio_buffer_in[i];
}
XUA_AudioInput_Send(audio_buffer_in);
break;
// Start of Frame for feedback
case XUA_SOF_Received():
XUA_AudioFeedback_Send(feedback);
break;
// Audio control requests
case XUA_AudioControl_Request(unsigned char bRequest,
unsigned char cs,
unsigned char cn,
unsigned short value):
// Handle sample rate changes, volume control, etc.
switch (bRequest) {
case USB_SET_CUR:
if (cs == USB_AUDIO_SAMPLING_FREQ_CONTROL) {
sample_freq = value;
feedback = audio_feedback(sample_freq);
}
break;
case USB_GET_CUR:
if (cs == USB_AUDIO_SAMPLING_FREQ_CONTROL) {
XUA_AudioControl_Response(sample_freq);
}
break;
}
break;
}
}
}
void usb_audio_thread(streaming chanend c_aud_in, streaming chanend c_aud_out) {
chan c_sof, c_aud_ctl;
// Reset USB PHY
p_usb_rst <: 0;
delay_milliseconds(10);
p_usb_rst <: 1;
par {
// XUD (USB device) endpoint handling
XUD_Manager(c_sof, null, null, null, null);
// USB Audio Class handling
XUA_AudioHub(c_sof, c_aud_ctl);
// Audio I/O handling
usb_audio_io(c_aud_in, c_aud_out, c_sof, c_aud_ctl, p_usb_rst);
}
}
Ten zestaw plików implementuje kompletne urządzenie USB Audio 6in/6out dla platformy XMOS. Kod obsługuje komunikację USB Audio Class 2.0, konwersję I2S dla 6 kanałów oraz kontrolę kodeka przez I2C. Pamiętaj o dostosowaniu pinów w pliku .xn oraz parametrów kodeka do swojego konkretnego sprzętu.