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:

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:

  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

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

  1. The example starts sending ### Uart demo polling read uart data ####\r\n , observe the string appearing on the PC terminal.

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

  1. Source Code Directory.

  2. Peripheral initialization will be introduced in chapter Initialization.

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

  1. Configure PAD: Set the pins, PINMUX mode, PowerOn, internal pull-up, and disable output.

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

  1. Enable RCC clock.

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

  1. Define the string ### Uart demo polling read uart data ###\r\n and execute uart_senddata_continuous to send the string content to the PC.

    1. In uart_senddata_continuous, polling the flag UART_FLAG_TX_FIFO_EMPTY to check whether the UART TX FIFO is empty.

    2. When the UART TX FIFO is empty, continuously load data into the TX FIFO in a loop to achieve multi-byte continuous data transmission.

    3. 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++;
        }
    }
    
  2. 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);
        }
    }