Real-Time Operating Systems (RTOS)

39
Real-Time Operating Systems (RTOS) Chaiporn Jaikaeo Department of Computer Engineering Kasetsart University 01204322 Embedded System Revised 2020-02-16 Materials partially taken from Lecture Slides by Prof. King

Transcript of Real-Time Operating Systems (RTOS)

Page 1: Real-Time Operating Systems (RTOS)

Real-Time Operating Systems(RTOS)

Chaiporn Jaikaeo

Department of Computer EngineeringKasetsart University

01204322 Embedded System

Revised 2020-02-16Materials partially taken from Lecture Slides by Prof. King

Page 2: Real-Time Operating Systems (RTOS)

2

Outline• Embedded operating systems• Real-time operating systems (RTOS)• RTOS characteristics

• RTOS implementations• Hands-on activity with Mbed OS

Page 3: Real-Time Operating Systems (RTOS)

3

Operating Systems• The collection of software that manages

a system’s hardware resources◦ File system, a GUI, and other components

• Often times, a kernel is the crucial part of such a collection

• Characteristics◦ Resource management◦ Interface between application and hardware◦ Library of functions for the application

User

Application

Operating System

Hardware

Page 4: Real-Time Operating Systems (RTOS)

4

Embedded Operating Systems• The OS and application logic are combined into a single

unit• Characteristics:

◦ Resource management◦ Primary internal resources

◦ Less overhead◦ Code of the OS and the

application mostly reside inROM

User

Operating System + Application

Hardware

Page 5: Real-Time Operating Systems (RTOS)

5

Desktop vs Embedded OS

User

Operating System + Application

Hardware

User

Application

Operating System

Hardware

Application Application

Desktop OSApplications are compiled

separately from the OS

Embedded OSApplication is compiled and linked together with the OS

Page 6: Real-Time Operating Systems (RTOS)

6

Application

Characteristics of Embedded OS• Embedded OS needs to be configurable:

◦ No single OS fit all needs à install only those needed◦ e.g., conditional compilation using #if and #ifdef

• Device drivers often not integrated into kernel◦ Embedded systems often application-specific à specific devices à move

devices out of OS to application tasks

kernel Kernel

Middleware

Device driverDevice driver

MiddlewareMiddleware

Kernel

Application

Middleware

Device driverDevice driver

Middleware

HardwareHardware

Embedded OS Standard OS

Page 7: Real-Time Operating Systems (RTOS)

7

Characteristics of Embedded OS• Protection is often optional

◦ Embedded systems are typically designed for a single purpose, untested programs rarely loaded, and thus software is considered reliable

◦ Privileged I/O instructions not necessary and tasks can do their own I/O

• Real-time capability◦ Many embedded systems are real-time (RT) systems ◦ Hence, the OS used in these systems must be real-time operating

systems (RTOSs)

Page 8: Real-Time Operating Systems (RTOS)

8

What is a Real-Time System?• Real-time systems:

J. Stankovic, “Misconceptions about Real-Time Computing,”IEEE Computer, 21(10), October 1988.

Those systems in which the correctness of the system depends not only on the logical result of the computation,

but also on the time at which the results are produced

Page 9: Real-Time Operating Systems (RTOS)

9

Job Scheduling in RTOS

https://www.digikey.com/en/maker/projects/getting-started-with-stm32-introduction-to-freertos/ad275395687e4d85935351e16ec575b1

General-Purpose OS

Real-Time OS

Page 10: Real-Time Operating Systems (RTOS)

10

Real-Time Characteristics• Sensors and actuators all controlled by a processor

◦ The big difference is their timing constraints (deadlines)

• Tasks can be broken into three categories◦ Periodic tasks: time-driven

◦ An air monitoring system taking a sample every 10 seconds

◦ Aperiodic tasks: event-driven◦ The airbag of a car having to react to an impact

◦ Background/idle tasks: non-time-critical◦ Compress data on SD card

Background processing

Sensor event handler

sensor event timer event

Timer event handler

Page 11: Real-Time Operating Systems (RTOS)

11

Soft, Firm and Hard Deadlines• The instant at which a result is needed is called a deadline• If the result has utility even after the deadline has passed,

the deadline is classified as soft, otherwise it is firm

• If a catastrophe could result if a firm deadline is missed, the deadline is hard

Page 12: Real-Time Operating Systems (RTOS)

12

Goals of an RTOS•Manage to meet real-time deadlines•Also

◦ Tasks stay out of each others’ way◦ Normally through memory protection

◦ Device drivers already written (and tested!) for us◦ Portable—runs on a huge variety of systems◦ Nearly no overhead so we can use a small device!

◦ That is a small memory and CPU footprint

Page 13: Real-Time Operating Systems (RTOS)

13

Functionality of RTOS Kernel• Processor management • Memory management• Timer management

• Task management (resume, wait, etc.)• Inter-task communication

◦ E.g., message queues

• Task synchronization◦ E.g., mutex and semaphores

resource management

Page 14: Real-Time Operating Systems (RTOS)

14

Task Scheduling• Preemtive multitasking

◦ Running task is preempted by kernel • Cooperative multitasking

◦ Tasks cooperatively give control back to kernel

Running

BlockedReady

Inactive

preempt

schedule

wait

event

terminate create

term

inat

e

crea

te

term

inat

e Running

BlockedReady

Inactive

yield

schedule

wait

event

terminate create

term

inat

e

crea

te

term

inat

e

(job = task = thread)

Page 15: Real-Time Operating Systems (RTOS)

15

Typical RTOS Memory Allocation

Stack (for main app and IRQs)

Free memory

Heap for RTOS

Heap (for main app)

Data memory

Task A

Task B

QueueStack of Task A

Task Control Block (TCB)

end

start

Page 16: Real-Time Operating Systems (RTOS)

16

ARM CMSIS Architecture• CMSIS – Cortex Microcontroller Software Interface Standard

https://developer.arm.com/tools-and-software/embedded/cmsis

Page 17: Real-Time Operating Systems (RTOS)

17

Some RTOS Options for Cortex-M• RTOS kernels

◦ FreeRTOS (now under stewardship of Amazon)◦ CMSIS-RTOS RTX by Keil

• APIs◦ Native FreeRTOS◦ ARM’s CMSIS-RTOS v1/v2◦ ARM Mbed RTOS API

Page 18: Real-Time Operating Systems (RTOS)

18

FreeRTOS• Designed to be small and simple

◦ Mainly focuses on task management

• Ported to 40+ MCU architectures◦ E.g., ARM Cortex-M, Tensilica Xtensa, PIC, AVR

• Free and open source◦ Distributed under the MIT license

• Supports both cooperative and preemptive task scheduling

• Bundled with STM32CubeIDE

Page 19: Real-Time Operating Systems (RTOS)

19

FreeRTOS in STM32CubeIDE• STM32CubeIDE supports CMSIS-RTOS v2 API on top of

FreeRTOS

Page 20: Real-Time Operating Systems (RTOS)

20

ARM Mbed OS• Designed specifically for ARM microcontrollers• Large collection of hardware abstraction APIs

◦ Similar to Arduino

• Mbed OS 5 comes with CMSIS-RTOS RTX• Free and open source

◦ Distributed under the Apache 2.0 license

• Collaborative online environment and community◦ Online IDE◦ Code repositories

Page 21: Real-Time Operating Systems (RTOS)

21

Mbed Development Environments• Cloud-based IDE

◦ Online ARM C/C++ Compiler◦ Code is easily shared with other

developers

• Mbed studio / Mbed CLI◦ Offline development◦ Built-in ARM C/C++ Compiler

• Other development environments◦ Eclipse◦ Visual Studio Code◦ System Workbench for STM32

Page 22: Real-Time Operating Systems (RTOS)

22

Mbed OS Architecture

Page 23: Real-Time Operating Systems (RTOS)

23

Thread Scheduling in Mbed OS• Low-latency preemtive scheduler

https://os.mbed.com/docs/mbed-os/v5.15/apis/rtos.html

Page 24: Real-Time Operating Systems (RTOS)

24

Supporting Real-Time Operations• CMSIS-RTOS RTX (RTOS used by Mbed) utilizes SysTick, SVC, and

PendSV interrupts to implement a low-latency preemptive scheduler

https://www.keil.com/pack/doc/CMSIS/RTOS2/html/theory_of_operation.html

configured to thelowest priority

Page 25: Real-Time Operating Systems (RTOS)

Hands-on Activity:Getting Started with Mbed OS

Page 26: Real-Time Operating Systems (RTOS)

26

Circuit and Wiring• Connect a switch to pin D3 and GND

◦ D3 is connected to STM32L432KC’s pin PB0

Page 27: Real-Time Operating Systems (RTOS)

27

Mbed Online IDE• Launch a browser and go to https://ide.mbed.com• Login or sign up as a new user• Click “Compiler” to launch the IDE

Page 28: Real-Time Operating Systems (RTOS)

28

Register a Platform• Click “No device selected” box on the top right corner • Click “Add Board” button

Page 29: Real-Time Operating Systems (RTOS)

29

Add NUCLEO-L432KC Board• In the search box, type NUCLEO-L432KC and click Search

◦ NUCLEO-L432KC should appear as the first result

• Click on the board and click “Add to your Mbed Compiler”

Page 30: Real-Time Operating Systems (RTOS)

30

Select Platform for Compiler• Back to the IDE page, click “No device selected” again• Select NUCLEO-L432KC and click “Select Platform” button

Page 31: Real-Time Operating Systems (RTOS)

31

Creating First Project• Create an empty program, named rtos-demo

Page 32: Real-Time Operating Systems (RTOS)

32

Import Mbed-OS Library• Right-click the project name and add Mbed-OS URL

◦ https://github.com/ARMmbed/mbed-os

Page 33: Real-Time Operating Systems (RTOS)

33

Add Program File• Right-click the project name and add main.cpp to the

project

Page 34: Real-Time Operating Systems (RTOS)

34

First Task: Heart Beat• Flash LED shortly every second

#include "mbed.h"

void task_heartbeat() {DigitalOut led3(PB_3);while (1) {

led3 = 1;wait(0.1);led3 = 0;wait(0.9);

}}

int main() {printf("Program started...\n");task_heartbeat();

}

Page 35: Real-Time Operating Systems (RTOS)

35

Compiling and Downloading• Click the Compile button

◦ If there is no error, the browser will start downloading firmware binary from the IDE

◦ Notes: first compilation will take a long time

Page 36: Real-Time Operating Systems (RTOS)

36

Firmware Upload• Plug in the board

• Drag the downloaded firmware and drop into the drive/folder representing the board (i.e., NODE_L432KC)◦ The firmware should start automatically◦ If not, you may need to press the reset

button or unplug/replug the board

• Observe the on-board LED

Page 37: Real-Time Operating Systems (RTOS)

37

Second Task: Switch Count• Add the following function and modify main() to call this task instead

• Compile and test◦ Open a serial terminal and

set baud rate to 9600◦ Press SW and observer

results in the terminal

void task_countsw() {DigitalIn sw(PB_0, PullUp);uint32_t count = 0;

while (1) {while (sw) // wait until switch is pressed

;wait(0.02); // simple debouncecount++;printf("Switch pressed %d times\n", count);while (!sw) // wait until switch is released

;wait(0.02); // simple debounce

}}

int main() {printf("Program started...\n");task_heartbeat();task_countsw();

}

Page 38: Real-Time Operating Systems (RTOS)

38

Concurrent Tasks• Create a thread object for each task so that both of them

can run concurrently

int main() {Thread t1, t2;printf("Program started...\n");t1.start(callback(task_heartbeat));t2.start(callback(task_countsw));wait(osWaitForever);

}

Page 39: Real-Time Operating Systems (RTOS)

39

References• https://www.freertos.org/

• CMSIS-RTOS RTX: Theory of Operation

• Introduction to Arm Mbed OS 5

• CS4101: Introduction to Embedded Systems by Prof. Chung-Ta King

• Getting Started with STM32: Introduction to FreeRTOS

• MOOC – FreeRTOS on STM32 (Youtube Playlist)

• รายการบทความเพื-อการเรียนรู:้ Embedded Systems และ IoT