IR GDMA Transmit

This sample uses the IR peripheral to send data by utilizing GDMA, achieving the data transmission function.

The GDMA will transfer predefined IR data from memory to the IR TX FIFO, and upon completion, it will trigger a GDMA interrupt.

A logic analyzer can be used to observe whether the IR transmitted data meets expectations.

Requirements

For requirements, please refer to the Requirements.

Wiring

Connect the IR transmit pin P2_5 to the logic analyzer.

Configurations

  1. The following macro can be configured to modify the pin definitions.

    #define IR_TX_PIN                       P2_5
    
  2. The following macros can be configured to modify the GDMA channel settings.

    #define IO_TEST_GDMA_CHANNEL_MUM        1           /*< Set this macro to select the GDMA Channel num. */
    
    // Set the following macros to modify the GDMA Channel configurations.
    #if (IO_TEST_GDMA_CHANNEL_MUM == 0)
    #define IO_TEST_GDMA_Channel                GDMA_Channel0
    #define IO_TEST_GDMA_Channel_IRQn           GDMA0_Channel0_IRQn
    #define IO_TEST_GDMA_Channel_Handler        GDMA0_Channel0_Handler
    #elif IO_TEST_GDMA_CHANNEL_MUM == 1
    #define IO_TEST_GDMA_Channel                GDMA_Channel1
    #define IO_TEST_GDMA_Channel_IRQn           GDMA0_Channel1_IRQn
    #define IO_TEST_GDMA_Channel_Handler        GDMA0_Channel1_Handler
    #elif IO_TEST_GDMA_CHANNEL_MUM == 2
    #define IO_TEST_GDMA_Channel                GDMA_Channel2
    #define IO_TEST_GDMA_Channel_IRQn           GDMA0_Channel2_IRQn
    #define IO_TEST_GDMA_Channel_Handler        GDMA0_Channel2_Handler
    #elif IO_TEST_GDMA_CHANNEL_MUM == 3
    #define IO_TEST_GDMA_Channel                GDMA_Channel3
    #define IO_TEST_GDMA_Channel_IRQn           GDMA0_Channel3_IRQn
    #define IO_TEST_GDMA_Channel_Handler        GDMA0_Channel3_Handler
    #endif
    
  3. The following macros can be configured to modify the GDMA data transfer length.

    #define IO_TEST_GDMA_TRANSFER_SIZE          IR_DATA_SIZE_MAX                       /*< Set this macro to modify the transfer size. */
    #define IR_DATA_SIZE_MAX                    68
    

Building and Downloading

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

Experimental Verification

Observe the IR transmit waveform with a logic analyzer.

Here should be a picture of the IR sending waveforms

IR Transmit 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\ir\tx+gdma\proj

  • Source code directory: sdk\samples\peripheral\ir\tx+gdma\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_ir_init(void)
    {
        Pad_Config(IR_TX_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_ENABLE, PAD_OUT_LOW);
    
        Pinmux_Config(IR_TX_PIN, IRDA_TX);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the IR clock.

  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

Sample Clock

IR_InitTypeDef::IR_Freq

38000

IR Duty Cycle

IR_InitTypeDef::IR_DutyCycle

3

IR Mode

IR_InitTypeDef::IR_Mode

IR_MODE_TX

IR Tx GDMA Enable

IR_InitTypeDef::IR_TxDmaEn

ENABLE

IR Tx Waterlevel

IR_InitTypeDef::IR_TxWaterLevel

15

  1. Call RCC_PeriphClockCmd() to enable the IR clock.

  2. Initialize the GDMA peripheral:

    1. Define a GDMA_InitTypeDef type GDMA_InitStruct, and call GDMA_StructInit() to pre-fill GDMA_InitStruct with default values.

    2. Modify the GDMA_InitStruct parameters as needed. The initialization parameters for the GDMA channel are configured as shown in the table below. Call GDMA_Init() to initialize the GDMA peripheral.

    3. Configure the GDMA total transfer complete interrupt GDMA_INT_Transfer and NVIC. For NVIC configurations, refer to Interrupt Configuration.

GDMA Initialization Parameters

GDMA Hardware Parameters

Setting in the GDMA_InitStruct

GDMA Channel

Channel Num

GDMA_InitTypeDef::GDMA_ChannelNum

1

Transfer Direction

GDMA_InitTypeDef::GDMA_DIR

GDMA_DIR_MemoryToPeripheral

Buffer Size

GDMA_InitTypeDef::GDMA_BufferSize

IO_TEST_GDMA_TRANSFER_SIZE

Source Address Increment or Decrement

GDMA_InitTypeDef::GDMA_SourceInc

DMA_SourceInc_Inc

Destination Address Increment or Decrement

GDMA_InitTypeDef::GDMA_DestinationInc

DMA_DestinationInc_Fix

Source Data Size

GDMA_InitTypeDef::GDMA_SourceDataSize

GDMA_DataSize_Word

Destination Data Size

GDMA_InitTypeDef::GDMA_DestinationDataSize

GDMA_DataSize_Word

Source Burst Transaction Length

GDMA_InitTypeDef::GDMA_SourceMsize

GDMA_Msize_1

Destination Burst Transaction Length

GDMA_InitTypeDef::GDMA_DestinationMsize

GDMA_Msize_1

Source Address

GDMA_InitTypeDef::GDMA_SourceAddr

GDMA_Send_Buf

Destination Address

GDMA_InitTypeDef::GDMA_DestinationAddr

(&IR->IR_TX_FIFO)

Destination Handshake

GDMA_InitTypeDef::GDMA_DestHandshake

GDMA_Handshake_IR_TX

Functional Implementation

  1. Define IR send data array: carrier data is represented by the number of carriers or 0x80000000, no carrier data is represented by the number of carriers or 0x00000000.

  2. Call GDMA_Cmd() to enable the corresponding GDMA channel transmission. Call IR_Cmd() to enable the IR peripheral transmission function. GDMA transfers data from GDMA_Send_Buf to (&IR->IR_TX_FIFO).

    IR_Send_Data.CarrierFreq = 38000;
    IR_Send_Data.DataLen = IO_TEST_GDMA_TRANSFER_SIZE;
    IR_Send_Data.DataBuf[0] =  0x80000000 | 0x200;
    IR_Send_Data.DataBuf[1] =  0x00000000 | 0x100;
    ...
    IR_Send_Data.DataBuf[IR_Send_Data.DataLen - 1] =  0x80000000 | 0x800;
    
    /* Test data buffer */
    for (uint32_t i = 0; i < IO_TEST_GDMA_TRANSFER_SIZE; i++)
    {
        GDMA_Send_Buf[i] = IR_Send_Data.DataBuf[i];
    }
    ...
    IR_Cmd(IR_MODE_TX, ENABLE);
    
  3. When the GDMA completes data transfer, trigger the GDMA_INT_Transfer interrupt. Within the interrupt function, print the relevant information, disable the GDMA transfer, and clear the interrupt flag.

    void IO_TEST_GDMA_Channel_Handler(void)
    {
        GDMA_INTConfig(IO_TEST_GDMA_CHANNEL_MUM, GDMA_INT_Transfer, DISABLE);
        GDMA_Cmd(IO_TEST_GDMA_CHANNEL_MUM, DISABLE);
        DBG_DIRECT("IO_TEST_GDMA_Channel_Handler\r\n");
        GDMA_ClearINTPendingBit(IO_TEST_GDMA_CHANNEL_MUM, GDMA_INT_Transfer);
    }
    

See Also

Please refer to the relevant API Reference: