P. 1
Introduction to Embedded Systems Timers and Interrupts 3466

Introduction to Embedded Systems Timers and Interrupts 3466

|Views: 39|Likes:
Published by Omveer Chaudhary

More info:

Published by: Omveer Chaudhary on Oct 28, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PPT, PDF, TXT or read online from Scribd
See more
See less

04/29/2012

pdf

text

original

Timers and Interrupts

Lecture 9

Introduction to Embedded Systems

Summary of Previous Lecture
• The lab 80200 Big-Endian Evaluation Board
– 80200 Intel Xscale® processor – Xilinx FPGA – PCI

• The X-Board and accessories
– Seven-segment LED, Serial ports

• Preparing you for Lab 2

 Flash memory  Serial ports

Introduction to Embedded Systems

Administrivia
• Lab 2 is out on the street • Lab 2 – Individual (and not a group) project

Introduction to Embedded Systems

Outline of This Lecture • Timers • Interrupt vs. the main lecture Introduction to Embedded Systems . but indirect/direct background/review material for. polled I/O – Examples • IRQ and FIQ Detours signify material outside of.

Questions on X-Board • What kind of power supply (wattage) are we using? – We are using the standard ATX PC power supply – Maximum of 300 W – The voltages required by the board are 3.3V or 5V (if the PCI card is connected) – The ATX can supply 3.3V) Introduction to Embedded Systems .3/5/12 V (the original adaptor could supply only 3.

– Lady Astor Introduction to Embedded Systems .Quote of the Day The penalty of success is to be bored by the people who used to snub you.

Introduction to Embedded Systems .

but capacitor charged in opposite polarity 5. Inductor tries to keep current moving and charges other plate of capacitor 4.How Oscillators Work • • • • Some form of energy needs to move back and forth Capacitor stores energy in an electrostatic form Inductor stores energy in a magnetic form Oscillation cycle 1. creating a magnetic field 3. Inductor field collapses. Capacitor starts off charged 2. Capacitor discharges through inductor. Capacitor re-starts the cycle • Question: When does this stop? • Timer = oscillator + counter Introduction to Embedded Systems .

3 MHz) – Ethernet • Divides the 50 MHz clock by 2 (~25 MHz) Introduction to Embedded Systems .6 MHz – Connected to FPGA • All clocks are derived from these sources – 80200 Processor • Multiplies 66.6 MHz clock by 6 at power-up (~ 400 MHz) • Firmware multiplies this by an integer ranging from 5 (~333 MHz) to 11 (~733 MHz) – Memory • Multiplies the 50 MHz clock by 2 (~100 MHz) – PCI and Serial Ports • Divides the 50 MHz clock by 1.5 (~33.X-Board System Clocks • Two oscillators – 50 MHz and 66.

X-Board System Clocks Introduction to Embedded Systems .

What is a Timer? • A device that uses highspeed clock input to provide a series of time or count-related events Counter Register System Clock 0x1206 Reload on Zero ÷ Clock Divider Interrupt to Processor 000000 Countdown Register I/O Control Introduction to Embedded Systems .

X-Board Timers • The X-Board has two timers • Timer A and Timer B • These timers can interrupt the 80200 processor – Interrupt will be held active until cleared by the 80200 • • • • 32-bit timers 33.3MHz peripheral bus clock is the time base 128 seconds maximum timing interval Each timer consists of – A down counter which reloads itself when it hits underflow • Can be programmed for one-shot operation – Disables itself when it hits zero Introduction to Embedded Systems .

What Kinds of Timers Exist? • What is a timeout? • Pause Function – Suspends task for a specified amount of time • One-shot timer – Single one-time-only timeout • Periodic timer – Multiple renewable timeouts • Time-slicing – Chunks of time to each task • Watchdog timer Introduction to Embedded Systems .

X-Board Timer Status/Control Registers • Timer Status/Control Register – One for each of the two timers – To control the features of the timer Introduction to Embedded Systems .

X-Board Timer Preload Registers Introduction to Embedded Systems .

X-Board Timer Current Value Introduction to Embedded Systems .

etc. • Preload register – Generally set to the highest possible value to get the longest possible time • Current value register – Contains the current value while the timer is enabled.Which Timers to Use for Lab 2? • We use all the 3 registers • Status-control register – Used for control operations – Enable/disable. • There is some sample "timer" code in the appendix of the Lab2 handout. clearing interrupts. Introduction to Embedded Systems .

Inside the Timer High Byte Counter Register at offsets 0x04. 0x00 (read only) Introduction to Embedded Systems . write a ``1'' to immediately write Current Counter value to readable Latch Reg Latched Counter at offsets 0x04. immediately moves Counter Reg value into Current Counter Current Counter (not directly readable by software) Latch Register offset 0x0C. 0x00 (write only) Low Byte GO Register offset 0x08.

typedef timer_s *timer_p. space3[3]. timer = (timer_p) TIMER1. latch. }. countHigh. timer_p timer. space4[3]. space2[3].Structs for the Timer • In ANSI C. go. space1[3]. Introduction to Embedded Systems . structs are laid out in RAM in ascending order – Example for the timer #define TIMER0 0x10200040 #define TIMER1 0x10200050 volatile struct timer_s { unsigned unsigned unsigned unsigned unsigned unsigned unsigned unsignec char char char char char char char char countLow.

2MHz / 100 = 20. timer>go = (unsigned char) 0x1. Introduction to Embedded Systems . timer>countHigh = (unsigned char) ((time > 8) & 0xff). 0x10200050 time = 2000000 / 100.000 #define TIMER1 int time. timer>countLow = (unsigned char) (time & 0xff).Setting the Timer's Counter Registers • Counter is usually programmed to reach zero X times per second – To program the timer to reach zero 100 times per second – Example: For a 2 MHz-based timer. timer = (timer_p) TIMER1.

time = time | (unsigned int) (timer>countHigh << 8). timer>latch = (unsigned char) 1. time = time | (unsigned int) (timer>countHigh << 8). } Introduction to Embedded Systems . time = (unsigned int) timer>countLow.Reading the Timer's Current Value • Must first dump the current value so that it can be read – Why? #define TIMER1 0x10200050 int time. • How do we know when the timer has reached zero? while (1){ timer>latch = (unsigned char) 1. timer = (timer_p) TIMER1. time = (unsigned int) timer>countLow.

g. FIQ) Introduction to Embedded Systems .Interrupt vs. Polled I/O • Polled I/O requires the CPU to ask a device (e. if the toggle switches have changed position – Software plans for polling the devices and is written to know when a device will be serviced • Interrupt I/O allows the device to interrupt the processor. toggle switches) if the device requires servicing – For example. announcing that the device requires attention – This allows the CPU to ignore devices unless they request servicing (via interrupts) – Software cannot plan for an interrupt because interrupts can happen at any time therefore. software has no idea when an interrupt will occur – This makes it more difficult to write code • Processors can be programmed to ignore interrupts – We call this masking of interrupts – Different types of interrupts can be masked (IRQ vs.

X-Board Interrupt Controller Introduction to Embedded Systems .

unsigned int tmpSwitchState. char * argv[]) { volatile unsigned int *switchBank = (unsigned int *) SWITCH_BASE. while (1) { /* loop until a switch is pressed */ while (prevSwitchState == (tmpSwitchState = (*switchBank & 0xff))) {}. unsigned int prevSwitchState.Polling Example • Polling the switches #define SWITCH_BASE 0x18200000 int main(int argc. /* get the current state of the switches */ prevSwitchState = *switchBank & 0xff. } } /* end main() */ Introduction to Embedded Systems .

InterruptDriven I/O • Polling requires code to loop until device is ready – Consumes lots of CPU cycles – Can provide quick response (guaranteed delay) • Interrupts don't require code to loop until the device is ready – Device interrupts processor when it needs attention – Code can go off and do other things – Interrupts can happen at any time • Requires careful coding to make sure other programs (or your own) don't get messed up Introduction to Embedded Systems .Polling vs.

i++) { char_buffer[i] = (unsigned char)NULL.Polling the Serial Port . *(volatile unsigned char*)UART2_FIFO_CNTRL = 0x06. unsigned int i. volatile unsigned char* UART_ptr. i<sizeof(char_buffer). char_ptr = char_buffer. // Empty any trash in the RX for(i=0. // Clear FIFO *char_ptr = *(volatile unsigned char*)UART2_RX_REG.I // Function to read from the serial keypad unsigned char* my_gets(void) { unsigned char* char_ptr. static unsigned char char_buffer[116]. Introduction to Embedded Systems . } UART_ptr = (volatile unsigned char*)UART2_LINE_STAT.

} *char_ptr = *(volatile unsigned char*)UART2_RX_REG. } Introduction to Embedded Systems . // Echo char back if ((*char_ptr&0xffff) == 125) sendchar("v").*char_ptr&0xffff).II // Keep looking at characters until '\r' while(1){ // Wait for Rx character forever while ((*UART_ptr & 0x01) == 0) { . // Read the character if(*char_ptr != BACKSPACE) { printf("%d".Polling the Serial Port .

// If Backspace. } Introduction to Embedded Systems . backup cursor. backup cursor char_ptr--. delete character on screen.Polling the Serial Port .III // Exit the 'while' when we see '\r' or the buffer fills if((*char_ptr == '\r') || (char_ptr == &char_buffer[sizeof(char_buffer)-1])) break. // delete char from buffer } } else { char_ptr++. } } *char_ptr = '\000'. backup char_ptr so next char will overwrite bad input if(*char_ptr == BACKSPACE){ if(char_ptr > char_buffer){ // If at least 1 character. // Terminate the string return(char_buffer).

the interrupt-controller automatically masks all interrupts of lower priority • IRQL (Interrupt Request Level) – Level of interrupt that the CPU is currently masking • HAL (Hardware Abstraction Layer) – Maps interrupt-controller’s interrupt levels onto hardware interrupts – IRQL Table • ISR (Interrupt Service Routine) – Interrupt handlers registered with the interrupt-controller • Spinlock – Synchronization primitive to prevent simultaneous ISR execution on multiple processors • DPC (Deferred procedure call) – ISR indicates that it can do work at a lower priority level Introduction to Embedded Systems .Windows NT Interrupt Architecture • When a device raises an interrupt.

Windows NT Interrupt Architecture Introduction to Embedded Systems .

Interrupts and the Blue Screen of Death? • Two kinds of ISRs: Default system ISRs and user-registered ISRs • Unregistered interrupts made to point at ISRs that indicate that an illegal system interrupt has been generated • Illegal system interrupts often lead to the Blue Screen of Death Introduction to Embedded Systems .

processor switches to FIQ32_mode registers or IRQ32_mode registers – On any interrupt (or SWI Trap) • Switch register banks • Copy PC and CPSR to R14 and SPSR • Change new CPSR mode bits Introduction to Embedded Systems . set the corresponding “F” or “I” bit to 1 • On interrupt.IRQ and FIQ • Program Status Register 31 30 29 28 27 … 8 N Z C V 7 I 6 F 5 4 3 2 1 0 M4 M3 M2 M1 M0 – To disable interrupts.

Interrupt Example Data Bus ARM 7500 Processor Address Bus A0 A2 A4 A6 A8 A10 A12 A14 A16 A18 A20 A22 A24 A26 A28 A30 8-bit register Vcc D7 D6 D5 D4 D3 D2 D1 D0 31 30 29 28 27 N Z C V … 8 7 I 6 F 5 4 3 2 1 0 M4 M3 M2 M1 M0 Introduction to Embedded Systems .

Summary of Lecture • Timers – – – – What is a timer? A peek inside the timers C structs for the timer Using the timer • Interrupt vs. polled I/O – polling example – interrupt example • IRQ and FIQ Introduction to Embedded Systems .

0/ W 39077:59...3/879903943403.0.08  190/0..0706:708807.08..08..8 ..705.250 19094089.0-0807.. 706:70890!&94.3 470.30/548943 $419../0.3814754390/0. /0.0 0  94089.

9.3-02.70..33495..4890!&943470/0.3.:7 %82..80/ #".220/94347039077:598 0.70.08847  .3 . .3-05747.3 920 90701470 8419.982.:994790..39.4890/0..834/0.4/0 W !74.5503..339077:594.9903943 %8..03.3147.943942-0//0/$89028 .0706:708..990/0.334:3.83 4139077:598 110703995084139077:598.8 "  3974/:.339077:59-0..09439077:59 90574.08:308890706:089807.088478. 39077:598  $419..:8039077:598..0892470/11..

7/39077:594397407 3974/:. 4.943942-0//0/$89028 .

.3 :3830/39  $%*$ :3830/39925$9..90 :3830/39570.$9.90 . .4.7.08 /0130$%*$   392.90:3830/39 89.$9.$9.250 W !439089.3 39.7 .!43.7.(   .

0990.:7703989.90419089.08 .

3  11 0   .$9.90 89.$9. 570..

8570880/ . 445:39.89.

.$9.90 89. 0 570.90 925$9.$9.3  11 < < <.$9.

03/2.3  .

 3974/:.943942-0//0/$89028 .

!43.8 39077:59 7.03.

9./06:.08 .  W !43 706:708..9903943 4/0.08:7049075747.08847039300/8.4/094445 :39/0.3..039077:598574.5503.70854380 :./ 0..943942-0//0/$89028 .0870.  W 39077:598/43 9706:70.4/094445 :3990/0../ 438:208498 41!&...28 474:7 43 /43 90920880/:5 3974/:.3//44907938 39077:598.7.34411.701:.0870.3920 W #06:708..4/3942.3900//0.3574.

!4390$07.!479  !4390$07.!479 .

.

7 &#%*597 :3830/39 89..4/  :3830/.05./174290807...:3830/./ :3830/.90:3830/.9.7 &#%* *%#  ..9439470..7 .4..90:3830/.7*-:1107( .7.7*597 .7 2*098 ..7*-:1107 ..4.:3.7*597..

.

7*597 ..4.7 &#%*#*#.90:3830/..7 .0.

.

7*-:1107( :3830/.7*-:1107   .397..7 & < &#%*597 .259..90:3830/...7 &#%**$%% 3974/:.8390 # 147  8041 .943942-0//0/$89028 .4.

!479  !4390$07.!479 .!4390$07.

.

..7.9078:39 7 0   .005443.9.

.

.90:3830/..7*597 ...9071470..4.07 0 &#%*597       < .9147#.7 &#%*#*#.7.

.

/90 ..907 1 ..7*597 1111 ..#0.7.7*597$!  57391 / ..

.

7*597 1111  803/.943942-0//0/$89028 ..4..7-.7 . 1 ....  < 3974/:.

!479 .!479  !4390$07.!4390$07.

.

7*-:1107 ( -70... ...990 0 030800 7 4790-:110718 1 .7*-:11078041 .7*597 .7*597 7  .

.

7*597$!  1 .7*-:1107  ...1..07790-./35:9 1 .:5...7*597.7*59784309.0 -.74....85.

.

.90.7003 -.:7847 /0090...907 -.7*597 .:7847 .907438...:5..:5.7.1.89..7.

.

7*597  ./0090...7*597 < <  ..71742-:1107 < <080 .

.

%0723.943942-0//0/$89028 ..7*-:1107  < 3974/:.90908973 709:73 .

 $3.07..9089.339077:59 9039077:59 . $#3/.808.3/0787089070/99039077:59 .9435729..088478 W ! 010770/574.304:8$#00.4397407 W $534.943.7/.90.084394.:942.04139077:599.9...3/48%39077:597.5839077:59 .743.7039077:598 #"%.4397407 839077:590.4397407 .9:70 W 03.0#4:930 39077:59.407574790.70-897./0.39077:5984140757479 W #" 39077:59#06:0890.094570.:770392.88 .943942-0//0/$89028 ..0 3974/:.-0 W $# 39077:59$07.3/447.83 W  .7/.2..99.03982:9..0/:70.990!&8.0 0.:94343 2:950574.07 .9.

9:70 3974/:.90.3/48%39077:597.943942-0//0/$89028 .

3/90:0$./9490:0$. 8890239077:59.9.:988902$#8.3/:807 7089070/$#8 W &37089070/39077:5982.39077:598.909.943942-0//0/$89028 ..3/90:0$.9$#89.93/.7003410.8890239077:598419030.7003410.9 3974/:.9 39077:598.90/ W 0.9 W %43/841$#801./0945439.30.7003410.8-0030307.

2$9.3030!$#24/0-98 3974/:.47708543/3  47  -994 W 339077:59 574.0894"*24/0708907847 #"*24/07089078 3.3/!$#94#.-039077:598 80990.0884789.38 W 45!.#".9:8#08907       '        %4/8.708907-.339077:59 47$%7.3/" W !747.3/$!$# W .5  W $9.943942-0//0/$89028 .

250 ..9.943942-0//0/$89028 .39077:59.08847 //7088:8                  -9708907 '.:8 # !74.               '        3974/:.

9207 50038/09092078 897:.98147909207 &83909207 W 39077:59.98.8 540/.9:70 W %2078 .$:22.7410.

5430.3/" 3974/:.943942-0//0/$89028 .250 39077:590.250 W #".

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->