0% found this document useful (0 votes)
22 views16 pages

Introduction To Free RTOS in STM32

This document provides an introduction to FreeRTOS for STM32 microcontrollers, explaining the setup process using CubeMX and the benefits of using an RTOS for multitasking. It covers creating tasks, managing priorities, and the importance of task scheduling for efficient execution. The tutorial aims to help users understand how to implement FreeRTOS in their embedded systems projects.

Uploaded by

anushri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

Introduction To Free RTOS in STM32

This document provides an introduction to FreeRTOS for STM32 microcontrollers, explaining the setup process using CubeMX and the benefits of using an RTOS for multitasking. It covers creating tasks, managing priorities, and the importance of task scheduling for efficient execution. The tutorial aims to help users understand how to implement FreeRTOS in their embedded systems projects.

Uploaded by

anushri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

Introduction to Free Related


Posts:
RTOS in STM32
Today in this tutorial, I am going to walk you through a very
important part of an embedded system. Yes, that is deploying a
RTOS into the microcontroller. FreeRTOS Tutorial
#3 -> How to use
Binary Semaphore
RTOS stands for Real Time Operating System. And as the name
suggests, it is capable of doing tasks, as an operating system
does. The main purpose of an OS is to have the functionality,
STM32 Ethernet #8
where we can use multiple tasks at the same time. Which
UDP Client using
obviously isn’t possible with bare metal. LWIP NETCONN
(RTOS)

This tutorial is the first in the series of many, and will cover the
following:-
1.) Setting up Free RTOS using CubeMX
2.) Benefit of using a RTOS. FreeRTOS Tutorials
#8 -> Software
3.) Creating tasks with or without CubeMX Timers
4.) Using priorities to sort out some common problems

Let’s start by setting up the CubeMX


STM32 Ethernet #7
UDP Server using
LWIP NETCONN
(RTOS)

CubeMX Setup
STM32
This website uses cookies to improve your experience. If you continue to use this site, you agree with ETHENRET
it. Ok
#2. UDP SERVER

1 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

After selecting the controller, the CubeMX will open the default
page for you. Now select the FREERTOS and follow the
screenshot below

STM32 ETHERNET
#3. UDP CLIENT

I am choosing version 1, because it is supported by majority of


STM32 devices.

Next, go to the ‘tasks and queues‘ tab and here you will see a
default task, already created for you. Double click it and you can
see the following information.

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

2 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

Don’t worry about all this information. For now, you have to focus
only on the Task name, priority, and the entry function.
Now, we will create one task here, and below are the properties
of that task

So I am calling it task 2, with normal priority, and the entry


function is Task2_init. You will get a better idea about these,
once we write the program.

Also one important thing about using RTOS is that, we can’t use
systick as the time base. So go to sys, and choose some other
This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

3 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

timebase as shown below

Other than these, I am using UART 2 for transmitting data, and


pins PA0, and PA1 as output.

After the code is generated, open the main.c file and it’s time to
know the importance of using RTOS.

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

4 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

Benefit of using a
RTOS

Let’s assume a situation, where we want to toggle 2 pins, and


each at some respective delays. So basically, we want both the
pins to toggle at the same time. Of-course, it’s not possible with
simple programming, because the microcontroller will execute 1
instruction first and than second. And first one have to wait for
the second execution to finish, even in a while loop.

To overcome this problem, we will use the RTOS. So, we have


created 2 tasks, and if you scroll down the main.c file, you will
see the entry functions for the tasks are defined there.

1 void StartDefaultTask(void const * argument)


2 {
3 �� USER CODE BEGIN 5 ��
4 �� Infinite loop ��
5 for(��)
6 {
7 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok
8 osDelay(1);

5 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

9 }
10 �� USER CODE END 5 ��
11 }
12
13 �� USER CODE BEGIN Header_Task2_init ��
14 ���
15 * @brief Function implementing the Task2 thread.
16 * @param argument: Not used
17 * @retval None
18 ��
19 �� USER CODE END Header_Task2_init ��
20 void Task2_init(void const * argument)
21 {
22 �� USER CODE BEGIN Task2_init ��
23 �� Infinite loop ��
24 for(��)
25 {
26 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_1);
27 osDelay(1);
28 }
29 �� USER CODE END Task2_init ��
30 }

We will toggle the pin PA0 in the default task, and pin PA1 in the
Task2. This way the scheduler will schedule the time for both of
these tasks, so that they get enough time to be executed.
You can check below the oscilloscope reading, when the above
code was executed.

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

6 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

Creating a Task

In order to create a new Task, we have to follow some set of


steps, and they are as follows:-

1.) Define a ThreadID for the task. This variable will store the
unique ID of the task, once created. Later, all the operations will
require this ID.

1 osThreadId Task3Handle;

2.) Define the entry function for the task. This is the main
function of the task. Your program will be written inside it.
This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

7 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

Remember that the tasks in the Free RTOS, are not designed to
handle any return value. So, the entry function should always
have an infinite loop, inside which, your whole program should
be written.

1 void Task3_init (void const * argument)


2 {
3 while (1)
4 {
5 �� do something
6 osDelay (1000); �� 3 sec delay
7 }
8 }

3.) Inside our main function, we need to define the


task first and than create it.

1 �� define thread
2 osThreadDef(Task3, Task3_init, osPriorityBelowNormal
3 ��create thread
4 Task3Handle = osThreadCreate(osThread (Task3), NULL)

• osThreadDef takes the parameters as the name of the


task, the entry function, the priority, instance, and the stack
size.

• After the task is defined, we can create it using


osThreadCreate, and assign the ID to the Task3Handle

Handling Priorities in
This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

8 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

Free RTOS

• Till now, we saw how to multitask using RTOS. But there


are certain problems which comes with it.

• For instance, assume that, we want to send some data


over the UART, via all three tasks, at the same time.

• When we write a program to do so, the result will not be the


same exactly. Instead, the transmission will take place in a
way that only one task will send the data in 1 second, than
another task for another second and so on.

• This happens, when we try to use the shared resources


among the tasks with same priorities.

• Second task have to wait for the first to complete it’s


execution, and than only the control comes to it.

• And similarly the third task will wait for the second one to
finish. You can see this in the video below, it’s better
explained there.

To avoid these situations, we use different priorities for different


tasks. That means we have to redefine our task priorities in the
main function.

1 osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal


2 osThreadDef(Task2, Task2_init, osPriorityAboveNormal
3 osThreadDef(Task3, Task3_init, osPriorityBelowNormal

• Now the Task2 have the highest priority, than Default task,
and Task3 with the lowest.

• When the program runs, Task2 will execute first, than


This websitedefault task
uses cookies and at your
to improve lastexperience.
the Task3. And
If you all three
continue to usetasks willyou agree with it.
this site, Ok

9 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

send the data at the same time.

• You can see this in the video attached below.

RESULT

Check out the Video


Below

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

10 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

This Page Cannot Be Displayed


Based on your organization's access policies, access to this web site ( https://www.youtube.
embed/muOL9SH0p9g?feature=oembed ) has been blocked because the web category
"Streaming Video" is not allowed.

Web Page Blocked as per BEL Internet Access Policy

If you have questions, please contact Please contact at IP-Ph:28998/27777


( [email protected] ) and provide the codes shown below.

DOWNLOAD
SECTION

DOWNLOAD DONATE

Also Read:

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

11 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

STM32 Ethernet #8
FreeRTOS Tutorial UDP Client using FreeRTOS Tutorials
#3 -> How to use LWIP NETCONN #8 -> Software
Binary Semaphore (RTOS) Timers

STM32 Ethernet #7
UDP Server using
LWIP NETCONN STM32 ETHENRET STM32 ETHERNET
(RTOS) #2. UDP SERVER #3. UDP CLIENT

STM32 Ethernet #10 STM32 Ethernet #9 Interface STONE


HTTP Server (Basic) TCP Server and HMI Display with
using LWIP… Client using LWIP… STM32

FreeRTOS Tutorial How to Interface


#2 -> Task STONE HMI with ESP32 #1 || How to
Operations ESP32 use UART || PART1

 Subscribe  Login

Join the discussion

{} [+] 

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

12 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

13 COMMENTS   Newest 

KomKGT

 1 year ago

Hello Master I’m new comer in FreeRTOS so I have one


question.How to calculate the “stack size” in each Task?

0 Reply

admin Author

 Reply to KomKGT  1 year ago

https://www.freertos.org/FreeRTOS_Support_Forum_Archive/
April_2014/
freertos_Exact_task_stack_size_8c443f40j.html#:~:text=The%
20stack%20is%20managed%20by,n%27%20bytes%20of%20
stack%20space.

0 Reply

zengkai

 2 years ago

very useful!

0 Reply

Charoon

 2 years ago

Very good knowledge tutorial STM32F4 with FreeRTOS

0 Reply

misso

 3 years ago

your website is the best tutorial material I ever found.

1 Reply
This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

13 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

negin shiran

 4 years ago

thank you for the nice tutorial!

1 Reply

Farid

 4 years ago

thank you for job!

0 Reply

Ulhas

 4 years ago

Please make single pdf file for this excellent tutorial series on
RTOS

6 Reply

noads

 4 years ago

voting system lies.. counting and result not matching… good


job!..

0 Reply

admin Author

 Reply to noads  4 years ago

Removing it

0 Reply

fraook

 5 years ago

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

14 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

where is the code download? I installed chrome just to download


your code.

3 Reply

admin Author

 Reply to fraook  5 years ago

sorry, i didn’t upload any code for it. This was a simple case.
just set everything from cube mx and run it.

2 Reply

Toan

 5 years ago

Could you help me to use modul NFC modul v3 for


stm32cubemx and Keilc v5 thanks

1 Reply

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

15 of 16 09/06/25, 14:39
Introduction to Free RTOS in STM32 https://controllerstech.com/introduction-to-free-rtos-i...

HOME

ABOUT US

Terms & Conditions

SHOP WITH US

DONATE HERE

CONTACT US

    

Search 

© 2024 ControllersTech®

This website uses cookies to improve your experience. If you continue to use this site, you agree with it. Ok

16 of 16 09/06/25, 14:39

You might also like