2.1. UART - Loopback
This demo is based on UART polling sending and receiving FIFO interrupt mode.
2.1.1. Hardware Connection
This demo is based on BL706_IOT and the connection method is as follows
GPIO function GPIO pin
----------------------------------
UART0_TX <--> GPIO14
UART0_RX <--> GPIO15
2.1.2. Software Implementation
See
examples/uart/uart_echo
for the software code
1#define BSP_UART_CLOCK_SOURCE ROOT_CLOCK_SOURCE_PLL_96M
2#define BSP_UART_CLOCK_DIV 0
-Configure the UART
device clock source, see bsp/board/bl706_iot/clock_config.h
1#define CONFIG_GPIO14_FUNC GPIO_FUN_UART0_TX
2#define CONFIG_GPIO15_FUNC GPIO_FUN_UART0_RX
Configure
UART
device multiplexing pins, seebsp/board/bl706_iot/pinmux_config.h
1#define BSP_USING_UART0
2
3#if defined(BSP_USING_UART0)
4#ifndef UART0_CONFIG
5#define UART0_CONFIG \
6{ \
7.id = 0, \
8.baudrate = 2000000,\
9.databits = UART_DATA_LEN_8, \
10.stopbits = UART_STOP_ONE, \
11.parity = UART_PAR_NONE, \
12.fifo_threshold = 1, \
13}
14#endif
15#endif
Enable
BSP_USING_UART0
and configure theUART
device, seebsp/board/bl706_iot/peripheral_config.h
1bflb_platform_init();
In the
bflb_platform_init
function, we have registered and opened a serial port device for debugging, to provide users with a basic function ofMSG
for printing out messages. The specific implementation is as follows
1 uart_register(board_get_debug_uart_index(), "debug_log", DEVICE_OFLAG_RDWR);
2 struct device *uart = device_find("debug_log");
3
4 if (uart)
5 {
6 device_open(uart, DEVICE_OFLAG_STREAM_TX | DEVICE_OFLAG_INT_RX);
7 device_set_callback(uart, NULL);
8 device_control(uart, DEVICE_CTRL_CLR_INT, (void *)(UART_RX_FIFO_IT));
9 }
First call the
uart_register
function to register theUART
device, currently registerUART0
Then use the
find
function to find the handle corresponding to the device and save it in theuart
handleFinally use
device_open
to open theuart
device with polling sending and interrupt receiving, the interrupt is closed by default and the receiving interrupt callback function is not registered
1if (uart)
2{
3 device_set_callback(uart, uart_irq_callback);
4 device_control(uart, DEVICE_CTRL_SET_INT, (void *)(UART_RX_FIFO_IT|UART_RTO_IT));
5}
Register the user-specified
UART0
receiving interrupt service function through thedevice_set_callback
function. Open theRX_FIFO
andRTO
interrupts through thedevice_control
function
1void uart_irq_callback(struct device *dev, void *args, uint32_t size, uint32_t state)
2{
3 if (state == UART_EVENT_RX_FIFO)
4 {
5 device_write(dev,0,(uint8_t *)args,size);
6 }
7 else if (state == UART_EVENT_RTO)
8 {
9 device_write(dev,0,(uint8_t *)args,size);
10 }
11}
This function is the interrupt service function of the example, and its function is to send out the received data.
state
will return the interrupt type of theUART
deviceargs
contains the return data pointersize
contains the length of the returned datadev
is the handle of the interrupteduart
device
When an interrupt occurs, the
device_write
function will be called to send the received data back.
2.1.3. Compile and Program
CDK compilation
Open project:uart_echo.cdkproj
Refer to the steps of Guide to using CDK (like MDK Keil) under Windows to compile and download
Command compilation
1 $ cd <sdk_path>/bl_mcu_sdk
2 $ make BOARD=bl706_iot APP=uart_echo
Program
2.1.4. Experimental Phenomena
Video display: