INTERRUPTS in microprocessor systems

Similar documents
Newbie s Guide to AVR Interrupts

AVR Timers TIMER0. Based on:

CN310 Microprocessor Systems Design

8086 Interrupts and Interrupt Responses:

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

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Introduction to Embedded Systems

PC Interrupt Structure and 8259 DMA Controllers

Microprocessors & Interfacing

Module 3. Embedded Systems I/O. Version 2 EE IIT, Kharagpur 1

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

Topic 11: Interrupts ISMAIL ARIFFIN FKE UTM SKUDAI JOHOR

Interrupt/Timer/DMA 1

INTERRUPT, TIMER/COUNTER. KONKUK UNIV. VLSI Design Lab. LSI Design Lab

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

Microcontroller basics

12.1. Unit 12. Exceptions & Interrupts

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

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

AGH University of Science and Technology Cracow Department of Electronics

AGH University of Science and Technology Cracow Department of Electronics

Interrupts and timers

Systems Programming and Computer Architecture ( ) Timothy Roscoe

FIFTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLOGY-MARCH 2014 EMBEDDED SYSTEMS (Common for CT,CM) [Time: 3 hours] (Maximum marks : 100)

Fundamental concept in computation Interrupt execution of a program to handle an event

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

MICROPROCESSOR BASED SYSTEM DESIGN

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

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

M68HC08 Microcontroller The MC68HC908GP32. General Description. MCU Block Diagram CPU08 1

Reset, Interrupts, Exceptions, and Break ECE 3534

Microprocessors B (17.384) Spring Lecture Outline

AVR XMEGA TM. A New Reference for 8/16-bit Microcontrollers. Ingar Fredriksen AVR Product Marketing Director

MICROPROCESSOR MEMORY ORGANIZATION

Lecture 14. Ali Karimpour Associate Professor Ferdowsi University of Mashhad

For more notes of DAE

12. Interrupts and Programmable Multilevel Interrupt Controller

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

AVR XMEGA Product Line Introduction AVR XMEGA TM. Product Introduction.

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

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

AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Features. Introduction. AVR 8-bit Microcontrollers APPLICATION NOTE

CHAPTER 11 INTERRUPTS PROGRAMMING

Last Time. Think carefully about whether you use a heap Look carefully for stack overflow Especially when you have multiple threads

ME 4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 6

Hardware OS & OS- Application interface

COSC 243. Input / Output. Lecture 13 Input/Output. COSC 243 (Computer Architecture)

Week 11 Programmable Interrupt Controller

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Timers and Interrupts. Mark Neil - Microprocessor Course

Interrupt: (verb) the interruption of a CPU s normal processing...using a mechanism provided for this purpose.

Design and Implementation Interrupt Mechanism

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

Interrupts Peter Rounce

SYLLABUS UNIT - I 8086/8088 ARCHITECTURE AND INSTRUCTION SET

ECE 372 Microcontroller Design Parallel IO Ports - Interrupts. ECE 372 Microcontroller Design Parallel IO Ports - Interrupts

18-349: Introduction to Embedded Real-Time Systems

Lab Course Microcontroller Programming

Software debouncing of buttons

School of Electrical, Computer and Telecommunications Engineering University of Wollongong Australia

COEN-4720 Embedded Systems Design Lecture 4 Interrupts (Part 1) Cristinel Ababei Dept. of Electrical and Computer Engineering Marquette University

Module Introduction. PURPOSE: The intent of this module is to explain MCU processing of reset and interrupt exception events.

AVR Microcontrollers Architecture

Interrupts. by Rahul Patel, Assistant Professor, EC Dept., Sankalchand Patel College of Engg.,Visnagar

Course Introduction. Purpose: Objectives: Content: 27 pages 4 questions. Learning Time: 20 minutes

original M68K requests/priorities Interrupts to execute important code interrupts (n will always be I) I-bits bits in SR accomplish this

COMP2121: Microprocessors and Interfacing

Automation Engineers AB Pvt Ltd, NOIDA Job-Oriented Course on Embedded Microcontrollers & Related Software Stack

Microprocessor Systems

Interrupts and Low Power Features

Interrupt is a process where an external device can get the attention of the microprocessor. Interrupts can be classified into two types:

CS 134. Operating Systems. April 8, 2013 Lecture 20. Input/Output. Instructor: Neil Rhodes. Monday, April 7, 14

Processor and compiler dependent

Mark Redekopp, All rights reserved. EE 357 Unit 10b. Interrupts Timers

Tutorial Introduction

SquareWear Programming Reference 1.0 Oct 10, 2012

TIMSK=0b ; /* enables the T/C0 overflow interrupt in the T/C interrupt mask register for */

Advanced Parallel Architecture Lesson 3. Annalisa Massini /2015

I/O - input/output. system components: CPU, memory, and bus -- now add I/O controllers and peripheral devices. CPU Cache

Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

Embedded Systems Design (630414) Lecture 1 Introduction to Embedded Systems Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

INPUT-OUTPUT ORGANIZATION

Interrupts Peter Rounce - room 6.18

ECE332, Week 8. Topics. October 15, Exceptions. Hardware Interrupts Software exceptions

COMP I/O, interrupts, exceptions April 3, 2016

QUESTION BANK CS2252 MICROPROCESSOR AND MICROCONTROLLERS

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

INPUT/OUTPUT ORGANIZATION

Fredrick M. Cady. Assembly and С Programming forthefreescalehcs12 Microcontroller. шт.

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

Summer 2003 Lecture 21 07/15/03

FIFTH SEMESTER B.TECH DEGREE EXAMINATION MODEL TEST QUESTION PAPER, NOVEMBER CS 305: Microprocessor and Microcontrollers PART A

CPEG300 Embedded System Design. Lecture 6 Interrupt System

Architecture of 8085 microprocessor

Input / Output. School of Computer Science G51CSA

Chapter 2. Overview of Architecture and Microcontroller-Resources

Introduction to Embedded Systems

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

The K Project. Interrupt and Exception Handling. LSE Team. May 14, 2018 EPITA. The K Project. LSE Team. Introduction. Interrupt Descriptor Table

Transcription:

INTERRUPTS in microprocessor systems Microcontroller Power Supply clock fx (Central Proccesor Unit) CPU Reset Hardware Interrupts system IRQ Internal address bus Internal data bus Internal control bus Program memory ROM Data memory RAM Basic Input/Output Devices Parallel Port Serial Port Counters General purpose Input/output ports Microcontroller all components of computer system in one an integrated circuit (chip) 1

What are Interrupts? One of the most fundamental and useful principles of modern embedded processors are interrupts. There seems to be a lot of confusion about interrupts; what are they, and how do we use them? In short, an interrupt is a way for an external (or, sometimes, internal) event to pause the current processor s activity, so that it can complete a brief task before resuming execution where it left off. What can cause interrupts? In systems programming, an interrupt is a signal to the processor or an instruction in software usually indicating an event that needs immediate attention. An interrupt signals the processor of a high-priority condition requiring the interruption of the current code the processor is executing, the current thread. The processor responds by suspending its current activities, saving its state, and executing a small program called an interrupt handler (interrupt service routine, ISR) to deal with the event. This interruption is temporary, and after the interrupt handler finishes, the processor resumes execution of the previous thread. 2

PC computer - what are Interrupts? A signal informing a program that an event has occurred. When a program receives an interrupt signal, it takes a specified action (which can be to ignore the signal). Interrupt signals can cause a program to suspend itself temporarily to service the interrupt. Interrupt signals can come from a variety of sources. For example, every keystroke generates an interrupt signal. Interrupts can also be generated by other devices, such as a printer, to indicate that some event has occurred. These are called hardware interrupts. Interrupt signals initiated by programs are called software interrupts. A software interrupt is also called a trap or an exception. PCs support 256 types of software interrupts and 15 hardware interrupts. Each type of software interrupt is associated with an interrupt handler -- a routine that takes control when the interrupt occurs. For example, when you press a key on your keyboard, this triggers a specific interrupt handler. The complete list of interrupts and associated interrupt handlers is stored in a table called the interrupt vector table, which resides in the first 1 K of addressable memory. Types of interrupts - hardware interrupt There are two main types of interrupt: A hardware interrupt is an electronic alerting signal to the processor from an external device, either a part of the computer itself such as a disk controller or an external peripheral. For example, pressing a key on the keyboard or moving the mouse triggers hardware interrupts that cause the processor to read the keystroke or mouse position. Unlike the software type, hardware interrupts are asynchronous and can occur in the middle of instruction execution, requiring additional care in programming. The act of initiating a hardware interrupt is referred to as an interrupt request (IRQ). 3

Types of interrupts - software interrupt A software interrupt is caused either by an exceptional condition in the processor itself, or a special instruction in the instruction set which causes an interrupt when it is executed. The former is often called a trap or exception and is used for errors or events occurring during program execution that are exceptional enough that they cannot be handled within the program itself. For example, if the processor's arithmetic logic unit is commanded to divide a number by zero, this impossible demand will cause a divideby-zero exception, perhaps causing the computer to abandon the calculation or display an error message. Software interrupt instructions function similarly to subroutine calls and are used for a variety of purposes, such as to request services from low level system software such as device drivers. For example, computers often use software interrupt instructions to communicate with the disk controller to request data be read or written to the disk. What can cause interrupts? Hardware interrupts were introduced as a way to avoid wasting the processor's valuable time in polling loops, waiting for external events. They may be implemented in hardware as a distinct system with control lines, or they may be integrated into the memory subsystem. If implemented in hardware, an interrupt controller circuit such as the IBM PC's Programmable Interrupt Controller (PIC) may be connected between the interrupting device and the processor's interrupt pin to multiplex several sources of interrupt onto the one or two CPU lines typically available. If implemented as part of the memory controller, interrupts are mapped into the system's memory address space. 4

Types of interrupts Interrupts can be categorized into these different types: Maskable interrupt (IRQ) is a hardware interrupt that may be ignored by setting a bit in an interrupt mask register's (IMR) bit-mask. Non-maskable interrupt (NMI) is a hardware interrupt that lacks an associated bit-mask, so that it can never be ignored. NMIs are used for the highest priority tasks such as timers, especially watchdog timers. Inter-processor interrupt (IPI) is a special case of interrupt that is generated by one processor to interrupt another processor in a multiprocessor system. Software interrupt is an interrupt generated within a processor by executing an instruction. Software interrupts are often used to implement system calls because they implement a subroutine call with a CPU ring level change. Spurious interrupt is a hardware interrupt that is unwanted. They are typically generated by system conditions such as electrical interference on an interrupt line or through incorrectly designed hardware. Maskable interrupt Processors typically have an internal interrupt mask which allows software to ignore all external hardware interrupts while it is set. This mask may offer faster access than accessing an interrupt mask register (IMR) in a PIC, or disabling interrupts in the device itself. In some cases, such as the x86 architecture, disabling and enabling interrupts on the processor itself act as a memory barrier, however it may actually be slower. 5

Interrupt priority level The interrupt priority level (IPL) is a part of the current system interrupt state, which indicates the interrupt requests that will currently be accepted. The IPL may be indicated in hardware by the registers in a Programmable Interrupt Controller, or in software by a bitmask or integer value. Interrupt priority Each hardware interrupt has: an interrupt level (source), trigger, interrupt priority. Interrupt Level The interrupt level defines the source of the interrupt and is often referred to as the interrupt source. Interrupt Trigger There are two types of trigger mechanisms, leveltriggered interrupts and edge-triggered interrupts. Interrupt Priorities The interrupt priority defines which of a set of pending interrupts is serviced first. 6

INTERRUPTS in microcontroller CPU Clock signal fx Reset Program memory ROM Data memory RAM External signals Interrupt hardware controller Input/ouput devices Counters/Timers, Pararel ports Serial ports External signals A/D Converter External analog signals D/A Converter PWM modulator INTERRUPTS in microcontroller Probably you won t be able to find a microcontroller without interrupt capability. These are essential attributes of any modern microcontroller or processor. Interrupt capability may seem confusing and tricky at first glance, but during the time you will find out that normal MCU operation is impossible without interrupts. Microcontrollers usually have multiple interrupt sources available. For instance Atmega32 has 21 of them. So program flow can be interrupted by various events like external pin go low, timer overflow or ADC conversion complete. If set properly these events will take over program flow once occur and give back resources once they ve been serviced. As we mentioned microcontroller can have multiple interrupt sources. AVR is equipped only with hardware interrupts. Other microcontrollers may have software interrupts as well. Hardware interrupts mean that they can only be generated by hardware events like pin change or other. If you look in to AVR datasheet you will always find a table of interrupt vectors like this: 7

Reset and Interrupt Handling This table is programmed into microcontroller memory along with your program as it is a reference table containing address of interrupt service routines a special functions that will be called once interrupt will occur. As you can see in table there is a column Program Address these are locations in flash where a interrupt service routine is located in program memory. For instance at the very beginning of flash (0 0000) there is a reset address stored. This means when MCU is powered program will look at this location and jumps to place where tour main() program starts. When programming in C interrupt table is created automatically during compilation, while ASM programmers have to set this table by themselves. Main sources of interrupts The 8-bit AVRs lack software interrupts, which are usually used for special operating system tasks like switching between user and kernel space, or for handling exceptions. Because of this, we ll only be looking at hardware interrupts. Each AVR model contains a different set of interrupt sources. We can find out which interrupts our chosen model has by looking in the Interrupts chapter of the AVR s datasheet. This contains a complete list of all the possible interrupts in our AVR, giving each interrupt s Vector table address, Source Interrupt Name, and Source Interrupt Description. Take a look at your AVR s list - see the variety of interrupt sources, from all sorts of on-chip peripherals of the AVR. Hardware interrupts can be useful to process sparingly occurring events, such as buttons presses or alarm inputs. They can also be useful for situations where a low, fixed latency is required, for example when processing inputs from a rotary encoder. 8

Simple interrupt handling sequence In the image you can see simple interrupt handling sequence. Let s say our program is doing some tasks in Main program flow. When interrupt signal occurs at any given moment of program flow it stops at INTERRUPT current location, remembers next operation address and then loads program counter with ISR address (1) stored in ISR vector table. From this moment interrupt handling function is performed (2). Once it s complete program counter is loaded with next interrupted program operation (3). So main program actually doesn t know that interrupt has occurred and continues normally. This is very simplified illustration of interrupt as we didn t mention preserving of register values during interrupt and so on. 1 3 ISR TABLE VECTOR MAIN PROGRAM FLOW (main loop) INTERRUPT SERVICE ROUTINE 2 Interrupt flags. Each hardware interrupt source has an associated interrupt flag - whenever the interrupt is triggered the corresponding interrupt flag is set. These flags are usually stored as bits within an interrupt register. The processor can read from and write to the interrupt register, reading from it to find out which interrupts occurred and writing to it to clear the interrupt flags. Interrupt mask The interrupt mask has a set of bits identical to those in the interrupt register. Setting any bit (or unmasking) lets the corresponding signal source generate an interrupt - causing the processor to execute the ISR. Note: When a bit in the mask is clear (masked) the ISR is not activated for that signal source but the interrupt register is still set by the signal source. So you can still detect what the hardware is doing by polling the interrupt register. Hardware interrupt vector The interrupt vector is a location in memory that you program with the address of your interrupt service routine (ISR). Whenever an unmasked interrupt occurs program execution starts from the address contained in the interrupt vector. Interrupt priority If only one interrupt request is issued, the pending interrupt will be handled If several interrupts occur at the same time, selecting the highest prioritized interrupt is done in hardware. 9

Interrupts in AVR By default, on the AVR devices, interrupts are themselves not interruptable. When an interrupt fires, the AVR CPU will automatically disable the Global Interrupt Enable bit, to prevent the ISR from being itself interrupted. This is to prevent stack overflows from too many interrupts occurring at once and to prevent the ISRs from running too long, as most uses of interrupts are to have minimal latency when processing an event. It s perfectly possible to set the Global Interrupt Enable bit again as part of the ISR so that nested interrupts can occur, but this is highly not recommended as it is dangerous. So how to we define an ISR? For an ISR to be called, we need three conditions to be true: Firstly, the AVR s global Interrupts Enable bit (I) must be set in the MCU control register SREG. This allows the AVR s core to process interrupts via ISRs when set, and prevents them from running when cleared. It defaults to being cleared on power up, so we need to set it. Secondly, the individual interrupt source s enable bit must be set. Each interrupt source has a separate interrupt enable bit in the related peripheral s control registers, which turns on the ISR for that interrupt. This must also be set, so that when the interrupt event occurs the processor runs the associated ISR. Thirdly, The condition for the interrupt must be met - for example, for the USART Receive Complete (USART RX) interrupt, a character must have been received. When all three conditions are met, the AVR will fire our ISR each time the interrupt event occurs. Again, the method used to define an ISR differs between languages and compilers. 10

Simple interrupt handling sequence Bus Initial program Device A Device B IRQ Interrupt Controller CPU IRQ interrupt enable bit INTERRUPT SERVICE ROUTINE 1 INTERRUPT SERVICE ROUTINE 2 Main program, Main loop Reset and Interrupt Handling The Atmel AVR provides several different interrupt sources. These interrupts and the separate reset vector each have a separate program vector in the program memory space. All interrupts are assigned individual enable bits which must be written logic one together with the Global Interrupt Enable bit in the Status Register in order to enable the interrupt. Depending on the Program Counter value, interrupts may be automatically disabled when Boot Lock bits BLB02 or BLB12 are programmed. This feature improves software security. 11

Simple interrupt handling sequence Without going in to details it is worth mention that interrupt vectors can be stored in boot section depending on fuse settings so bootloader could use benefits of interrupts. But leave this for some time later. Interrupt no. 1 Interrupt no. 2 Main program Interrupt Service no.1 Main Interrupt program Service no.2 Main program Time 12

Interrupts in AVR C language The Avr C-library provides default interrupt routines, which get used unless overridden If you wish to provide an interrupt routine for a particular signal you must, in addition to any required AVR setup, create the function using one of the SIGNAL() or INTERRUPT() macros, along with the appropriate signal names. There are a number of preset signal names, such as: ISR_ADC (ADC conversion done) ISR_EEPROM_READY ISR_INTERRUPT0..7 (external interrupts 0 to 7) ISR_OVERFLOW0..3 (timer/counter overflow) ISR_UART0_DATA, SIG_UART0_RECV, ISR_UART0_TRANS (UART empty/receive/transmit interrupts) To create a interrupt routine, select a macro-signal combination and: #include <avr/signal.h> ISR(SIG_INTERRUPT0) { /* Your interrupt handler routine */ } Example #define F_CPU 16000000ul #include <avr/io.h> #include <avr/interrupt.h> //definition of fxtal for CPU in Hertz, ul - unsigned long //Standard AVR header //is a header file whit interrupt service functions int main( void ) { DDRA = 0b10000000; //Configure PORTA.7 as output DDRC = 0b01000000; //Configure PORTC.6 as output DDRD = 0b00100000; // Configure PORTD.5 as output TIMSK=(1<<TOIE0) (1<<TOIE1); //enable timer overflow interrupt for both Timer0 and Timer1 TCNT0=0x00; //set Timer0 counter initial value to 0 TCNT1=0x0000; //set Timer1 counter initial value to 0 TCCR0 = (1<<CS02) (1<<CS00); //Normal Mode, start 8-bit Timer0 with 1024 prescaler TCCR1B = (1 << CS10) (1 << CS12);//Normal Mode, start 16-bit Timer1 also with 1024 sei();//global enable interrupts while(1)//main loop { PORTC ^=_BV(6); //Toggle bit PORTC.6 only } } //------------------------------ Interrupt Routines ------------------------------ ISR(TIMER1_OVF_vect) {// Timer1 overflow PORTD ^=_BV(5); //Toggle bit PORTD.5 only } ISR(TIMER0_OVF_vect) {// Timer0 overflow PORTA ^=_BV(7); //Toggle bit PORTA.7 only } 13