# STM32 - USART
###### tags: `firmware_hardware` `electrical_system` `NTURT`
#### Author: @andre-liang

source: [link](https://keepcalms.com/p/hello-can-you-hear-me-now/)
### Preface
In the realm of managing multiple MCUs, sensors, and actuators within a system, effective communication between various devices holds paramount importance. A multitude of communication protocols exist, and among them, USART emerges as a straightforward option. Unlike more intricate protocols like I2C or CAN, which support communication across multiple modules, USART is notably simplistic. This protocol accommodates communication exclusively between two modules, making it an ideal choice for certain scenarios.
### USART Protocol
USART ( *Universal Synchronous/Asynchronous Receiver/Transmitter* ) is a serial communication interface between two modules.

source: [link](http://wiki.csie.ncku.edu.tw/embedded/USART)
The distinction between full-duplex and half-duplex lies in their data transmission capabilities. In a full-duplex system, data can be both sent and received concurrently, enabling simultaneous two-way communication. Conversely, in a half-duplex system, the transmission and reception of data cannot occur simultaneously, limiting communication to one direction at a time.
#### UART Dataframe

source: [link](https://www.analog.com/en/analog-dialogue/articles/uart-a-hardware-communication-protocol.html)
#### USART

source: [link](http://wiki.csie.ncku.edu.tw/embedded/USART)
The distinction between asynchronous and synchronous systems pertains to their handling of timing signals. Synchronous systems are characterized by the presence of a clock signal, which, although introducing additional circuit complexity, augments data throughput by omitting the need for Start and Stop bits. In contrast, asynchronous systems lack this clock signal and rely on Start and Stop bits to synchronize data transmission.
### Setup
Throughout this tutorial, we'll be utilizing the NUCLEO-H723ZG development board. This board incorporates a TTL-USB converter, which facilitates direct communication between the MCU and our personal computer. This integrated feature streamlines the process of interacting with the microcontroller through a direct USB connection.
As a default configuration, the port labeled as `usart3` (STLK_VCP_xx) is connected to the USB interface. To enhance the robustness of communication, it's recommended to adjust the baud rate to 9600. Keep in mind that a lower baud rate translates to improved resilience but reduced communication speed.
### Send Message
To transmit a message through the designated UART port, you can employ the HAL_UART_Transmit function. This function requires you to provide a pointer referencing the message content, the length of the message, and the maximum timeout duration. With these parameters in place, the function facilitates the seamless transmission of your message via the specified UART port.
In our illustrative application, a recurring message "Hello from stm32" is sent to the usart3 port at intervals of 1 second.
```clike=
/* USER CODE BEGIN 2 */
char message[] = "Hello from stm32\r\n";
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_UART_Transmit(&huart2, (uint8_t*)&message, strlen(message), HAL_MAX_DELAY);
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
```
### Results
Launch a serial monitor, like Cutecom, on the Linux platform to verify the functionality. Observe the monitor's interface to confirm the anticipated outcome: the reception of the intended message at precisely 1-second intervals.