Table of Contents
Intro
From this blog, we will be starting with RTOS(FREERTOS). In RTOS there are many concepts & things related to kernel, tasks management, task scheduling, IPC, and many other terminologies. In the tutorial series, we are not going into a theoretical understanding of the RTOS topics, we would be mainly focusing on developing different applications in RTOS. Like how to use mutexes, and semaphores in different applications, we will get into the RTOS kernel and understand its different components like scheduler, task management and etc., compare different scheduling algorithms in terms of time and how to create multiple tasks and pass information between them, how to use Software timers, queues and etc. while writing firmware for industrial projects. As there are many resources & tutorials to understand these theoretical concepts and how to use RTOSes APIs to build different kinds of applications is not much there. So, we would be focusing on that thing.
RTOS is used in applications like that AGV(Automated Guidance Vehicles), Automotive ECU, and industrial automation products (Like metering instruments, industrial smart sensors and etc.). I have a personal interest and passion for these, so I would be making projects using RTOS related to these by interfacing sensors and modules used in these applications. Ultimately making the different tasks run together to utilize the power of RTOS to make an efficient project.
To Know about what RTOS is, refer to this blog What is Real Time Operating System (RTOS) | gettobyte
Now to start with RTOS, there are many open source RTOSes available like FreeRTOS, embed OS, Zypher OS, ThreadX OS and etc. But starting with FreeRTOS would be my choice that too with STM32 because the integration of STM32CubeIDE with FREERTOS is pretty easy & gives an easy starting point for beginners. Subsequently, we will migrate to NXP controllers with FreeRTOS. Once getting hands-on with FreeRTOS via STM32.
Requites:
- Bare metal coding On MCU\’s (Register level programming, working of MCU\’s, SoC/processor architecture, boot process: linker script, startup code, GNU GCC, Debugging and etc.).
- Knowledge of peripheral of UART, I2C, SPI, ADC, Interrupts, DMA and etcC/CPP programming, etc).
- Device driver development to integrate different sensors & Modules with MCU. (Datasheet reading, communication protocols, sensors and modules working/Cpp programming).
- Knowledge of C/CPP programming.
- Software Development Kit, IDE, HAL, RTD drivers and etc.
FreeRTOS
What is FreeRTOS?
FreeRTOS is Real Time Operating System, which is highly used in industry. It is also best recommended for beginners to start with if one wants to learn RTOS, because of its wide number of resources available, documentation, and community support. FreeRTOS is a market-leading embedded System RTOS, which is available for many processor architectures, it supports more than 40 processor architectures including ARM cortex processors. Also portable to a wide number of compilers and hardware platforms.
FreeRTOS is supported by a wide number of silicon vendor chips like NXP Semiconductors, STMicroelectronics, Texas Instruments, and many others. You can find a full list of that here: Microcontrollers and compiler toolchains supported by FreeRTOS.
Features of RTOS
- Highly configurable kernel.
- FreeRTOS has Microkernel architecture.
- Pre-emptive or co-operative operation.
- Inter Processor Communication techniques like Mutex and Semaphore for synchronization.
- Support for message queues.
- Software Timers.
- Event groups.
- Memory management to maintain heap memory.
- Also, advanced features for debugging the tasks in Real Time.
FreeRTOS Kernel
The kernel is the most important thing and core of the RTOS. For the understanding you can have analogy that Human body is RTOS, and Human Brain is RTOS kernel. As without our brain we can\’t do any work and loose our capabilities. Same without the kernel, RTOS can\’t work. It is kernel which makes electronic devices, a multitasking system. It is kernelling whose responsibility is to run different tasks and have communication between them. Kernel act as the interface between system resources and user application. Kernel has different services for making multitasking system. Just like human brain has different biological parts to make us work. ( In the upcoming blogs and in this also, i would be using analogy of Human brain and Human body for understanding).
The basic services of RTOS kernel are:
- Task/Process Managment (Creation and deletion of tasks)
- Task/Process Scheduling (Scheduler)
- task/Process synchronization (Inter Process Communication)
- Memory Managment (heap & static)
- Interrupt Handling
- Error/Exception handling
Coming to the FreeRTOS, it has highly configurable kernel. We can configure how to use the FreeRTOs kernel and which features/services of it. FreeRTOS is configured by FreeRTOSConfig.h header file. This file contains application specific definitions to configure FreeRTOS kernel. Every RTOS has one such configuration file which has macros, which can be enabled or disabled. Like we can configure which scheduling algorithm to use, how to create the tasks (static or dynamic), which IPCs to use for communication between tasks, enabling of run-time trace and debug features.
When you will download the FreeRTOS kernel zip file, you will find many numbers of files. But core FreeRTOS is runned by just 2 files tasks.c and list.c. They are located in FreeRTOS/Source directory. In addition to these files, imp files whose Api\’s we would be using are queue.c, timers.c, event_groups.c, croutines.c.
Examples of RTOS Kernel:
FreeRTOS Libraries
In addition to FreeRTOS kernel, there are add on libraries to extend the RTOS functionality.
- FreeRTOS+: To add support of TCP/IP stack and command line interpreter.
- FreeRTOS Core: To add support of cloud connectivity with HTTP, MQTT, JSON, Cellular interface.
- FreeRTOS AWS: To add support of AWS services.
How To Start with RTOS?
So, as of now, I have told you many things about RTOS and FreeRTOS. Most of you must have come up with that now how to start with FreeRTOS to learn RTOS. When we go to the FreeRTOS website for starting with RTOS, there are many things on the website, lots of documentation, articles, and resources on the website that might become overwhelming and confusing for beginners to start with it.
At least for me, it does become like that, so I gathered my mind and come to one conclusion that at first, I would get a high-level overview and understanding of RTOS, RTOS kernel, and its components. So, for viewers to get this insight, I would highly recommend reading this PDF. After that, I analyzed that theoretical understanding would only be cleared when I build some applications using the RTOS components and services.
That is making a project which has multiple inputs, processing input data, and then controlling multiple outputs. And using the RTOS to make the project real-time and efficient. Many times people start to think of complex projects to learn the RTOS, but I would keep it simple. Just keep on integrating different sensors and modules in one project, component by component. You will see that your project would ultimately get fit the criteria to use the RTOS.
Say we have taken 2 sensors at the input, both of these sensors continuously sense the electrical parameters of the respective loads attached to them.
- INA219 DC metering IC, and
- The HLW8012 AC metering IC.
Simultaneously processing of the sensed data takes place to calculate the electrical parameters of the respective sensor. On the other hand, in the output
- the data has to be shown on an OLED screen,
- logging the data on flash memory,
- sending data to the cloud using a Wi-Fi-module to make it a SMART meter.
- Use of RS485 or RS232 protocol to send data to local systems.
So, you can see we have multiple tasks to be handled which can still be breakdown into more sub-tasks. So, the first thing that we have to understand is how are tasks created in RTOS, how task management and synchronization take place, how-to tasks communicate with each other and etc.
So, We will start with Creating different tasks in FreeRTOS at first. How does their memory allocation takes place, what configuration that we have to do for the creation of tasks(Task Control Block) and understand concepts related to it
How To Start with FreeRTOS?
We will be starting with FreeRTOS using STM32 controllers and STM32CubeIDE. The reason for selecting the STM32 platform to start with FreeRTOS is because of the kind of resources it provides for building different applications. Using its MX configuration tool, it becomes very easy for beginners to automatically generate the code of FreeRTOS services/Features. By this one can easily and directly understand the APIs available in FreeRTOS for its different services and how they are working internally. Also, STM32CubeIDE directly downloads the FreeRTOS kernel package and includes it in our project.
Also STM32CubeIDE provide some debugging features specific to FreeRTOS, so we would also explore them to understand that how should be debug RTOS based projects and applications.
So initial thing that we would be doing is creating different tasks on the STM32F103 controller using FreeRTOS API. We would be integrating different sensors and modules into our controller by creating their driver using MCU peripherals.
- ESP8266 as Client With HOST MCU.
- W25QSPI Flash memory for logging with HOST MCU.
- INA219 DC Current sensor with HOST MCU.
- HLW8012 AC metering IC with HOST MCU.
- OLED Screen Display with HOST MCU.
- DHT11 temperature sensor with HOST MCU.
And then these would be used as tasks that would be synchronized and managed by the FreeRTOS kernel.
CMSIS & RTOS(FreeRTOS)
CMSIS stands for Cortex Microcontroller Software Interface Standard), is just like a wrapper API\’s to use Cortex processor peripherals and middleware’s.
CMSIS provides the APIs to use Cortex processor peripherals like NVIC, MPUs, SYS, DEBUG Coresight and etc. CMSIS also provides generic API\’s to be used on Cortex-based MCU for using RTOS.
Means, it is like if their are some functions of FreeRTOS RTOS to create a TASK, then CMSIS RTOS will provide a wrapper API(osthreadCreate()) that will create tasks. Internally this Wrapper API(osthreadCreate()) will be using the function of FreeRTOS(xTaskCreate) to create a task. Now say if a user is working with mbed RTOS, so there would be some function in it to create a task, then in that case also CMSIS RTOS will provide the same wrapper API(osthreadCreate()), which will create the task, and internally CMSIS RTOS API\’s will be using the function of mbedOS to create tasks.
So in a way for the application developer, it becomes easy to port to different RTOSes using CMSIS RTOS. There are only 2 files in CMSIS RTOS: cmsisrtos.h and cmsisrtos.c.
However, it is not recommended to use CMSIS RTOS APIs as it adds extra HAL to our software. Also, many features and functionalities of RTOS cant be used because of the limitations of CMSIS RTOS API. So we are going to avoid the use of CMSIS RTOS APIs wherever possible but STM32CubeIDE by default generates the FreeRTOS code using the CMSIS interface. We need to tackle CMSIS RTOS APIs to some extent but once we get a strong foundation, we will directly use FreeRTOS APIs to generate our code.
Author: Kunal Gupta
Author