0% found this document useful (0 votes)
31 views27 pages

Topic8 RTOS-Intro Mco556

The document provides an introduction to real-time operating systems (RTOS) and FreeRTOS. It defines an RTOS as an operating system optimized for embedded real-time applications to ensure timely and predictable responses to events. FreeRTOS is introduced as a popular open-source RTOS that provides task scheduling, inter-task communication and synchronization. Tasks are the basic units of execution in an RTOS and FreeRTOS supports assigning priorities and time slices to tasks to determine which runs at what time. The document outlines some basic structures for organizing an FreeRTOS project with multiple tasks.

Uploaded by

Dark Matter
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)
31 views27 pages

Topic8 RTOS-Intro Mco556

The document provides an introduction to real-time operating systems (RTOS) and FreeRTOS. It defines an RTOS as an operating system optimized for embedded real-time applications to ensure timely and predictable responses to events. FreeRTOS is introduced as a popular open-source RTOS that provides task scheduling, inter-task communication and synchronization. Tasks are the basic units of execution in an RTOS and FreeRTOS supports assigning priorities and time slices to tasks to determine which runs at what time. The document outlines some basic structures for organizing an FreeRTOS project with multiple tasks.

Uploaded by

Dark Matter
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/ 27

Seneca College – School of Electronics and Mechanical Engineering Technology (SEMET)

MCO556 – ARM® Microcontroller for Real Time


Embedded Applications

Topic 8 – Introduction to the Real


Time Operating System (RTOS)

www.freertos.org

Author and Presenter: Benjamin Shefler


Copyright © 2020 by SEMET

1
Agenda:

• Foreword
• What is an operating system (OS)?
• What is a Real-time operating system (RTOS)?
• Introduction to FreeRTOS
• Tasks and task scheduling
• How a FreeRTOS-based project is organized
• System Stack and Heap
• Overview of the Lab on RTOS

2
Foreword

• The RTOS topic is vast and complicated


• RTOS is utilized in complex multitasking embedded systems
• RTOS utilization is done primarily by competent developers
• In industry, RTOS is widely used because of its prevailing advantages
• MCO556 course will cover the basics of RTOS, so that students will be able to
create simple, yet practical RTOS-based applications
• RTOS utilized in this course is called FreeRTOS from Amazon Web Services (AWS)

www.freertos.org

3
Embedded System without OS Embedded System with OS
(bare-metal)

Application
Application
Operating
Hardware System

Hardware

4
The Structure Diagram of an RTOS-based MCU System

Application Code

RTOS
Networking Device
Protocols Drivers

RTOS KERNEL:
Scheduler
Synchronization Primitives

Debug
File System
Services

Board Support Package

5
What is a General-Purpose Operating System?

• An operating system is a computer program that supports a computer's basic


functions, and provides services to other programs (or applications) that run on
the computer
• The applications provide the functionality that the user of the computer wants or
needs
• Most operating systems appear to allow multiple programs to execute at the
same time. This is called multi-tasking. In reality, each processor core can only be
running a single thread of execution at any given point in time
• A part of the operating system called Scheduler is responsible for deciding which
program to run at what time, and provides the illusion of simultaneous execution
by rapidly switching between each program

6
A Review of Bare-metal (no OS) Scheduling – “Super Loop”
Tasks are executed sequentially:

7
Some software concurrency can be created by using ISRs:

• Sequential tasks + Interrupt Service Routines → conveys the impression of multitasking


• Some hardware concurrency is done by using built-in Timers, ADCs, and DMA
• Using switch statements and flags allow selective task execution
8
What is a Real Time Operating System (RTOS)?

• A Real Time Operating System is an operating system that is optimised for use in
embedded real time applications
• Their primary objective is to ensure a timely and deterministic (predictable)
response to events. An event can be external, like a limit switch being hit, or
internal, like a character being received
• Using a real time operating system allows a software application to be written as
a set of independent tasks. Each task is assigned a priority to ensure that the task
with the highest priority will be running when it is able to run
• The Scheduler in a Real Time Operating System is designed to provide a
predictable (deterministic) execution pattern
• The Kernel is the part of the operating system that is responsible for task
management, and inter task communication and synchronization

9
Do I need an RTOS?

YES, if
• your system contains multiple deterministic tasks (multitasking)
• there are complex software timing requirements (predictable time taken for tasks
and known maximum response times)
• there is a need for one task to interrupt another (pre-emption, task prioritization)
• there is a need to synchronize tasks and pass data around the application
• you integrate multiple software components from multiple sources
• connectivity stacks such as USB, Bluetooth, TCP/IP, and Wi-Fi are present
• maintaining software is easier by using RTOS
Note: you can use RTOS diagnostic tools to save time during the debugging process.

10
Super Loop vs. RTOS on Embedded System

Super Loop’s main advantage – it is straightforward to implement.


Super Loop’s known problems:
• It depends on the length of interrupt service routines (ISR)
• It needs to keep the synchronization between ISRs
• It has poor predictability (nested ISRs) and extensibility
• If there is a change of the ISR – and the Super Loop ripples through the entire
system

11
Super Loop vs. RTOS on Embedded System continues

RTOS’s advantages:
• All computation requests are encapsulated into tasks and scheduled based on
the demand
• Better program flow and event response
• (Illusionary) multitasking
• Concise ISRs thus deterministic
• Better inter-task communication
• Better resource management
• Tasks can be created, suspended, restored, and deleted “on the fly”
RTOS’s known dangers:
• Priority inversion, Deadlock, Thread starvation, etc.

12
What is FreeRTOS?

• FreeRTOSTM is a market leading RTOS from Amazon Web Services (AWS) that
supports more than 35 architectures
• It is professionally developed, strictly quality controlled, robust, supported, and
free to embed in commercial products without any requirement to expose your
proprietary source code
• FreeRTOS is a class of RTOS that is designed to be small enough in size to run on a
microcontroller, and therefore,
• FreeRTOS provides the core real-time scheduling functionality, inter-task
communication, timing, and synchronisation primitives (mutexes, semaphores,
etc.)
• Additional functionality, such as a command console interface, or networking
stacks, can then be included with add-on components

13
Tasks and Task Scheduling in FreeRTOS

• A thread of execution is called a task


• The Real Time Scheduler is the part of the RTOS kernel that is responsible for
deciding which task should be executing
• After the scheduler has been started, only tasks and interrupts will ever execute
• The Idle task is created automatically when the scheduler is started
• The scheduler must be appropriately configured by the developer because
there are multiple, different ways of implementing task scheduling

14
Tasks and Task Scheduling in FreeRTOS continues

The developer can:


• assign equal time period (slice) for every task
• configure a specific time interval at which an individual task will enter the running
state (will execute) guaranteed every time
• assign a priority for each task
• block a task for a specified period of time
• synchronize tasks and much more…

15
Just two examples of how tasks can be scheduled:

Task 1 Task 2 Task 3 Task 1  The simplest RTOS round robin scheduling – all
tasks have the same priority and equal time slices
time Time
t1 t2 t3 t4 t5 tn
slice

custom allocated
total time for Task 1

Task 1 blocked Task 1 blocked


 Task 1 has a higher priority than tasks 2 and 3.
Task 1 must have a time delay to stay in the
Task 2 3 3 2
blocked state to allow low priority tasks to be
executed, otherwise, only Task 1 will be selected
by the scheduler
Task 3 Task 3 Task 3
pre-empted resumed completed

16
A Simple Program Structure using FreeRTOS With Two Tasks

#include "FreeRTOS.h“ /*FreeRTOS kernel includes*/


|
#define Task_A_PRIORITY (1) /*task priorities defines*/
#define Task_B_PRIORITY (1)
|
static void Task_A(void *pvParameters); /*task prototypes*/
static void Task_B(void *pvParameters);
|
int main(void){
|
xTaskCreate(Task_A,---); /*function to create a task*/
xTaskCreate(Task_B,---);
|
vTaskStartScheduler(); /*task scheduler*/
for (;;); /*endless loop and absolutely nothing else!*/
}
/*task routines outside main*/
static void Task_A(void *pvParameters){---}
static void Task_B(void *pvParameters){---}

17
Task Functions
Tasks are implemented as C functions. Each task is a small program in its own right. It has an entry
point and it will normally run forever within an infinite loop. It will not exit and not “return”
anything but “void” as in the Lab 7 example:

static void hello_task(void *pvParameters)


{
for (;;)
{
PRINTF("Hello world.\r\n");
}
}
Another special thing about the task function is its prototype – it will take only a void pointer
parameter as in the example below:

static void hello_task(void *pvParameters);

18
Creating a Task in FreeRTOS
xTaskCreate(hello_task,"Hello_task",configMINIMAL_STACK_SIZE + 100,NULL,hello_task_PRIORITY, NULL);

xTaskCreate /* function to create a task */


(hello_task, /*name of the function that implements the task (a pointer to the
task)*/
"Hello_task", /*descriptive name for the task (for debugging)*/
configMINIMAL_STACK_SIZE + 100, /*allocated memory space for the tasks’
stack in words, not in bytes*/
NULL, /*parameters to pass to the task*/
hello_task_PRIORITY,/*priority at which the task will execute*/
NULL /*used to pass a handle to the created task – optional, can be set to NULL*/
);

19
Typical Project File Structure without RTOS in MCUXpresso IDE

• No FreeRTOS files (or any middleware) are


included in the project
• board folder includes all hardware and clock
configuration items
• source folder includes the application file that
runs the example (hello_world.c). This file can
be edited by the user to change the functionality
of the demo
• doc folder includes a readme.txt file with basic
demo setup instructions, functionality, and
expected output

20
Project File Structure with FreeRTOS in MCUXpresso IDE

• A FreeRTOS-based project includes an additional folder


labelled freertos, which has a subfolder labelled
freertos_kernel that contains the FreeRTOS kernel
files, see the upper red arrow on the left.

• The source folder, in addition to the application .c file,


includes the RTOS’ configuration file
FreeRTOSConfig.h, which contains typical configuration
and porting items that relate to the project, see the
lower red arrow on the left.

21
The FreeRTOSConfig.h Header File

• FreeRTOS is configured by a header file called FreeRTOSConfig.h


• FreeRTOSConfig.h is used to tailor FreeRTOS for use in a specific application
• Every demo application contains a FreeRTOSConfig.h file, and therefore, it is
never necessary to create a FreeRTOSConfig.h file from scratch. Instead, it is
recommended to start with, then adapt, the FreeRTOSConfig.h used by the demo
application (provided for the FreeRTOS port [board] in use)
• FreeRTOSConfig.h contains application specific definitions like the clock tick
frequency, stack size, heap size, max priority levels, and many other items

22
An Example Content of the FreeRTOSConfig.h Header File
#define configUSE_PREEMPTION 1
1 <– feature is available
#define configUSE_TICKLESS_IDLE 0
0 <– feature is not
#define configCPU_CLOCK_HZ (SystemCoreClock)
#define configTICK_RATE_HZ ((TickType_t)200)
available
#define configMAX_PRIORITIES 5
#define configMINIMAL_STACK_SIZE ((unsigned short)90) To make a feature
#define configMAX_TASK_NAME_LEN 20 available, change 0 to 1
#define configIDLE_SHOULD_YIELD 1 and vice versa
#define configUSE_TASK_NOTIFICATIONS 1
#define configUSE_MUTEXES 1
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_COUNTING_SEMAPHORES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configUSE_TIME_SLICING 0
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
#define configTOTAL_HEAP_SIZE ((size_t)(10 * 1024))
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1

23
Creating Your Own Application with FreeRTOS

• The simplest and highly recommended way to create your own FreeRTOS
application is to base it on the pre-configured demo application, which is
provided for your chosen port (microcontroller) by the MCUXpresso SDK package
• Once you have the demo application running first, then incrementally remove the
demo functions and source files and replace them with your own application
code
• The pre-configured demo applications are supplied to ensure projects already
exist with the correct RTOS kernel source files and header files included, and the
correct compiler options set, and therefore, build with the minimum of user
effort

24
MCUXpresso SDK – “boards” folder with FreeRTOS examples

rtos_examples folder includes:


• FreeRTOS examples that showcase RTOS
specific features such as: events,
mutexes, semaphores, software timers
• Driver examples that feature the native-
RTOS drivers (I2S, SPI, UART)
• “freertos_hello” demo that serves as an
excellent starting project for RTOS-based
applications

25
Lecture Summary
• A Real Time Operating System (RTOS) is an operating system designed to manage
hardware resources of an embedded system with very precise timing and a high
degree of reliability
• RTOS characteristics include multitasking, task interrupts, task priorities,
determinism, known max response times, predictable time taken for tasks, and
many more
• It is highly recommended that new applications are created by modifying an
existing pre-configured RTOS demo application
• When implementing an RTOS, the developer must ensure that every task will run
when it is supposed to run and each task will occupy only the projected time
interval
• During the next lab and lecture you will learn some of FreeRTOS synchronization
methods, which are used in multitasking systems

26
Overview of the lab on FreeRTOS

Lab objective:
• Clone the SDK’s RTOS-based demo program “freertos_hello”
• Analyze it thoroughly
• Modify it so that there will be two tasks and later three tasks running in turns
• Learn how to use two simple RTOS scheduling methods:
vTaskDelay() – to bring a running task into a blocked state, which will allow
another task to run
taskYIELD() – to allow another task to run immediately after the running task is
done

27

You might also like