S05a: Stacks / Interrupts - PowerPoint PPT Presentation

s05a stacks interrupts n.
Skip this Video
Loading SlideShow in 5 Seconds..
S05a: Stacks / Interrupts PowerPoint Presentation
Download Presentation
S05a: Stacks / Interrupts

play fullscreen
1 / 87
S05a: Stacks / Interrupts
Download Presentation
Download Presentation

S05a: Stacks / Interrupts

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 1996 1998 1982 1995 S05a: Stacks / Interrupts Required: PM: Ch 7.4, pgs 95-96Wiki: Stack_(data_structure)Recommended: Google "Activation Record" (Read 1 article)

  2. CS 224 Stacks / Interrupts

  3. Learning Outcomes… • The Stack • Subroutines • Subroutine Linkage • Saving Registers • Stack Operations • Activation Records • Recursive Subroutines • Interrupt Stack Usage Stacks / Interrupts

  4. Terms… • Activation Record– parameters activated on the stack by a subroutine call • Callee-Safe– subroutine saves registers used. • Caller-Safe– caller saves registers needing to be saved. • Interrupt– asynchronous subroutine call. • LIFO– Last In First Out, a stack. • Loosely Coupled– all parameters passed as arguments. • Pop– removing top element of stack. • Push– putting an element on a stack • Stack– first in, first out abstract storage data structure. • Stack Pointer– address of stack. • Stong Cohesion– subroutine performs one specific task. • Subroutine– synchronous task or unit. Stacks / Interrupts

  5. Levels of Transformation Problems Algorithms Language (Program) Programmable Computer Specific Machine (ISA) Architecture Microarchitecture Manufacturer Specific Circuits Devices Stacks / Interrupts

  6. The Stack Stacks • Stacks are the fundamental data structure of computers today. • A stack is a Last In, First Out (LIFO) abstract data structure. • A true stack is a restricted data structure with two fundamental operations, namely push and pop. • Elements are removed from a stack in the reverse order of their addition. • Memory stacks are used for random access of local variables. Stacks / Interrupts

  7. The Stack MSP430 Stack • Hardware support for stack • Register R1 – Stack Pointer (SP) • Initialized to highest address of available RAM • MSP430G2553  0x0400 (512 bytes) • MSP430F2274  0x0600 (1k bytes) • Stack grows down towards lower memory addresses. • Initialize stack pointer at beginning of program STACK .equ0x0400 ; top of stack start: mov.w #STACK,SP ; initstack pointer Stacks / Interrupts

  8. The Stack MSP430 Stack • The MSP430 stack is a word structure • Elements of the stack are 16-bit words. • The LSB of the Stack Pointer (SP) is always 0. • The SP points to the last word added to the stack (TOS). • The stack pointer is used by • PUSH – push a value on the stack • POP – pop a value off the stack • CALL – push a return address on the stack • RET – pop a return address off the stack • RETI – pop a return address and status register off the stack • Interrupts – push a return address and status register on the stack Stacks / Interrupts

  9. 1996 1998 1982 1995 Down Up Up Down The Stack Computer Memory – Up or Down? x0000 xFFFF Stack TOS Unlike a coin stack, a memory stack DOES NOT move the Data in memory, just the pointer to the top of stack. xFFFF x0000 Stacks / Interrupts

  10. Quiz 5.1 List the values found in the stack and the value of the stack pointer after each instruction. Instructions: Push #0x0018 Push #0x0025 Push #0x0058 Pop R15 Push #0036 R1 x0280 TOP 0x0282 0x0280 0x027E 0x027C 0x027A 0x0278 Stacks / Interrupts

  11. Subroutines Subroutines • A subroutine is a program fragment that performs some useful function. • Subroutines help to organize a program. • Subroutines should have strong cohesion – perform only one specific task. • Subroutines should be loosely coupled – interfaced only through parameters (where possible) and be independent of the remaining code. • Subroutines keep the program smaller • Smaller programs are easier to maintain. • Reduces development costs while increasing reliability. • Fewer bugs – copying code repeats bugs. • Subroutines are often collected into libraries. Stacks / Interrupts

  12. Subroutines The Call / Return Mechanism Smaller programs. Easier to maintain. Reduces development costs. Increased reliability. Fewer bugs do to copying code. More library friendly. Faster programs. Less overhead. Stacks / Interrupts

  13. Subroutine Linkage Subroutine Linkage • A subroutine is “called” in assembly using the MSP430 CALL instruction. • The address of the next instruction after the subroutine call is saved by the processor on the stack. • Parameters are passed to the subroutine in registers and/or on the stack. • Local variables are created on the stack at the beginning of the subroutine and popped from the stack just before returning from the subroutine. • At the end of a subroutine, a RET instruction “pops” the top value from the stack into the program counter. Stacks / Interrupts

  14. Stack Operations Single operand instructions: Emulated instructions: Subroutine Linkage Stacks / Interrupts

  15. Quiz 5.2 What is the value of the stack pointer after the second call to delay? Is there a problem with the program? start: mov.w #0x0400,SP mov.w #WDTPW+WDTHOLD,&WDTCTL bis.b #0x01,&P1DIR ; P1.0 as output mainloop: bis.b #0x01,&P1OUT ; turn on LED push #1000 call #delay bic.b #0x01,&P1OUT ; turn off led call #delay jmpmainloop delay:mov.w2(sp),r15 ; get delay counter delaylp2: sub.w#1,r15 ; delay over? jnz delaylp2 ; n ret ; y .sect ".reset" ; reset Vector .word start ; start address .end R1→ Stacks / Interrupts

  16. Saving Registers Saving and Restoring Registers • Called routine -- “callee-save” • At beginning of subroutine, save all registers that will be altered (unless a register is used to return a value to the calling program or is a scratch register!) • Before returning, restore saved registers in reverse order. • Or, avoid using registers altogether. • Calling routine -- “caller-save” • If registers need to be preserved across subroutine calls, the calling program would save those registers before calling routine and restore upon returning from routine. • Obviously, avoiding the use of registers altogether would be considered caller-safe. • Values are saved by storing them in memory, preferably on the stack. Stacks / Interrupts

  17. Save Registers Save Registers call subroutine call subroutine subroutine subroutine Restore Registers Restore Registers Saving Registers Caller-Save vs. Callee-Save Stacks / Interrupts

  18. Quiz 5.3 What is wrong (if anything) with the following code? How many times will delay execute for each loop? How long will myDelay delay? Is myDelaycallee-save? loop: xor.b #0x01,&P4OUT ; toggle LED call #myDelay ; delay some jmp loop ; repeat myDelay: mov.w #0,r15 call #delay call #delay delay: sub.w #1,r15 jne delay ret Stacks / Interrupts

  19. Stack Operations Single operand instructions: Emulated instructions: Stack Operations Stacks / Interrupts

  20. Stack Operations 0xf826 0xf826 0xf826 0xf826 0xf826 0xf826 0xf826 r15 r15 r15 r15 r15 r15 r14 r14 r14 r14 r14 SP SP SP SP SP SP SP SP Subroutine Linkage 0xf820: ... 0xf822: call #subroutine 0xf826: ... subroutine: 0xf852: push r15 0xf854: push r14 ... 0xf882: pop r14 0xf884: pop r15 0xf886: ret Unprotected! Stacks / Interrupts

  21. Activation Records Activation Records • A subroutine is activated when called and an activation record is allocated (pushed) on the stack. • An activation record is a template of the relative positions of local variables on the stack as defined by the subroutine. • Return address • Memory for local subroutine variables • Parameters passed to subroutine from caller • Saved registers used in subroutine (callee-save) • A new activation record is created on the stack for each invocation of a subroutine or function. • A frame pointer indicates the start of the activation record. • When the subroutine ends and returns control to the caller, the activation record is discarded (popped). Stacks / Interrupts

  22. Activation Record Example Activation Records .cdeclsC,"msp430.h" ; MSP430 DELAY .equ (50/8) .text ; beginning of code reset: mov.w #0x0400,SP ; init stack pointer mov.w #WDTPW+WDTHOLD,&WDTCTL ; stop WDT bis.b #0x01,&P1DIR ; set P1.0 as output mainloop: xor.b #0x01,&P1OUT ; toggle P1.0 push.w #DELAY ; pass delay count on stack call #delay ; call delay subroutine jmpmainloop ; delay subroutine: stack usage 4| DELAY | \ ; 2| ret | subroutine frame (6 bytes) ; (SP) => 0| r15 | / delay: push.w r15 ; callee-save mov.w #0,r15 ; use R15 as inner counter delay02: sub.w #1,r15 ; inner delay over? jne delay02 ; n sub.w #1,4(SP) ; y, outer done? jne delay02 ; n pop.w r15 ; y, restore register(s) mov.w @SP+,0(SP) ; pop input delay count ret ; return from subroutine .sect ".reset" ; MSP430 reset Vector .word reset ; start address .end Delay Activation Record: 4(SP) = delay count 2(SP) = return address 0(SP) = r15 Stack: 2(SP) = delay count 0(SP) = return address Stack: 0(SP) = return address Stack: (emply) Stacks / Interrupts

  23. Quiz 5.4 Change the following code to use a callee-save, loosely coupled, cohesive subroutine. .cdeclsC,"msp430.h" .text start:mov.w #0x0400,SP mov.w #WDTPW+WDTHOLD,&WDTCTL bis.b #0x01,&P1DIR ; P1.0 as output mainloop: bis.b #0x01,&P1OUT ; turn on LED mov.w #10000,r15 ; delay counter delaylp1: sub.w#1,r15 ; delay over? jnz delaylp1 ; n bic.b #0x01,&P1OUT ; turn off led mov.w #0,r15 ; delay counter delaylp2: sub.w#1,r15 ; delay over? jnz delaylp2 ; n mov.w #0,r15 ; delay counter delaylp3: sub.w#1,r15 ; delay over? jnz delaylp3 ; n jmpmainloop ; y, toggle led .sect ".reset" ; reset vector .word start ; start address .end Stacks / Interrupts

  24. Recursive Subroutine A subroutine that makes a call to itself is said to be a recursive subroutine. Recursion allows direct implementation of functions defined by mathematical induction and recursive divide and conquer algorithms Factorial, Fibonacci, summation, data analysis Tree traversal, binary search Recursion solves a big problem by solving one or more smaller problems, and using the solutions of the smaller problems, to solve the bigger problem. Reduces duplication of code. MUST USE STACK! Recursive Subroutines Stacks / Interrupts

  25. S05b: Interrupts Required: PM: Ch 9.1-7, pgs 129-139 PM: Ch 10.1-2, pgs 151-156 PM: Ch 10.4-5, pgs 159-166 PM: Ch 10.7-8, pgs 169-188 PM: Ch 11.5, pgs 227-229Recommended: Intro to PWMFUG: Watchdog Timer+

  26. Interrupts Interrupts • Execution of a program normally proceeds predictably, with interrupts being the exception. • An interrupt is an asynchronous signal indicating something needs attention. • Some event has occurred • Some event has completed • The processing of an interrupt subroutine uses the stack. • Processor stops with it is doing, • stores enough information on the stack to later resume, • executes an interrupt service routine (ISR), • restores saved information from stack (RETI), • and then resumes execution at the point where the processor was executing before the interrupt. Stacks / Interrupts

  27. Interrupt Service Routine Interrupt Service Routines Interrupt Main Routine (synchronous) Main Routine (synchronous) Main Routine (synchronous) Main Routine (synchronous) Interrupt Service Routine (asynchronous) Interrupt Service Routine (asynchronous) Interrupt Service Routine (asynchronous) Stacks / Interrupts

  28. Interrupts Interrupt Flags • Each interrupt has a flag that is raised (set) when the interrupt is pending. • Each interrupt flag has a corresponding enable bit – setting this bit allows a hardware module to request an interrupt. • Most interrupts are maskable, which means they can only interrupt if 1) Individually enabled and 2) general interrupt enable (GIE) bit is set in the status register (SR). • Reset and Non-Maskable Interrupts (NMI) are reserved for system interrupts such as power-up (PUC), external reset, oscillator fault, illegal flash access, watchdog, and illegal instruction fetch. Device Interrupt Interrupt Enable Interrupt MPU General Interrupt Enable (GIE) Reset / Non-Maskable (NMI) Stacks / Interrupts

  29. Interrupt Vector Table 0xFFC0 0xFFBF Program Code 0xC000 0xBFFF 0x0400 0x03FF Stack 0x0200 0x01FF Input/Output Interrupts Interrupt Vectors 0xFFFF • The CPU must know where to fetch the next instruction following an interrupt. • The address of an ISR is defined in an interrupt vector. • The MSP430 uses vectored interrupts where each ISR has its own vector stored in a vector table located at the end of program memory. • Note: The vector table is at a fixed location (defined by the processor data sheet), but the ISRs can be located anywhere in memory. 16 vectors (ISR Addresses) 0x0000 Stacks / Interrupts

  30. Interrupts MSP430 Interrupt Vectors Non-Maskable Interrupts Timers Ports Stacks / Interrupts

  31. Interrupts Processing an Interrupt… • Processor completes execution of current instruction. • Master Clock (MCLK) started (if CPU was off). • Processor pushes Program Counter (PC) on stack. • Processor pushes Status Register (SR) on stack. • Interrupt w/highest priority is selected. • Interrupt request flag cleared (if single sourced). • Status Register is cleared: • Disables further maskable interrupts (GIE cleared) • Terminates low-power mode • Processor fetches interrupt vector and stores it in the program counter. • User ISR must do the rest! Stacks / Interrupts

  32. Prior to Interrupt Item 1 SP SP SP Item 2 • Interrupt (hardware) • Program Counter pushed on stack • Status Register pushed on stack • Interrupt vector moved to PC • Further interrupts disabled • Interrupt flag cleared Item 1 Item 1 Item 2 Item 2 PC PC SR SR • Return from Interrupt (reti) • Status Register popped from stack • Program Counter popped from stack Interrupts Interrupt Stack add.w r4,r7 jnc $+4 add.w #1,r6 add.w r5,r6 add.w r4,r7 jnc $+4 add.w #1,r6 add.w r5,r6 PC PC PC Execute Interrupt Service Routine (ISR) xor.b #1,&P1OUT reti Stacks / Interrupts

  33. Return From Interrupt Single operand instructions: Emulated instructions: Interrupt Service Routine Stacks / Interrupts

  34. Interrupt Service Routine Interrupt Latency • The time between the interrupt request and the start of the ISR is called latency • MSP430 requires 6 clock cycles before the ISR begins executing • An ISR may be interrupted if interrupts are enabled in the ISR • Well-written ISRs: • Should be short and fast – get in and get out • Require a balance between doing very little – thereby leaving the background code with lots of processing – and doing a lot and leaving the background code with nothing to do • Applications that use interrupts should: • Disable interrupts as little as possible • Respond to interrupts as quickly as possible • Communicate w/ISR only through global variables (never through registers!!!) Stacks / Interrupts

  35. Watchdog

  36. Watchdog Watchdog Timer • The MSP430 watchdog can be configured as a COP (computer operating properly) device or as a timer. • The primary function of the watchdog timer (WDT+) module is to perform a controlled system restart after a software problem occurs. • After a power-up cycle (PUC), the WDT+ module is automatically configured in watchdog. • The user must setup or halt the WDT+ prior to the expiration of the initial reset interval, else an unmasked system reset is generated. • If the watchdog function is not needed in an application, the module can be configured as an interval timer and can generate interrupts at selected time intervals. Stacks / Interrupts

  37. Watchdog How To Watchdog Timer • Configure the MSP430 watchdog as a timer using WDTCTL register: • WDT_MDLY_32 32 msinterval (default) • WDT_MDLY_8 8 ms • WDT_MDLY_0_5 0.5 ms • WDT_MDLY_0_064 0.064 ms • Write a watchdog ISR (callee-safe + reti) • Create a watchdog interrupt vector. • .sect ".int10" ; Watchdog Vector • .word WDT_ISR ; Watchdog ISR • Enable watchdog to interrupt. • bis.b#WDTIE,&IE1 ; enable WDT interrupt • bis.w#GIE,SR ; enable interrupts Stacks / Interrupts

  38. Watchdog Example 5.1 – Watchdog ISR .cdecls C,"msp430.h" SMCLK .equ1200000 ; 1.2 Mhz clock WDT_CPS .equSMCLK/8000 ; WD clocks / second count ; Data Section ------------------------------------------------------------ .bss WDTSecCnt,2 ; WDT second counter ; Code Section ------------------------------------------------------------ .text start: mov.w #0x400,SP ; initialize stack pointer mov.w #WDT_MDLY_8,&WDTCTL ; set WD timer interval mov.w #WDT_CPS,&WDTSecCnt ; initialize 1 sec WD counter bis.b#0x01,&P1DIR ; P1.0 output bis.b #WDTIE,&IE1 ; enable WDT interrupt bis.w#GIE,SR ; enable interrupts loop: ;<< program >> jmp loop ; loop indefinitely ; Watchdog ISR ------------------------------------------------------------ WDT_ISR: sub.w #1,&WDTSecCnt ; decrement counter, 0? jne WDT_02 ; n mov.w #WDT_CPS,&WDTSecCnt ; y, re-initialize counter xor.b #0x01,&P1OUT ; toggle P1.0 WDT_02: reti ; return from interrupt ; Interrupt Vectors ------------------------------------------------------- .sect ".int10" ; Watchdog Vector .word WDT_ISR ; Watchdog ISR .sect ".reset" ; PUC Vector .word start ; RESET ISR .end 1. Configure Watchdog as a timer 2. Write a Watchdog Interrupt Service Routine 3. Create aWDTInterrupt Vector 4. Enable Watchdog to interrupt Stacks / Interrupts

  39. Quiz 5.5 • What conditions must be met before a device can interrupt the computer? • What is saved on the stack when an interrupt occurs? • Where are interrupt vectors located in memory? ISRs? • (T or F) Interrupts are predictable asynchronous events. Stacks / Interrupts

  40. Traffic Light Example .bssi,2 main: mov.w #WDT_MDLY_8,&WDTCL mov.b #0x41,&P1DIR loop:bis.b #GREEN,&P1OUT bic.b #RED,&P1OUT mov.w #SEC5,r15 call #delay mov.w #0,&i loop2:cmp.w #6,&i jge next1 xor.b #GREEN,&P1OUT mov.w #SEC1,r15 call #delay add.w #1,&i jmp loop2 next1:mov.w #0,&i loop4:cmp.w #20,&i jge next2 xor.b #GREEN,&P1OUT mov.w #SEC02,r15 call #delay add.w #1,&i jmp loop4 next2:bic.b #GREEN,&P1OUT bis.b #RED,&P1OUT mov.w #SEC10,r15 call #delay jmp loop Stacks / Interrupts

  41. Energy Consumption

  42. Energy Consumption U.S. Energy Consumption • The United States is the 2nd largest energy consumer in terms of total use in 20101in the world. • Not getting better – everyone must do their part. • How could I lower home energy consumption? • Turn off lights • Turn down thermostat • Unplug appliances not in use • Use slower motor speeds • Heat/cool at a slower rate • Improve insulation • Purchase energy efficient appliances • Eliminate unnecessary living space • How could I make computers more energy efficient? 1. Barr, Robert. "China surpasses US as top energy consumer". Associated Press. Retrieved 16 June 2012. Stacks / Interrupts

  43. Energy Consumption Computer Energy Consumption • Computers occupy a small but growing percentage of annual U.S. electricity consumption. • Estimates vary for 3% to 13% of entire U.S. supply. • Hidden costs – every 100 watts consumed by computers requires 50 watts of cooling. • Computers are ubiquitous – found in every energy sector. • Battery capacity doubles every 10 years while processing power doubles every 2 years. • Server farms, supercomputers, web hosting, scientific simulations, 3D rendering, search engines,… • Google uses enough energy to continuously power 200,000 homes (260 million watts – ¼ output of a nuclear power plant). • One Google search is equal to turning on a 60W light bulb for 17 seconds. Stacks / Interrupts • http://techland.time.com/2011/09/09/6-things-youd-never-guess-about-googles-energy-use/

  44. Clocks

  45. Processor Clocks Processor Clock Speeds • Often, the most important factor for reducing power consumption is slowing the clock down. • Faster clock = Higher performance, more power required • Slower clock = Lower performance, less power required ; Set ACLK to 12kHz mov.w#LFXT1S_2,&BCSCTL3 ; Set DCO to 8 MHz: mov.b #CALBC1_8MHZ,&BCSCTL1 mov.b #CALDCO_8MHZ,&DCOCTL 10% loss of power 60% loss of power Stacks / Interrupts

  46. Interrupts Example 5.2 – Clock Speed .cdecls C,"msp430.h" SMCLK .equ1200000 ; 1.2 Mhz clock WDT_CPS .equSMCLK/8000 ; WDT clocks / second count MHz8 .set 1 ; 8 MHz flag ; Code Section ------------------------------------------------------------ .text start: mov.w #0x0400,SP ; init stack pointer mov.w#WDT_MDLY_8,&WDTCTL ; stop WDT .if MHz8 ; set DCO to 8 MHz mov.b #CALBC1_8MHZ,&BCSCTL1 ; set range mov.b #CALDCO_8MHZ,&DCOCTL ; set DCO step + modulation .endif bis.b #0x01,&P1DIR ; set P1.0 as output mainloop: xor.b #0x01,&P1OUT ; toggle P1.0 mov.w #8,r14 ; use R14 as outer loop counter delaylp1: mov.w #0,r15 ; use R15 as delay counter delaylp2: sub.w #1,r15 ; delay over? jne delaylp2 ; n sub.w #1,r14 ; y, outer loop done? jne delaylp1 ; n jmpmainloop ; y, toggle led ; Interrupt Vectors ------------------------------------------------------- .sect ".reset" ; MSP430 RESET Vector .word start ; start address .end Conditionally assemble setting new DCO constants (for 8 MHz) Nested delay loop to blink LED Stacks / Interrupts

  47. Low-Power Mode

  48. Low Power Modes MSP430 Clock Modes • Another method to reduce power consumption is to turn off some (or all) of the system clocks. • A device is said to be sleeping when in low-power mode; waking refers to returning to active mode. “Only Mostly Dead” (7 Years vs 1 Day) Average 300x Savings (1 Year vs 1 Day) Stacks / Interrupts

  49. Low Power Modes MSP430 Clock Settings (r2) SMCLK and ACLK Active Only ACLK Active No Clocks! Sleep Modes ; enable interrupts / enter low-power mode 0 bis.w#LPM0|GIE,SR ; LPM0 w/interrupts Can be combined Stacks / Interrupts

  50. Low-Power Mode Example 5.3 – Low Power .cdecls C,"msp430.h" SMCLK .equ 1200000 ; 1.2 Mhz clock WDT_CPS .equ SMCLK/8000 ; WDT clocks / second count STACK .equ 0x0400 ; top of stack ; Data Section ------------------------------------------------------------ .bss WDTSecCnt,2 ; WDT second counter ; Code Section ------------------------------------------------------------ .text start: mov.w #STACK,SP ; initialize stack pointer mov.w#WDT_MDLY_8,&WDTCTL ; set WD timer interval mov.w #WDT_CPS,&WDTSecCnt ; initialize 1 sec WD counter bis.b #WDTIE,&IE1 ; enable WDT interrupt bis.b #0x01,&P1DIR ; P1.0 output loop: bis.w #LPM0|GIE,SR ; sleep/enable interrupts xor.b #0x01,&P1OUT ; toggle P1.0 jmp loop ; loop indefinitely ; Watchdog ISR ------------------------------------------------------------ WDT_ISR: sub.w #1,&WDTSecCnt ; decrement counter, 0? jne WDT_02 ; n mov.w #WDT_CPS,&WDTSecCnt ; y, re-initialize counter bic.b #LPM0,0(SP) ; wakeup processor WDT_02: reti ; return from interrupt ; Interrupt Vectors ------------------------------------------------------- .sect ".int10" ; Watchdog Vector .word WDT_ISR ; Watchdog ISR .sect ".reset" ; PUC Vector .word start ; RESET ISR .end • Enable interrupts / Goto Sleep • (Low-power Mode 0) • 2. Blink LED when awakened Configure Watchdog as a timer and enable device to interrupt Activity Profile 300µA active sleep 1. Reset counter every second 2. Set Active Mode in saved SR 3. Wakeup processor on RETI average 1µA Stacks / Interrupts