ESP32 Arduino: Using FreeRTOS functions

The objective of this post is to introduce some basic concepts of FreeRTOS on the ESP32.


Introduction

The objective of this post is to introduce some basic concepts of FreeRTOS on the ESP32.

First of all, it’s important to take in consideration that we can use functions from the FreeRTOS libraries when coding for the ESP32 with the Arduino IDE. This can easily be seen by this example provided in the libraries, which uses semaphores and mutexes.

The FreeRTOS libraries are available here in the GitHub repository.

So, the possibility of using FreeRTOS functionalities opens a very powerful set of tools that extend beyond the capabilities of the Arduino support.

Nevertheless, as we will in future posts, we can still use Arduino functions. For example, we will use our regular Serial.begin and Serial.print to handle the printing of messages to the serial port.

The concepts of tasks and multi tasking in a operating system are too complex for explaining in this introductory post. Nevertheless, there are lots of good material available around the web that explain these concepts. Also, FreeRTOS as a very good set of explanation pages.

So, I will try to provide some context on future posts, but there will be some things that are going to be left out. I will also leave some links in the related content section that can help you getting started.

For now, we will start by a brief explanation of what is FreeRTOS and what are tasks.

 

What is FreeRTOS?

FreeRTOS is a real time operating system designed to run on microcontrollers [1]. Naturally, since microcontrollers are resource constrained, any operating system to run on them needs to be adequately designed.

Nevertheless, as mentioned, FreeRTOS is a real time operating system, which is a particular type of operating systems. To understand what a real time operating system is, we need to take a look at the regular operating systems that we are used to.

For example, when we are using Windows or Linux, we can have multiple processes and applications open at the same time, and it seems that they are all running at the same time. For the end user, this is transparent.

Nevertheless, for a computer with a single core (things changed a bit with multi core systems, but let’s stick to a simple example) only one process can be executing at the same time [2]. So, what computers do is changing the execution between tasks very rapidly, so the the end user has the sensation that things are occurring in parallel [2].

Naturally, these changing of processes may occur accordingly to different algorithms but typically it ensures that each process gets a fair amount of CPU to execute. The specifics will depend on the operating system in question. But typically, one cannot predict the flow of execution of the multiple processes, which are not deterministic.

On the other hand, a scheduler in a Real Time Operating System is built to have a deterministic execution pattern [1], which is needed for applications with real time requirements. Note that real time execution means meeting every defined deadline [3], it doesn’t necessarily mean executing fast.

So, we can have parallelism, so we don’t need to execute each task sequentially, as we are used to, for example, in an Arduino environment. On top of that, we have a lot of control over these tasks, since we can predict the flow of execution accordingly to their priorities.

What are tasks?

Tasks are typically the building blocks of Real Time Operating Systems [4]. They execute on their own context and the scheduler is responsible for deciding which task is executing at a given moment [5] in a single core.

Nevertheless, as said before, we can run multiple tasks in parallel (only one executes at each time, but there may be many instantiated) so our programs are easier to code.

In FreeRTOS tasks are implemented as C functions and follow a pre-defined prototype [6], as can be seen bellow.

void taskImplementingFunction( void * parameter )

So tasks will be a very important concept that we need to analyse in future tutorials.


Related content


References

[1] http://www.freertos.org/about-RTOS.html

[2] http://www.tldp.org/HOWTO/Unix-and-Internet-Fundamentals-HOWTO/multitasking.html

[3] http://stackoverflow.com/questions/17308956/differences-between-hard-real-time-soft-real-time-and-firm-real-time

[4] https://sites.google.com/site/rtosmifmim/home/tasks-and-states

[5] http://www.freertos.org/taskandcr.html

[6] http://www.freertos.org/implementing-a-FreeRTOS-task.html

Advertisements
This entry was posted in ESP32 and tagged , , , , . Bookmark the permalink.

3 Responses to ESP32 Arduino: Using FreeRTOS functions

  1. Pingback: ESP32 Arduino: Creating a task | techtutorialsx

  2. Pingback: ESP32 Arduino: Passing a variable as argument of a FreeRTOS task | techtutorialsx

  3. Pingback: ESP32: Running code on a specific core | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s