Professional Documents
Culture Documents
Chapter 4: Interrupts
1. 2. 3. 4.
Microprocessor Architecture
Most microprocessors and their assembly languages are generally similar to one another. An assembly language is a more easily readable form of the instructions that a microprocessor actually works with. The assembly language is translated into binary numbers by a program called an assembler before execution. When translating C, most of the statements are converted into multiple assembly instructions by the compiler. Each microprocessor family will have a different assembly language (e.g. the Intel 8085, 8051 etc.) however the individual microprocessors in the same family generally have the same assembly language. Microprocessors typically have a set of (general-purpose) registers that can store data values.
3
is read move the address of Temperature to register R5 A variable in parentheses refers to the value of the variable.
MOVE R5, (Temperature)
will place the value of Temperature in register R5. Anything following a semicolon is a comment.
The instruction adds contents of R3 to R2, inverts the result in R2, jumps unconditionally to the NO_ADD label (the instruction ADD R2, R5 never gets executed) and stores the result in R7.
6
Here jump is executed if result of the subtraction is zero. PUSH and POP: These are stack instructions. PUSH adjusts the stack pointer and adds data to the stack while POP retrieves the data and adjusts the pointer. CALL: Used to execute functions and subroutines. Followed by a RETURN instruction for getting back.
CALL SUBTRACT_THESE MOVE R7,R2 . . SUBTRACT_THESE: SUB R2,R3 RETURN
Interrupt Basics
Interrupts are triggered when certain events occur in the hardware. e.g. when a serial chip has sent data to a microprocessor and wants it to read it from its pin, it sends an interrupt to the processor, usually by sending a signal to one of the processors IRQ (interrupt request) pins. On receiving an interrupt, the microprocessor stops its current execution, saves the address of the next instruction on the stack and jumps to an interrupt service routine (ISR) or interrupt handler.
8
Interrupt Example
The following shows an e.g. of an ISR
Task Code ... MOVE R1, R7 MUL R1, 5 ADD R1, R2 DIV R1, 2 JCOND ZERO, END SUBTRACT R1, R3 ... ... END: MOVE R7, R1 ... ... ISR
PUSH R1 PUSH R2 ... ;ISR code comes here ... POP R2 POP R1 RETURN
10
Disabling Interrupts
Most microprocessors allow programs to disable interrupts. In most cases the program can select which interrupts to disable during critical operations and which to keep enabled by writing corresponding values into a special register. Nonmaskable interrupts however cannot be disabled and are normally used to indicate power failures or other serious event. Certain processors assign priorities to interrupts, allowing programs to specify a threshold priority so that only interrupts having higher priorities than the threshold are enabled and the ones below it are disabled.
12
13
15
18
Long lSecondsSinceMidnight (void) { long lReturn; /* When we read the same value twice, it must be good.*/ lReturn = lSecondsToday; while(lReturn != lSecondsToday) lReturn = lSecondsToday; return(lReturn); }
Void interrupt vUpdateTime (void) { ++lSecondsToday; if(lSecondsToday == 60L * 60L * 24L) lSecondsToday = 0L; }
19
10
Interrupt Latency
Interrupt latency is the amount of time taken to respond to an interrupt. This depends on several factors:1. 2. 3.
4.
Longest period during which the interrupt is disabled Time taken to execute ISRs of higher priority interrupts Time taken for the microprocessor to stop the current execution, do the necessary bookkeeping and start executing the ISR Time taken for the ISR to save context and start executing instructions that count as a response
The third factor is measured by knowing the instruction execution times from the processor manual, when instructions are not cached.
21
11
23
Disabling Interrupts
Alternatives to Disabling Interrupts As disabling interrupts increases latency, some alternatives might be required in certain situations. Code on the following slide uses two array sets iTemperaturesA and iTemperaturesB and a variable (fTaskCodeUsingTempB) to ensure that the ISR always works with the array not being used by the task code.
24
12
{ if(fTaskCodeUsingTempsB) if(iTemperaturesB[0] != iTemperaturesB[1]) !!Set off howling alarm; else if(iTemperaturesA[0] != iTemperaturesA[1]) !!Set off howling alarm;
fTaskCodeUsingTempsB = !fTaskCodeUsingTempsB; }
13
Void interrupt vReadTemperatures (void) { /*If the queue is not full..*/ if (!((iHead+2==iTail) || (iHead==QUEUE_SIZE-2 && iTail==0))) { iTemperatureQueue[iHead] = !!read one temperature; iTemperatureQueue[iHead + 1] = !!read other temperature; iHead += 2; if(iHead == QUEUE_SIZE) iHead = 0; } else !!throw away next value }
27
while(TRUE) { /*If there is any data..*/ if(iTail != iHead) { iTemperature1= iTemperatureQueue[iTail]; iTemperature2= iTemperatureQueue[iTail + 1]; iTail += 2; if(iTail == QUEUE_Size) iTail = 0; !! Do something with iValue; } } }
28
14