Receive Polling
This example demonstrates data communication with a PC terminal using UART polling method.
The PC terminal program (such as PUTTY or UartAssist) sends data, the SoC continuously checks for received data in the main program, and sends the same data back to the PC terminal, where the corresponding information can be seen in the PC terminal program.
Requirements
The sample supports the following development kits:
Hardware Platforms |
Board Name |
---|---|
RTL8752H HDK |
RTL8752H EVB |
For more requirements, please refer to Quick Start.
Wiring
Connect P3_0 (UART TX) to the RX pin of the FT232 and P3_1 (UART RX) to the TX pin of the FT232.
Building and Downloading
This sample can be found in the SDK folder:
Project file: board\evb\io_sample\UART\Polling\mdk
Project file: board\evb\io_sample\UART\Polling\gcc
Please follow these steps to build and run the example:
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
Launch PuTTY or UartAssist or other PC terminals, connect to the used COM port, and configure the following UART settings:
Baud rate: 115200
8 data bits
1 stop bit
No parity
No hardware flow control
Testing Phase
The example starts sending
### Uart demo polling read uart data ####\r\n
, observe the string appearing on the PC terminal.Input a string on the PC terminal and observe whether the SoC replies with the same string.
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 Function Implementation.
Source Code Directory
Project directory:
sdk\board\evb\io_sample\UART\Polling
Source code directory:
sdk\src\sample\io_sample\UART\Polling
Source files are currently categorized into several groups as below.
└── Project: polling
└── secure_only_app
└── include
├── app_define.h
└── rom_uuid.h
├── cmsis includes CMSIS header files and startup files
├── overlay_mgr.c
├── system_rtl876x.c
└── startup_rtl876x.s
├── lib includes all binary symbol files that user application is built on
├── rtl8752h_sdk.lib
├── gap_utils.lib
└── ROM.lib
├── peripheral includes all peripheral drivers and module code used by the application
├── rtl876x_rcc.c
├── rtl876x_pinmux.c
├── rtl876x_nvic.c
└── rtl876x_uart.c
├── profile
└── app includes the ble_peripheral user application implementation
└── main.c
Initialization
When the EVB is reset, the main
function is executed, following these steps:
int main(void)
{
extern uint32_t random_seed_value;
srand(random_seed_value);
uart_demo();
while (1)
{
}
}
In uart_demo
, it includes PAD/PINMUX settings and UART peripheral initialization processes.
void uart_demo(void)
{
uint16_t demo_str_len = 0;
uint8_t rx_byte = 0;
board_uart_init();
driver_uart_init();
...
}
board_uart_init
is the PAD/PINMUX settings related to UART, including the following steps:
Configure PAD: Set the pins, PINMUX mode, PowerOn, internal pull-up, and disable output.
Configure PINMUX: Set the pins for UART0_TX and UART0_RX functions.
driver_uart_init
is the initialization of the UART peripheral, including the following steps:
Enable RCC clock.
Configure the UART baud rate to 115200.
void driver_uart_init(void) { UART_DeInit(UART0); RCC_PeriphClockCmd(APBPeriph_UART0, APBPeriph_UART0_CLOCK, ENABLE); /* uart init */ UART_InitTypeDef UART_InitStruct; UART_StructInit(&UART_InitStruct); /* Config uart baudrate */ 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; UART_Init(UART0, &UART_InitStruct); }
Functional Implementation
Define the string
### Uart demo polling read uart data ###\r\n
and executeuart_senddata_continuous
to send the string content to the PC.In
uart_senddata_continuous
, polling the flagUART_FLAG_TX_FIFO_EMPTY
to check whether the UART TX FIFO is empty.When the UART TX FIFO is empty, continuously load data into the TX FIFO in a loop to achieve multi-byte continuous data transmission.
The string data sent by the SoC can be seen on the PC serial port assistant.
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->RB_THR = *pSend_Buf++; } vCount -= UART_TX_FIFO_SIZE; } while (UART_GetFlagStatus(UARTx, UART_FLAG_TX_FIFO_EMPTY) == 0); while (vCount--) { UARTx->RB_THR = *pSend_Buf++; } }
Continuously monitor the status of UART received data. When data is detected to be sent from the PC, receive the data and send it back to the PC. In the PC serial port assistant, the received data will be observed to be the same as the data just sent.
/* Loop rx and tx */ while (1) { if (UART_GetFlagStatus(UART0, UART_FLAG_RX_DATA_AVA) == SET) { rx_byte = UART_ReceiveByte(UART0); UART_SendByte(UART0, rx_byte); } }