Welcome to ROSMASTER X3 Plus repository
4.5 Serial communication
5. Serial communication
5. Serial communication 5.1. Purpose of the experiment 5.2. Configuration pin information 5.3. Analysis of the experimental flow chart 5.4. core code explanation 5.5. Hardware connection 5.6. Experimental effect
5.1. Purpose of the experiment
Use the serial port communication of STM32, receive the data of the serial port assistant, and return the received data to the serial port, redefine the printf function.
5.2. Configuration pin information
Since each new project needs configuration information, it is more troublesome. Fortunately, STM32CubeIDE provides the function of importing .ioc files, which can help us save time.
1.Import the ioc file from the BEEP project and name it Serial.
Change the mode of serial port 1 to Asynchronous synchronous communication, the baud rate is 115200, the data width: 8 bits, the test: None, and the stop bit: 1 bit.

2.Add DMA sending channel

- Open serial port 1 interrupt settings.

5.3. Analysis of the experimental flow chart

5.4. core code explanation
- Create new buzzer driver library bsp_uart.h and bsp_uart.c files in BSP. Add the following to bsp_uart.h:

- Add the following in bsp_uart.c:
USART1_Init(): Initialize the related content of the serial port, open the serial port to receive 1 data.

USART1_Send_U8(ch): Serial port 1 sends a byte.

USART1_Send_ArrayU8(BufferPtr,Length): Serial port 1 sends a string of data, BufferPtr is the first address of the data, and Length is the length of the data. ENABLE_UART_DMA is the switch of serial port 1 DMA.

- Since the serial port 1 receive interrupt is only performed once, it is necessary to call the receive data again after receiving the data. In order to facilitate the test, the serial port is called in an interrupt to send data. In practical applications, data should not be sent in an interrupt. It is time-consuming to send data through the serial port, which may cause problems such as packet loss or even serial port errors.

- Redefine printf to use serial port 1 to send data.

- In BSP initialization, call USART1_Init() function to request to receive data.

- After the button is pressed, add the printf() function to print information through serial port 1.

5.5. Hardware connection
The expansion board needs to be connected to the USB port of the computer using a micro-USB data cable. The connection diagram is as follows:

5.6. Experimental effect
After programming the program, the LED light flashes once every 200 milliseconds. Connect the expansion board to the computer through the micro-USB data cable and open the serial port assistant (the specific parameters are shown in the figure below). The buzzer will sound every time the button is pressed. After 50 milliseconds, you can see that the serial port assistant will display press:xx, and each time you press the button xx will automatically increase by 1. The serial port assistant sends the character a, and the expansion board will automatically return the character a. Since the above uses to send data in an interrupt, you cannot send too many characters at a time, otherwise characters will be lost or even serial port errors will occur.
