embedded systems interrupts c.-z. yang czyang sept.-dec. 2001
Post on 22-Dec-2015
218 views
TRANSCRIPT
[email protected] Embedded Systems - Interrupts 2
元智大學資訊工程系
Context Awareness
• Since embedded systems are closely relevant to the contexts, how can these external events be noticed?
[email protected] Embedded Systems - Interrupts 3
元智大學資訊工程系
I/O Interrupts
• The most common approach is to use interrupts.– Interrupts cause the microprocessor in the embedded
system
– to suspend doing whatever it is doing and
– to execute some different code instead.
• Interrupts can solve the response problem, but not without some difficult programming, and without introducing some new problems of their own.
[email protected] Embedded Systems - Interrupts 4
元智大學資訊工程系
I/O Interrupts
• The flow– Step 1: Interrupts start with a
signal from the hardware.
– Step 2: The signal notifies the CPU to react the event.
[email protected] Embedded Systems - Interrupts 5
元智大學資訊工程系
Interrupt Service Routines
• The main job is to react the interrupts.
• Typically, interrupt service routines also must do some miscellaneous housekeeping chores,– Reset the interrupt-detecting hardware
– Enable processing interrupts of lower priorities
[email protected] Embedded Systems - Interrupts 6
元智大學資訊工程系
ISR vs. Procedure Calls
• To execute ISRs, there is NO CALL instruction.– The microprocessor does the call automatically.
• An array, interrupt vector, of addresses is used to point to appropriate ISRs.– ISRs must be loaded when the computer is turned on.
• Interrupts can be masked.
• The context need to be saved.
[email protected] Embedded Systems - Interrupts 7
元智大學資訊工程系
Two Basic Models
• Synchronous– Returning the control of CPU
to the user process must be after the completion of the ISR.
• Asynchronous– The control is returned
without waiting for the I/O to complete.
[email protected] Embedded Systems - Interrupts 8
元智大學資訊工程系
Some Common Questions
• How does the microprocessor know where to find the interrupt routine when the interrupt occurs?
• How do microprocessors that use an interrupt vector table know where the table is?
• Can a microprocessor be interrupted in middle of an instruction?
[email protected] Embedded Systems - Interrupts 9
元智大學資訊工程系
Some Common Questions
• If two interrupts happen at the same time, which interrupt routine does the microprocessor do first?
• Can an interrupt request signal interrupt anther interrupt routine?
• What happens if an interrupt is signaled while the interrupts are disabled?
[email protected] Embedded Systems - Interrupts 10
元智大學資訊工程系
Some Common Questions
• What happened if I disable interrupts and then forget to re-enable them?
• Can I write my interrupt routines in C?
[email protected] Embedded Systems - Interrupts 12
元智大學資訊工程系
A Powerful ISR Design
• Can the ISR do everything you want?– Yes, but this is very impractical.
main(){int i; // setting up ISRs // do nothing but an empty loop while(1) i=i+0;}
SampleISR()
{
newVal:= ReadADConverter()
call StartNewConversion()
…
…
…
}
SampleISR()
{
newVal:= ReadADConverter()
call StartNewConversion()
…
…
…
}
[email protected] Embedded Systems - Interrupts 13
元智大學資訊工程系
A Practical Approach
• The ISR should do the necessary tasks– moving data from I/O devices to the memory buffer or
vice versa
– handling emergent signals
– signaling the task subroutine code or the kernel
• The ISR needs to notify some other procedure to do follow-up processing.
Task Code ISRISR
Shared Variables
[email protected] Embedded Systems - Interrupts 14
元智大學資訊工程系
Accessing the Shared Data
• However, this may cause the shared data problem.
ISR
Task Code
Interrupt
[email protected] Embedded Systems - Interrupts 15
元智大學資訊工程系
The Shared Data Problem
• The unexpected interrupt – may cause two readings to be different,
– even though the two measured temperatures were always the same.
• What can we improve this?– Testing these temperature measurements directly!
[email protected] Embedded Systems - Interrupts 16
元智大學資訊工程系
Directly Testing
• The new code
ISR
Task Code
Testing
[email protected] Embedded Systems - Interrupts 17
元智大學資訊工程系
A Bug Harder to Be Detected
• The compiler translation
Interruptible
[email protected] Embedded Systems - Interrupts 18
元智大學資訊工程系
Characteristics of the Shared-Data Bug
• A bug that is very hard to find.– It does not happen every time the code runs.
– The program may run correctly in most time.
– However, a false alarm may be set off sometimes.
• How can you trace back if the embedded system has already exploded?
[email protected] Embedded Systems - Interrupts 20
元智大學資訊工程系
The Simplest Solution
• Just disable interrupts when the shared data are used by the task code.
[email protected] Embedded Systems - Interrupts 21
元智大學資訊工程系
The Assembly Code
• Disabling Interrupts
[email protected] Embedded Systems - Interrupts 22
元智大學資訊工程系
Atomic Instructions/Sections
• A More Convenient Way – Use some atomic instructions supported by the hardware.
• An instruction is atomic if it cannot be interrupted.
• The collection of lines can be atomic by adding an interrupt-disable instruction.
• A set of instruction that must be atomic for the system to work properly is often called a critical section.
[email protected] Embedded Systems - Interrupts 23
元智大學資訊工程系
Another Example
• A buggy program– imprecise answer
Interrupt
[email protected] Embedded Systems - Interrupts 24
元智大學資訊工程系
Several Possible Solutions
• Disable the interrupt
A buggy solution - Interrupts will not be appropriately enabled.
A buggy solution - Interrupts will not be appropriately enabled.
[email protected] Embedded Systems - Interrupts 25
元智大學資訊工程系
A Better Code
• Changing the timing of return()
[email protected] Embedded Systems - Interrupts 27
元智大學資訊工程系
Another Potential Solutions
• Doing things in the ISR
[email protected] Embedded Systems - Interrupts 28
元智大學資訊工程系
Potential Bugs
• If the microprocessor’s registers are large enough to hold a long integer, then the ASM code is atomic.
• However, if the registers are too small to hold a long integer, the the ASM will have the same problem.
[email protected] Embedded Systems - Interrupts 29
元智大學資訊工程系
A volatile Case
• Where is the variables?
[email protected] Embedded Systems - Interrupts 30
元智大學資訊工程系
A volatile Case
• The volatile keyword allows you to warn the compiler that certain variables may change because of interrupt routines.
• If this keyword is not supported, you can still get the similar result by turning off the compiler optimizations.
[email protected] Embedded Systems - Interrupts 32
元智大學資訊工程系
One Obvious Question
• How fast does my system respond to each interrupt?– The longest period of time during which that interrupt is
disabled.
– The period of time it takes to execute any ISR for interrupts that are of higher priority than one in question.
– How long it takes the microprocessor to stop what it is doing, do the necessary bookkeeping, and start executing instructions within ISR.
– How long it takes the ISR to save the context and then do enough work that what it has accomplished counts as a “response”.
[email protected] Embedded Systems - Interrupts 33
元智大學資訊工程系
Next Question
• How do I get the times associated with the four factors?– Factor 3
• from the data book
– Factor 1, 2, and 4
• through a benchmark program
• count the machine cycles
[email protected] Embedded Systems - Interrupts 34
元智大學資訊工程系
So, Make Your ISR Short
• The four factors control interrupt latency and, therefore, response.
• Although lower-priority interrupts are presumably lower priority because their response time requirements are less critical, this is not necessarily license to make their response dreadful.
[email protected] Embedded Systems - Interrupts 35
元智大學資訊工程系
Disabling Interrupts
• The remaining factor that contributes to interrupt latency is the practice of disabling interrupts.
• However, this needs to be carefully designed to meet the deadline requirements.
[email protected] Embedded Systems - Interrupts 37
元智大學資訊工程系
If Design Requirement is Changed
• Low-end processor
• Network enhancement
[email protected] Embedded Systems - Interrupts 38
元智大學資訊工程系
Alternatives to Disabling Interrupts
• Two variable sets
[email protected] Embedded Systems - Interrupts 39
元智大學資訊工程系
Two Variable Sets
• This simple mechanism solves the shared-data problem, because the ISR will never write into the set of temperatures that the task code is reading.
• However, the while-loop may be executed twice before it sets off the alarm.
[email protected] Embedded Systems - Interrupts 40
元智大學資訊工程系
A Queue Approach
• The shared-data problem is also eliminated.
[email protected] Embedded Systems - Interrupts 42
元智大學資訊工程系
A Queue Approach
• However, this code is very fragile.
• Either of these seemingly minor changes can cause bugs.