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:
Hardware Platforms |
Board Name |
---|---|
RTL87x2G HDK |
RTL87x2G EVB |
For more requirements, please refer to Quick Start.
Configurations
The sample configurable macros are as follows:
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:
Open sample project file.
To build the target, follow the steps listed on the Generating App Image in Quick Start.
After a successful compilation, the app bin
app_MP_xxx.bin
will be generated in the directorymdk\bin
orgcc\bin
.To download app bin into EVB board, follow the steps listed on the MP Tool Download in Quick Start.
Press reset button on EVB board and it will start running.
Experimental Verification
Preparation Phase
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
Set Hardware flow control option on the PC terminal when
UART_CONFIG_HW_FLOW_CTRL
is enabled.
Testing Phase
After resetting the EVB, observe the log information as shown in the Debug Analyzer.
Start uart rx polling test!
This sample starts with transmitting
### Uart demo polling read uart data ###\r\n
. Observe that the string appears on the PC terminal.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:
Peripheral initialization will be introduced in chapter Initialization.
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:
Config PAD: Set pins as PINMUX mode, PowerOn, internal Pull-Up.
Config PINMUX: Assign pins for UART3_TX, UART3_RX functions respectively.
driver_uart_init
contains the initialization of the UART peripheral.
Enable PCC Clock.
Set baud rate to 115200.
If
UART_CONFIG_HW_FLOW_CTRL
is enabled, setUART_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
Execute
uart_senddata_continuous
to transmit### Uart demo polling read uart data ###\r\n
to the PC terminal.After the PC terminal transmits the string, RX data is detected, and
UART_FLAG_RX_DATA_RDY
flag is set. ExecuteUART_ReceiveByte()
to receive data and executeUART_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);
}
}