Welcome to ROSMASTER R2 repository
5. Serial communication
5. Serial communication
5. Serial communication5.1. Purpose of the experiment5.2 Configuring pin information5.3. Experimental flow chart analysis5.4. Core Code Explanation5.5 Hardware Connection5.6 Experimental effect
5.1. Purpose of the experiment
Use STM32's serial communication to receive data from the serial assistant and return the received data to the serial port and redefine the printf function.
5.2 Configuring pin information
Because every time we create a new project, we need to configure the information, which is quite troublesome. It is good that STM32CubeIDE provides the function of importing .ioc files, which can help us save time.
- Import the ioc file from BEEP's project and name it Serial.
Modify the mode of serial port 1 to Asynchronous synchronous communication, baud rate is 115200, data width: 8 bits, check: None, stop bit: 1 bit.

- Add DMA send channel

- Open the serial port 1 interrupt setting.

5.3. Experimental flow chart analysis

5.4. Core Code Explanation
- Create a new buzzer driver library bsp_uart.h and bsp_uart.c file in BSP. Add the following to bsp_uart.h:

- Add the following contents in bsp_uart.c:
USART1_Init():Initialize the serial port related contents and turn on 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, Length is the length of the data.ENABLE_UART_DMA is the switch of Serial port 1 DMA.

- As the serial port 1 receive interrupt only once, so after receiving data need to be called again to receive data. Here in order to facilitate the test, so in the interrupt call the serial port to send data, the actual application should not be in the interrupt to send data, the serial port to send data is more time-consuming, which may lead to packet loss or even serial port errors and other issues.

- Redefine printf to send data using serial port 1.

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

- Add the printf() function to print information through serial port 1 after a key is pressed.

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

5.6 Experimental effect
After burning the program, the LED flashes every 200 milliseconds, connect the expansion board to the computer via micro-USB cable and open the serial assistant (the specific parameters are shown in the following figure), every time you press the key, the buzzer will sound for 50 milliseconds, and you can see that the serial assistant will display press:xx, and every time you press the key, the xx is automatically increased by 1. The serial assistant sends the character a and the expansion board will The serial assistant sends the character a and the expansion board will automatically return the character a. Since the above use to send data in interrupt, so can't send too many characters at a time, otherwise there will be character loss or even serial port error.
