According to the introduction of the Bear Pi development board and the manual of the main chip Hi3861, this chip provides three groups of UART communication interfaces, namely UART0, UART1 and UART2, of which one group UART1 is led out in E53 mode, see the following schematic diagram:
To facilitate testing, use this set of UART1 to perform serial communication testing.
B6 in the example is a demonstration of the UART communication function. Enter the B6_basic_uart directory and open the main program uart_example.c:
static void UART_Task(void)
{
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_ptr = uart_buff;
uint32_t ret;
WifiIotUartAttribute uart_attr = {
//baud_rate: 9600
.baudRate = 9600,
//data_bits: 8 bits
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
//Initialize uart driver
ret = UartInit(WIFI_IOT_UART_IDX_1, &uart_attr, NULL);
if (ret != WIFI_IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
printf("UART Test Start\n");
while (1)
{
printf("========================================\r\n");
printf("************UART_example****************\r\n");
printf("========================================\r\n");
// Send data through serial port 1
UartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
// Receive data through serial port 1
UartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
printf("Uart1 read data:%s", uart_buff_ptr);
usleep(1000000);
}
}
The program is also very simple. After successfully initializing the serial port UART1, it enters a loop: write the data "Hello, BearPi!" to the serial port, and then read the data received by the serial port UART1. If we use a DuPont line to short-circuit TxD and RxD, the serial port UART1 can achieve self-transmission and self-reception, as shown in the following figure:
Modify the build.gn file in the sample directory as follows:
#"B1_basic_led_blink:led_example",
#"B2_basic_button:button_example",
#"B3_basic_pwm_led:pwm_example",
#"B4_basic_adc:adc_example",
#"B5_basic_i2c_nfc:i2c_example",
"B6_basic_uart:uart_example",
That is, comment out other example programs and remove the comment of B6.
The compilation and burning process is the same as the previous tests.
Open the serial terminal, and the program running results are shown as follows:
Serial port UART1 reads the data “Hello, BearPi!” sent by itself, thus achieving self-transmission and self-reception.
If TxD and RxD are not short-circuited, the program will run as follows:
While the program is writing data through the serial port, it is waiting for the serial port to read data, but because RxD is not connected, the data will not be read and displayed.
For the test of the communication interface I2C, please refer to the NFC test. The NFC chip uses the I2C interface, so this test will not repeat the I2C test.