Embedded Software TI2726 B. 4. Interrupts. Koen Langendoen. Embedded Software Group

Similar documents
In4073 Embedded Real-Time Systems. Embedded Programming

IN4073 Embedded Real-Time Systems. X32 Microcontroller Soft core

Bringing Organization to our Code (the shared-data problem)

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

Embedded Software TI2725 C. 5. Software architectures. Koen Langendoen. Embedded Software Group

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

Figure 4.4: code example. 4.3: The shared data problem. Figure 4.4: analysis. Fig 4.4: observations. Figure 4.5: Does this fix problem?

Exam TI2720-C/TI2725-C Embedded Software

Real Time Embedded Systems. Lecture 10 January 31, 2012 Interrupts

Embedded Software TI2726 B. 7. Embedded software design. Koen Langendoen. Embedded Software Group

Interrupts Peter Rounce - room 6.18

Interrupts Peter Rounce

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Design and Implementation Interrupt Mechanism

Interrupts & Interrupt Service Routines (ISRs)

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

CPUs. Input and output. Supervisor mode, exceptions, traps. Co-processors. Computers as Components 4e 2016 Marilyn Wolf

Process Coordination and Shared Data

Interrupts CS4101 嵌入式系統概論. Prof. Chung-Ta King. Department of Computer Science National Tsing Hua University, Taiwan

Interrupts and Low Power Features

ECE/CS 5780/6780: Embedded System Design

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

Bare Metal Application Design, Interrupts & Timers

What happens when an HC12 gets in unmasked interrupt:

ECE 485/585 Microprocessor System Design

Newbie s Guide to AVR Interrupts

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text

Computer Labs: I/O and Interrupts

These three counters can be programmed for either binary or BCD count.

Introduction to Embedded Systems

VORAGO VA108x0 I 2 C programming application note

The Embedded Computing Platform

ARM Interrupts. EE383: Introduction to Embedded Systems University of Kentucky. James E. Lumpp

Types of Interrupts:

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz III

Interrupts in Zynq Systems

Interrupts and Serial Communication on the PIC18F8520

Embedded Systems. 3. Hardware Software Interface. Lothar Thiele. Computer Engineering and Networks Laboratory

Tasks. Task Implementation and management

CSC227: Operating Systems Fall Chapter 1 INTERRUPTS. Dr. Soha S. Zaghloul

1 Execution of main program is suspended. 2 All registers are pushed onto the stack. 3 The ISR, or background thread, is executed.

NCSU - ECE 306- Exam 2 March 27, 2003

Embedded Systems. Input/Output Programming

Chapter 09. Programming in Assembly

Input/Output Programming

INTERRUPTS in microprocessor systems

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

Lecture #17 Concurrency Embedded System Engineering Philip Koopman Monday, 21-March-2016 Example application Remote Keyless Entry

Lecture #17 Concurrency Embedded System Engineering Philip Koopman Monday, 21-March-2016

SYLLABUS UNIT - I 8086/8088 ARCHITECTURE AND INSTRUCTION SET

8051 Timers and Serial Port

Single thread Scheduler All processes called once each sample

8051 Microcontroller

PC Interrupt Structure and 8259 DMA Controllers

Applying User-level Drivers on

Real-Time Programming

ECE 353 Lab 4. General MIDI Explorer. Professor Daniel Holcomb Fall 2015

CS3733: Operating Systems

CHAPTER 11 INTERRUPTS PROGRAMMING

Intertask Communication

MCS-51 Serial Port A T 8 9 C 5 2 1

Wireless Sensor Networks (WSN)

Interrupts, Low Power Modes

In Class Assignment 2

I Introduction to Real-time Applications By Prawat Nagvajara

Input/Output. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Introduction to general architectures of 8 and 16 bit micro-processor and micro-controllers

Embedded Systems and Software. Serial Communication

Embedded Systems. Read pages

Embedded Systems and Software

CODE TIME TECHNOLOGIES. Abassi RTOS. I2C Support

Interrupt Basics Karl-Ragmar Riemschneider

Understanding the basic building blocks of a microcontroller device in general. Knows the terminologies like embedded and external memory devices,

Interrupts and Using Them in C

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

CprE 288 Introduction to Embedded Systems (Timers/Input Capture) Instructors: Dr. Phillip Jones

V8-uRISC 8-bit RISC Microprocessor AllianceCORE Facts Core Specifics VAutomation, Inc. Supported Devices/Resources Remaining I/O CLBs

April 4, 2001: Debugging Your C24x DSP Design Using Code Composer Studio Real-Time Monitor

SpiNNaker Application Programming Interface (API)

Chapter 6: Process Synchronization

Using Input Capture on the 9S12

2. Introduction to Software for Embedded Systems

Programming Languages

Section I Section Real Time Systems. Processes. 1.4 Inter-Processes Communication (part 1)

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

1. What is Microprocessor? Give the power supply & clock frequency of 8085?

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

Input Output (IO) Management

Chapters 2, 3: bits and pieces. Chapters 2 & 3. Chapters 2, 3: bits and pieces. Chapters 2, 3: bits and pieces. Using C. A last word about hardware

How to use the PSoC based 16C450 Replacement

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Embedded System Curriculum

Interrupts and timers

Process Synchronization and Communication

Maxim > Design Support > Technical Documents > Application Notes > Microcontrollers > APP 4465

Recap from last class

Microprocessors B (17.384) Spring Lecture Outline

Embedded Software TI2725-C. 8. Debugging techniques. Koen Langendoen. Embedded Software Group

MICROPROCESSORS & MICRO CONTROLLER COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK

Transcription:

Embedded Software 4. Interrupts TI2726 B Koen Langendoen Embedded Software Group

What is an Interrupt? Asynchronous signal from hardware Synchronous signal from software Indicates the need for attention Indicates the need for execution change 3

Why Interrupts? Avoid wasting time in polling loops for external events Make a system reactive and simple Make debugging more fun CPU I2C Switches Internal errors Buttons Sensors Timers Actuators RS232 4

Interrupts: Principle dev 1 IRQ # IRQ contr CPU Context switch dev N Program:... MOVE R1, (var-addr) MULT R1, 9 DIVIDE R1, 5 ADD R1, 32... HW CALL ISR: PUSH R1... POP R1 RET 5

Where is the interrupt code? Intel 8051 default locations for interrupt service routines Interrupt vector table Where is the interrupt vector table? Default location (Intel 80186) Programmable location 6

Some questions Can a microprocessor be interrupted in the middle of an instruction? Which interrupt goes first? Interrupt during another interrupt? What happens immediately after the interrupts are enabled? What is the status of interrupts when the processor starts? 7

ATmega2560 too complex 100 pins 54 interrupt sources 8

Roll your own: FPGA + Soft Core Xilinx XC3S200 50 MHz, 200k gates I/O, LEDs, SSD, buttons, RS232 X32 soft core 32 bit processor peripherals: UART, PWM, timer, decoder 9

X32 Peripheral Interrupts buttons, LEDs, SSD, switches, timers, RS232, off-board I/O ports Peripheral 0 - K IRQ 0 IRQ n Interrupt Controller CPU multiple IRQ lines per peripheral possible (e.g., tx+rx IRQ per UART) CPU 10

X32: Interrupt Sources CPU: divide by 0, overflow (disable unless needed!) Buttons, switches, I/O ports positive AND negative edge triggered Timers: counter value > threshold reg UART: rx buffer char received / tx buffer empty 11

X32: Interrupt Controller IRQ 0 Interrupt Controller interrupt acknowledge IRQ n IE k, prio k, vector k IE_global vector priority exec level CPU FSM for each IRQ: IRQ. IE IE_global. (priority > exec level) stdby IE scheduled acknowledge servicing / interrupt 12

X32 Interrupts IRQ # IRQ contr CPU IRQ controller preprocesses multiple IRQ s Each device: (IRQ #, priority, associated with IRQ vector) Vectored IRQ Interrupts NOT disabled automatically Automatic ISR preemption if prio IRQ > prio current ISR Normal call saves context > no interrupt keyword 13

X32: Demo Demo.. (x32_projects.tgz, leds.c, buttons.c) 14

Shared Data Problem? void isr_read_temps(void) itemp[0] = peripherals[..]; itemp[1] = peripherals[..]; void NOT ATOMIC! main(void)... while (TRUE) tmp0 = itemp[0]; tmp1 = itemp[1]; if (tmp0!= tmp1) panic(); Possible Context Switch Book: page 92 15

Finding this bug Can be very tricky The bug does not occur always! Frequency depends on The frequency of interrupts Length of the critical section Problem can be difficult to reproduce Advise: double check the access on data used by ISR! 16

Solving the Data Sharing Problem? void void isr_read_temps(void) itemp[0] = peripherals[..]; itemp[1] = peripherals[..]; main(void)... while (TRUE) if (itemp[0]!= itemp[1]) panic(); MOVE R1, (itemp[0]) MOVE R2, (itemp[1]) SUBSTRACT R1,R2 JCOND ZERO, TEMP_OK TEMP_OK: 17

Solution #1 Disable interrupts for the ISRs that share the data... while (TRUE)!! DISABLE INT tmp0 = itemp[0]; tmp1 = itemp[1];!! ENABLE INT if (tmp0!= tmp1) panic(); The critical section is now atomic 18

X32: Demo Demo.. (x32_projects.tgz, critical.c) 19

Atomic & critical section A part of a program is atomic if it cannot be interrupted Interrupts and program code share data atomic can also refer to mutual exclusion Two pieces of code sharing data They can be interrupted The instructions that must be atomic = critical section 20

Be careful! static int iseconds, iminutes; void interrupt vupdatetime(void) ++iseconds; if (iseconds>=60) iseconds=0; ++iminutes; long lseconds(void) disable(); return (iminutes*60+iseconds); enable(); too little, too late 21

Function calls and enable() enable() can be a source of bugs! void function1 () // enter critical section disable(); temp = f2(); // exit critical section enable(); int f2 () disable(); enable(); should test if this is fine 22

More on shared data static long int lsecondstoday; void interrupt vupdatetime() ++lsecondstoday; MOVE R1,(lSecondsToday) MOVE R2,(lSecondsToday+1) RETURN long lgetseconds() return (lsecondstoday); 23

Any issues here? static long int lsecondstoday; void interrupt vupdatetime() ++lsecondstoday; long lgetseconds() long lreturn; lreturn = lsecondstoday; while (lreturn!=lsecondstoday) lreturn = lsecondstoday; return (lreturn); ingenious code without interrupts 24

Any issues here? volatile static long int lsecondstoday; void interrupt vupdatetime() ++lsecondstoday; long lgetseconds() long lreturn; Otherwise compiler might optimize this code! lreturn = lsecondstoday; while (lreturn!=lsecondstoday) lreturn = lsecondstoday; return (lreturn); 25

Interrupt Latency Quick response to IRQ may be needed Depends on previous rules: The longest period of time in which interrupts are disabled The time taken for the higher priority interrupts Overhead operations on the processor (finish, stop, etc.) Context save/restore in interrupt routine The work load of the interrupt itself worst case latency = t_maxdisabled + t_higher prio ISRs + t_myisr + context switches 26

Example #1 EI ISR main DI EI DI IRQ ISR blocked ISR code Interrupt execution time worst case analysis 27

Example #2 DI EI lp ISR hp ISR main hp IRQ lp IRQ blocked hp ISR actual load Interrupt execution time worst case analysis in the presence of a second interrupt 28

Alternatives to disable() Do not disable interrupts but write ingenious code By alternating data buffers (Fig. 4.15/page 108) By using queues (Fig. 4.16/page 109) ISR and (main) code never access the same data Problem: code becomes error prone and hard to read Rule of the thumb: Keep it simple, just disable interrupts as long as: keep the critical sections SHORT keep the ISRs SHORT (to minimize latency) 29

Avoiding interrupts #1 static int temp[2]; static bool busy = FALSE; void interrupt readtemp() if (!busy) temp[0]= ; temp[1]= ; else // try again later void main(void) while (TRUE) busy = TRUE; if (temp[0]!=temp[1]) ; busy = FALSE; Idea: use a Boolean flag to protect critical section 30

Avoiding interrupts #1 static int tempa[2]; static int tempb[2]; static bool useb = FALSE; void main(void) while (TRUE) void interrupt readtemp() if (useb) tempa[0]= ; tempa[1]= ; else tempb[0]= ; tempb[1]= ; if (useb) if (tempb[0]!=tempb[1]) ; else if (tempa[0]!=tempa[1]) ; useb =!useb; Alternating data buffers example (page 108) 31

Avoiding interrupts #2 Make use of a circular queue (example page 109) Interrupt produces temperature readings Main code consumes temperature readings Operation Interrupt adds readings to the queue (modifies the head pointer) Main code extracts readings from the queue (modifies the tail pointer) Queue New values Tail pointer Head pointer 32

Questions? 34