Skip to content

XU316 Audio Interface Control Code Examples

Document Description

This document provides code examples for XU316 audio interface control, mainly including the following content:

  1. Serial communication protocol definition
  2. Audio interface configuration parameters
  3. Audio sample rate and channel configuration
  4. Volume control parameter settings

These code examples can help developers quickly implement communication functions between XU316 and MCU, as well as configure 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 parsing */
#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 byte
    uint8_t len;      // Data length
    uint8_t data[32]; // Data field
    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 buffering

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 I2S master device
// Synchronization mode configuration
#define SYNC_MODE SYNC // Synchronous mode 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 for high-resolution audio)
#define DSD_OUT_MODE DSD_OUT_MODE_ENABLE // DSD output function enabled

Audio Parameter Configuration

// Initialize audio sample 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 duration    2   0-65535 (ms)
#define MUTE_DURATION 100
// Microphone default volume
#define MIC_VOLUME 10
// DAC left channel default volume  1   See DAC volume field description
#define DAC_L_VOLUME 30
// DAC right channel default volume 1   See DAC volume field description
#define DAC_R_VOLUME 20

```