How Interrupts Work
We show the interrupt flow in a slightly different fashion in the following diagram. As you can see,
when an enabled interrupt occurs, the processor will look up the ISR’s branch-to address from a
specific address in memory (called the interrupt vector). For the MSP430, this address is defined
using the vector pragma.
#pragma vector=WDT_VECTOR
interrupt myISR(void){
}
4. Interrupt Service Routine (ISR)
Using Interrupt Keyword
Compiler handles context save/restore
Call a function? Then full context is saved
No arguments, no return values
You cannot call any TI-RTOS scheduler
functions (e.g. Swi_post)
Nesting interrupts is MANUAL
&myISR
Vector Table
…currently executing code
interrupt occurs
next_line_of_code
}
• Save context of system
• (optional) Re-enable interrupts
•
*
If group INT, read assoc IV Reg
(determines source & clears IFG)
• Run your interrupt’s code
• Restore context of system
• Continue where it left off (RETI)
The context of the system – for example, the CPU registers used by the ISR – must be saved
before running your code and restored afterwards. Thankfully, the compiler handles this for you
when the function is declared as an interrupt. (As part of the “context restore”, the compiler will
return to running the previous thread of code by using the RETI instruction).
Please note the bullets under “Using the Interrupt Keyword” from the preceding diagram.
Using this keyword, the compiler handles all of the context save/restore for you and knows how to
return to your previous code – even restoring the original value for the Status Register (SR).
Hint: If you call a function within your ISR, the compiler will have to save/restore every CPU
register, not just the ones that it uses to implement your C code. This is because it
doesn’t know what resources the function call may end up using.
Since the interrupt occurs asynchronously to the background thread, you cannot pass arguments
to and receive return values from the ISR. You must communicate between threads using global
variables (or other appropriate data objects).
TI’s real-time operating system (TI-RTOS) provides a rich set of scheduling functions that are
often used within interrupt service routines. Be aware, though, that some of these functions can
only be used with RTOS “managed” interrupts. In fact, it’s actually easier to let TI-RTOS manage
your interrupts; it automatically handles plugging the interrupt vector as well as context
save/restore. (All you have to do is write a standard C function.) But, the details of TI-RTOS are
outside the scope of this workshop. While we provide a brief discussion of TI-RTOS at the end of
this chapter, please refer to the Introduction to TI-RTOS Kernel
workshop for more details.
MSP430 Workshop - Interrupts 5 - 15