Real-Time Operating Systems (RTOS)

01204322 Embedded System

Chaiporn Jaikaeo Department of Computer Engineering Kasetsart University

Materials partially taken from Lecture Slides by Prof. King Revised 2020-02-16 Outline • Embedded operating systems • Real-time operating systems (RTOS) • RTOS characteristics • RTOS implementations • Hands-on activity with OS

2 Operating Systems • The collection of software that manages a system’s hardware resources ◦ File system, a GUI, and other components User

• Often times, a kernel is the crucial part Application of such a collection • Characteristics ◦ Resource management ◦ Interface between application and hardware Hardware ◦ Library of functions for the application

3 Embedded Operating Systems • The OS and application logic are combined into a single unit • Characteristics: User ◦ Resource management Operating System + ◦ Primary internal resources Application ◦ Less overhead ◦ Code of the OS and the Hardware application mostly reside in ROM

4 Desktop vs Embedded OS

Desktop OS Embedded OS Applications are compiled Application is compiled and separately from the OS linked together with the OS

User User

Operating System + Application Application Application Application

Operating System Hardware

Hardware

5 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 Embedded OS Standard OS Application Application

Middleware Middleware Middleware Middleware Middleware Device driver Device driver Kernel

kernel Kernel Device driver Device driver Hardware Hardware

6 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)

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

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

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

8 Job Scheduling in RTOS General-Purpose OS

Real-Time OS

https://www.digikey.com/en/maker/projects/getting-started-with-stm32-introduction-to-freertos/ad275395687e4d85935351e16ec575b1 9 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 sensor event timer event

Background processing Sensor event Timer event handler handler

10 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

11 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

12 Functionality of RTOS Kernel • Processor management

• Memory management resource management • Timer management • Task management (resume, wait, etc.) • Inter-task communication ◦ E.g., message queues • Task synchronization ◦ E.g., mutex and semaphores

13 Task Scheduling

• Preemtive multitasking • Cooperative multitasking ◦ Running task is preempted by kernel ◦ Tasks cooperatively give control back to kernel

event event Ready Blocked Ready Blocked schedule schedule wait wait preempt yield Running Running

terminate create terminate create terminate terminate create create terminate terminate Inactive Inactive

(job = task = thread)

14 Typical RTOS Memory Allocation

end Stack (for main app and IRQs)

Free memory

Heap for RTOS Queue Stack of Task A Task B Task A Heap (for main app) Task Control Block start (TCB) Data memory

15 ARM CMSIS Architecture

• CMSIS – Cortex Microcontroller Software Interface Standard

https://developer.arm.com/tools-and-software/embedded/cmsis 16 Some RTOS Options for Cortex-M • RTOS kernels ◦ FreeRTOS (now under stewardship of Amazon) ◦ CMSIS-RTOS RTX by Keil • ◦ Native FreeRTOS ◦ ARM’s CMSIS-RTOS v1/v2 ◦ ARM Mbed RTOS API

17 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

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

19 ARM Mbed OS • Designed specifically for ARM microcontrollers • Large collection of 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

20 Mbed Development Environments • Cloud-based IDE ◦ Online ARM /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

21 Mbed OS Architecture

22 Thread Scheduling in Mbed OS • Low-latency preemtive scheduler

https://os.mbed.com/docs/mbed-os/v5.15/apis/rtos.html 23 Supporting Real-Time Operations

• CMSIS-RTOS RTX (RTOS used by Mbed) utilizes SysTick, SVC, and PendSV to implement a low-latency preemptive scheduler

configured to the lowest priority

https://www.keil.com/pack/doc/CMSIS/RTOS2/html/theory_of_operation.html 24 Hands-on Activity: Getting Started with Mbed OS Circuit and Wiring • Connect a switch to pin D3 and GND ◦ D3 is connected to STM32L432KC’s pin PB0

26 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

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

28 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”

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

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

31 Import Mbed-OS Library • Right-click the project name and add Mbed-OS URL ◦ https://github.com/ARMmbed/mbed-os

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

33 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(); }

34 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

35 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

36 Second Task: Switch Count

• Add the following function and modify main() to call this task instead

• Compile and test void task_countsw() { DigitalIn sw(PB_0, PullUp); ◦ Open a serial terminal and uint32_t count = 0; set baud rate to 9600 while (1) { ◦ Press SW and observer while (sw) // wait until switch is pressed ; results in the terminal wait(0.02); // simple debounce count++; 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(); }

37 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); }

38 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

39