About 20 years ago I naively built a steel thickness gauge without
an RTOS, only to later have to shoehorn one in. There were too many
async things going on, the in-line code grew to outlandish complexity.
I'm still trying to figure out how to explain that particular sin to
St. Peter.
A particularly vexing problem is to ensure the system will respond
to external inputs in a timely manner. How can we guarantee that an
interrupt will be recognized and processed fast enough to keep the
system reliable?
Let's look in some detail at the first of the requirements: that an
interrupt be recognized in time. Simple enough, it seems. Page through
the processor's data book and you'll find a specification called
"latency," a number always listed at submicrosecond levels (Figure
9.3 below). No doubt a footnote defines latency as the longest time
between when the interrupt occurs and when the CPU suspends the current
processing context. That would seem to be the interrupt response
time—but it ain't.
 |
| Figure
9.3: The Latency Is the Time from When the Interrupt Signal Appears,
Until the ISR Starts |
Latency as defined by CPU vendors varies from zero (the processor is
ready to handle an interrupt RIGHT NOW) to the maximum time specified.
It's a product of what sort of instruction is going on. Obviously it's
a bad idea to change contexts in the middle of executing an
instruction, so the processor generally waits until the current
instruction is complete before sampling the interrupt input.
Now, if it's doing a simple register-to-register move that may be
only a single clock cycle, a mere 50 nsec on a zero wait state 20-MHz
processor. Not much of a delay at all.
Other instructions are much slower. Multiplies can take dozens of
clocks. Read-modify-write instructions (like "increment memory") are
also inherently pokey. Maximum latency numbers come from these slowest
of instructions.
Many CPUs include looping constructs that can take hundreds, even
thousands, of microseconds. A block memory-to-memory transfer, for
instance, initiated by a single instruction, might run for an awfully
long time, driving latency figures out of sight.
All processors I'm aware of will accept an interrupt in the middle
of these long loops to keep interrupt response reasonable. The block
move will be suspended, but enough context is saved to allow the
transfer to resume when the ISR (Interrupt Service Routine) completes.
Therefore, the latency figure in the datasheet tells us the longest
time the processor can't service interrupts. The number is totally
useless to firmware engineers. OK, if you're building an extreme
cycle-countin', nanosecond-poor, gray-hair-inducing system then perhaps
that 300 nsec latency figure is indeed a critical part of your system's
performance.
For the rest of us, real latency - the 99% component of interrupt
response - comes not from what the CPU is doing, but from our own
software design. And that, my friend, is hard to predict at design
time. Without formal methods we need empirical ways to manage latency.
If latency is time between getting an interrupt and entering the
ISR, then surely most occurs because we've disabled interrupts! It's
because of the way we wrote the darn code. Turn interrupts off for even
a few C statements and latency might run to hundreds of microseconds,
far more than those handful of nanoseconds quoted by CPU vendors.
No matter how carefully you build the application, you'll be turning
interrupts off frequently. Even code that never issues a "disable
interrupt" instruction does, indeed, disable them often. For, every
time a hardware event issues an interrupt request, the processor itself
does an automatic disable, one that stays in effect till you explicitly
re-enable them inside of the ISR. Count on skyrocketing latency as a
result.
Of course, on many processors we don't so much as turn interrupts
off as change priority levels. A 68 K receiving an interrupt on level 5
will prohibit all interrupts at this and lower levels until our code
explicitly re-enables them in the ISR. Higher priority devices will
still function, but latency for all level 1 to 5 devices is infinity
until the code does its thing.
Therefore, in an ISR re-enable interrupts as soon as possible. When
reading code one of my "rules of thumb" is that code that does the
enable just before the return is probably flawed.
Most of us were taught to defer the interrupt enable until the end
of the ISR. But that prolongs latency unacceptably. Every other
interrupt (at least at or below that priority level) will be shut down
until the ISR completes. Better, enter the routine, do all of the
nonreentrant things (like handling hardware), and then enable
interrupts. Run the rest of the ISR, which manages reentrant variables
and the like, with interrupts on. You'll reduce latency and increase
system performance.
The downside might be a need for more stack space if that same
interrupt can re-invoke itself. There's nothing wrong with this in a
properly designed and reentrant ISR, but the stack will grow until all
pending interrupts get serviced.
The second biggest cause of latency is excessive use of the disable
interrupts instruction. Shared resources - global variables, hardware,
and the like - will cause erratic crashes when two asynchronous
activities try to access them simultaneously.
It's up to us to keep the code reentrant by either keeping all such
accesses atomic, or by limiting access to a single task at a time. The
classic approach is to disable interrupts around such accesses. Though
a simple solution, it comes at the cost of increased latency.