Week 11
Interrupts in ARMlite
Interrupts in ARMlite · COS10004 Lecture 11.2
Interrupts in ARMlite
Revisiting our Flashing LED Example
- Recall our flashing LED program from last week, and in particular the delay function:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; delay function
;; inputs R0 - time delay in seconds
delay:
push {R3,R4,R5,R6}
MOV R3, R0 ; move delay time param into R3
LDR R4, .Time ; get start imetimer:
LDR R5, .Time ; update time SUB R6, R5, R4 ; calc elapsed time CMP R6, R3 ; compare elapsed to delay time
BLT timer
pop {R3,R4,R5,R6}RET
Revisiting our Flashing LED Example
- This is a busy-wait timer, or in other words, an example of polling:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; delay function
;; inputs R0 - time delay in seconds
delay:
push {R3,R4,R5,R6}
MOV R3, R0 ; move delay time param into R3
LDR R4, .Time ; get start imetimer:
LDR R5, .Time ; update time SUB R6, R5, R4 ; calc elapsed time CMP R6, R3 ; compare elapsed to delay time
BLT timer
pop {R3,R4,R5,R6}RET
Revisiting our Flashing LED Example
- The CPU constants checks for a state – in this case, that the time exceeds a delay time.;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; delay function;; inputs R0 – time delay in seconds delay: push {R3,R4,R5,R6} MOV R3, R0; move delay time param into R3 LDR R4,.Time; get start ime timer: LDR R5,.Time; update time SUB R6, R5, R4; calc elapsed time CMP R6, R3; compare elapsed to delay time BLT timer pop {R3,R4,R5,R6} RET
Revisiting our Flashing LED Example
- The CPU is 100% engaged in this task! It is doing nothing else but wasting CPU cycles;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; delay function;; inputs R0 – time delay in seconds delay: push {R3,R4,R5,R6} MOV R3, R0; move delay time param into R3 LDR R4,.Time; get start ime timer: LDR R5,.Time; update time SUB R6, R5, R4; calc elapsed time CMP R6, R3; compare elapsed to delay time BLT timer pop {R3,R4,R5,R6} RET
Revisiting our Flashing LED Example
- There is a better way! Using Interrupts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; delay function
;; inputs R0 - time delay in seconds
delay:
push {R3,R4,R5,R6}
MOV R3, R0 ; move delay time param into R3
LDR R4, .Time ; get start imetimer:
LDR R5, .Time ; update time SUB R6, R5, R4 ; calc elapsed time CMP R6, R3 ; compare elapsed to delay time
BLT timer
pop {R3,R4,R5,R6}RET
Now back to our Flashing LED
- Lets use ARMlite’s Clock Interrupt for this!
- To set itup the interrupt handling:
- The handler will be called everytime the clock raises an interrupt
- So each time it will draw either a green (“LED on”) pixelor a white (“LED off”) depending on what state the LED is currently in
- This is quite different to previously where we inserted a delay between each state change!
- Now we’re waiting to be told when to change state:
- Event driven!
Flashing LED with INT: Interrupt Handler
///////////////////////////////// /// interrupt handler: toggleLED // toggles state of LED (on "green" or off "white") // toggleLED:
PUSH {R0}
CMP R3, #0 // check state
BNE off
MOV R3, #1 // if R3 0, then 1 MOV R0, #.green
B drawpixel off:
MOV R3, #0 // if R3 1, then 0 MOV R0, #.white
drawpixel:
STR R0, .Pixel367 // draw the pixel
POP {R0}RFE
Flashing LED with INT: Interrupt Handler
///////////////////////////////// /// interrupt handler: toggleLED // toggles state of LED (on "green" or off "white") // toggleLED:
CMP R3, #0 // check state Check next state to enact (on or off)
BNE off
MOV R3, #1 // if R3 0, then 1 MOV R0, #.green
B drawpixel off:
MOV R3, #0 // if R3 1, then 0 MOV R0, #.white
drawpixel:
STR R0, .Pixel367 // draw the pixel
POP {R0}RFE
Flashing LED with INT: Interrupt Handler
///////////////////////////////// /// interrupt handler: toggleLED // toggles state of LED (on "green" or off "white") // toggleLED:
CMP R3, #0 // check state
BNE off
MOV R3, #1 // if R3 0, then 1 MOV R0, #.green
B drawpixel off:
MOV R3, #0 // if R3 1, then 0 next state is “off” ? MOV R0, #.white Then turn LED off
drawpixel:
STR R0, .Pixel367 // draw the pixel
POP {R0}RFE
Flashing LED with INT: Interrupt Handler
///////////////////////////////// /// interrupt handler: toggleLED // toggles state of LED (on "green" or off "white") // toggleLED:
CMP R3, #0 // check state
BNE off
MOV R3, #1 // if R3 0, then 1 next state is “ON” ? MOV R0, #.green Then turn LED ON
B drawpixel off:
MOV R3, #0 // if R3 1, then 0 MOV R0, #.white
drawpixel:
STR R0, .Pixel367 // draw the pixel
POP {R0}RFE
Flashing LED with INT: Interrupt Handler
///////////////////////////////// /// interrupt handler: toggleLED // toggles state of LED (on "green" or off "white") // toggleLED:
CMP R3, #0 // check state
BNE off
MOV R3, #1 // if R3 0, then 1 MOV R0, #.green
B drawpixel off:
MOV R3, #0 // if R3 1, then 0 MOV R0, #.white
drawpixel:
STR R0, .Pixel367 // draw the pixel Draw the pixel (R0 will be set as
POP {R0} either green or white)RFE
Flashing LED with INT: Interrupt Handler
///////////////////////////////// /// interrupt handler: toggleLED // toggles state of LED (on "green" or off "white") // toggleLED:
CMP R3, #0 // check state We push and popR0 so we can
BNE off restoreitafterthefunctionis
MOV R3, #1 // if R3 0, then 1 finished. MOV R0, #.green
B drawpixel We don’tdo thisforR3though – off: why?
MOV R3, #0 // if R3 1, then 0 MOV R0, #.white
drawpixel:
STR R0, .Pixel367 // draw the pixel
POP {R0}RFE
Flashing LED with INT: Full Program
Program 11|/////////////////////////////////
1|// Set up Interrupt handling 12|/// interrupt handler: toggleLED
2| MOV R0,#toggleLED 13|// toggles state of LED (on "green" or off "white")
3| STR R0,.ClockISR 14|//
4| MOV R0,#1000 15|toggleLED:
5| STR R0,.ClockInterruptFrequency 16| PUSH {R0}
6| MOV R0,#1 17| CMP R3, #0 // check state
7| STR R0,.InterruptRegister //Enable all interrupts 18| BNE off
8| MOV R3, #1 // R3 keeps track of state of LED 19| MOV R3, #1 // if R3 0, then 1
20| MOV R0, #.green9|mainProgram: 21| B drawpixel 10| B mainProgram //Here, just an empty loop! 22|off:
23| MOV R3, #0 // if R3 1, then 0 24| MOV R0, #.white
25|drawpixel:
26| STR R0, .Pixel367 // draw the pixel
27| POP {R0}28| RFE
Flashing LED with INT: Full Program
1|// Set up Interrupt handling 12|/// interrupt handler: toggleLED
2| MOV R0,#toggleLED 13|// toggles state of LED (on "green" or off "white")
3| STR R0,.ClockISR 14|//
4| MOV R0,#1000 15|toggleLED:
5| STR R0,.ClockInterruptFrequency 16| PUSH {R0}
6| MOV R0,#1 17| CMP R3, #0 // check state
7| STR R0,.InterruptRegister //Enable all interrupts 18| BNE off
8| MOV R3, #1 // R3 keeps track of state of LED 19| MOV R3, #1 // if R3 0, then 1
20| MOV R0, #.green9|mainProgram: 21| B drawpixel 10| B mainProgram //Here, just an empty loop! 22|off:
23| MOV R3, #0 // if R3 1, then 0 This is just an infinite loop because we are only 24| MOV R0, #.white
flashing LED and there is nothing else to do but wait 25|drawpixel:
for clock interrupts, however this could potentially be 26| STR R0, .Pixel367 // draw the pixel
doing other useful things 27| POP {R0}28| RFE
Flashing LED with INT: Full Program
- Lets look in ARMlite