Embedded Operating Systems – Basic notions of Operating Systems Objectives of an

Make the computer system convenient to use in an efficient manner. Internet of Things Hide the details of the hardware resources from the users. Embedded Operating Systems Provide users a convenient interface to use the computer system. Act as an intermediary between the hardware and its users, Ioannis Chatzigiannakis making it easier for the users to access and use other resources. Sapienza University of Rome Manage the resources of a computer system. Department of Computer, Control, and Management Engineering (DIAG) Keep track of who is using which resource, granting resource requests, and mediating conflicting requests from different Lecture 3: programs and users. Embedded Operating Systems Provide efficient and fair sharing of resources among users and programs.

1 / 36 2 / 36

Embedded Operating Systems – Basic notions of Operating Systems Embedded Operating Systems – Basic notions of Operating Systems Main elements of an Operating System Main services of an Operating System

Operating System (OS) Operating System (OS) A collection of that manages the hardware resources and A collection of software that manages the hardware resources and provides various service to the users. provides various service to the users.

1 Processor 1 Process management. Performs the data processing functions. Load into main memory, execute programs. Controls the operation of the computer. 2 Resource Allocation 2 Main Memory 3 volatile storage. Allocate/de-allocate memory. 3 I/O Modules Virtual memory. non volatile storage, 4 File System communications, 5 Communication terminals. Interprocess communication, across Computer Networks. 4 System Bus 6 Error detection and response. communication among components. 7 Authentication, Authorization, Accounting.

3 / 36 4 / 36 Embedded Operating Systems – Basic notions of Operating Systems Embedded Operating Systems – Basic notions of Operating Systems Interacting with the Operating System Type of Operating Systems

Kernel mode: 1 Batch systems: jobs entered as a whole and in sequence. has complete access to all Purpose specific. the hardware 2 Interactive systems: allow multiple jobs Purpose specific or General purpose. can execute any instruction Turnaround: Faster than batch – slower than real-time. that a machine is capable of 3 Hybrid systems – combination of batch and interactive. executing 4 Network operating systems – for network devices. has high privileged (rights) 5 Real-time systems Purpose specific – reliability is critical. User mode: Used in time-critical environments – fixed time constraints. can execute only subset Hard vs Soft: Hard: total failure if deadline missed (few) of the machine Soft: suffer performance degradation due to a missed deadline. instructions 6 Embedded systems – single machine / purpose specific. has less privileged (rights) 7 Wireless sensor systems – multiple nodes / purpose specific. 8 Smart card operating systems

5 / 36 6 / 36

Embedded Operating Systems – Operating Systems for IoT Embedded Operating Systems – Operating Systems for IoT Software platforms for IoT Operating systems for IoT

Memory: Kilobytes instead of Gigabytes. CPU: Megahertz instead of Gigahertz. TinyOS – statically linked applications. Energy: Milliwatt instead of Watt. ContikiOS – dynamic linking of binaries, no support. Potentially limited storage capabilities. FreeRTOS – real-time operating system. Probably without direct connectivity to Internet. RIOT – focuses on constrained devices + interoperability. Diverse hardware peripherals that can be connected. ARM mbed – targeting ARM Cortex M series. . . . no standard platform to develop applications. Zephyr – real-time on constrained devices.

7 / 36 8 / 36 Embedded Operating Systems – STM32 Nucleo Development Process Embedded Operating Systems – ARM MBed ARM Mbed OS STM32 family provides wide range of devices with limited resources: Memory as low as 16KB. Some may have direct connectivity to Internet. Arm Mbed OS is a free, open-source embedded operating system designed specifically for the Applications can be ”things” in the Internet of Things. It includes all developed by tools: the features you need to develop a connected 1 STM32CubeMX product based on an Arm Cortex-M 2 Middleware (32 bit). 3 IDE

9 / 36 10 / 36

Embedded Operating Systems – ARM MBed Embedded Operating Systems – ARM MBed Main Features

Modular - Ease of Use With a modular library structure, the necessary underlying support for your application will be automatically included on your device. By using the Mbed OS API, your application code can remain clean, portable and simple. End to End Security It addresses security in device hardware, software, communication. Open Source https://github.com/ARMmbed/mbed-os Community Big community support and Forum. Possibility to use drivers and libraries developed by third party.

11 / 36 12 / 36 Embedded Operating Systems – ARM MBed Embedded Operating Systems – ARM MBed Toolchains and IDE ARM MCU to Cloud

Two possibilities:

Offline toolchains, (Arm Online IDE, no setup and Compiler 6, GCC and IAR) that easiest way to get started. support Arm Mbed CLI.

13 / 36 14 / 36

Embedded Operating Systems – ARM MBed Embedded Operating Systems – ARM MBed ARM MBED Cloud Platform

15 / 36 16 / 36 Embedded Operating Systems – Zephyr OS Embedded Operating Systems – Zephyr OS Zephyr OS Main Features

Provide an OS that runs best on MCUs for wearable and IoT devices, where the cost of the silicon is minimal. Highly Configurable. Kernel mode only Zephyr OS is a scalable, real-time operating Nanokernel – Limited functionality targeting small footprint system (RTOS) for embedded devices. (below 10k) (superset of nanokernel): with additional Open source. functionality and features. Cross-architecture with broad SoC and No user space and no dynamic runtimes development board support. Memory and Resources are typically statically allocated Cross architecture (IA32, ARM, ARC)

17 / 36 18 / 36

Embedded Operating Systems – Zephyr OS Embedded Operating Systems – Zephyr OS Native Execution Native vs Normal

Build Zephyr as native application Enable large scale simulation of network or tests without involving HW Improve test coverage of application layers Use any native tools available for debugging and profiling Develop GUI applications entirely on the desktop Optionally connect to real devices with TCP/IP, Bluetooth, and CAN Reduce requirements for HW test platforms during development

19 / 36 20 / 36 Embedded Operating Systems – Zephyr OS Embedded Operating Systems – Zephyr OS Zephyr Architecture

Highly Configurable, Highly Modular Cooperative and Preemptive Threading Memory and Resources are typically statically allocated Integrated interface Memory Protection: Stack overflow protection, Kernel object and device driver permission tracking, Thread isolation (BLE 5.1) with both controller and host, BLE Mesh 802.15.4 OpenThread Native, fully featured and optimized networking stack

21 / 36 22 / 36

Embedded Operating Systems – Zephyr OS Embedded Operating Systems – Zephyr OS Zephyr Ecosystem Zephyr Project Governance

23 / 36 24 / 36 Embedded Operating Systems – RIOT Embedded Operating Systems – RIOT Goals for IoT Operating Systems Interoperability Goal

Driven by a grassroots community Modular structure, adaptive to diverse hardware instead from the hardware providers/manufacturers. Micro-kernel architecture (contrary to Linux) Linux-like approach. minimal requirements around 1kB RAM Open source + Free licensed under LGPLv2.1. 2.8kB RAM, 3.2kB ROM on 32-bit Cortex-M Less garbage with less IoT device -down support for 50+ different IoT boards/devices. Functionalities of a full-fledged operating system. Efficient Hardware Abstraction Layer (HAL) Needed memory & energy efficiency to fit IoT devices. minimized hardware-dependent code. Faster innovation by spreading IoT software dev. costs support for 8/16/32 bit, ARM, AVR, MIPS. Setting Privace & Security at core: Supported vendors: Microchip, NXP, STMicroelectronics, 1 Long-term IoT software robustness & security Nordic, TI, ESP, RISC-, etc. 2 Trust, transparency & protection of IoT users’ privacy Large list of sensors and actuators supported (e.g drivers) native board: run RIOT as process on your computer +100 boards supported Compliance with common system standards POSIX sockets, pthreads standard , C++ application coding

25 / 36 26 / 36

Embedded Operating Systems – RIOT Embedded Operating Systems – RIOT Hardware Abstraction Layer Modular Operating System

Features are libraries → only build what’s required 3 blocks: boards, cpus, drivers CPUs are organized as follows: xtimer – high-level timer subsystem architecture (ARM) → family () → type (stm32l4) → Full abstraction from the model (stm32l476rg) hardware timer Can set callbacks, put a Generic API for cpu peripherals thread to sleep, etc (gpio, uart, spi, pwm, etc) shell – interactive command same API for all architectures line Only based on vendor header files Others: crypto, fmt, math, (CMSIS) etc less code duplication, more efficient but more work Also: External packages provided and maintained by third parties. One application → one board → one cpu model

27 / 36 28 / 36 Embedded Operating Systems – RIOT Embedded Operating Systems – RIOT Core: Real-time scheduler Core: Multi-Threading and IPC

Tickless scheduler – energy efficient. Fixed priorities – deterministic. Separate thread contexts with separate Highest priority thread runs until finished or blocked. thread memory stack. 16 priority levels. Minimal thread control block (TCB). the lower level the higher priority. Idle thread has priority 15. Thread synchronization using mutexes, Main thread has priority 7. semaphores and messaging. Preemption O(1) operations – real-time. Interrupt service runtime (ISR) can preempt any thread at any 2 threads by default: time 1 the main thread: running the main function If all threads are blocked: 2 the idle thread: Switch to special IDLE thread lowest priority Goes into lowest possible power mode → fallback thread when all other threads are blocked or Low latency interrupt handler – reactivity. terminated switches the system to low-power mode

29 / 36 30 / 36

Embedded Operating Systems – RIOT Embedded Operating Systems – RIOT Core: Memory Management Overview of the boot sequence

Threads have their own memory stack The stack also contains the thread control block (tcb) There is no memory protection A stack overflow can destroy another stack 1 board_init() is implemented in boards//board.c file 2 cpu_unit() is implemented in cpu//cpu.c file 3 kernel_init() is implemented in core/kernel_init.c file Example for ARM Cortex-M: the entry point is reset_handler_default implemented in cpu/cortexm_common/vectors_cortexm.c

31 / 36 32 / 36 Embedded Operating Systems – RIOT Embedded Operating Systems – RIOT Network Stacks Other Network Support

IP oriented stacks: designed for Ethernet, WiFi, 802.15.4 networks In-house Controller Area Network (CAN) GNRC: the in-house 802.15.4/6LowPAN/IPv6 stack of RIOT LoRaWAN stack – Compliant with LoRaWAN 1.0.2 Thread: 802.15.4 IPv6 stack provided by the ThreadGroup SigFox support for ATA8520e modules OpenWSN (experimental): a deterministic MAC layer implementing the IEEE 802.15.4e TSCH protocol Full featured USB stack (CDC-ACM, CDC-ECM, etc) Other IPv6 stacks: SUIT: Standard and secure software update implementation lwIP: full-featured network stack designed for low memory consumption emb6: A fork of network stack that can be used without proto-threads IPv6 over BLE, NimBLE stack support

33 / 36 34 / 36

Embedded Operating Systems – RIOT Embedded Operating Systems – RIOT Source code organization

boards: board specific definitions, cpu model, clock, peripherals config, documentation, serial and flasher config core: kernel initialization, thread, ipc cpu: support for microcontroller (cmsis, definitions, peripheral drivers), entry point (_reset_handler_default_) dist: management and utility tools (script, ci, static checkers, etc) doc: doxygen documentation drivers: high-level device drivers (sensors, actuators, radios), HAL API examples: sample applications makefiles: build and management system makefiles pkg: external packages sys: system libraries, network, shell, xtimer, etc tests: unittests, test applications (can be used as examples)

35 / 36 36 / 36