1. Introduction

The communication methods between devices can be divided into serial communication and parallel communication, and each of these two communication methods has its own advantages and disadvantages. Serial communication is to transmit data in bit order. The advantage of this is that it occupies less pin resources, which is very beneficial for MCUs with tight pin resources. However, since only one data can be transmitted at a time, it has the disadvantage of slower transmission speed (relative to parallel transmission). Parallel communication is the simultaneous transmission of each bit of data. The advantage is slow data transmission, and the disadvantage is that it occupies more pin resources.

Serial communication is divided into simplex, half-duplex and full-duplex according to the transmission direction; simplex can be understood as data can only be transmitted in one direction, half-duplex means that it can be transmitted in both directions, but there can only be one transmission direction at a time, and full-duplex means that it can be transmitted in both directions, and it can have two transmission directions at the same time. According to the communication method, it can be divided into synchronous communication and asynchronous communication; synchronous communication requires the participation of a clock line (used as the synchronization of the clock signal), such as SPI and IIC communication interfaces, while asynchronous communication does not have a clock line, such as UART and one-wire.

STM32 provides two serial communication interfaces, UART (Universal Asynchronous Receiver/Transmitter) and USART (Universal Synchronous/Asynchronous Receiver/Transmitter), for data exchange with other devices. This article will introduce the basics of STM32 serial communication to help readers understand and master its basic principles and configuration methods.

  1. Overview of STM32 Serial Communication

The serial communication module of STM32 mainly includes two interfaces, UART and USART. UART is an asynchronous communication protocol that uses start bit, data bit, check bit and stop bit to define the transmission format of a character. USART is a synchronous/asynchronous communication protocol that supports full-duplex communication and has higher data transmission rate and better anti-interference ability. In the common STM32F103C8T6, according to the chip manual, it can be known that this model has 3 USARTs, but it can also be used as UART. UART pin TXD is the data output pin, RXD is the data input pin, and the UART connection between the two devices is a cross connection between TXD and RXD, and GND also needs to be connected together. When the UART device needs to be connected to other communication interface devices, a level conversion chip is required, such as CH340 (USB to TTL), MAX485 (UART to 485), etc.

  1. STM32 serial communication parameter configuration

Before performing STM32 serial communication, the serial communication parameters need to be configured. These parameters include baud rate, data bit, stop bit, check bit, etc. Among them, the baud rate represents the bit transmitted per second, which can reach up to 4.5Mbps in the STM32F103 series; the data bit represents the data length of each character (8 bits or 9 bits), the stop bit is used to indicate the end of the character (1bit or 2bit), and the check bit is used to check the correctness of data transmission (no check, odd check or even check). In STM32, these parameters can be implemented by configuring the corresponding registers. For example, the baud rate can be set by configuring the USART_BRR register, and the data bit, stop bit, and check bit can be set by configuring the USART_CR1 and USART_CR2 registers.

  1. STM32 serial communication programming implementation

When programming STM32 serial communication, you need to use the API functions provided by the HAL library or the standard peripheral library to configure the serial communication parameters and send/receive data. The following is a simple serial communication programming example:

Initialize serial communication parameters: Use the functions provided by the HAL library or the standard peripheral library to configure serial communication parameters, such as baud rate, data bit, stop bit, and check bit.

Send data: Use the send function provided by the HAL library or the standard peripheral library to write data to the serial port send buffer, and then wait for the data to be sent. When sending data, you need to pay attention to the length and format of the data to ensure the correct transmission of the data.

Receive data: Use the receive function provided by the HAL library or the standard peripheral library to read data from the serial port receive buffer. When receiving data, you need to determine whether there is data to read in the receive buffer, and read the corresponding data length and format.

Error handling: During serial communication, various error conditions may occur, such as send timeout, receive overflow, etc. When programming, you need to add corresponding error handling code to handle these abnormal conditions.

  1. STM32 serial communication programming example

Based on the HAL library and STM32CubeMX configuration software, a simple STM32 serial port routine is demonstrated.

5.1 Create a CUBE project: File->New Project

5.2 Model selection

5.3 Clock RCC configuration and SYS configuration

5.4 UART parameter configuration

5.5.Clock tree configuration

5.6 Engineering Management

5.7.Program modification

  1. intmain(void)
  2. {
  3. /* USER CODE BEGIN 1 */
  4. uint8_t testbuf[] = {"stm32 uart\r\n"};
  5. /* USER CODE END 1 */
  6. /* MCU Configuration--------------------------------------------------------*/
  7. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  8. HAL_Init();
  9. /* USER CODE BEGIN Init */
  10. /* USER CODE END Init */
  11. /* Configure the system clock */
  12. SystemClock_Config();
  13. /* USER CODE BEGIN SysInit */
  14. /* USER CODE END SysInit */
  15. /* Initialize all configured peripherals */
  16. MX_GPIO_Init();
  17. MX_USART1_UART_Init();
  18. /* USER CODE BEGIN 2 */
  19. /* USER CODE END 2 */
  20. /* Infinite loop */
  21. /* USER CODE BEGIN WHILE */
  22. while (1)
  23. {
  24. /* USER CODE END WHILE */
  25. /* USER CODE BEGIN 3 */
  26. HAL_UART_Transmit(&huart1,testbuf,sizeof(testbuf),0xFFFF);
  27. HAL_Delay(1000);
  28. }
  29. /* USER CODE END 3 */
  30. }

STM32 serial port communication is an indispensable part of embedded system development. By mastering the basic knowledge and programming implementation methods of STM32 serial port communication, embedded system development can be carried out more efficiently.