I2S Master Send

This sample code demonstrates I2S data transmission function by using TX polling. In this sample, I2S is configured as the master. The chip sends data to I2S slave continuously.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P0_0 to the LRCK of the I2S slave, connect P0_1 to the BCLK of the I2S slave, and connect P0_2 to the SDI of the I2S slave.

The hardware connection of I2S sample code is shown in the figure below.

../../../_images/I2S_Master_Send_Sample_Code_Hardware_Connection_Diagram.png

I2S Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify pin definitions.

    • #define I2S_LRCK_PIN P0_0

    • #define I2S_BCLK_PIN P0_1

    • #define I2S_SDO_PIN P0_2

  2. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    i2s_master_send_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. Press the Reset button on the EVB.

  2. The I2S master device continuously transmits data, starting with the left channel at 0x1234 and the right channel at 0x5678, formatted as 16-bit data.

  3. During transmission, the data for both the left and right channels increments by 1 in a repeating sequence.

Code Overview

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\i2s\master_send\i2s_master_send_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

I2S initialization flow is shown in the following figure.

../../../_images/I2S_Master_Init_Flow.png

I2S Master Init Flow Chart

Note

As mentioned in PINMUX and PAD function descriptions, I2S PAD should be configured as software mode and pulled down when I2S is disabled to prevent PAD from floating during low power mode.

  1. Call Pad_Config() and Pinmux_Config() to initialize the pin.

    static void board_i2s_init(void)
    {
        /* set PAD_SW_MODE & PAD_PULL_DOWN when I2S disable to prevent PAD floating */
        Pad_Config(I2S_BCLK_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_LOW);
        Pad_Config(I2S_LRCK_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_LOW);
        Pad_Config(I2S_SDO_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_LOW);
    
        Pinmux_Config(I2S_BCLK_PIN, I2S_BCLK_PINMUX);
        Pinmux_Config(I2S_LRCK_PIN, I2S_LRCK_PINMUX);
        Pinmux_Config(I2S_SDO_PIN, I2S_SDO_PINMUX);
    }
    
  2. Initialize the I2S peripheral:

    1. Define the I2S_InitTypeDef type I2S_InitStruct, and call I2S_StructInit() to pre-fill I2S_InitStruct with default values.

    2. Modify the I2S_InitStruct parameters as needed. The I2S initialization parameter configuration is shown in the table below.

    3. Call I2S_Init() to initialize the I2S peripheral.

    I2S Initialization Parameters

    I2S Hardware Parameters

    Setting in the I2S_InitStruct

    I2S

    Device Role

    I2S_InitTypeDef::I2S_DeviceMode

    I2S_DeviceMode_Master

    Bit Clock Divider (Mi)

    I2S_InitTypeDef::I2S_BClockMi

    0x271

    Bit Clock Divider (Ni)

    I2S_InitTypeDef::I2S_BClockNi

    0x30

    LR Clock Divider

    I2S_InitTypeDef::I2S_BClockDiv

    0x3F

    Data Width

    I2S_InitTypeDef::I2S_TxDataWidth

    I2S_Data_Width_16Bits

    Channel Width

    I2S_InitTypeDef::I2S_TxChannelWidth

    I2S_Channel_Width_32Bits

    Channel Type

    I2S_InitTypeDef::I2S_TxChannelType

    I2S_Channel_Stereo

    Data Format

    I2S_InitTypeDef::I2S_TxDataFormat

    I2S_Mode

  3. Call I2S_Cmd() to enable I2S.

Functional Implementation

Master Send Data

The I2S master call I2S_SendData(), and send data to I2S slave repeatedly.

static void i2s_send_data(void)
{
    uint32_t pattern = 0x12345678;

    while (1)
    {
        if (I2S_GetTxFIFODepth(I2S_NUM))
        {
            /* in 16-bits format, low half word send first */
            I2S_SendData(I2S_NUM, pattern);

            /* increase high & low half word by 1 respectively */
            pattern += (BIT16 | BIT0);
        }
    }
}