Department of Mathematics and Computer Science μC/OS-II 2IN60: Real-time Architectures (for automotive systems)

Slides:



Advertisements
Similar presentations
More on Processes Chapter 3. Process image _the physical representation of a process in the OS _an address space consisting of code, data and stack segments.
Advertisements

Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Chapter 6: Process Synchronization
Mike Holenderski, Synchronization 2IN60: Real-time Architectures (for automotive systems)
ECE 526 – Network Processing Systems Design Software-based Protocol Processing Chapter 7: D. E. Comer.
Figure 2.8 Compiler phases Compiling. Figure 2.9 Object module Linking.
OS Fall ’ 02 Introduction Operating Systems Fall 2002.
Introduction to Operating Systems – Windows process and thread management In this lecture we will cover Threads and processes in Windows Thread priority.
Home: Phones OFF Please Unix Kernel Parminder Singh Kang Home:
OS Spring’03 Introduction Operating Systems Spring 2003.
Real-Time Kernels and Operating Systems. Operating System: Software that coordinates multiple tasks in processor, including peripheral interfacing Types.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
MicroC/OS-II Embedded Systems Design and Implementation.
Processes in Unix, Linux, and Windows CS-502 Fall Processes in Unix, Linux, and Windows CS502 Operating Systems (Slides include materials from Operating.
Real-Time Operating Systems Suzanne Rivoire November 20, 2002
Performance Evaluation of Real-Time Operating Systems
Evolution of Microcontroller Firmware Development David Benjamin.
Introduction to Embedded Systems
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 2 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
The  C/OS-II Real-Time Operating System.  C/OS-II Real-time kernel –Portable, scalable, preemptive RTOS –Ported to over 90 processors Pronounced “microC.
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
2-1 The critical section –A piece of code which cannot be interrupted during execution Cases of critical sections –Modifying a block of memory shared by.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
CE Operating Systems Lecture 11 Windows – Object manager and process management.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Real Time Operating Systems
Real-time OS Hao-yuan Chin Feb. 18, Institute of Electronics, National Chiao Tung University Real-time OS 1 Outline Introduction to RTOS Introduction.
Processes Introduction to Operating Systems: Module 3.
Processes and Process Control 1. Processes and Process Control 2. Definitions of a Process 3. Systems state vs. Process State 4. A 2 State Process Model.
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
ECGR-6185 µC/OS II Nayana Rao University of North Carolina at Charlotte.
Real Time Operating Systems Michael Thomas Date: Rev. 1.00Date.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
MicroC/OS-II S O T R.  MicroC/OS-II (commonly termed as µC/OS- II or uC/OS-II), is the acronym for Micro-Controller Operating Systems Version 2.  It.
1 VxWorks 5.4 Group A3: Wafa’ Jaffal Kathryn Bean.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
1 Process Description and Control Chapter 3. 2 Process A program in execution An instance of a program running on a computer The entity that can be assigned.
Lab 3 Department of Computer Science and Information Engineering National Taiwan University Lab3 – μC/OS 2015/10/13/ 13 1.
Slides created by: Professor Ian G. Harris Operating Systems  Allow the processor to perform several tasks at virtually the same time Ex. Web Controlled.
1 J.O. KLEIN Institut Universitaire de Technologie de CACHAN Université PARIS SUD - FRANCE An Introduction to Real Time Operating System.
Unit - I Real Time Operating System. Content : Operating System Concepts Real-Time Tasks Real-Time Systems Types of Real-Time Tasks Real-Time Operating.
Interrupts and Exception Handling. Execution We are quite aware of the Fetch, Execute process of the control unit of the CPU –Fetch and instruction as.
SOC Consortium Course Material SoC Design Laboratory Lab 8 Real-time OS - 1 Speaker: Yung-Chih Chen Advisor: Prof. Chun-Yao Wang November 17, 2003 Department.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
CSCI/CMPE 4334 Operating Systems Review: Exam 1 1.
1 Chapter 2: Operating-System Structures Services Interface provided to users & programmers –System calls (programmer access) –User level access to system.
2-1 Chapter 2 Real-Time Systems Concepts. 2-2 Foreground/Background Systems BackgroundForeground ISR Time Code execution application consists of an infinite.
Chapter 3: Kernel Structure 1. Objectives To understand what a task is. To learn how μ C/OS-II manages tasks. To know how an interrupt service routine.
Chapter 13: Porting μC/OS-II 1. Outline Requirements Hardware Software Tasks of Porting µC/OS-II OS_CPU_C.H OS_CPU_C.C OS_CPU_A.ASM Testing a port 2.
Chapter 3: Kernel Structure 1. Objectives To understand what a task is. To learn how μ C/OS-II manages tasks. To know how an interrupt service routine.
Chapter 1: Getting Started with μC/OS-II 1. kernel Introduction 2 LinuxμC/OS-II Task (process) kernel Device driver User mode (0-3G) (Kernel mode) 3G-4G.
Outlines  Introduction  Kernel Structure  Porting.
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Introduction to Operating Systems Concepts
REAL-TIME OPERATING SYSTEMS
Processes and threads.
Process concept.
Chapter 13: Porting μC/OS-II
CS 6560: Operating Systems Design
Topics Covered What is Real Time Operating System (RTOS)
OPERATING SYSTEMS CS3502 Fall 2017
Interrupt and Time Management in µC/OS-III
Getting Started with the µC/OS-III Real Time Kernel
Mid Term review CSC345.
Lecture Topics: 11/1 General Operating System Concepts Processes
Foundations and Definitions
In Today’s Class.. General Kernel Responsibilities Kernel Organization
Presentation transcript:

Department of Mathematics and Computer Science μC/OS-II 2IN60: Real-time Architectures (for automotive systems)

Department of Mathematics and Computer Science Goals for this slide set Explain the motivation behind an operating system and describe the layered architecture Describe how the task state is maintained Explain how the μC/OS-II scheduler works Describe the timer management in μC/OS-II 2

Department of Mathematics and Computer Science Outline Introduction to operating systems Overview of μC/OS-II Tasks Scheduling Interrupts 3

Department of Mathematics and Computer Science Why operating systems? 4

Department of Mathematics and Computer Science Operating system Hardware abstraction – Generic interfaces hiding hardware details – Convenient abstractions, addressing common problems Tasks, file system, unix pipes,... Virtualization – Give applications the illusion they have access to dedicated resources Resource management – Multiplex application tasks efficiently on the shared resources Processor, bus, network, memory, timers, … 5

Department of Mathematics and Computer Science Operating system Monolithic vs. microkernel based operating system – Microkernel: Kernel implements only basic services (task and memory management, and task communication) Higher level services are implemented on top Increased maintainability, security and stability – Monolithic: Provides an integrated set of services (basic + higher level) 6

Department of Mathematics and Computer Science Real-time operating system (RTOS) Manage tasks and communication between tasks – Task scheduling – Context switching between tasks – Task synchronization and communication: Semaphores, mutexes, timers,... Interrupt handling Predictable performance – low and bounded latencies and jitter for API calls and ISRs Small memory foot print (for embedded use) – Configurable (no cost for unused functionality) 7

Department of Mathematics and Computer Science Example: OSEK/VDX Joint project in German/French automotive industry Interface specification (API) Motivation: – High, recurring expenses in the development of control software (i.e. non-application) – Incompatibility of control units made by different manufactures due to different interfaces and protocols Goal: “create an industry standard for an open-ended architecture for distributed control units in vehicles” – Support for portability and reusability, through: Specification of abstract interfaces (i.e. independent of applications and hardware) Configurable and efficient architecture 8

Department of Mathematics and Computer Science Example: OSEK/VDX Several OSEK specifications: – Operating System (OS) Real-time execution of ECU software and base for the other OSEK/VDX modules – Communication Data exchange within and between ECUs – Network Management Configuration and monitoring 9

Department of Mathematics and Computer Science Example: OSEK OS Task management – Basic and Extended tasks Activation, suspension and termination of tasks – Task switching – Task synchronization – Note: tasks, semaphores,... must be statically allocated! Interrupt management Alarms (i.e. Timers) Error treatment 10

Department of Mathematics and Computer Science Outline Introduction to operating systems Overview of μC/OS-II Tasks Scheduling Interrupts 11

Department of Mathematics and Computer Science What is μC/OS-II? Real-time operating system Used in medical, military, aerospace, automotive, consumer electronics,... Commercial, but “open source” 12

Department of Mathematics and Computer Science Properties of μC/OS-II Fixed-priority preemptive multitasking – Up to 64 or 256 tasks (configurable) Small and deterministic overheads – Short and predictable interrupt path Scalable – Many services: semaphores, mutexes, flags, mailboxes,... – Enable services with conditional compilation directives No periodic tasks 13

Department of Mathematics and Computer Science μC/OS-II architecture 14

Department of Mathematics and Computer Science μC/OS-II + RELTEQ architecture 15

Department of Mathematics and Computer Science Outline Introduction to operating systems Overview of μC/OS-II Tasks Scheduling Interrupts 16

Department of Mathematics and Computer Science Nested function calls int main(void) { int i = 0; int j = 0; int k = 0; while (i < 100) { j = 0; while (j < i) { k++; j++; } i++; } (void)k; return (0); } int f(unsigned int n) { if (n == 0) { return 0; } else { return n + f(n - 1); } int main(void) { int k = f(99); (void)k; return (0); } Iterative implementation Recursive implementation 17

Department of Mathematics and Computer Science int f(unsigned int n) { if (n == 0) { return 0; } else { return n + f(n - 1); } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 99) { if (99 == 0) { return 0; } else { return 99 + f(98); } int main(void) { int k = f(99); (void)k; return (0); } Nested function calls int f(unsigned int 98) { if (98 == 0) { return 0; } else { return 98 + f(97); } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 97) { if (97 == 0) { return 0; } else { return 97 + f(96); } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 1) { if (1 == 0) { return 0; } else { return 1 + f(0); } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 0) { if (0 == 0) { return 0; } else { return n + f(n - 1); } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 1) { if (1 == 0) { return 0; } else { return 1 + 0; } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 2) { if (2 == 0) { return 0; } else { return 2 + 1; } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 97) { if (97 == 0) { return 0; } else { return ; } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 98) { if (98 == 0) { return 0; } else { return ; } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int 99) { if (99 == 0) { return 0; } else { return ; } int main(void) { int k = f(99); (void)k; return (0); } int f(unsigned int n) { if (n == 0) { return 0; } else { return n + f(n - 1); } int main(void) { int k = 4950; (void)k; return (0); } 2 int f(unsigned int 2) { if (2 == 0) { return 0; } else { return 2 + f(1); } int main(void) { int k = f(99); (void)k; return (0); } Memory

Department of Mathematics and Computer Science Stacks stored inside of memory 19

Department of Mathematics and Computer Science Function call 20 f(1, 2); f: PULB PULA ABA PSHA RTS : LDAX #1 LDAY #2 PSHX PSHY JSR f PULX int f(int x, int y) { return x + y; }

Department of Mathematics and Computer Science Task state At any moment in time a task is defined by its state – CPU status registers (PC, SP, …) – Local variables The state of a task is stored in its TCB and on its stack – TCB contains Static parameters (priority, period, phasing, …) Stack Pointer (SP) pointing to the top of the stack – Each “stack frame” on the stack contains: CPU status registers (PC, CCR, …) Note: SP is stored in the TCB Local variables Return address (to the calling function) Function parameters and result address 21

Department of Mathematics and Computer Science Switching tasks Task switch (from A to B): 1.Store the state of task A on the stack 2.Store the SP inside the TCB of task A 3.Load the SP from the TCB of task B 4.Load the state of task B from the stack 22

Department of Mathematics and Computer Science OSTaskCreate() INT8U OSTaskCreate(void (*task)(void* pd), void* pdata, OS_STK* ptos, INT8U prio); task function argument to task function pointer to the stack task priority 23

Department of Mathematics and Computer Science Task example #define Task1Prio 1 OS_STK Task1Stack[TASK_STACK_SIZE]; void TaskCheckPad(void* p_args) { int pad = (int)p_args; while (1) { ToggleLed(LED_D22); SetLed(LED_D23, ATDReadChannel(pad) > 0); OSTimeDly(1000); } void main(void) {... OSTaskCreate(TaskCheckPad, PAD14, &Task1Stack[TASK_STK_SIZE-1], Task1Prio);... } 24

Department of Mathematics and Computer Science Task example... void TaskCheckPad(void* p_args) { int pad = (int)p_args; while (1) { ToggleLed(LED_D22); SetLed(LED_D23, ATDReadChannel(pad) > 0); OSTimeDly(1000); } void main(void) {... OSTaskCreate(TaskCheckPad, PAD14, &Task1Stack[TASK_STK_SIZE-1], Task1Prio); OSTaskCreate(TaskCheckPad, PAD10, &Task2Stack[TASK_STK_SIZE-1], Task2Prio);... } 25

Department of Mathematics and Computer Science OSTaskCreatePeriodic() INT8U OSTaskCreatePeriodic(void (*task)(void), INT16U period, INT16U phasing, OS_STK* ptos, INT8U prio); task function period pointer to the beginning of the stack priority 26 phasing (Provided by the RELTEQ extension)

Department of Mathematics and Computer Science Periodic task example #define Task1Prio 1 OS_STK Task1Stack[TASK_STACK_SIZE]; void TaskCheckPad14(void) { ToggleLed(LED_D22); SetLed(LED_D23, ATDReadChannel(PAD14) > 0); } void main(void) {... OSTaskCreatePeriodic(TaskCheckPad14, 1000, 0, &Task1Stack[TASK_STK_SIZE-1], Task1Prio);... } 27

Department of Mathematics and Computer Science Direct vs. suspending task Suspending task (also called a thread) – Suspension: release the processor allowing lower priority tasks to run – Preserves its state on its own stack Stack stored in main memory Can preserve state between activations Direct task – Cannot suspend – Can be executed as plain function call – Executed within the context of the ISR State can be preserved between activations using global variables 28 ✓

Department of Mathematics and Computer Science Outline Introduction to operating systems Overview of μC/OS-II Tasks Scheduling Interrupts 29

Department of Mathematics and Computer Science Task states [Labrosse, 2002] 30

Department of Mathematics and Computer Science Scheduler Fixed-Priority Preemptive Scheduler Unique priorities (lower number means higher priority) Triggered synchronously and asynchronously w.r.t. control flow – Synchronously: called from e.g. OSTimeDly() or OSSemPend() – Asynchronously: called from OSIntExit() Scheduler: 1.Selects highest priority ready task 2.Switches if it has higher priority than current task 31

Department of Mathematics and Computer Science Scheduler How does the scheduler select the highest priority task? – Maintain a ready queue keeping track which tasks are ready and which are not – Maintain the queue when tasks become ready or not ready – When scheduler is invoked, consult the queue to select the highest priority task 32

Department of Mathematics and Computer Science Scheduler: ready queue Possible implementations: – A list ordered by priority Task becomes ready: insert task into the list Task becomes not ready: remove task from the list Select highest priority task: take the task from the head – A bitmap, with one bit per task Bit is 1 means task is ready, bit is 0 means task is not ready Task becomes ready: set the corresponding bit to 1 Task becomes not ready: set the corresponding bit to 0 Select highest priority task: select the “earliest” set bit 33 ✓

Department of Mathematics and Computer Science [Labrosse, 2002] 34

Department of Mathematics and Computer Science Scheduler The ready state of all tasks is managed with global variables – OSRdyTbl[] and OSRdyGrp Making a task ready Checking if a task is ready Both can be done in constant time! – At the memory cost of lookup tables OSMapTbl and OSUnMapTbl 35 OSRdyGrp|= OSMapTbl[prio >> 3]; OSRdyTbl[prio >> 3] |= OSMapTbl[prio & 0x07]; y= OSUnMapTbl[OSRdyGrp]; x= OSUnMapTbl[OSRdyTbl[y]]; prio = (y << 3) + x;

Department of Mathematics and Computer Science Outline Introduction to operating systems Overview of μC/OS-II Tasks Scheduling Interrupts 36

Department of Mathematics and Computer Science Interrupts An incoming interrupt dispatches the associated interrupt service routine (ISR) – Represents a high priority event (which needs to be handled) ISRs have higher priority than any task – ISRs interfere with tasks – Needs to have a short and predictable execution time – Tasks can disable interrupts 37

Department of Mathematics and Computer Science Interrupt Service Routine General structure of an ISR in μC/OS-II: ISR executes within the context of the currently running task – It uses the stack space of the current task 38 void SomeISR(void) { Save processor registers; Call OSIntEnter(); Call SomeISRBody(); Call OSIntExit(); Restore processor registers; Execute a return from interrupt instruction; }

Department of Mathematics and Computer Science Interrupts 39

Department of Mathematics and Computer Science Interrupt timing Interrupt latency – Max time interrupts are disabled – Time to start executing first instruction of the ISR Interrupt response – Interrupt latency – Time to save CPU state (i.e. registers) – Time to enter the ISR ( OSIntEnter() ) Interrupt recovery – Time to exit the ISR ( OSIntExit() ) – Time to restore CPU state 40 void SomeISR(void) { Save processor registers; Call OSIntEnter(); Call SomeISRBody(); Call OSIntExit(); Restore processor registers; Return from interrupt; }

Department of Mathematics and Computer Science Timer interrupt Timer interrupts are especially important – Keep track of time by incrementing the global OSTime variable 41

Department of Mathematics and Computer Science Timer interrupt System clock is connected to the MCU via a pin – Clock operates independently of the MCU 1.Clock sets the pin high periodically 2.Setting a pin high triggers an interrupt on the MCU 3.Interrupt is handled by an ISR, which sets the pin low 42

Department of Mathematics and Computer Science Timer interrupt (interrupts enabled) 43

Department of Mathematics and Computer Science Timer interrupt (interrupts disabled) 44

Department of Mathematics and Computer Science Disabling interrupts Tasks can disable interrupts –OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL() – Can lead to increased interrupt latency or missed interrupts – Keep interrupts disabled for as little time as possible! – Use only for short critical sections Tasks can also disable preemption while keeping interrupts enabled – OSSchedLock() and OSSchedUnlock() – Scheduler is disabled, but interrupts are not disabled – Task maintains control of the CPU, but incoming interrupts are handled 45

Department of Mathematics and Computer Science Timer interrupt Timer interrupts are especially important – Keep track of time by incrementing the global OSTime variable – Delay a task for number of ticks: OSTimeDly() 46

Department of Mathematics and Computer Science Timer interrupt 47

Department of Mathematics and Computer Science Timer interrupt 48

Department of Mathematics and Computer Science Timer interrupt 49

Department of Mathematics and Computer Science Timer interrupt 50

Department of Mathematics and Computer Science Timer interrupt Timer interrupts are especially important – Keep track of time by incrementing the global OSTime variable – Delay a task for number of ticks: OSTimeDly() Handled by OSTimeTick() – Called within the timer ISR – Loops through all tasks: Decrement the OSTimeDly field in their TCB If OSTimeDly is 0, then make the task ready 51

Department of Mathematics and Computer Science Timer ISR 52 void OSTickISR(void) { Save processor registers; Call OSIntEnter(); Call OSTimeTick(); Call OSIntExit(); Restore processor registers; Execute a return from interrupt instruction; }

Department of Mathematics and Computer Science OSTimeTick() in μC/OS-II void OSTimeTick(void) { for all tasks { OS_ENTER_CRITICAL(); if (task->OSTCBDly > 0) { if (--task->OSTCBDly == 0) { (* make task ready *) } OS_EXIT_CRITICAL(); } OS_ENTER_CRITICAL(); OSTime++; OS_EXIT_CRITICAL(); } 53

Department of Mathematics and Computer Science References Recommended reading: –“ MicroC/OS-II, The Real-time Kernel ”, J. Labrosse, 2002, Second Endition –“ μC/OS-II Reference Manual ” Chapter 16 from the book (available on the website) – μC/OS-II source code Download from Included in the exercises from last week 54