EECE 276 – Embedded SystemsInterrupts 1EECE 276Embedded SystemsInterrupts: BasicsShared data problemLatencyEECE 276 – Embedded SystemsInterrupts 2Recap: IT TechniqueInterrupt-driven I/O: CPU I/O IRQ* MEM IT Vector TableISROn IT Request: Finish instructionStack machine stateDetermine IT sourceCall ISR In SW (ISR) Acknowledge ITFinish I/ORTI In HW:Return to interrupted codeEECE 276 – Embedded SystemsInterrupts 3IT Technique: Key points IT is always generated by a special event • HW (mostly), SW (sometime) If enabled, an IT can be serviced between any two instructions Instructions are always fully executedNOTE: C statements often mean multiple instructionsvoid foo(int i) {int j;j = i + 123;…}LDAA $8,XADDA #123STAA $6,XISR:……RTIITEECE 276 – Embedded SystemsInterrupts 4IT Technique: Key points All registers must be saved/restored at the beginning/end of the ISR Saving/restoring the context (a.k.a. context switch) Locating the ISR: IT vector table Multiple, simultaneous IT-s: priority scheme HC11/12: fixed scheme on internals, single external source Others: IT priority controller (extra HW) Interrupting the ISR: IT nesting HC11/12: Upon entry to an ISR, IT-s are disabled Others: Higher priority IT-s are enabledEECE 276 – Embedded SystemsInterrupts 5Shared Data Problemstatic int iTemps[2];void interrupt vReadTemps() {iTemps[0] = // read in value from HW;iTemps[1] = // read in value from HW:}void main(void) {int iTemp0, iTemp1;while (TRUE) {iTemp0 = iTemps[0];iTemp1 = iTemps[1];if (iTemp0 != iTemp1) {// Set off ALARM}}}O Code monitors two temperaturesO Temperatures must be equalO If not -> ALARMO vReadTemps() is the ISR, called periodicallyBehavior:The code occasionally sets off the ALARM, even if everything seems normal. ???EECE 276 – Embedded SystemsInterrupts 6Shared Data ProblemSequence of events:1. main: iTemp0 = iTemps[0];2. ISR: updates iTemps[0] and iTemps[1]3. main: iTemp1 = iTemps[1];4. main: iTemp0 != iTemp1 Æ ALARM!Alternative main():Does it fix the problem? NO!IT always comes at thewrong time.….void main(void) {while (TRUE) {if (iTemps[0] != iTemps[1]) {// Set off ALARM}}EECE 276 – Embedded SystemsInterrupts 7Shared Data ProblemSource of the problem:iTemps[] array is shared between the main() and the ISR. If IT happens while main() is using the array -> the data may be in an inconsistent state. Solving the problem:Enable/disable ITsAtomic/critical section…void main(void) {int iTemp0, iTemp1;while (TRUE) {disableIT();iTemp0 = iTemps[0];iTemp1 = iTemps[1];enableIT();if (iTemp0 != iTemp1) {// Set off ALARM}…EECE 276 – Embedded SystemsInterrupts 8Interrupt latencyHow fast will a system react to interrupts? Depends on:1. Max. time while IT-s are disabled.2. Max. time taken to execute higher priority IT-s. 3. Time taken by ISR invocation (context save, etc.) and return (context restore)4. “Work” time in ISR to generate a response.Values: For 3: see processor docs.Others: count instructions – does not work well for processors with cache! General rule: WRITE SHORT IT SERVICE ROUTINES!EECE 276 – Embedded SystemsInterrupts 9Interrupt latency: Disabling InterruptsExample system:O Must disable IT-s for 125uS to process pressure variables.O Must disable IT-s for 250uS to manage timerO Must respond to a network IT within 600uS, the network ISR takes 300uS to execute. Will it work?EECE 276 – Embedded SystemsInterrupts 10Interrupt latency: Disabling InterruptsWill it work? Yes, as the longest time IT-s are disabled is 250uS and 300uS is required for the network ISR. WCRT = 550uS < 600 uS. Will it work with a processor of half the speed?No! WCRT = 500uS + 600uS = 1100uS > 600uSEECE 276 – Embedded SystemsInterrupts 11Alternative to disabling IT-sint iTempAs[2];int iTempBs[2];bool fUsingB = FALSE;void interrupt vReadTemps() {if(fUsingB) {iTempAs[0] = // read from HWiTempAs[1] = // read from HW} else {iTempBs[0] = // read from HWiTempBs[1] = // read from HW}}Two sets of variablesOne flag to control which set is usedEECE 276 – Embedded SystemsInterrupts 12Alternative to disabling IT-svoid main () {while (TRUE) {if(fUsingB) {if (iTempBs[0] != iTempBs[1]) {// set off ALARM}} else {if (iTempAs[0] =! iTempAs[1]) {// set off ALARM}}fUsingB = !fUsingB;}}Assumption:Changing the flag is an atomic
View Full Document