STM32 Tutorial 03 - UART Communication Using HAL (And FreeRTOS)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

STM32 Tutorial NUCLEO F103RB GPIO Pins

UART communication on STM32 Microcontrollers using HAL

You can use the STM32CubeMX tool to create the necessary config. files to enable the UART-Drivers.
The HAL library provides the necessary functions to communicate to with the UART protocol. This
feature comes in handy for debugging (printing out variables).

This tutorial uses the following equipment:


- NUCLEO-F072RB Board
- Keil uVision 5 with the necessary packages for Nucleo boards installed
- STLink USB Driver
- STM32CubeMX
- PuTTy, Termite or a similar Serial / COM console

STM32CubeMX
Generating the config. files from STM32CubeMX.

1. Open STM32CubeMX and open a new project.


2. Select the Nucleo-F072RB from the Borards tab
3. Enable FreeRTOS
4. Set the RCC (HSE & LSE) to Crystal/Ceramic Resonator
5. Enable the USART2 port in Asynchronous mode
6. Go to Project > Generate code
7. Enter a project name and select MDK-ARM V5
8. Generate the code and open the project in Keil uVision

V1.1.1 created on 20.05.2016 simon burkhardt page 1 /5


STM32 Tutorial NUCLEO F103RB GPIO Pins

Now lets see what the code generator did

167 void MX_USART2_UART_Init(void)


168 {
169
170 huart2.Instance = USART2;
171 huart2.Init.BaudRate = 9600;
172 huart2.Init.WordLength = UART_WORDLENGTH_7B;
173 huart2.Init.StopBits = UART_STOPBITS_1;
174 huart2.Init.Parity = UART_PARITY_NONE;
175 huart2.Init.Mode = UART_MODE_TX_RX;
176 huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
177 huart2.Init.OverSampling = UART_OVERSAMPLING_16;
178 huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
179 huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
180 HAL_UART_Init(&huart2);
181
182 }
183

Important, to make it work you have to change the WordLength parameter to


UART_WORDLENGTH_8B, since we are sending 8-Bit ascii chars.

You can also adjust the baud rate with the BaudRate parameter.

How to send information (strings) to the console (PC)


To send text data over the debug adapter to the USB-COM port of the computer.

Dont forget to include the string library


36 #include "string.h"

Function to write directly to UART


1 /* USER CODE BEGIN 4 */
2 void debugPrint(UART_HandleTypeDef *huart, char _out[]){
3 HAL_UART_Transmit(huart, (uint8_t *) _out, strlen(_out), 10);
4 }
5

It takes the following parameters:


- A pointer to the UART instance to write the data
- The Output string (char)

Function to write to UART and new line termination


6 void debugPrintln(UART_HandleTypeDef *huart, char _out[]){
7 HAL_UART_Transmit(huart, (uint8_t *) _out, strlen(_out), 10);
8 char newline[2] = "\r\n";
9 HAL_UART_Transmit(huart, (uint8_t *) newline, 2, 10);
10 }
11 /* USER CODE END 4 */
12

In addition to the previous function, this one terminates the string with a newline (0x0A) and a
carriage return (0x0D) command.

V1.1.1 created on 20.05.2016 simon burkhardt page 2 /5


STM32 Tutorial NUCLEO F103RB GPIO Pins

Use the functions as follows.

1 void StartDefaultTask(void const * argument)


2 {
3
4 /* USER CODE BEGIN 5 */
5 for(;;)
6 {
7 debugPrint(&huart2, "oi, mate!"); // print
8 debugPrint(&huart2, "\r\n"); // manual new line
9 debugPrintln(&huart2, "how are you?"); // print full line
10 osDelay(1000);
11 }
12 /* USER CODE END 5 */
13 }
14

How to read information (strings) from the console (PC)

1 void StartDefaultTask(void const * argument)


2 {
3
4 /* USER CODE BEGIN 5 */
5 for(;;)
6 {
7 uartPrintln(&huart2, "type something:");
8 char in[8];
9 HAL_UART_Receive(&huart2, (uint8_t *)in, 8, 1000);
10 uartPrint(&huart2, "\n");
11 HAL_UART_Transmit(&huart2, (uint8_t *)in, 8, 1);
12 uartPrint(&huart2, "\n\n");
13 /* USER CODE END 5 */
14 }
15 }
16

Create a char array, where you want to save the string to.
HAL_UART_Receive() takes this array as the second parameter.
The third parameter specifies the length of the input string, so you have to type word of exactly this
size later in the terminal.

V1.1.1 created on 20.05.2016 simon burkhardt page 3 /5


STM32 Tutorial NUCLEO F103RB GPIO Pins

Using a COM-port emulator to receive the output data


I used PuTTY to do this, but there are a ton of other tools out there, which should work just fine.

First thing, you should check to which port your STLink debugger is connected to.
Look this one up in the Device Manager.

In PuTTY, switch the mode to Serial, enter your COM-Port address, and make sure, you have selected
the same baud rate as configured on your STM32 chip.
Then open the connection and you should see the Output in the console.

V1.1.1 created on 20.05.2016 simon burkhardt page 4 /5


STM32 Tutorial NUCLEO F103RB GPIO Pins

Document Created by Simon Burkhardt

This tutorial is very basic and might not show the best way to use the STM32 environment.
It still might help you get into the whole HAL philosophy of STM if you are coming from another
platform. This document is free of copyright under the Creative Commons Zero attribution.

Simon Burkhardt
electronical engineering
simonmartin.ch

History:
V1.0 tested the code, created this document
V1.1 added information on receiving strings
V1.1.1 added contact info and cc0 notice

V1.1.1 created on 20.05.2016 simon burkhardt page 5 /5

You might also like