Receive Polling
This sample demonstrates data communication with a PC terminal using UART in polling mode.
By sending data using a PC terminal program (such as PUTTY or UartAssist), the SoC continuously checks for incoming data within the main program and sends the same data back to the PC terminal. The PC terminal program allows users to observe the data response from the SoC.
Users can change the pin configuration in the sample, enable or disable hardware flow control, and more, through different macro configurations. For detailed macro configurations, see Configurations.
Requirements
For 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.
Configurations
The following macro can be configured to enable or disable the hardware flow control feature.
#define UART_CONFIG_HW_FLOW_CTRL 1 /*< Set this macro to 1 to enable the hardware flow control. */
The following macro can be configured to modify the pin definition.
#define UART_TX_PIN P3_0 #define UART_RX_PIN P3_1 #define UART_CTS_PIN P2_4 #define UART_RTS_PIN P2_5
Wiring
Connect P3_0(UART TX Pin) to the RX pin of the FT232 and P3_1(UART RX Pin) to the TX pin of the FT232.
If UART_CONFIG_HW_FLOW_CTRL
is configured as 1
, it is also necessary to connect P2_4 (UART CTS Pin) to the RTS of FT232, and connect P2_5 (UART RTS Pin) to the CTS of FT232.
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
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
Testing Phase
After resetting the EVB, observe the log information as shown in the Debug Analyzer.
Start uart rx polling test!
This SoC 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 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\uart\rx_polling\proj
Source code directory:
sdk\samples\peripheral\uart\rx_polling\src
Initialization
The initialization flow for peripherals can refer to Initialization Flow in General Introduction.
Call
Pad_Config()
andPinmux_Config()
to configure the PAD and PINMUX of the corresponding pins. IfUART_CONFIG_HW_FLOW_CTRL
is set to1
, the CTS and RTS pins still need to be configured.void board_uart_init(void) { Pad_Config(UART_TX_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(UART_RX_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pinmux_Config(UART_TX_PIN, UART3_TX); Pinmux_Config(UART_RX_PIN, UART3_RX); #if UART_CONFIG_HW_FLOW_CTRL Pad_Config(UART_CTS_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pad_Config(UART_RTS_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_DISABLE, PAD_OUT_HIGH); Pinmux_Deinit(UART_CTS_PIN); Pinmux_Deinit(UART_RTS_PIN); Pinmux_Config(UART_CTS_PIN, UART3_CTS); Pinmux_Config(UART_RTS_PIN, UART3_RTS); #endif }
Call
RCC_PeriphClockCmd()
to enable the UART clock.Initialize the UART peripheral:
Define the
UART_InitTypeDef
typeUART_InitStruct
, and callUART_StructInit()
to pre-fillUART_InitStruct
with default values.Modify the
UART_InitStruct
parameters as needed. The UART initialization parameter configuration is shown in the table below.Call
UART_Init()
to initialize the UART peripheral.
UART Hardware Parameters |
Setting in the |
UART |
---|---|---|
UART Baudrate Parameter - div |
|
|
UART Baudrate Parameter - ovsr |
|
|
UART Baudrate Parameter - ovsr_adj |
|
|
Hardware Flow Control |
|
Functional Implementation
Execute
uart_senddata_continuous
to transmit### Uart demo polling read uart data ###\r\n
to the PC terminal. Within the functionuart_senddata_continuous
, wait for the UART TX FIFO to be empty, and then fill the data into the FIFO in batches.void uart_senddata_continuous(UART_TypeDef *UARTx, const uint8_t *pSend_Buf, uint16_t vCount) { uint8_t count; while (vCount / UART_TX_FIFO_SIZE > 0) { while (UART_GetFlagStatus(UARTx, UART_FLAG_TX_FIFO_EMPTY) == 0); for (count = UART_TX_FIFO_SIZE; count > 0; count--) { UARTx->UART_RBR_THR = *pSend_Buf++; } vCount -= UART_TX_FIFO_SIZE; } while (UART_GetFlagStatus(UARTx, UART_FLAG_TX_FIFO_EMPTY) == 0); while (vCount--) { UARTx->UART_RBR_THR = *pSend_Buf++; } }
After the PC terminal sends a string, when the SoC detects that data has been received and the
UART_FLAG_RX_DATA_RDY
status is SET, it callsUART_ReceiveByte()
to receive the data and callsUART_SendByte()
to send the data 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); } }