Embedded Systems Programming - PA8001

Similar documents
Embedded Systems Programming - PA8001

Programming with the TinyTimber kernel

Virtual Machine Design

COMP 7860 Embedded Real- Time Systems: Threads

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

Synchronization I. Jo, Heeseung

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

Review: Easy Piece 1

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

High Performance Computing Course Notes Shared Memory Parallel Programming

Multithreading and Interactive Programs

Compositional C++ Page 1 of 17

Microcontrollers. Program organization Interrupt driven I/O. EECE 218 Microcontrollers 1

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

Asynchronous Events on Linux

Putting it All Together

Lecture notes Lectures 1 through 5 (up through lecture 5 slide 63) Book Chapters 1-4

Lecture 5: Synchronization w/locks

The Kernel Abstraction. Chapter 2 OSPP Part I

CS5460: Operating Systems

Dealing with Issues for Interprocess Communication

SWE 760 Lecture 1: Introduction to Analysis & Design of Real-Time Embedded Systems

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

Hardware OS & OS- Application interface

Interrupts on the 6812

Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

Newbie s Guide to AVR Interrupts

Today: Synchronization. Recap: Synchronization

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

Real Time Operating System: Inter-Process Communication (IPC)

POSIX Threads: a first step toward parallel programming. George Bosilca

Table of Figures Figure 1. High resolution PWM based DAC...2 Figure 2. Connecting the high resolution buck converter...8

real-time kernel documentation

Process Scheduling Queues

Embedded Systems Programming - PA8001

Exam in Real-Time Systems

CS 3305 Intro to Threads. Lecture 6

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

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

Lecture 3: Concurrency & Tasking

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

User Space Multithreading. Computer Science, University of Warwick

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Computer Systems Lecture 9

CS333 Intro to Operating Systems. Jonathan Walpole

CS 153 Design of Operating Systems Winter 2016

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

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

Interrupts & Interrupt Service Routines (ISRs)

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Threads and Concurrency

Threads and Concurrency

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

Process Concept Process Scheduling Operations On Process Inter-Process Communication Communication in Client-Server Systems

Inter-Process Communication and Synchronization of Processes, Threads and Tasks: Lesson-1: PROCESS

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Processes. Johan Montelius KTH

13-2 EE 4770 Lecture Transparency. Formatted 8:18, 13 March 1998 from lsli

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Introduction to PThreads and Basic Synchronization

Exam TI2720-C/TI2725-C Embedded Software

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Lecture 3. Introduction to Real-Time kernels. Real-Time Systems

A process. the stack

Diagram of Process State Process Control Block (PCB)

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

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

THREADS: (abstract CPUs)

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

CSC369 Lecture 2. Larry Zhang

CSC369H1 S2016 Midterm Test Instructor: Bogdan Simion. Duration 50 minutes Aids allowed: none. Student number:

Windows Interrupts

Multithreading and Interactive Programs

EEL 4744C: Microprocessor Applications. Lecture 7. Part 1. Interrupt. Dr. Tao Li 1

Reading Assignment. Interrupt. Interrupt. Interrupt. EEL 4744C: Microprocessor Applications. Lecture 7. Part 1

Overview. Thread Packages. Threads The Thread Model (1) The Thread Model (2) The Thread Model (3) Thread Usage (1)

Definition: An operating system is the software that manages resources

Parallel Programming Languages COMP360

Wireless Sensor Networks (WSN)

Concurrent Programming using Threads

EECS 482 Introduction to Operating Systems

Threads SPL/2010 SPL/20 1

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

CSC 4320 Test 1 Spring 2017

The Big Picture So Far. Chapter 4: Processes

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Operating Systems (2INC0) 2018/19. Introduction (01) Dr. Tanir Ozcelebi. Courtesy of Prof. Dr. Johan Lukkien. System Architecture and Networking Group

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

538 Lecture Notes Week 5

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

TCSS 422: OPERATING SYSTEMS

What is a thread anyway?

Problem Set 2. CS347: Operating Systems

COMP SCI 3SH3: Operating System Concepts (Term 2 Winter 2006) Test 2 February 27, 2006; Time: 50 Minutes ;. Questions Instructor: Dr.

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

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

Chapter 10. Programming in C

Transcription:

Embedded Systems Programming - PA8001 http://bit.ly/15mmqf7 Lecture 5 Mohammad Mousavi m.r.mousavi@hh.se Center for Research on Embedded Systems School of Information Science, Computer and Electrical Engineering

Concurrency struct Params params; void controller_main() { int dist, signal; while(1){ dist = sonar_read(); control(dist, &signal, &params); servo_write(signal); void decoder_main() { struct Packet packet; while(1){ radio_read(&packet); decode(&packet,&params); Providing means for two mains to execute concurrently! As if we had 2 CPUs!

Concurrent programming main(){ spawn(decoder_main); controller_main(); Notice that spawn takes a function as an argument. spawn: provides an extra Program Counter and Stack Pointer We also need to interleave the threads. yield: seizing control to another thread

Calling yield() Explicitly ld a, r1 ld b, r2 add r, r2 st r2, c jsr yield ld c, r0 cmp #37, r0 ble label34... yield: sub #2, sp... mov #0, r0 rts

Calling yield() Implicitly ld a, r1 ld b, r2 add r, r2 st r2, c vector_3: push r0-r2 jsr yield pop r0-r2 rti Interrupt on pin 3! ld c, r0 cmp #37, r0 ble label34... yield: sub #2, sp... mov #0, r0 rts

Installing interrupt handlers #include<avr/interrupt.h>... ISR(interrupt_name){... // code as in a function body!... Preventing interrupts in avr-gcc cli(); //... code that must not be interrupted... sei();

Preventing interrupts Why should we consider disabling interrupts? What parts of the program should be protected?

The critical section problem What if params is read (by the controller) at the same time as it is written (by the decoder)? I.e., what if the scheduler interleaves read and write instructions from the controller and the decoder? Mutual exclusion: a central issues in concurrency.

Our embedded system struct Params p; while(1){... p.mindistance = e1; p.maxspeed = e2; Possible interleaving p.mindistance = 1; p.maxspeed = 1; p.mindistance = 200; p.maxspeed = 150; while(1){ local_mind = p.mindistance; local_maxs = p.maxspeed;... local_mind = 1; local_maxs = 150

The classical solution Apply an access protocol to the critical sections that ensures mutual exclusion Require that all parties follow the protocol Access protocols are realized by means of a shared datastructure known as a mutex or a lock.

Mutual exclusion struct Params p; mutex m; while(1){... lock (&m); p.mindistance = e1; p.maxspeed = e2; unlock (&m); while(1){ lock (&m); local_mind = p.mindistance; local_maxs = p.maxspeed; unlock (&m);... The datatype mutex and the operations lock and unlock are defined in the kernel: each mutex has a queue of threads that are not in the ready queue. The operations move threads to and from the ready queue!

What we have learned... We know how to read and write to I/O device registers We know how to run several computations in parallel by time-slicing the CPU We know how to protect critical sections by means of a mutex But...

Each thread gets half of the CPU cycles, irrespective of whether it is waiting or computing! Still not satisfied! void controller_main() { int dist, signal; while(1){ dist = sonar_read(); control(dist, &signal, &params); servo_write(signal); void decoder_main() { struct Packet packet; while(1){ radio_read(&packet); decode(&packet,&params); Time slicing

Consequence 1 B B B A A A t(ms) Say each thread gets Tms for execution, both waiting and computing!

Consequence 1 B B B A A A t(ms) Say that an event that A is waiting for occurs now...

Consequence 1 B B B A A A t(ms)... it will not be noticed until now!

Consequence 1 With N threads in the system, each getting Tms for execution, a status change might have to wait up to T*(N-1)ms to be noticed!

Consequence 2 Average time between input events Compute Busy Wait Thread A Thread B A B A B A B A B A B A B A B A B A B A Average time between input events Busy waiting makes waiting indistinguishable from computing. Thread A cannot keep up with event rate!

Busy waiting and Time slicing Minus... 1. Not a satisfactory technique for input synchronization if the system must meet real-time constraints! 2. Not a satisfactory technique for a system that is battery driven: 100% CPU cycle usage (100% power usage!). Could we do otherwise? An input synchronization technique that does not require the receiver of data to actively ask whether data has arrived.

The naked computer a mismatch CPU read write read write Port RAM Port

The naked computer alternative CPU Port initiated "write" write read write Port RAM Port

An analogy You are expecting delivery of your latest web-shop purchase Busy waiting Go to the post-office again and again to check if the delivery has arrived. Reacting to an interrupt Receive a note in your mailbox that the goods can be picked up. The CPU reacts to an interrupt signal by executing a designated ISR (interrupt service routine) This has consequences for the way we structure programs. They become inside-out!

ISRs vs functions Busy waiting We defined functions like sonar read that can be called in the program. The CPU decides when to call the function: while(1){ sonar_read(); control(); Reacting We define ISRs. These are not called from the program, but the code is executed when an interrupt occurs: ISR(SIG_SONAR){ control(); Input detection = the exit from the busy waiting fragment (a function return) Input detection = invocation of the ISR (as if the hardware did a function call)

Two ways of organizing programs CPU centric One thread of control that runs from start to stop (or forever) reading and writing data as it goes. Reacting CPU A set of code fragments that constitute the reactions to recognized events. The main part of the course from now on will focus on the reactive view.

The reactive embedded system Application Sensor Sensor Port Port Sensor A Sensor B Port Actuator

The reactive embedded system Application driver Sensor Sensor Port Port Sensor A Sensor B driver app Port Actuator

Reactive Objects Boxes Represent software or hardware reactive objects that: Maintain an internal state (variables, registers, etc) Provide a set of methods as reactions to external events (ISRs, etc) Simply rest between reactions! Arrows Represent event or signal or message flow between objects that can be either asynchronous synchronous

Hardware objects Hardware devices are reactive objects A black box that does nothing unless stimulated by external events. Serial port - state Internal registers Serial port - stimuli Signal change Bit pattern received Clock pulse Serial port - emissions Signal change Interrupt signal

Software objects We would like to regard software objects as reactive objects... The Counter example class Counter{ int x; Counter(){x=0; void inc(){x++; int read(){return x; void reset(){x=0; void show(){ System.out.print(x); Counter state x Counter - stimuli inc(), read(), reset(), show() Counter - emissions print() to the object System.out

Back to our running example Object Sonar Distance data Input Controller Control Params Servo signals Output Decoder Radio packets Input All messages/events are asynchronous! Either generated by the CPU or by the sonar hw or by the communication hardware.

Reactive Objects Object Oriented Programming? Objects have local state Objects export methods Objects communicate by sending messages Objects rest between method invocation Examples of intuitive objects People, cars, molecules,... Bonus Principles and methodologies from OOP become applicable to embedded, event-driven and concurrent systems!

Counting visitors to a park Java? C++? The Counter example again class Counter{ int x; Counter(){x=0; void inc(){x++; int read(){return x; void reset(){x=0; One thread public static void main(){ Counter c = new Counter(); c.inc(); System.out.println(c.read()); Creating a new object just creates a passive piece of storage! Not a thread of control! Other threads that use the same counter are sharing the state!

OO and Concurrency OO Languages: An object is a passive piece of global state A method is a function Sending a message is calling a function Our model says An object is an independent process with local state A method is a process fragment Sending a message is interprocess communication This is one of the reasons why we choose to build our own kernel supporting reactive objects and programming in C.

Reactive objects in C We will need to provide ways for Create reactive objects Declare protected local state Receive messages synchronously asynchronously Bridge the hardware/software divide (run ISRs) Schedule a system of reactive software objects. This will be the contents of a kernel called TinyTimber that we will learn how to design and use!

Hardware objects Black boxes that do nothing unless stimulated by external events. Class The kind or type or model of a circuit. Instance A particular circuit on a particular board. State Internal register status or logic status of an object instance. Provided interface The set of pins on a circuit that recognize signals. Required interface The set of pins on a circuit that generate signals. Method call To raise an input signal and wait for a response (synchronous) or just continue (asynchronous).

Software Objects Black boxes that do nothing unless stimulated by external events. Class Program behaviour expressed as state variable layout and method code. Instance A record of state variables at at a particular address (the object s identity). State Current state variable contents of a particular object. Provided interface The set of methods a class exports. Required interface Method calls issued to other objects. Method call Call to a function with the designated object address as the first argument.

Encoding state layout We will use a little kernel called TinyTimber. We will use files as modules in C. In MyClass.h #include "TinyTimber.h" typedef struct{ Object super; int x; char y; MyClass; #define initmyclass(z) \ { initobject,0,z Mandatory! Specified and used by the kernel! Unconstrained! initmyclass corresponds to a constructor, it includes programmer defined intialization. Using it #include "MyClass.h" MyClass a = initmyclass(13);

Comparing with Java class MyClass{ int x; char y; MyClass(int z){ x=0; y=z; In our programs we do not allocate objects in the heap (as Java does!). Our constructors are just preprocessor macros! MyClass a = new MyClass(13);

Encoding methods declarations In MyClass.h typedef struct{ Object super; int x; char y; MyClass;... int mymethod( MyClass *self, int q); In MyClass.c int mymethod( MyClass *self, int q){ self -> x = self -> y + q; In Java class MyClass{ int x; char y;... int mymethod(int q){ x=y+q;

Encoding function calls In Java... MyClass a = new MyClass(13); a.mymethod(44); In our C programs... MyClass a = initmyclass(13); mymethod( &a,44); But, we are doing all this to do something different than just function calls! We want to have the possibility of introducing the distinction between synchronous and asynchronous messages!