UART TX Data in Interrupt Mode

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

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_tx_demo();
    

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: 115200.

  • 8 data bits.

  • 1 stop bit.

  • No parity.

  • No hardware flow control.

Testing Phase

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

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\uart\interrupt\uart_tx_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

UART initialization flow is shown in the following figure.

../../../_images/UART_TX_Interrupt_Initialization_Flow_Chart.png

UART Initialization Flow Chart

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

    static void board_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 RCC_PeriphClockCmd() to enable the UART clock and function.

  3. 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

    20

    ovsr

    UART_InitTypeDef::ovsr

    12

    ovsr_adj

    UART_InitTypeDef::ovsr_adj

    0x252

    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

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

Functional Implementation

Send Data

Start transmitting ### Welcome to use RealTek Bumblebee ###\r\n 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_INTConfig() to enable TX FIFO empty interrupt.

Interrupt Handle

UART interrupt handle flow is shown in the following figure.

../../../_images/UART_TX_Interrupt_Handle_Flow.png

UART Interrupt Handle Flow

When UART TX FIFO is empty, UART TX FIFO empty interrupt is triggered and enters the interrupt handler.

  1. Call UART_GetIID() to get interrupt ID.

  2. If interrupt ID is UART_INT_ID_TX_EMPTY, call UART_INTConfig() to disable TX FIFO empty interrupt.

  3. Call UART_GetIID() to clear TX FIFO empty interrupt.

  4. If there is any data to be transferred:

    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_INTConfig() to enable TX FIFO empty interrupt.