Tx Polling

This example demonstrates the data transmission function using the I2S peripheral in polling mode.

Connect the I2S TX pin to the Logic Analyzer to observe the waveform of the transmitted data.

Requirements

For requirements, please refer to the Requirements.

Wiring

Connect P3_2, P3_3, and P4_0 on the EVB to a Logic Analyzer.

P3_2 is LRCK, P3_3 is BCLK, and P4_0 is DATA.

Building and Downloading

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

Experimental Verification

  1. After the EVB starts, observe the I2S output waveform using a Logic Analyzer. The I2S output waveform is shown in the following figure.

Here should be I2S output waveform

I2S output waveform

Code Overview

This section introduces the code and process description for initialization and corresponding function implementation in the sample.

Source Code Directory

The directory for project file and source code are as follows:

  • Project directory: sdk\samples\peripheral\i2s\tx_polling\proj

  • Source code directory: sdk\samples\peripheral\i2s\tx_polling\src

Initialization

The initialization flow for peripherals can refer to Initialization Flow in General Introduction.

  1. Call Pad_Config() and Pinmux_Config() to configure the PAD and PINMUX of the corresponding pins.

    void board_i2s_init(void)
    {
       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_DATA_TX_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_DATA_TX_PIN, I2S_DATA_TX_PINMUX);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the I2S clock.

  3. Initialize the I2S peripheral:

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

    2. Modify the parameters of I2S_InitStruct as required. The initialization parameter configuration for I2S 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

Clock Source

I2S_InitTypeDef::I2S_ClockSource

I2S_CLK_40M

Sample Rate (Mi)

I2S_InitTypeDef::I2S_TxBClockMi

0x271

Sample Rate (Ni)

I2S_InitTypeDef::I2S_TxBClockNi

0x10

Device Mode

I2S_InitTypeDef::I2S_DeviceMode

I2S_DeviceMode_Master

Channel Type

I2S_InitTypeDef::I2S_TxChannelType

I2S_Channel_stereo

Data Width

I2S_InitTypeDef::I2S_TxDataWidth

I2S_Width_16Bits

Data Format

I2S_InitTypeDef::I2S_TxDataFormat

I2S_Mode

  1. Enable the I2S TX function.

    I2S_Cmd(I2S_NUM, I2S_MODE_TX, ENABLE);
    

Functional Implementation

Define the data to be sent, and use I2S_SendData() to continuously send the data. The data sent by I2S can be observed in the Logic Analyzer.

void i2s_senddata(void)
{
    uint32_t i = 0x12348800;

    while (1)
    {
        if (I2S_GetTxFIFOFreeLen(I2S_NUM))
        {
            /* 16bit format, lower half word send first! */
            I2S_SendData(I2S_NUM, i++);
        }
    }
}