Discovering the STM32 CUBE IDE Part 2: Learning the UART
2023-09-08 | By DWARAKAN RAMANATHAN
License: General Public License ARM mbed
Introduction:
The Universal Asynchronous Receiver/Transmitter (UART) protocol simplifies data exchange in embedded systems. This comprehensive guide equips developers with the knowledge to configure and use UART in STM32 microcontrollers. Learn the fundamentals, from initializing the UART interface to transmitting and receiving data. STM32 CUBE IDE's integrated development environment streamlines the process, enabling seamless communication between STM32 microcontrollers and external devices. Whether you're a beginner or experienced, this tutorial empowers you to harness the full potential of UART for your projects. Implement UART effectively in your STM32 applications and explore a world of efficient and reliable serial communication possibilities.
Note: Before learning this part of this STM32 series, I would recommend you first complete learning part 1 of this series. The link to the first part of this series is provided here: Getting started with STM32 Cube IDE
Creating new Project:
To create a project, go to File > New Project. There will be a new window opened for you to select the board or MCU. In this project, we will use B-L4S5I-IOT01A, the part is also linked below. Go to Board Selector and select the board by typing in the commercial part number (B-L4S5I-IOT01A) and click on Next.
After clicking Next, type the project name (In this case: Simple_uart) and click on Finish.
After clicking finish, STM32 CUBE IDE will automatically download the software packages that your board requires. After the download is complete you will see a workspace something like this (a .ioc file will be opened):
Assigning ports and generating code:
In this project, we will be sending messages to our console from the device (STM32 Board). STM32 Board uses UART-based communication. Now, to enable UART in your board go to connectivity > USART1 and check if it is in Asynchronous Mode. Now, you will see a warning symbol near USART1. To remove that please reset PB3, PB4, and PB5. After doing so, you will see that the warning is now gone, and a green tick near the USART1 is visible.
Generating code:
As you have completed assigning the ports you can generate the code. Click Ctrl+S to save the file and then generate code by going to Project > Generate Code.
After Generating the code, you will have to edit three sections:
- The User Include Section
- The User Code Begin 3 Inside the while loop
- The User Code Begin 4
The codes of each section are given below:
1. The User Include Section
#include "stdio.h"
#include "string.h"
2. The User Code Begin 3
printf("Hello World");
printf("\r\n");
HAL_Delay(150);
3. The User Code Begin 4
int __io_putchar(int ch){
HAL_UART_Transmit(&huart1, (uint8_t *) &ch, 1, HAL_MAX_DELAY);
return ch;
}
Understanding:
The Include section is filled with stdio.h and string.h libraries because we are using both print statements as well as string datatype. The User Code Begin 3 is the actual code that will Run on the Microcontroller Device (Send messages to the console). The User Code Begin 4 is for transmitting characters (Data) from the Microcontroller to the Console using UART. We are just enabling the microcontroller to use UART and transmit data.
Building and Debugging:
To build the project go to Project > Build Project. If you see no errors, then the code is ready to be debugged.
After building the project connect your Microcontroller board and check the Communication port in the device manager (Ex: COM7). The device name in my case can be different from yours. Do not panic; just check for the port number for any STM-based Device.
In this case, the port is 3. Now, Debug the main.c file and check whether the console shows "Download verified successfully".
After debugging the code, you should use the ability of the device to use UART to send messages to your console, and for that, you have to create a console mentioning the port that your device is connected. To do so, go to the console window below the workspace and select Command shell console under Open Console.
After clicking on it, you will see a new window opened for you to choose the port and name of the console. The connection type is Serial port and click on "New".
After clicking New, type the name of your port, select the serial port to which your device is connected, and click Finish.
After clicking finish, you will see a new console created for you, and it will tell you that the console is connected.
Now, you are ready to resume the debugging. Click on "resume" which is available on the topmost taskbar. After that, you will see that your microcontroller is sending you the messages that you programmed it to send.
Coming up:
In the next part of this series, we will work on how to connect an analog device and get the values that it reads using UART.
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum