Multitasking. Embedded Systems

Similar documents
Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

Slide 6-1. Processes. Operating Systems: A Modern Perspective, Chapter 6. Copyright 2004 Pearson Education, Inc.


Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Real-Time Programming

Lesson 5: Software for embedding in System- Part 2

Synchronization I. Jo, Heeseung

VORAGO VA108xx FreeRTOS port application note

EE4144: Basic Concepts of Real-Time Operating Systems

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CS 571 Operating Systems. Midterm Review. Angelos Stavrou, George Mason University

Threads Tuesday, September 28, :37 AM

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

FreeRTOS X. Task Notifications Semaphores Family Critical Section FreeRTOS Producer Consumer Tasks

Introduction to Real-Time Systems and Multitasking. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

RT3 - FreeRTOS Real Time Programming

Multithreading and Interactive Programs

MARUTHI SCHOOL OF BANKING (MSB)

ArdOS The Arduino Operating System Reference Guide Contents

Chapter 9: Virtual-Memory

University of Washington EE 472: Microcomputer Systems Exam Autumn 2015

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Introduction to the ThreadX Debugger Plugin for the IAR Embedded Workbench C-SPYDebugger

Timers 1 / 46. Jiffies. Potent and Evil Magic

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Embedded System Curriculum

CS370 Operating Systems

CS 5460/6460 Operating Systems

An Interrupt is either a Hardware generated CALL (externally derived from a hardware signal)

Process Coordination and Shared Data

Introduction. CS3026 Operating Systems Lecture 01

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

EECS 482 Introduction to Operating Systems

Concurrent Programming

Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

C09: Process Synchronization

Operating System. Operating System Overview. Structure of a Computer System. Structure of a Computer System. Structure of a Computer System

3.1 Introduction. Computers perform operations concurrently

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Computer Systems A Programmer s Perspective 1 (Beta Draft)

MCUXpresso IDE FreeRTOS Debug Guide. Rev November, 2017

Memory: Overview. CS439: Principles of Computer Systems February 26, 2018

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Introduction to Real-Time Operating Systems

Short Notes of CS201

Interrupts Peter Rounce

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Midterm Exam. October 20th, Thursday NSC

Main Points of the Computer Organization and System Software Module

FreeRTOS. Alberto Bosio. February 27, Université de Montpellier Alberto Bosio (UM) FreeRTOS February 27, / 52

CS201 - Introduction to Programming Glossary By

Embedded Systems. 6. Real-Time Operating Systems

ECE 3055: Final Exam

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Preview. Memory Management

FreeRTOS. A Brief Overview. Christopher Kenna. October 1, Avionics. FreeRTOS 1 / 34

CSE 153 Design of Operating Systems Fall 2018

CS516 Programming Languages and Compilers II

CP2 Revision. theme: dynamic datatypes & data structures

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Chapter 1 Computer System Overview

Module 9: Virtual Memory

Problem Set 2. CS347: Operating Systems

Optimal Algorithm. Replace page that will not be used for longest period of time Used for measuring how well your algorithm performs

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

CS 550 Operating Systems Spring Interrupt

Chapter 3 Process Description and Control

Embedded Systems: OS. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapters 9 & 10: Memory Management and Virtual Memory

Parallel Assertion Processing using Memory Snapshots

NanoRK. EECE 494 Sathish Gopalakrishnan

AN HONORS UNIVERSITY IN MARYLAND UMBC. AvrX. Yousef Ebrahimi Professor Ryan Robucci

Hazard Pointers. Number of threads unbounded time to check hazard pointers also unbounded! difficult dynamic bookkeeping! thread B - hp1 - hp2

Chapter 8: Virtual Memory. Operating System Concepts

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

DSP/BIOS Kernel Scalable, Real-Time Kernel TM. for TMS320 DSPs. Product Bulletin

Midterm Exam Amy Murphy 6 March 2002

Module 9: Virtual Memory

EECS 482 Introduction to Operating Systems

Background. Virtual Memory (2/2) Demand Paging Example. First-In-First-Out (FIFO) Algorithm. Page Replacement Algorithms. Performance of Demand Paging

Chapter 9: Virtual Memory

TCSS 422: OPERATING SYSTEMS

Predictable Interrupt Management and Scheduling in the Composite Component-based System

Chapter 4. Enhancing ARM7 architecture by embedding RTOS

Computer System Overview

Dealing with Issues for Interprocess Communication

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Memory management. Requirements. Relocation: program loading. Terms. Relocation. Protection. Sharing. Logical organization. Physical organization

CSC 2405: Computer Systems II

Micrium µc/os II RTOS Introduction EE J. E. Lumpp

Using the FreeRTOS Real Time Kernel

Physical memory vs. Logical memory Process address space Addresses assignment to processes Operating system tasks Hardware support CONCEPTS 3.

Embedded Systems: OS

This section covers the MIPS instruction set.

OPERATING SYSTEM CONCEPTS UNDERSTAND!!! IMPLEMENT!!! ANALYZE!!!

Transcription:

Multitasking in Embedded Systems 1 / 39 Multitasking in Embedded Systems v1.0

Multitasking in ES What is Singletasking? What is Multitasking? Why Multitasking? Different approaches Realtime Operating Systems FreeRTOS Synchronization Example Project Debugging Multitasking Software 2 / 39 Multitasking in Embedded Systems v1.0

What is Singletasking? Do only one thing at a time Most efficient way to solve problems Applicable to every algorithm No management overhead No internal synchronization required Easy to code Busy-wait for I/O No Interrupts 3 / 39 Multitasking in Embedded Systems v1.0

Singletasking Example while (1) { if ( Byte = receivebyte() ) { // blocks! Buffer[Index++] = Byte; if ( Index >= MessageSize) { M_t* M = (M_t*) Buffer; switch (M->Type) { case mt_commanda:... case mt_commandb: default: break; } Index = 0; } } } 4 / 39 Multitasking in Embedded Systems v1.0

What is Multitasking? Aim for multiple targets Switch context often Management overhead Synchronization required Interrupts required Hard to code/ debug Implicit delays Increased memory usage Implementation difficult for many algorithms 5 / 39 Multitasking in Embedded Systems v1.0

Multitasking Example main() { int Queue = xqueuecreate(...); ttc_task_create(receive, Queue, ); ttc_task_create(process, Queue, ); } void Process(int Q) { while (1) { M = (M_t*) ttc_queue_pop_front(q); if (M) { switch (M->Type) { case mt_cmda:... case mt_cmdb:... default: break; } } } } void Receive(int Q) { char Buffer[10][100]; int Index = 0; while (1) { char* Writer = &(Buffer[Index,0]); int Remaining = MessageSize; while (Remaining > 0) { if ( Byte = readbyte() ) { // sleeps! *Writer++ = Byte; Remaining--; } ttc_queue_push_back(q, &(Buffer[Index,0]);); Index++; if (Index > 99) Index = 0; } } 6 / 39 Multitasking in Embedded Systems v1.0

Why Multitasking? Functions spawnable multiple times Eases handling of slow IO Benefits from multiple CPU-cores Only 1 central Timer required Short Interrupt Service Routines Less global variables required 7 / 39 Multitasking in Embedded Systems v1.0

No life without Multitasking! Every Embedded System needs MT MT often implemented via Interrupts Complex Service Routines Data Exchange via Global Variables Difficult to debug Typical approach: Super-Loop Periodically starts set of functions Similar to task scheduler 8 / 39 Multitasking in Embedded Systems v1.0

Different Approaches Multiprogramming Ancient mechanism for Periheral Access Realized via Terminal Stay Ready (TSR) Cooperative Multitasking Central Scheduler manages Processes Each process grants CPU to other processes Single process can block whole system Preemptive Multitasking Scheduler interrupts each process periodically Requires central Interrupt-Timer Preemptible Multitasking High priority Applications can interrupt others (OS/2, Linux, FreeRTOS) Allows faster response times 9 / 39 Multitasking in Embedded Systems v1.0

Realtime Scheduling Definition of Realtime Real-time computing means a hardware + software system that must response within a strict time constraint. Realtime constraint Hard Violation causes hazardous results Firm Infrequent violations tolerable but degrade system's quality of service Soft Usefullness of results degrade after their deadline http://en.wikipedia.org/wiki/real-time_computing 10 / 39 Multitasking in Embedded Systems v1.0

FreeRTOS Multitasking Scheduler Premptible Multitasking High triority tasks block low priority ones Inter Thread Communication Semaphores Queues Developed specially for Embedded Systems Open Source FreeWare with Commercial Support Ported to several μc Architectures http://www.freertos.org/ 11 / 39 Multitasking in Embedded Systems v1.0

TTC Queues Base of inter task communication Send message Task Task Interrupt Service Routine Task Call by value Reading from empty queue Function call waits until Queue is filled No CPU time is wasted during Wait Operations ttc_queue_byte_push_back(q, Byte); Byte = ttc_queue_byte_pull_front(q); Task A 3 2 1 Task B 12 / 39 Multitasking in Embedded Systems v1.0

TTC - Semaphores Task A Task B Counting Implemented as unsigned integer Counter Task A can increase Semaphore by up to N token at once Data in Queue is of no interest ttc_semaphore_take(s, N) ttc_semaphore_give(s, N) 13 / 39 Multitasking in Embedded Systems v1.0

TTC - Mutex Implemented as Counter 0 1 Best suited for Resource locking Non recursive Can be locked only once Recursive thread can lock multiple times must be unlocked as often as being locked Task B waits until Task A activates once Operations ttc_mutex_lock(m) ttc_mutex_unlock(m) Task A Task B 14 / 39 Multitasking in Embedded Systems v1.0

Multithreading with Queues Activate Extensions for Queues example on STM32-P107 > activate.600_example_threading_queues.sh 15 / 39 Multitasking in Embedded Systems v1.0

Multithreading with Queues Spawning Tasks Producer threading_start() Consumer Print 16 / 39 Multitasking in Embedded Systems v1.0

Multithreading with Queues Communicate via Queues Producer 6 5 4 3 LED1 PJ PJ PJ Print s8_t Column; s8_t Row; char Text[10]; s16_t Value; Consumer LED2 enqueue/ dequeue setport()/ clrport() 17 / 39 Multitasking in Embedded Systems v1.0

Multithreading with Semaphores Activate Extensions for Semaphores example on STM32-P107 > activate.600_example_threading_semaphores.sh 18 / 39 Multitasking in Embedded Systems v1.0

Multithreading with Semaphores Spawning Tasks Producer1 Producer2 threading_start() Consumer1 Consumer2 Print 19 / 39 Multitasking in Embedded Systems v1.0

Multithreading with Semaphores Communicate via Semaphore Producer1 enqueue/ dequeue setport()/ clrport() 0-10/s Data irrelevant 5/s Consumer1 LED1 5/s Consumer2 LED2 0-10/s Semaphore 5/s Consumer3 Producer2 5/s Consumer4 PJ PJ PJ Print 20 / 39 Multitasking in Embedded Systems v1.0

Debugging Multitasking Software Different types of Memories Multitasking & Stacks Watchdogs Binary file formats 21 / 39 Multitasking in Embedded Systems v1.0

Different types of Memories Storage RAM Heap Stack Datatypes Constants Global Variables Local Variables 22 / 39 Multitasking in Embedded Systems v1.0

Constants Stored in Flash along code Automatically initialized (initial value contained in binary) Reference to value points to address > 0x0800 0000 0x4000 0000 Peripherals 0x2000 0000 RAM Example: char* HelloWorld = Hello world ; Hello World 0x0800 0000 Flash 23 / 39 Multitasking in Embedded Systems v1.0

RAM Area with read- / write access Multiple Memories possible On chip (start at 0x2000 0000) Off Chip via memory controller Static RAM Dynamic RAM 0x4000 0000 Peripherals 0x2000 0000 RAM 0x0800 0000 Flash 24 / 39 Multitasking in Embedded Systems v1.0

Stack Located at one end of RAM Grows towards other end by each Function call Local variable Shrinks by each local variable end of scope Return from function call 0x4000 0000 Peripherals 0x2000 0000 RAM Stack 0x0800 0000 Flash 25 / 39 Multitasking in Embedded Systems v1.0

Global Variables Stored in RAM Initialized by code at program start Assembled into block by linker Reference to value points to address > 0x2000 0000 Example: int V = 42; void main() { V++; } 0x4000 0000 Peripherals Globals 0x2000 0000 RAM Stack 0x0800 0000 Flash 26 / 39 Multitasking in Embedded Systems v1.0

Memory Heap Block of memory in RAM Allocated via malloc() Freed via free() Different managers heap1, heap2, heap3 see corresponding extensions 0x4000 0000 Peripherals Globals Heap 0x2000 0000 RAM Stack 0x0800 0000 Flash 27 / 39 Multitasking in Embedded Systems v1.0

Multitasking & Stacks Stacks in Singletask Applications Stacks in Multitasking Applications Stacks in FreeRTOS Stackoverflow 28 / 39 Multitasking in Embedded Systems v1.0

Stacks in Singletask Applications One single stack Grows from one end of RAM Efficient memory usage (no holes) Always big egnough Only one stack size to calculate Mostly invisible to developer 0x2000 0000 RAM Stack 29 / 39 Multitasking in Embedded Systems v1.0

Stacks in Multitasking Applications One stack per thread Grows at different locations Memory holes between stacks sizes are tradeoff Each size hard to calculate Developer has to re-check size New local variables More function calls 0x2000 0000 RAM Stack Stack Stack Stack 30 / 39 Multitasking in Embedded Systems v1.0

Stacks in FreeRTOS Stacks handled by FreeRTOS Stack size defined at stack creation Argument to xtaskcreate() Cannot be changed during task execution Tasks normally do not end Stacks allocated on memory heap 31 / 39 Multitasking in Embedded Systems v1.0

Stackoverflow Two detection schemes in FreeRTOS Activated in FreeRTOSConfig.h Stack size checked whenever a task is put to sleep activate.300_scheduler_free_rtos_stack_check_1.sh Checks stack pointer to be within allowed boundaries Cannot find overflow occured during task run time Fast check (only two pointer comparisons) activate.300_scheduler_free_rtos_stack_check_2.sh First checks stack pointer via method 1 Each stack filled with known value Last 16 bytes of stack checked for changed values Will find out if stack has grown into last 16 bytes Slower than method 1 but more reliable 32 / 39 Multitasking in Embedded Systems v1.0

Stack size calculation Three step approach 1) Double stack sizes until no overflow occurs 2) Run application 3) Use high water marks to minimize sizes Problems How to force tasks to use maximum memory? Global variable: bool TestMode Recalculate after code changes 33 / 39 Multitasking in Embedded Systems v1.0

High Water Marks Configured in #define INCLUDE_uxTaskGetStackHighWaterMark 1 Activate via activate.300_scheduler_free_rtos_high_watermarks.sh FreeRTOS - HighWaterMark 34 / 39 Multitasking in Embedded Systems v1.0

Hardfault Handler CPU reads/ writes illegal memory address bus fault error generates interrupt calls cm3_hard_fault_handler_c() cm3_hard_fault_handler_c() Gathers information about fault reason Calls Assert_Halt_EC(ec_HardFault) Assert_Halt_EC() Stops execution in endless loop Contains Breakpoint 35 / 39 Multitasking in Embedded Systems v1.0

Debugging a Hardfault 36 / 39 Multitasking in Embedded Systems v1.0

Memfault Handler 37 / 39 Multitasking in Embedded Systems v1.0

Watchdogs 38 / 39 Multitasking in Embedded Systems v1.0

Binary file formats objdump 39 / 39 Multitasking in Embedded Systems v1.0