Re Real-Ti Time Operating Systems (R (RTOS) 01204322 Embedded - - PowerPoint PPT Presentation

re real ti time operating systems r rtos
SMART_READER_LITE
LIVE PREVIEW

Re Real-Ti Time Operating Systems (R (RTOS) 01204322 Embedded - - PowerPoint PPT Presentation

Re Real-Ti Time Operating Systems (R (RTOS) 01204322 Embedded System Chaipo Chaiporn J n Jaik aikae aeo Department of De f Computer Engineering Kasetsart Unive versity Revised 2020-02-16 Materials partially taken from Lecture Slides


slide-1
SLIDE 1

Re Real-Ti Time Operating Systems (R (RTOS)

Chaipo Chaiporn J n Jaik aikae aeo De Department of f Computer Engineering Kasetsart Unive versity

01204322 Embedded System

Revised 2020-02-16

Materials partially taken from Lecture Slides by Prof. King

slide-2
SLIDE 2

2

Ou Outline

  • Embedded operating systems
  • Real-time operating systems (RTOS)
  • RTOS characteristics
  • RTOS implementations
  • Hands-on activity with Mbed OS
slide-3
SLIDE 3

3

Op Operating g 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
  • f such a collection
  • Characteristics
  • Resource management
  • Interface between application and hardware
  • Library of functions for the application

User Application Operating System Hardware

slide-4
SLIDE 4

4

Embedded O Embedded Oper perating ing S Systems ems

  • 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 in ROM

User Operating System + Application Hardware

slide-5
SLIDE 5

5

De Desktop

  • p vs Embedded OS

OS

User Operating System + Application Hardware User Application Operating System Hardware Application Application Desktop OS Applications are compiled separately from the OS Embedded OS Application is compiled and linked together with the OS

slide-6
SLIDE 6

6

Application

Ch Characteri ristics of

  • f Embedded OS

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 driver Device driver Middleware Middleware Kernel Application Middleware Device driver Device driver Middleware Hardware Hardware Embedded OS Standard OS

slide-7
SLIDE 7

7

Ch Characteri ristics of

  • f Embedded OS

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
  • wn 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)

slide-8
SLIDE 8

8

Wh What t is a Real-Ti 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

slide-9
SLIDE 9

9

Job Job Scheduling g in RTOS OS

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

General-Purpose OS Real-Time OS

slide-10
SLIDE 10

10

Re Real-Ti Time Ch Characteri ristics

  • 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

slide-11
SLIDE 11

11

Soft Soft, F , Fir irm an and H Har ard De Dead adlin lines

  • 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

slide-12
SLIDE 12

12

Go Goals o als of an R f 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
slide-13
SLIDE 13

13

Func Functiona nality o

  • f R

f RTOS K Kernel ernel

  • 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

slide-14
SLIDE 14

14

Ta Task Scheduling

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

to kernel

Running Blocked Ready Inactive

preempt schedule wait event terminate create terminate create terminate

Running Blocked Ready Inactive

yield schedule wait event terminate create terminate create terminate

(job = task = thread)

slide-15
SLIDE 15

15

Ty 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 Queue Stack of Task A Task Control Block (TCB) end start

slide-16
SLIDE 16

16

ARM ARM CMSIS Ar Arch chitect cture

  • CMSIS – Cortex Microcontroller Software Interface Standard

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

slide-17
SLIDE 17

17

Som Some R RTOS O S Option ions f for

  • r C

Cor

  • rtex-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
slide-18
SLIDE 18

18

Fr FreeR eeRTOS

  • 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
slide-19
SLIDE 19

19

Fr FreeR eeRTOS in in S STM32CubeIDE

  • STM32CubeIDE supports CMSIS-RTOS v2 API on top of

FreeRTOS

slide-20
SLIDE 20

20

ARM ARM Mb Mbed OS 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
slide-21
SLIDE 21

21

Mb 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
slide-22
SLIDE 22

22

Mb Mbed OS Architecture

slide-23
SLIDE 23

23

Th Thread Scheduling in in Mb Mbed OS OS

  • Low-latency preemtive scheduler

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

slide-24
SLIDE 24

24

Su Suppor

  • rtin

ing R g Real al-Ti 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 the lowest priority

slide-25
SLIDE 25

Ha Hands nds-on

  • n Activi

vity: Ge Getting St ing Start arted w d wit ith h Mb Mbed ed OS OS

slide-26
SLIDE 26

26

Ci Circuit and Wiri ring

  • Connect a switch to pin D3 and GND
  • D3 is connected to STM32L432KC’s pin PB0
slide-27
SLIDE 27

27

Mb Mbed On Online IDE DE

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

28

Re Register a Platform

  • Click “No device selected” box on the top right corner
  • Click “Add Board” button
slide-29
SLIDE 29

29

Ad Add NUCLEO-L4 L432KC B Boar ard

  • 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”
slide-30
SLIDE 30

30

Se Sele lect Pla Platfor

  • rm f

for

  • r C

Com

  • mpile

iler

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

31

Cr Creating First Proje ject

  • Create an empty program, named rtos-demo
slide-32
SLIDE 32

32

Impo Import Mb Mbed-OS OS Library

  • Right-click the project name and add Mbed-OS URL
  • https://github.com/ARMmbed/mbed-os
slide-33
SLIDE 33

33

Ad Add Program File

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

project

slide-34
SLIDE 34

34

Fi First T Task sk: He Heart art B Beat at

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

slide-35
SLIDE 35

35

Co Compiling and Down wnloading

  • 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
slide-36
SLIDE 36

36

Fi Firm rmware U e Upl pload

  • 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
slide-37
SLIDE 37

37

Se Secon

  • nd T

Tas ask: : Sw 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 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(); }

slide-38
SLIDE 38

38

Co Concurr rrent Task sks

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

slide-39
SLIDE 39

39

Re 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