IR TX Data in Interrupt 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 interrupt 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            2

  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_interrupt_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_2_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_interrupt_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

IR initialization flow is shown in the following figure.

../../../_images/IR_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_LOW

    IR Tx Threshold

    IR_InitTypeDef::IR_TxFIFOThrLevel

    IR_TX_FIFO_THR_LEVEL

  4. Call NVIC_Init() to enable NVIC of IR.

Functional Implementation

Send Data

IR send data operation flow is shown in the following figure.

../../../_images/IR_Send_Data_Operation_Flow_Chart.png

IR Send Data Operation Flow Chart

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

  2. Call IR_NECEncode to encode the data by the NEC protocol.

  3. Call IR_SendBuf() to continuously write data to the TX FIFO. The number of data continuously written to the TX FIFO is the size of the TX FIFO.

  4. Call IR_INTConfig() to enable the IR transmit FIFO to reach threshold interrupt IR_INT_TF_LEVEL.

  5. Call IR_Cmd() to enable IR peripheral.

Interrupt Handle

IR interrupt handle flow is shown in the following figure.

../../../_images/IR_send_flow_in_interrupt_handler.png

IR Interrupt Handle Flow Chart

When the number of IR transmit FIFO data is less than the set transmission threshold, it triggers the IR_INT_TF_LEVEL interrupt and enters the interrupt handling function.

  1. Call IR_MaskINTConfig() to mask the IR transmit FIFO to reach threshold interrupt IR_INT_TF_LEVEL.

  2. If the amount of remaining data is greater than or equal to the TX FIFO length:

    1. Call IR_SendBuf() to continuously write data to the TX FIFO. The number of data continuously written to the TX FIFO is the value of IR_TX_FIFO_SIZE minus IR_TX_FIFO_THR_LEVEL.

  3. If the amount of remaining data is less than the TX FIFO length:

    1. Call IR_SetTxThreshold() to set TX threshold to 0.

    2. Call IR_SendBuf() to continuously write data to the TX FIFO.

  4. If all data has been sent:

    1. Call IR_INTConfig() to disable the IR transmit FIFO to reach threshold interrupt IR_INT_TF_LEVEL.

    2. Send a message to the task, and after the task receives the message:

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

      2. Call IR_Cmd() to disable IR peripheral.

  5. Call IR_ClearINTPendingBit() to clear the IR transmit FIFO to reach threshold interrupt IR_INT_TF_LEVEL_CLR.

  6. Call IR_MaskINTConfig() to unmask the IR transmit FIFO to reach threshold interrupt IR_INT_TF_LEVEL.