IR TX Data in Polling Mode

This sample code guide is designed to help users easily and comprehensively understand IR sample. The IR peripheral is used to transmit a large amount of data by polling mode to achieve infrared transmission functionality. A logic analyzer can be used to observe the IR transmission waveform.

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P2_2 to channel 0 of the logic analyzer.

Configurations

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

    • #define IR_SEND_PIN                     P2_2

  2. The following macros can be configured to modify the TX threshold.

    • #define IR_TX_FIFO_THR_LEVEL            4

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

    ir_send_polling_mode_demo();
    

Building and Downloading

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

Experimental Verification

Observe the IR TX waveform within the logic analyzer. The expected result of IR sample code is shown in the figure below.

../../../_images/IR_Demo_1_Expected_Result_Diagram.png

IR Sample Code Expected Result Diagram

Code Overview

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

Source Code Directory

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

  • Source code directory: sdk\src\sample\io_demo\ir\send\ir_send_polling_mode_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

IR initialization flow is shown in the following figure.

../../../_images/IR_Polling_Initialization_Flow_Chart.png

IR TX Mode Initialization Flow Chart

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

    static void board_ir_init(void)
    {
       Pad_Config(IR_SEND_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW);
       Pinmux_Config(IR_SEND_PIN, IRDA_TX);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the IR clock and function.

  3. Initialize the IR peripheral:

    1. Define the IR_InitTypeDef type IR_InitStruct, and call IR_StructInit() to pre-fill IR_InitStruct with default values.

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

    3. Call IR_Init() to initialize the IR peripheral.

    IR Initialization Parameters

    IR Hardware Parameters

    Setting in the IR_InitStruct

    IR

    IR Clock Frequency

    IR_InitTypeDef::IR_Freq

    38

    IR Duty Cycle

    IR_InitTypeDef::IR_DutyCycle

    2

    IR Mode

    IR_InitTypeDef::IR_Mode

    IR_MODE_TX

    IR TX Inverse

    IR_InitTypeDef::IR_TxInverse

    IR_TX_DATA_NORMAL

    IR TX Output Level in Idle

    IR_InitTypeDef::IR_TxIdleLevel

    IR_IDLE_OUTPUT_HIGH

    IR Tx Threshold

    IR_InitTypeDef::IR_TxFIFOThrLevel

    IR_TX_FIFO_THR_LEVEL

Functional Implementation

Send Data

  1. Call IR_ClearTxFIFO() to clear IR TX FIFO.

  2. Call IR_SendBuf() to continuously write data to the TX FIFO. The number of data continuously written to the TX FIFO must not exceed the size of the TX FIFO.

  3. Call IR_Cmd() to enable IR peripheral.

  4. If there is data to be sent:

    1. Call IR_GetFlagStatus() to check IR_FLAG_TX_RUN flag state and wait for IR_FLAG_TX_RUN flag to be set, and call IR_GetTxFIFOFreeLen() to get free size of TX FIFO and wait for free size of TX FIFO to be greater than or equal to the value of IR_TX_FIFO_SIZE minus IR_TX_FIFO_THR_LEVEL.

    2. Call IR_SendBuf() to continuously write data to the TX FIFO. The number of data continuously written to the TX FIFO must not exceed the value of IR_TX_FIFO_SIZE minus IR_TX_FIFO_THR_LEVEL.

  5. If all data has been sent:

    1. Call IR_GetFlagStatus() to check IR_FLAG_TF_EMPTY flag state and IR_FLAG_TX_RUN flag state, and wait for IR_FLAG_TF_EMPTY flag to be reset or IR_FLAG_TX_RUN flag to be set.

    2. Call IR_Cmd() to disable IR peripheral.