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

Lecture 22 Handout

This lecture introduces Real-Time Operating Systems (RTOS) and emphasizes the importance of multitasking, explaining concepts such as tasks, scheduling algorithms, and the distinction between soft and hard real-time requirements. It discusses various scheduling algorithms, including cooperative, round-robin, and preemptive scheduling, and highlights FreeRTOS as a practical example of an RTOS. The lecture aims to equip students with an understanding of task creation, scheduling, and resource management in the context of embedded systems.

Uploaded by

Ngọc Anh
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 views34 pages

Lecture 22 Handout

This lecture introduces Real-Time Operating Systems (RTOS) and emphasizes the importance of multitasking, explaining concepts such as tasks, scheduling algorithms, and the distinction between soft and hard real-time requirements. It discusses various scheduling algorithms, including cooperative, round-robin, and preemptive scheduling, and highlights FreeRTOS as a practical example of an RTOS. The lecture aims to equip students with an understanding of task creation, scheduling, and resource management in the context of embedded systems.

Uploaded by

Ngọc Anh
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/ 34

Introduction to Real Time

Operating Systems (RTOS)


Lecture 22

Josh Brake
Harvey Mudd College
Learning Objectives
By the end of this lecture you will be able to:

Articulate the importance of multitasking and how computers are not true multitasking
systems.
Describe the basic concepts of a real-time operating system
Compare and contrast the tradeoffs of various scheduling algorithms.
Outline
Motivation for multitasking and real-time operating systems
Introduction to key concepts
Tasks
Scheduling
Semaphores
Queues
Interrupts and Events
Introduction to FreeRTOS
Multitasking Scenarios
Printing information to a display in response to keyboard input
Car
Airbag response
Braking system
Robotics
Flight system in a drone
Control or signal processing algorithms

Soft vs. hard real-time requirements

Soft real-time requirements are those that state a time deadline—but breaching the
deadline would not render the system useless.
Hard real-time requirements are those that state a time deadline—and breaching the
deadline would result in absolute failure of the system.
Why multitasking?
Bare-metal programming
Setup and initialization – runs once
Infinite loop – runs continuously and handles main tasks
Bare-metal + interrupts
Can now incorporate additional functionality which quickly responds to inputs and
can guarantee that we don’t miss important events.
Examples: Receiving and processing UART data, catching button inputs
But as we build more and more complicated programs, it is hard to guarantee specific
timing constraints are met.
Many different scenarios
Lots of edge cases which makes things very difficult to debug.
Why multitasking?
Enter the concept of multitasking
Multitasking: means that several tasks (or programs) are processed in parallel on the
same CPU
Only have a single core on your microcontroller so this is not true parallelism, just
swapping different tasks in and out
Operations inside your bare-metal infinite loops are tasks.
For example, consider that you want to blink 2 LEDs at different frequencies. In bare-
metal, you could have your infinite loop use timers to poll a timer and then toggle
the LEDs based on the current time.
Works, but inefficient as the processor is always running.
Task States

Barry, Richard. Mastering the FreeRTOS Real Time Kernel: A Hands-On Tutorial Guide. 2016.
The Mudd Multitasking Kernel
You are taking E155, Clinic, and an HSA. In addition, you want to sleep 8 hours a night
and have time to hang out with your friends playing board games over Zoom.
Imagine you must manage your clinic mid-year report, MicroPs final project, and must
read a book and write a paper for your HSA in addition to chatting with your friends.
You only have one brain.
What are different ways you can manage your tasks?
Scheduling Algorithms
The scheduling algorithm decides which task is running on the core.
Three main algorithms:

Co-operative scheduling
Round-robin scheduling
Preemptive scheduling
Co-operative scheduling
1 Task1() {
2 // Task 1 code
3 }
4
5 Task2() {
6 // Task 2 code
7 }
8
9 Task3() {
10 // Task 3 code
11 }
12
13 while(1) {
14 Task1();
Ibrahim, Dogan. ARM-Based Microcontroller Multitasking
15 Task2(); Projects: Using the FreeRTOS Multitasking Kernel.
16 Task3();
17 }

Each task must yield control or else it can starve all other tasks.
Co-operative scheduling
Rules

1. Tasks must not block the overall execution, for example, by using delays or waiting for
some resources and not releasing the CPU.
2. The execution time of each tasks should be acceptable to other tasks.
3. Tasks should exit as soon as they complete their processing.
4. Tasks do not have to run to completion and they can exit for example before waiting for
a resource to be available.
5. Tasks should resume their operations from the point after they release the CPU.
Round-robin scheduling
The scheduler creates a periodic time slice and equally divides CPU use between tasks.

Advantages:

It is easy to implement.
Every task gets an equal share of the CPU.
Easy to compute the average response
time.
Ibrahim, Dogan. ARM-Based Microcontroller Multitasking
Disadvantages Projects: Using the FreeRTOS Multitasking Kernel.

It is not generally good to give the same


CPU time to each task.
Some important tasks may not run to
completion.
Not suitable for real-time systems where
tasks usually have different processing
requirements.
Preemptive Scheduling
Most common scheduling algorithm in real-time systems
Tasks are assigned
Higher priority tasks can preempt lower priority tasks to take the CPU
Need to be careful to assign priorities appropriately or you can starve lower priority
tasks

Q: How are tasks and their priorities


different than interrupts?

Ibrahim, Dogan. ARM-Based Microcontroller Multitasking


Projects: Using the FreeRTOS Multitasking Kernel.
Scheduling Algorithm Goals
Be fair such that each process gets a fair share of the CPU.
Be efficient by keeping the CPU busy.
The algorithm should not spend too much time to decide what to do.
Maximize throughput by minimizing the time users must wait.
Be predictable so that same tasks take the same time when run multiple times.
Minimize response time.
Maximize resource use.
Enforce priorities.
Avoid starvation.
Other scheduling algorithms
First-come first-served
Shortest time remaining first
Longest time remaining first
Multilevel queue scheduling
Dynamic priority scheduling
FreeRTOS
Introduction to FreeRTOS
We will use FreeRTOS as our example
Other popular RTOSes include Zephyr, NuttX, VxWorks. Varying licensing
argreements.
Like a programming language: once you learn one RTOS, concepts transfer to
others.
FreeRTOS licensed under MIT license – very permissive.
Can be used in commercial applications and users retain all ownership of their IP.
Code Structure of FreeRTOS

Barry, Richard. Mastering the FreeRTOS Real Time Kernel: A Hands-On Tutorial Guide. 2016.
Data Types and Naming Conventions
Port specific data types - holds tick count value
– Base type value which is most efficient data type on a given
system. Typically the word size.
Naming Conventions: Variable Names
Variable Names - prefixes tell their type
Prefix Type
c char
s int16_t (short)
i int32_t (long)
x BaseType_t and other non-standard types (structs,
task handles, queue handles, etc.)
Naming Conventions: Function Names
Function Description
vTaskPrioritySet() returns a void and is defined within task.c.
xQueueReceive() returns a variable of type BaseType_t and is defined
within queue.c.
pvTimerGetTimerID() returns a pointer to void and is defined within
timers.c.
Template Project

Barry, Richard. Mastering the FreeRTOS Real Time Kernel: A Hands-On Tutorial Guide.
2016.
Creating Tasks
Parameters

pvTaskCode - pointer to C function that


implements that task
pcName – Descriptive name for the task
Return
usStackDepth – size of stack to be
pdPass or pdFail - indicates if task was allocated by the kernel when creating the
successfully created. stack (in words)
pvParameters – pointer to void to pass
in parameters. Need to cast void pointer to
correct type inside the function to use it.
uxPriority – Defines the priority of the
task
pxCreatedTask – handle to created
task
Printing to Terminal

1 int main(void) {
2 xTaskCreate(vTask1, “Task 1”, 1000, NULL, 1, NULL);
3 xTaskCreate(vTask2, “Task 2”, 1000, NULL, 1, NULL);
4 vTaskStartScheduler();
5
6 for( ;; );
7 }

Barry, Richard. Mastering the FreeRTOS Real Time Kernel: A Hands-On Tutorial Guide. 2016.
Printing to Terminal Example Output
1 int main(void) {
2 xTaskCreate(vTask1, “Task 1”, 1000, NULL, 1, NULL);
3 xTaskCreate(vTask2, “Task 2”, 1000, NULL, 1, NULL);
4 vTaskStartScheduler();
5
6 for( ;; );
7 }
Example: Priorities
Revisiting Not Running State
Three Options

Suspended
Ready
Blocked
Example: Printing with better delay using blocked
state
Example: Sending tasks to blocked state
Key Terms in Real-time Systems
Tasks – C functions which indicate things to do. Implemented as infinite loops.
Scheduling – The process of determining what task is currently running
Semaphores – an abstract data type which controls access to a resource used by
multiple tasks
Queues – A way to communicate between tasks (Chapter 4 of Mastering FreeRTOS)
Interrupts and Events – how to safely integrate interrupts with the RTOS kernel (Chapter
6 of Mastering FreeRTOS)
Summary
Multitasking is an important concept in advanced embedded systems
Have timing constraints that must be met (both soft and hard deadlines)
Hard to debug and manage systems with increasing complexity while guaranteeing
all deadlines are met.
Real-time operating systems introduce a scheduler which enables the programmer to
efficiently use - CPU cycles while ensuring deadlines are met.
FreeRTOS is an open and accessible platform to learn RTOS concepts like tasks, queues,
semaphores, and resource management.
FreeRTOS Examples
Learning Goals
To understand the following key concepts of Real-time Operating Systems through
examples in FreeRTOS

Task creation
Basic scheduling
Task priorities and preemption
Outline
FreeRTOS Refresher
Examples for today
1. Task creation with LED blink example: 01_task_creation_blink_led.c
2. Passing parameters into task using pvParameters:
02_passing_parameters_blink_led.c
3. Multiple tasks with two serial prints: 03_multiple_tasks_print.c
4. Simple preemption example: poll button and blink LED and single print:
04_simple_preemption.c

You might also like