UART RX Data in GDMA Mode

This sample code guide is designed to help users easily and comprehensively understand UART sample. This sample demonstrates how UART receives data in GDMA mode. This sample code demonstrates the communication between chip and PC terminal. PC terminal transmits some data to chip.

Requirements

For hardware requirements, please refer to the Requirements.

In addition, it is necessary to install serial port assistant tools such as PuTTY or UartAssist on the PC terminal.

Wiring

Connect P3_1 (UART TX Pin) to the RX pin of the FT232 and P3_0 (UART RX Pin) to the TX pin of the FT232.

The hardware connection of UART sample code is shown in the figure below.

../../../_images/UART_Sample_Code_Hardware_Connection_Diagram.png

UART Sample Code Hardware Connection Diagram

Configurations

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

    • #define UART_TX_PIN         P3_1

    • #define UART_RX_PIN         P3_0

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

    uart_rx_dma();
    

Building and Downloading

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

Experimental Verification

Preparation Phase

Start a PC terminal program like PuTTY or UartAssist and connect to the used COM port with the following UART settings:

  • Baud rate: 3000000.

  • 8 data bits.

  • 1 stop bit.

  • No parity.

  • No hardware flow control.

Testing Phase

  1. Press the Reset button on the EVB, chip starts with transmitting ### Welcome to use RealTek Bumblebee ###rn. Observe that the string appears on the PC terminal program.

  2. Use PC terminal program to send data to chip. After the chip receives the data, it will print the amount of received data in Debug Analyzer and the data will be stored in array uart_receive_buf.

    data_uart_handler: rx_count xx
    

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\gdma\uart_idle_rx\uart_rx_dma.c.

UART Initialization

The initialization flow for peripherals can refer to Initialization Flow.

UART initialization flow is shown in the following figure.

../../../_images/UART_RX_Interrupt_Initialization_Flow_Chart.png

UART Initialization Flow Chart

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

    static void board_dma_uart_init(void)
    {
       Pad_Config(UART_TX_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_NONE, PAD_OUT_DISABLE, PAD_OUT_LOW);
       Pad_Config(UART_RX_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_LOW);
    
       Pinmux_Config(UART_TX_PIN, UART0_TX);
       Pinmux_Config(UART_RX_PIN, UART0_RX);
    }
    
  2. Call UART_DeInit() to deinitialize the UART.

  3. Call RCC_PeriphClockCmd() to enable the UART clock and function.

  4. Initialize the UART peripheral:

    1. Define the UART_InitTypeDef type uartInitStruct, and call UART_StructInit() to pre-fill uartInitStruct with default values.

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

    3. Call UART_Init() to initialize the UART peripheral.

    UART Initialization Parameters

    UART Hardware Parameters

    Setting in the uartInitStruct

    UART

    div

    UART_InitTypeDef::div

    1

    ovsr

    UART_InitTypeDef::ovsr

    8

    ovsr_adj

    UART_InitTypeDef::ovsr_adj

    0x492

    Parity Check

    UART_InitTypeDef::parity

    UART_PARITY_NO_PARTY

    Stop Bit

    UART_InitTypeDef::stopBits

    UART_STOP_BITS_1

    Data Format

    UART_InitTypeDef::wordLen

    UART_WROD_LENGTH_8BIT

    Hardware Flow Control

    UART_InitTypeDef::autoFlowCtrl

    UART_AUTO_FLOW_CTRL_DIS

    RX Trigger Level

    UART_InitTypeDef::rxTriggerLevel

    29

    GDMA Enable

    UART_InitTypeDef::dmaEn

    UART_DMA_ENABLE

    RX Waterlevel

    UART_InitTypeDef::RxWaterlevel

    1

    RX GDMA Enable

    UART_InitTypeDef::RxDmaEn

    FunctionalState::ENABLE

  5. Call UART_INTConfig() to enable RX idle timeout interrupt UART_INT_IDLE and receiver line status interrupt UART_INT_LINE_STS.

  6. Call NVIC_Init() to enable NVIC of UART.

GDMA Initialization

GDMA initialization flow is shown in the following figure.

../../../_images/UART_RX_GDMA_Initialization_Flow_Chart.png

GDMA Initialization Flow Chart

  1. Call RCC_PeriphClockCmd() to enable the GDMA clock and function.

  2. Call GDMA_channel_request to request an unused GDMA channel.

  3. Initialize the GDMA peripheral:

    1. Define the 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 GDMA initialization parameter configuration is shown in the table below.

    3. Call GDMA_Init() to initialize the GDMA peripheral.

    GDMA Initialization Parameters

    GDMA Hardware Parameters

    Setting in the GDMA_InitStruct Variables

    GDMA

    Channel Num

    GDMA_InitTypeDef::GDMA_ChannelNum

    UART_RX_DMA_CHANNEL_NUM

    Transfer Direction

    GDMA_InitTypeDef::GDMA_DIR

    GDMA_DIR_PeripheralToMemory

    Buffer Size

    GDMA_InitTypeDef::GDMA_BufferSize

    128

    Source Address Increment or Fix

    GDMA_InitTypeDef::GDMA_SourceInc

    DMA_SourceInc_Fix

    Destination Address Increment or Fix

    GDMA_InitTypeDef::GDMA_DestinationInc

    DMA_DestinationInc_Inc

    Source Data Size

    GDMA_InitTypeDef::GDMA_SourceDataSize

    GDMA_DataSize_Byte

    Destination Data Size

    GDMA_InitTypeDef::GDMA_DestinationDataSize

    GDMA_DataSize_Byte

    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

    (uint32_t)(&(UART0->RB_THR))

    Destination Address

    GDMA_InitTypeDef::GDMA_DestinationAddr

    (uint32_t)(uart_receive_buf)

    Source Handshake

    GDMA_InitTypeDef::GDMA_SourceHandshake

    GDMA_Handshake_UART0_RX

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

  5. Call GDMA_INTConfig() to enable GDMA transfer complete interrupt GDMA_INT_Transfer.

  6. Call GDMA_Cmd() to enable GDMA.

Functional Implementation

Send Data

Transmit ### Welcome to use RealTek Bumblebee ###rn to the PC terminal:

  1. Call UART_SendData() 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.

  2. Call UART_GetFlagState() to check UART_FLAG_THR_EMPTY flag state and wait for transmitter holding register to be empty.

  3. Repeat the above process to send all data to TX FIFO in batches.

UART Interrupt Handle

After the PC terminal sends a string, the chip will trigger UART interrupts.

  1. Call UART_GetIID() to get the interrupt ID.

  2. If No data is received in RX idle timeout time after the RX FIFO is empty (data is received before), the UART_FLAG_RX_IDLE interrupt is triggered:

    1. Call UART_GetFlagState() to check UART_FLAG_RX_IDLE interrupt flag state.

    2. Call UART_ClearINT() to clear UART_FLAG_RX_IDLE interrupt.

    3. Call GDMA_GetChannelStatus() to get the status of the GDMA channel used by the UART.

    4. If the status of the GDMA channel used by the UART is not free, call GDMA_SuspendCmd() to suspend GDMA transmission from the source.

    5. Call GDMA_GetFIFOStatus() to check whether GDMA FIFO is empty and wait for GDMA FIFO is empty.

    6. Call GDMA_GetTransferLen() to get GDMA transfer data length.

    7. Call GDMA_Cmd() to disable the GDMA channel used by the UART.

    8. Call GDMA_SetDestinationAddress() to set GDMA transmission destination address.

    9. Call GDMA_SuspendCmd() to suspend GDMA transmission from the source.

    10. Call GDMA_Cmd() to enable the GDMA channel used by the UART.

  3. When parity error or frame error or break error or overrun error occurs, UART_INT_ID_LINE_STATUS interrupt is triggered:

    1. Call UART_INTConfig() to disable receiver line status interrupt UART_INT_LINE_STS.

    2. Call UART_GetLineStatus() to get line status.

    3. Call UART_INTConfig() to enable receiver line status interrupt UART_INT_LINE_STS.

GDMA Interrupt Handle

When GDMA transfer completion to the destination, GDMA_INT_Transfer interrupt is triggered:

  1. Call GDMA_ClearINTPendingBit() to clear GDMA_INT_Transfer interrupt.