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
The following macros can be configured to modify pin definitions.
#define IR_SEND_PIN P2_2
The following macros can be configured to modify the TX threshold.
#define IR_TX_FIFO_THR_LEVEL 4
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.

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.

IR TX Mode Initialization Flow Chart
Call
Pad_Config()
andPinmux_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); }
Call
RCC_PeriphClockCmd()
to enable the IR clock and function.Initialize the IR peripheral:
Define the
IR_InitTypeDef
typeIR_InitStruct
, and callIR_StructInit()
to pre-fillIR_InitStruct
with default values.Modify the
IR_InitStruct
parameters as needed. The IR initialization parameter configuration is shown in the table below.Call
IR_Init()
to initialize the IR peripheral.
IR Initialization Parameters IR Hardware Parameters
Setting in the
IR_InitStruct
IR
IR Clock Frequency
38
IR Duty Cycle
2
IR Mode
IR TX Inverse
IR TX Output Level in Idle
IR Tx Threshold
IR_TX_FIFO_THR_LEVEL
Functional Implementation
Send Data
Call
IR_ClearTxFIFO()
to clear IR TX FIFO.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.Call
IR_Cmd()
to enable IR peripheral.If there is data to be sent:
Call
IR_GetFlagStatus()
to checkIR_FLAG_TX_RUN
flag state and wait forIR_FLAG_TX_RUN
flag to be set, and callIR_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 ofIR_TX_FIFO_SIZE
minusIR_TX_FIFO_THR_LEVEL
.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 ofIR_TX_FIFO_SIZE
minusIR_TX_FIFO_THR_LEVEL
.
If all data has been sent:
Call
IR_GetFlagStatus()
to checkIR_FLAG_TF_EMPTY
flag state andIR_FLAG_TX_RUN
flag state, and wait forIR_FLAG_TF_EMPTY
flag to be reset orIR_FLAG_TX_RUN
flag to be set.Call
IR_Cmd()
to disable IR peripheral.