Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
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
2
Outline• Embedded operating systems• Real-time operating systems (RTOS)• RTOS characteristics
• RTOS implementations• Hands-on activity with Mbed OS
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
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
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
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
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)
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
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
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
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
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
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
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)
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
16
ARM CMSIS Architecture• CMSIS – Cortex Microcontroller Software Interface Standard
https://developer.arm.com/tools-and-software/embedded/cmsis
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
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
19
FreeRTOS in STM32CubeIDE• STM32CubeIDE supports CMSIS-RTOS v2 API on top of
FreeRTOS
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
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
22
Mbed OS Architecture
23
Thread Scheduling in Mbed OS• Low-latency preemtive scheduler
https://os.mbed.com/docs/mbed-os/v5.15/apis/rtos.html
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
Hands-on Activity:Getting Started with Mbed OS
26
Circuit and Wiring• Connect a switch to pin D3 and GND
◦ D3 is connected to STM32L432KC’s pin PB0
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
28
Register a Platform• Click “No device selected” box on the top right corner • Click “Add Board” button
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”
30
Select Platform for Compiler• Back to the IDE page, click “No device selected” again• Select NUCLEO-L432KC and click “Select Platform” button
31
Creating First Project• Create an empty program, named rtos-demo
32
Import Mbed-OS Library• Right-click the project name and add Mbed-OS URL
◦ https://github.com/ARMmbed/mbed-os
33
Add Program File• Right-click the project name and add main.cpp to the
project
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();
}
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
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
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();
}
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);
}
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