Skip to content

Code Examples for XU316 Audio Interface Control

Document Instructions

This document provides code examples related to the control of the XU316 audio interface, mainly including the following contents:

  1. Definition of serial communication protocol
  2. Configuration parameters of audio interface
  3. Configuration of audio sampling rate and channels
  4. Setting of volume control parameters

These code examples can assist developers in rapidly achieving the communication between XU316 and MCU and configuring various audio interface parameters.

Serial Communication Protocol

Protocol Definition

#define SWAP16(x) ((((x) >> 8) & 0x00FF) | (((x) << 8) & 0xFF00))
#define SWAP32(x) ((((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) | (((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000))
/*Protocol analysis*/
#define UART_FRAME_HEAD 0xAA
#define UART_FRAME_TAIL 0x55
#define HEX_UPPERCASE 0
#define HEX_LOWERCASE 1
#define HEX_TABLE HEX_UPPERCASE
// Data frame structure
typedef struct
{
    uint8_t head;     // Frame header 0xAA
    uint8_t cmd;      // Command word
    uint8_t len;      // Data length
    uint8_t data[32]; // Data area
    uint8_t check;    // Checksum
    uint8_t tail;     // Frame tail 0x55
} uart_frame_t;

Serial Buffer Management

/*Serial port*/
/* buffer sizes */
#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 256
#define TX_BUFFER_NUM 2 // Double buffer

typedef struct
{
    uint8_t buffer[TX_BUFFER_SIZE];
    uint16_t size;
    uint8_t busy;
} TX_BUFFER;

typedef struct
{
    TX_BUFFER tx_buf[TX_BUFFER_NUM];
    uint8_t curr_buf;
    uint8_t next_buf;
    uint8_t dma_sending;
} UART_TX_MANAGER;
typedef enum
{
    LOG_SEND,
    LOG_RECV,
    LOG_USER,
    LOG_ERR
} log_dir_t;

Audio Interface Configuration

Interface Mode Configuration

// I2S master - slave mode configuration
#define I2S_MODE I2S_MODE_MASTER // I2S master mode, XU316 as the I2S master device
// Synchronization mode configuration
#define SYNC_MODE SYNC // Synchronization mode, used for audio clock synchronization
// MIDI interface configuration
#define MIDI_MODE MIDI_MODE_ENABLE // MIDI function enabled
// SPDIF input interface configuration
#define SPDIF_IN_MODE SPDIF_IN_MODE_ENABLE // SPDIF input function enabled
// SPDIF output interface configuration
#define SPDIF_OUT_MODE SPDIF_OUT_MODE_ENABLE // SPDIF output function enabled
// ADAT input interface configuration (ADAT is an 8 - channel digital audio transmission format)
#define ADAT_IN_MODE ADAT_IN_MODE_ENABLE // ADAT input function enabled
// ADAT output interface configuration
#define ADAT_OUT_MODE ADAT_OUT_MODE_ENABLE // ADAT output function enabled
// DSD output interface configuration (DSD is Direct Stream Digital, used for high - resolution audio)
#define DSD_OUT_MODE DSD_OUT_MODE_ENABLE // DSD output function enabled

Audio Parameter Configuration

// Initialize audio sampling rate
#define AUDIO_SAMPLE_RATE (uint8_t)AUDIO_SAMPLE_RATE_44100
#define MQA_MODE MQA_MODE_DISABLE
#define AUDIO_CLASS AUDIO_CLASS_UAC1
#define AUDIO_WIDTH AUDIO_WIDTH_24
// Initialize audio channels
#define AUDIO_INPUT_CHANNEL 9
#define AUDIO_OUTPUT_CHANNEL 3
// Initialize audio type
#define AUDIO_MODE USB_MODE
// Mute time 2 0 - 65535 (ms)
#define MUTE_DURATION 100
// Default microphone volume
#define MIC_VOLUME 10
// Default DAC left - channel volume 1 See DAC volume field description
#define DAC_L_VOLUME 30
// Default DAC right - channel volume 1 See DAC volume field description
#define DAC_R_VOLUME 20