1 Interrupts and Using Them in C These lecture notes created by Dr. Alex Dean, NCSU.

Slides:



Advertisements
Similar presentations
Chapter 3 Basic Input/Output
Advertisements

Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Chung-Ta King National Tsing Hua University
Interrupts Chapter 8 – pp Chapter 10 – pp Appendix A – pp 537 &
A look at interrupts What are interrupts and why are they needed.
ECE 447 Fall 2009 Lecture 9: TI MSP430 Interrupts & Low Power Modes.
68HC11 Polling and Interrupts
EET 2261 Unit 9 Interrupts  Read Almy, Chapters 17 – 19.  Homework #9 and Lab #9 due next week.  Quiz next week.
ECE 372 – Microcontroller Design Parallel IO Ports - Interrupts
Interrupts What is an interrupt? What does an interrupt do to the “flow of control” Interrupts used to overlap computation & I/O – Examples would be console.
COMP3221: Microprocessors and Embedded Systems Lecture 15: Interrupts I Lecturer: Hui Wu Session 1, 2005.
A look at interrupts What are interrupts and why are they needed in an embedded system? Equally as important – how are these ideas handled on the Blackfin.
6-1 I/O Methods I/O – Transfer of data between memory of the system and the I/O device Most devices operate asynchronously from the CPU Most methods involve.
Interrupt Processing Haibo Wang ECE Department
A look at interrupts What are interrupts and why are they needed.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Midterm Wednesday 11/19 Overview: 25% First Midterm material - Number/character representation and conversion, number arithmetic - DeMorgan’s Law, Combinational.
The 8051 Microcontroller and Embedded Systems
INTERRUPTS PROGRAMMING
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
Introduction to Embedded Systems
COMP201 Computer Systems Exceptions and Interrupts.
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Ch. 9 Interrupt Programming and Real-Time Sysstems From Valvano’s Introduction to Embedded Systems.
V 0.91 Polled IO versus Interrupt Driven IO Polled Input/Output (IO) – processor continually checks IO device to see if it is ready for data transfer –Inefficient,
A Simple Tour of the MSP430. Light LEDs in C LEDs can be connected in two standard ways. Active high circuit, the LED illuminates if the pin is driven.
MSP430 Mixed Signal Microcontroller – Parte 2 Afonso Ferreira Miguel Source: slau056d – Texas instruments.
Chapter 4 TIMER OPERATION
MICROPROCESSOR INPUT/OUTPUT
CoE3DJ4 Digital Systems Design Chapter 6: Interrupts.
CHAPTER 3 TOP LEVEL VIEW OF COMPUTER FUNCTION AND INTERCONNECTION
Khaled A. Al-Utaibi  Interrupt-Driven I/O  Hardware Interrupts  Responding to Hardware Interrupts  INTR and NMI  Computing the.
PIC16F877 ISR Points to note Interrupt Triggers –Individual interrupt flag bits are set, regardless of the status of their corresponding mask bit, PEIE.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 5 part 4 Exceptions.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This course provides an overview of the CPU architecture.
Interrupt.
Microprocessors 1 MCS-51 Interrupts.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This training course provides an overview of the CPU architecture.
13-Nov-15 (1) CSC Computer Organization Lecture 7: Input/Output Organization.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
1 © Unitec New Zealand Interrupt Lecture 6 Date: - 20 Sept, 2011 Embedded Hardware ETEC 6416.
1 Interrupts and Using Them in C Lecture In These Notes... Interrupts –How they work –Creating and debugging C interrupt routines –Sources M16C.
1 ARM University Program Copyright © ARM Ltd 2013 Using Direct Memory Access to Improve Performance.
1 Interrupts, Resets Today: First Hour: Interrupts –Section 5.2 of Huang’s Textbook –In-class Activity #1 Second Hour: More Interrupts Section 5.2 of Huang’s.
AT91 Interrupt Handling. 2 Stops the execution of main software Redirects the program flow, based on an event, to execute a different software subroutine.
ECE 447 Fall 2009 Lecture 7: MSP430 Polling and Interrupts.
I NTRODUCTION TO PIC PROGRAMMING By : S HERIF H ARHASH.
Handling multiple input signals Version #2 – co-operative scheduler Version #3 – pre-emptive scheduler.
CSCI1600: Embedded and Real Time Software Lecture 16: Advanced Programming with I/O Steven Reiss, Fall 2015.
Embedded Systems Design 1 Lecture Set 8 MCS-51 Interrupts.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
CS-280 Dr. Mark L. Hornick 1 Sequential Execution Normally, CPU sequentially executes instructions in a program Subroutine calls are synchronous to the.
The 8051 Microcontroller Chapter 6 INTERRUPTS. 2/29 Interrupt is the occurrence of a condition an event that causes a temporary suspension of a program.
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
68HC11 Interrupts & Resets.
Microprocessor Systems Design I
Mon. Oct 2 Announcements Quiz Postponed to Wednesday – still only on 2.a + 2.b Video lecture for 2.a posted Lab 6 experiment extension You must come to.
Timer and Interrupts.
Interrupts In 8085 and 8086.
Introduction to Micro Controllers & Embedded System Design Interrupt
Chapter 10 The Stack.
* * * * * * * 8051 Interrupts Programming.
CSCI1600: Embedded and Real Time Software
Interrupts, Tasks and Timers
Lecture 9: TI MSP430 Interrupts & Low Power Modes
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

1 Interrupts and Using Them in C These lecture notes created by Dr. Alex Dean, NCSU

2 In These Notes... Interrupts –How they work –Creating and debugging C interrupt routines Readings –M16C Software Manual, Interrupt chapter –M16C Hardware Manual, Interrupt chapter –C Language Programming Manual, Interrupt Processing chapter –If not already done, read… “Introduction to Interrupts,” Russell Massey “Interrupt Latency,” Jack Ganssle “Introduction to Interrupt Debugging,” Stuart Ball

3 Interrupts and Polling Consider the task of making coffee –We need to boil water, but don’t know exactly how long it will take to boil How do we detect the water is boiling? –Keep watching the pot until we see bubbles This is called polling Wastes time – can’t do much else –Put the water in a kettle which will whistle upon boiling The whistle is an interrupt Don’t need to keep watching water. Instead you can do something else until the kettle whistles. Much more efficient

4 Breakfast Timeline Start preparing breakfast: Put water in kettle and turn on burner Put ground coffee in filter in cone Put cone on cup Get milk out of fridge Get cereal from cabinet Resume breakfast preparation: Get bowl from cabinet Pour cereal into bowl Open milk Pour milk into bowl Close milk Get spoon Put milk back in fridge Water boils and kettle whistles An interrupt occurs Put down whatever is in hands Turn off burner Pour boiling water into coffee filter Pick up whatever was put down Interrupt Service Routine

5 Microcontroller Interrupts Types of interrupts –Hardware interrupts Asynchronous: not related to what code the processor is currently executing Examples: INT0 input becomes active, character is received on serial port, or ADC converter finishes conversion –Software interrupts Synchronous: are the result of specific instructions executing Examples: BRK, INT, undefined instructions, overflow occurs for a given instruction –We can enable and disable (mask) most interrupts as needed (maskable), others are non-maskable Interrupt service routine (ISR) –Subroutine which processor is forced to execute to respond to a specific event –After ISR completes, MCU goes back to previously executing code

6 Interrupt Processing Sequence for Enabled Interrupt Other code (background) is running Interrupt trigger occurs Processor does some hard-wired processing Processor executes ISR (foreground), including return-from- interrupt instruction at end Processor resumes other code Main Code (Background) ISR (Foreground)

7 TMP FLG Hard-Wired Processor Response to Interrupt 1.Finish or interrupt current instruction 2.Read a special control register (at address 00000h) for interrupt source information 3.Save flag register FLG in temporary FLG register: 4.Clear certain flags in FLG: Interrupt enable - I Debug – D Stack pointer select – U (except for sw ints 32-63) 5.Push the temporary FLG register and PC onto stack (see left) 6.Set interrupt priority level in IPL 7.Fetch interrupt vector from vector table and load into PC M16C62P Hardware Manual, (Interrupt)

8 Interrupt Considerations Vectors Saving and Restoring Context Prioritization Response time Maximum interrupt rate

9 Vector Table – Where Do We Go? FFFFC FFFF8 FFFF4 FFFF0 Reset Interrupt Vector Non-maskable Interrupt Vector Debug Control Interrupt Vector Watchdog Timer Interrupt Vector Reset Interrupt Service Routine Watchdog Timer Interrupt Service Routine Debug Control Interrupt Service Routine Non-maskable Interrupt Service Routine start WDT_ISR DBC_ISR NMI_ISR DBC_ISR

10 M16C Vectors Details Need a table to list where the ISRs are located in memory –Each table entry is called a vector – 4 bytes holding ISR address –M16C62P Hardware manual Two kinds of interrupts: fixed and variable (M16C feature) –Fixed have vectors to ISRs at hardware-defined address Examples: undefined instruction, overflow, BRK, address match, single step, WDT, DBC, UART1, Reset –Variable have vectors to ISRs at user-definable address Why have two kinds? –Flexibility for programmer, can use different ISRs in different operating modes

11 Saving and Restoring Context Problem –Since ISR is asynchronous with main code (could happen at any time), we have no idea which registers are live (being used). Conceivably all* registers (R0-3, A0-1, FB, SB) could be live –The ISR needs to use some registers to do its work Solution 1: use stack –Before modifying any registers, ISR must push values of registers it may use onto the stack –Before returning, pop these values back into the registers Solution 2: use a different register bank –Before modifying any registers, ISR must switch to use an alternate register bank –Before returning, switch back to the original register bank To exit ISR, use return from interrupt (REIT) instruction –Restores even more context – what processor automatically saved when responding to interrupt Pops FLG from stack Pops PC from stack –Makes CPU resume executing original code! –Takes 6 cycles

12 Prioritization Interrupt requests are considered simultaneous if they occur between the same two clock ticks Interrupts are prioritized to order the response simultaneous interrupt requests –Priorities of some interrupts are fixed by the hardware design Reset (highest priority) NMI DBC Watchdog timer Peripheral I/O Single step Address match –Priorities of other (peripheral) interrupts are adjustable Value loaded into the interrupt priority level register (ILVL2-0) for that interrupt If there are multiple simultaneous interrupt requests with the same priority level, the hardware prioritizes them

13 Interrupt Response Latency Latency = time delay Why do we care? –This is overhead which wastes time, and increases as the interrupt rate rises –This delays our response to external events, which may or may not be acceptable for the application, such as sampling an analog waveform How long does it take? –Finish executing the current instruction (up to 30 cycles for DIVX) or freeze it –Push various registers on to the stack, fetch vector C IntResponseOvhd : Overhead for responding to each interrupt (18 to 20 cycles for M16C) Depends on alignment of stack pointer and interrupt vector odd pointers are unaligned (words start at even addresses) so they take 2 cycles each to dereference (follow to memory) –If we have external memory with wait states, this takes longer Maximum 50 cycles to respond –50 / 16 MHz = microseconds -- > need to design for this

14 Maximum Interrupt Rate We can only handle so many interrupts per second –F Max_Int : maximum interrupt frequency –F CPU : CPU clock frequency –C ISR : Number of cycles ISR takes to execute –F Max_Int = F CPU /C ISR –Note that this assumes there are no other active interrupts in the system –Example: If the ISR takes 56 cycles to execute (worst case), the maximum interrupt rate we can handle is 24 MHz / 56 cycles = 428 kHz When processor is responding to interrupts, it isn’t executing our other code –U Int : Utilization (fraction of processor time) consumed by interrupt processing –U Int = 100%*F Int * (C ISR +C IntResponseOvhd )/ F CPU –Example: If that 56-cycle ISR is being triggered at 100 kHz, U Int = 100%*100kHz*(56+20)/24 MHz = 31.7%. CPU looks like it’s running the other (background) code at about 16 MHz, not 24 MHz

15 Checklist for Using Interrupts in C Read –M16C Software Manual, (Interrupt) –C Language Programming Manual, (Interrupt Processing) Configure MCU –Set up peripheral to generate interrupt –Enable interrupts for system (set I bit in FLG) Write ISR my_isr and identify it as an ISR using #pragma INTERRUPT my_isr Register ISR in interrupt vector table

16 Configure Peripheral to Generate Interrupt XXX ic is interrupt control register for the peripheral At least four fields – look in sfr62p.h for field names –Interrupt level (3 bits) Sets priority of interrupt. If 0, is disabled. Processor will not respond to interrupts with priority level < processor interrupt priority level ilvl0_XXXic, ilvl1_XXXic, ilvl2_XXXic. –Interrupt Request (1 bit) – if 1, the condition for generating an interrupt is true (can read this field even if interrupt is disabled) Hardware Interrupt Types –INT0-5: input signals from MCU pins –Peripherals can also generate interrupt requests: ADC, serial, etc.

17 Interrupt Control Register for INT0-5

18 Interrupt Control Register for Other Peripherals

19 Configure MCU to respond to the interrupt Set global interrupt enable flag I (in FLG) –This flag does not enable all interrupts; instead, it is an easy way to disable interrupts –Use inline assembly code to set the flag: _asm(“fset i”) or use ENABLE_IRQ macro in qsk_bsp.h MCU has eight interrupt priority levels (IPL bits in FLG), is 0 after reset –Set using inline assembly code To respond to interrupt, I flag must be 1, interrupt request bit must be 1 and interrupt priority level must be >= processor IPL

20 #pragma INTERRUPT my_isr /* or INTERRUPT/B */ void my_isr(void) { /* do whatever must be done */ } Write Interrupt Service Routine Rules for writing ISRs –No arguments or return values – void is only valid type –Keep it short and simple Much easier to debug Improves system response time Tell compiler it’s an ISR –ISR has a different stack frame compared with subroutine Saves all registers Flag register saved –Different return needed (REIT vs. EXITD) –So use #pragma INTERRUPT my_isr Potentially use register bank switching for faster interrupt response –Use INTERRUPT/B –Compiler inserts code to switch to register bank 1 for ISR and back to bank 0 on return (fset B, fclr B) –This only works if interrupts can’t be interrupted (don’t set I flag while in ISR)

21 Comparing Functions and ISRs void normal_function(void) { _normal_function: count++; ENTER #00H } ADD.W #1H,0400H EXITD #pragma INTERRUPT isr _isr: void isr(void) { PUSHM R0,R1,R2,R3,A0,A1 count++; ENTER #00H } ADD.W #1H,0400H POPM FB POPM R0,R1,R2,R3,A0,A1 REIT #pragma INTERRUPT/B reg_bank_b_isr _reg_bank_b_isr: void reg_bank_b_isr(void) { FSET B count++; ENTER #00H } ADD.W #1H,0400H POPM FB REIT

22 Register the Interrupt Vector Modify vector table in sect30.inc to point to our ISR so MCU vectors to our code when our interrupt occurs –Find the correct interrupt vector to replace –Replace dummy_int vector with _my_isr (address of the function) dummy_int is an ISR which returns immediately, doing nothing –All unused vectors are filled with this by default in sect30.inc Compiler prepends _ to C symbol names for uniqueness when compiling So, we need to prepend _ when referencing C symbols (functions and variables) from asm –Add.glb _my_isr directive to tell assembler the symbol _my_isr is defined elsewhere (“global”).sectionvector; variable vector table.orgVECTOR_ADR ….lworddummy_int; TIMER B1 (for user) (vector 27).lworddummy_int; TIMER B2 (for user) (vector 28).lworddummy_int ; INT0 (for user) (vector 29).lworddummy_int ; INT1 (for user) (vector 30).lworddummy_int; Reserved …

23 Register the Interrupt Vector Modify vector table in sect30.inc to point to our ISR so MCU vectors to our code when our interrupt occurs –Find the correct interrupt vector to replace (e.g. vector 29) Note that sect30.inc for QSK62P has two variable vector tables, but only one is used (the “.else” case of “.if M60TYPE == 1” (MCU isn’t this type) –Replace dummy_int vector with _my_isr (address of the function) dummy_int is an ISR which returns immediately, doing nothing –All unused vectors are filled with this by default in sect30.inc Compiler prepends _ to C symbol names for uniqueness when compiling So, we need to prepend _ when referencing C symbols (functions and variables) from asm –Add.glb _my_isr directive to tell assembler the symbol _my_isr is defined elsewhere (“global”).sectionvector; variable vector table.orgVECTOR_ADR ….lworddummy_int; TIMER B1 (for user) (vector 27).lworddummy_int; TIMER B2 (for user) (vector 28).glb _my_isr.lword_my_isr; INT0 (for user) (vector 29).lworddummy_int ; INT1 (for user) (vector 30).lworddummy_int; Reserved …

24 Building a Program – Start with the basics Program requirements: –Req1: When S1 is pressed, toggle the red LED –Req2: When S2 is pressed, toggle the yellow LED –Req3: Count the switch presses for S1 and S2 –Req4: Use interrupts for switch press detection All of the switches on our board are attached to port 8 (see schematic) –S1 = p8_3 => Interrupt 1 –S2 = p8_2 => Interrupt 0 –S3 = p8_1 => (see schematic) Button presses are “negative logic” so the interrupt will be based on the negative edge (1 -> 0) of the press.

25

26 System Tasks and Data Flow INT0 ISR INT1 ISR RedCount YellowCount Main Global Variable ISRTask (does initialization, then does nothing)

27 Building a Program – Break into Pieces First break into threads, then break thread into steps Main thread: –First initialize system init_switches: configure the ports connected to S1, S2 and S3 to be inputs init_LEDs: configure the ports connected to the LEDs to be outputs init_ints: initialize the controller for interrupts 0 & 1 –Then do nothing switch1_ISR thread: –Toggle red LED –Update red_count switch2_ISR thread: –Toggle yellow LED –Update yellow_count

28 Start with the Basics, and Trust Nothing IT IS ALWAYS BEST TO START SIMPLE!!!!!!! –Verify that each part of the basic system runs as you build software. –Easier to debug – easier to exclude potential sources of problems, so bugs can be found faster Use an incremental approach to verify system works –Initialize ports for LEDs and switches –Test 1: Verify LEDs work – Turn on all LEDs –Test 2: Verify switches work – light LEDs based on switches –Test 3: Add interrupts and test they work Do LEDs toggle? Set breakpoints – do counts increase correctly? Use #defined symbols rather than “magic numbers” to help convey meaning of operation

29 Defining Names and Connections #include “qsk_bsp.h” // dir selection for GPIO #define DIR_IN (0) #define DIR_OUT (1) // polarity selection for // interrupts #define FALLING_EDGE (0) #define RISING_EDGE (1) int red_count; // count S1 presses int yellow_count; // count S2 presses void init_switches(void) { // set up GPIO bits for switches to // be inputs S1_DIR = S2_DIR = S3_DIR = DIR_IN; } void init_LEDs(void) { RED_DDR = YLW_DDR = GRN_DDR = DIR_OUT; RED_LED = YLW_LED = GRN_LED = LED_ON; RED_LED = YLW_LED = GRN_LED = LED_OFF; red_count = yellow_count = 0; }

30 Testing LEDs and Switches void init_LEDs(void) { RED_DDR = YLW_DDR = GRN_DDR = DIR_OUT; RED_LED = YLW_LED = GRN_LED = LED_ON; RED_LED = YLW_LED = GRN_LED = LED_OFF; red_count = yellow_count = 0; } void test_switches(void) { for(;;) { // infinite loop if (!S1) RED_LED = LED_ON; else RED_LED = LED_OFF; if (!S2) YLW_LED = LED_ON; else YLW_LED = LED_OFF; if (!S3) GRN_LED = LED_ON; else GRN_LED = LED_OFF; } Test LEDs here. Step through with debugger, since LEDs would be on for less than a microsecond at normal speed Test switches here. Can run code at normal speed since it echoes user behavior

31 Adding and Testing Interrupts /** Sets INT0 and INT1 to priority * level 7, triggering on falling * edge. */ void init_ints(void) { // set interrupt priority level // for int0 to 7 (binary 111) ilvl0_int0ic = 1; ilvl1_int0ic = 1; ilvl2_int0ic = 1; // set int0 to trigger on // negative edge (1->0) pol_int0ic = FALLING_EDGE; // set interrupt priority level // for int1 to 7 (binary 111) ilvl0_int1ic = 1; ilvl1_int1ic = 1; ilvl2_int1ic = 1; // set int1 to trigger on // negative edge (1->0) pol_int1ic = FALLING_EDGE; } #pragma INTERRUPT switch1_isr void switch1_isr(void) { red_count++; if(red_count % 2) RED_LED = LED_ON; else RED_LED = LED_OFF; } #pragma INTERRUPT switch2_isr void switch2_isr(void) { yellow_count++; if(yellow_count % 2) YEL_LED = LED_ON; else YEL_LED = LED_OFF; } sect30.inc – note M60TYPE != 1.glb_switch2_isr.lword_switch2_isr ; INT0 (vector 29).glb_switch1_isr.lword_switch1_isr ; INT1 (vector 30)

32 Main Function void main(void) { mcu_init(); init_switches(); init_LEDs(); init_ints(); RED_LED = LED_ON; // remove once the program works test_switches(); // remove once the program works _asm("fset i"); // enable interrupts while (1) { // can do other work here, and the LEDs will still // toggle on a switch press, because the interrupt will // interrupt any work being done here. }