Receive Polling

This sample demonstrates data communication with a PC terminal using UART polling.

The PC terminal program (for example PUTTY or UartAssist) transmits data, the SoC detects the received data cyclically within the main program and sends the same data back to the PC terminal, and the corresponding information can be seen in the PC interrupt program.

Requirements

The sample supports the following development kits:

Development Kits

Hardware Platforms

Board Name

RTL87x2G HDK

RTL87x2G EVB

For more requirements, please refer to Quick Start.

Configurations

The sample configurable macros are as follows:

  1. UART_CONFIG_HW_FLOW_CTRL : Enable this macro to enable the UART Hardware Flow Control function.

Wiring

Connect P3_0 to the RX pin of the FT232 and P3_1 to the TX pin of the FT232.

If UART_CONFIG_HW_FLOW_CTRL is enabled, connect P2_4 to the RTS pin of the FT232 and connect P2_5 to the CTS pin of the FT232.

Building and Downloading

This sample can be found in the SDK folder:

Project file: samples\peripheral\uart\rx_polling\proj\rtl87x2g\mdk

Project file: samples\peripheral\uart\rx_polling\proj\rtl87x2g\gcc

To build and run the sample, follow the steps listed below:

  1. Open sample project file.

  2. To build the target, follow the steps listed on the Generating App Image in Quick Start.

  3. After a successful compilation, the app bin app_MP_xxx.bin will be generated in the directory mdk\bin or gcc\bin.

  4. To download app bin into EVB board, follow the steps listed on the MP Tool Download in Quick Start.

  5. Press reset button on EVB board and it will start running.

Experimental Verification

Preparation Phase

  1. Start a PC terminal 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

  1. Set Hardware flow control option on the PC terminal when UART_CONFIG_HW_FLOW_CTRL is enabled.

Testing Phase

  1. After resetting the EVB, observe the log information as shown in the Debug Analyzer.

    Start uart rx polling test!
    
  2. This sample starts with transmitting ### Uart demo polling read uart data ###\r\n. Observe that the string appears on the PC terminal.

  3. Type strings on the PC terminal and observe that the same string appears on the PC terminal.

Code Overview

This chapter will be introduced according to the following several parts:

  1. Source Code Directory.

  2. Peripheral initialization will be introduced in chapter Initialization.

  3. Functional implementation after initialization will be introduced in chapter Functional Implementation.

Source Code Directory

  • Project directory: sdk\samples\peripheral\uart\rx_polling\proj

  • Source code directory: sdk\samples\peripheral\uart\rx_polling\src

Source files are currently categorized into several groups as below.

└── Project: output_toggle
    └── secure_only_app
        └── Device                   includes startup code
            ├── startup_rtl.c
            └── system_rtl.c
        ├── CMSIS                    includes CMSIS header files
        ├── CMSE Library             Non-secure callable lib
        ├── lib                      includes all binary symbol files that user application is built on
            └── rtl87x2g_io.lib
        ├── peripheral               includes all peripheral drivers and module code used by the application
            ├── rtl_pinmux.c
            ├── rtl_rcc.c
            └── rtl_uart.c
        └── APP                      includes the ble_peripheral user application implementation
            ├── io_uart.c
            └── main_ns.c

Initialization

The initialization process includes board_uart_init and driver_uart_init .


board_uart_init contains the PAD and PINMUX settings:

  1. Config PAD: Set pins as PINMUX mode, PowerOn, internal Pull-Up.

  2. Config PINMUX: Assign pins for UART3_TX, UART3_RX functions respectively.


driver_uart_init contains the initialization of the UART peripheral.

  1. Enable PCC Clock.

  2. Set baud rate to 115200.

  3. If UART_CONFIG_HW_FLOW_CTRL is enabled, set UART_HardwareFlowControl to ENABLE.

  RCC_PeriphClockCmd(APBPeriph_UART3, APBPeriph_UART3_CLOCK, ENABLE);
  ...
  UART_InitStruct.UART_Div            = BaudRate_Table[BAUD_RATE_115200].div;
  UART_InitStruct.UART_Ovsr           = BaudRate_Table[BAUD_RATE_115200].ovsr;
  UART_InitStruct.UART_OvsrAdj        = BaudRate_Table[BAUD_RATE_115200].ovsr_adj;
#if UART_CONFIG_HW_FLOW_CTRL
        UART_InitStruct.UART_HardwareFlowControl              = UART_HW_FLOW_CTRL_ENABLE;
#endif
  ...

Functional Implementation

  1. Execute uart_senddata_continuous to transmit ### Uart demo polling read uart data ###\r\n to the PC terminal.

  2. After the PC terminal transmits the string, RX data is detected, and UART_FLAG_RX_DATA_RDY flag is set. Execute UART_ReceiveByte() to receive data and execute UART_SendByte() to send it back to the PC terminal.

uart_senddata_continuous(UART_DEMO, String_Buf, demoStrLen);
while (1)
{
    if (UART_GetFlagStatus(UART_DEMO, UART_FLAG_RX_DATA_AVA) == SET)
    {
        rx_byte = UART_ReceiveByte(UART_DEMO);
        UART_SendByte(UART_DEMO, rx_byte);
    }
}