Interrupts, The Big Picture
Threads: Foreground and Background
We conclude our Interrupts introduction by defining a few common terms used in interrupt-driven
systems: Thread, Foreground and Background.
If you look at the “code” below, you will see that there are three individual – and independent –
code segments below: main, ISR1, and ISR2.
We use the word independent because, if you were to examine the code in such a system, there
are no calls between these three routines. Each one begins and ends execution without calling
the others. It is common to call these separate segments of code: “Threads”.
Foreground / Background Scheduling
main()
{
}
while(1){
background
or LPMx
}
//Init
initPMM();
initClocks();
...
ISR1
get data
process
System Initialization
The beginning part of main() is usually dedicated
to setting up your system (Chapters 3 and 4)
Background
Most systems have an endless loop that runs
‘forever’ in the background
In this case, ‘Background’ implies that it runs at a
lower priority than ‘Foreground’
In MSP430 systems, the background loop often
contains a Low Power Mode (LPM) command –
this sleeps the CPU/System until an interrupt
event wakes it up
Foreground
Interrupt Service Routine (ISR) runs in response
to enabled hardware interrupt
These events may change modes in Background –
such as waking the CPU out of low-power mode
ISR’s, by default, are not interruptible
Some processing may be done in ISR, but it’s
usually best to keep them short
ISR2
set a flag
As we’ve seen in the workshop already, it is our main() thread that begins running once the processor has
been started. The compiler’s initialization routine calls main() when its work is done. (In fact, this is why all C
programs start with a main() function. Every compiler works the same way, in this regard.)
With the main() thread started, since it is coded with a while(1) loop, it will keep running forever. That is,
unless a hardware interrupt occurs.
When an enabled interrupt is received by the CPU, it preempts the main() thread and runs the associated
ISR routine – for example, ISR1. In other words, the CPU stops running main() temporarily and runs ISR1;
when ISR1 completes execution, the CPU goes back to running main().
5 - 6 MSP430 Workshop - Interrupts