Topic8 RTOS-Intro Mco556
Topic8 RTOS-Intro Mco556
www.freertos.org
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
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
5
What is a General-Purpose Operating System?
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:
• 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
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
14
Tasks and Task Scheduling in FreeRTOS continues
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
16
A Simple Program Structure using FreeRTOS With Two Tasks
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:
18
Creating a Task in FreeRTOS
xTaskCreate(hello_task,"Hello_task",configMINIMAL_STACK_SIZE + 100,NULL,hello_task_PRIORITY, NULL);
19
Typical Project File Structure without RTOS in MCUXpresso IDE
20
Project File Structure with FreeRTOS in MCUXpresso IDE
21
The FreeRTOSConfig.h Header File
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
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