Getting started with UART

Revision as of 09:43, 8 December 2022 by Registered User (→‎Generate project and edit main.c)
Under construction.png Coming soon

This article explains what is UART and how to use it through examples

1. What is a universal asynchronous receiver transmitter (UART)

The universal synchronous/asynchronous receiver transmitter (USART/UART) offers a flexible means of Full-duplex data exchange with external equipment requiring an industry standard NRZ asynchronous serial data format. The USART offers a very wide range of baud rates using a programmable baud rate generator. It supports synchronous one-way communication and Half-duplex Single-wire communication, as well as multiprocessor communications. It also supports the LIN (Local Interconnect Network), Smartcard protocol and IrDA (Infrared Data Association) SIR ENDEC specifications and Modem operations (CTS/RTS). High speed data communication is possible by using the DMA (direct memory access) for multibuffer configuration. Also, the UART can be used with interrupt.
The main of this article is to describe the UART features and precisely:

  • Simple UART communication
  • UART with Interrupt
  • UART with DMA

2. Objectives

The objectives of this wiki article are to know how to setup UART in STM32CubeIDE, how to generate code with STM32CubeIDE and how to use HAL functions.

2.1. Create the project in STM32CubeIDE

  • File > New > STM32 Project in main panel.

create STM32CubeIDE project.png

In this example the NUCLEO-L476RG board is used.

  • Select NUCLEO-L476RG board using Board Selector as shown in the below figure.

Select NUCLEO-L476RG board.png

In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically, it may take some time.

  • Save the project.

UART project name.png

  • Initialize all peripherals with their default settings.

Answer “Yes” to initialize all peripherals with their default mode? Popup as below:

Popup.png

3. UART Configuration

  • Choose the USART2 mode on Asynchronous mode.
  • Set the parameter settings as shown in the below figure.

USART Configuration.png

4. GPIO Configuration

To choose the correct GPIOs pins, it is necessary to refer to the datasheet and precisely Alternate function table.
For the STM32L476 USART communication is connected to PA2 for transmission and PA3 for reception as shown below.
File:STM32L476 datasheet table.png

For that, PA2 and PA3 are configured in alternate function mode.
File:GPIO configuration.png

5. Generate source code

Click “Ctrl+S” to generate the project
Generate project.png

5.1. Simple UART Communication

5.1.1. Hardware preparation

Work in pairs boards, one will create transmitter and second receiver.
For that, the two boards are connected as follows:

  • Rx board 1 connected with Tx board 2.
  • Tx board 1 connected with Rx board 2.
  • Connected the GND board 1 with GND board 2.

File:Hardware USART communication.png

5.1.2. HAL library workflow summary

File:HAL lib.png

  • Transmit data

File:HAL transmit.png

  • Receive data

File:HAL receive.png

5.1.3. Generate project and edit main.c to transmit data

  • Create data
Info white.png Information
Insert your code to create data structure to transmit between /* USER CODE BEGIN 0 */ and /* USER CODE END 0 */ tags
/* USER CODE BEGIN 0 */
uint8_t data[]={0,1,2,3,4,5,6,7,8,9};
/* USER CODE END 0 */
  • Transmit data
Info white.png Information
Insert your code to transmit data between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
/* USER CODE BEGIN 3 */
  /* Infinite loop */
  while (1)
  {
    HAL_UART_Transmit(&huart2,data,10,1000);
  }
  /* USER CODE END 3 */
  • Compile and flash

First of all, build your project Built.png.
Then debug it Debug.png when you used only NUCLEO-L476RG.
Connect the second Nucleo-L476RG.
Open a console emulator such as TeraTerm. Configure the console baud rate and data bits length. the COM Port name may differ on your PC for two Nucleo-L476 RG boards.
Configure the two Ports in this case COM4 and COM23 as shown in the below figures:
File:Tera term serial.png

File:Serial setup.png

Click setup restore steup in the tera term.
Open file teraterm.INI and make sure for these parameters.

; Display all characters (debug mode)
Debug=on
; Debug mode type which can be selected by user.
;   on|all   = All types
;   off|none = Disabled debug mode
;   normal   = usual teraterm debug mode
;   hex      = hex output
;   noout    = disable output completely
DebugModes=all

Save the file if you modify any line.
Click open in Restore setup.
File:Restore setup.png
Click on Resume button Resume button.png to execute your code on the STM32CubeIDE.
Click “Shift+ESC” to modify the display ASCII to hexa.
TeraTerm displays “1, 2, 3, 4, 5, 6, 7, 8, 9” string confirming you were able to program.
File:Tera term communication.png

5.1.4. Generate project and edit main.c to receive data

  • Create data
Info white.png Information
Insert your code to create data structure to receive between /* USER CODE BEGIN 0 */ and /* USER CODE END 0 */ tags
/* USER CODE BEGIN 0 */
uint8_t data[10];
/* USER CODE END 0 */
  • Receive data
Info white.png Information
Insert your code to transmit data between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags
/* USER CODE BEGIN 3 */
  /* Infinite loop */
  while (1)
  {
    HAL_UART_Receive(&huart2,data,10,1000);
  }
  /* USER CODE END 3 */
  • Compile and flash

First of all, build your project Built.png
Then debug it Debug.png when you used only NUCLEO-L476RG
Connect the second Nucleo-L476RG.
Open a console emulator such as TeraTerm. Configure the console baud rate and data bits length. the COM Port name may differ on your PC for two Nucleo-L476 RG boards.
Configure the two Ports in this case COM4 and COM23 as already mentioned in 5.1.3 Generate project and edit main.c to transmit data
Tap 10 value data in this example we choose “0, 1, 2, 3, 4, 5, 6, 7, 8, 9”
You can see the result in the tera term or in the data expression as shown in the below figures.
File:Tera term receive1.png

File:Tera term receive2.png

File:Expressions show view.png

5.2. UART with Interrupt

5.2.1. Hardware preparation

Make a jumper between Tx and Rx pins to create loopback as shown in the below figure.
File:Hardware USART Interrupt.png

5.2.2. HAL library workflow summary

File:HAL lib Interrupt.png

5.2.3. Configure UART Interrupt

Open the UART.ioc file in the STM32Cube IDE project as shown in the below figure.
Enable the USART2 global interrupt.
File:Configure UART Interrupt.png
Click “Ctrl+S” to generate the project

5.2.4. Generate project and edit main.c

Click “Ctrl+S” to generate the project

  • Definite buffer
Info white.png Information
Insert your code to definite the buffer between /* USER CODE BEGIN 0 */ and /* USER CODE END 0 */ tags
/* USER CODE BEGIN 0 */
uint8_t tx_buff[]={0,1,2,3,4,5,6,7,8,9};
uint8_t rx_buff[10];
/* USER CODE END 0 */
  • Send and Receive data
Info white.png Information
Insert your code to transmit data between /* USER CODE BEGIN 2 */ and /* USER CODE END 2 */ tags
/* USER CODE BEGIN 2 */
  HAL_UART_Receive_IT(&huart2,rx_buff,10);
  HAL_UART_Transmit_IT(&huart2,tx_buff,10);
  /* USER CODE END 2 */
  • Complete callback
Info white.png Information
Insert your code between /* USER CODE BEGIN 4 */ and /* USER CODE END 4 */ tags
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  __NOP();//test if we reach this position
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
  __NOP();//test if we reach this position
}
/* USER CODE END 4 */
  • Compile and flash

First of all, build your project Built.png
Then debug it Debug.png
Click on Resume button Resume button.png to execute the code. Click on suspend Suspend button.png to show the expressions in the tx_buff and rx_buff. File:Expressions show view UART with interrupt

5.3. UART with DMA


[[category:Getting_started_with_STM32_system_peripherals | 15]]