26.02.2014 Views

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

Getting Started with QNX Neutrino - QNX Software Systems

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

© 2009, <strong>QNX</strong> <strong>Software</strong> <strong>Systems</strong> GmbH & Co. KG. Clocks and timers<br />

Clock interrupt sources<br />

That’s the end of the timer five-cent tour.<br />

Actually, there’s a little bit more to it than first meets the eye.<br />

So where does the clock interrupt come from? Here’s a diagram that shows the<br />

hardware components (and some typical values for a PC) responsible for generating<br />

these clock interrupts:<br />

1.1931816<br />

MHz clock<br />

Time of<br />

day<br />

82C54<br />

÷ 11931<br />

Clock<br />

handler<br />

Timers<br />

Applications<br />

Applications<br />

PC clock interrupt sources.<br />

As you can see, there’s a high-speed (MHz range) clock produced by the circuitry in<br />

the PC. This high-speed clock is then divided by a hardware counter (the 82C54<br />

component in the diagram), which reduces the clock rate to the kHz or hundreds of Hz<br />

range (i.e., something that an ISR can actually handle). The clock ISR is a component<br />

of the kernel and interfaces directly <strong>with</strong> the data structures and code of the kernel<br />

itself. On non-x86 architectures (MIPS, PowerPC), a similar sequence of events<br />

occurs; some chips have clocks built into the processor.<br />

Note that the high-speed clock is being divided by an integer divisor. This means the<br />

rate isn’t going to be exactly 10 ms, because the high-speed clock’s rate isn’t an<br />

integer multiple of 10 ms. Therefore, the kernel’s ISR in our example above might<br />

actually be interrupted after 9.9999296004 ms.<br />

Big deal, right? Well, sure, it’s fine for our 15-second counter. 15 seconds is 1500<br />

timer ticks — doing the math shows that it’s approximately 106 μs off the mark:<br />

15 s - 1500 × 9.9999296004 ms<br />

= 15000 ms - 14999.8944006 ms<br />

= 0.1055994 ms<br />

= 105.5994 μs<br />

Unfortunately, continuing <strong>with</strong> the math, that amounts to 608 ms per day, or about<br />

18.5 seconds per month, or almost 3.7 minutes per year!<br />

You can imagine that <strong>with</strong> other divisors, the error could be greater or smaller,<br />

depending on the rounding error introduced. Luckily, the kernel knows about this and<br />

corrects for it.<br />

April 30, 2009 Chapter 3 • Clocks, Timers, and <strong>Getting</strong> a Kick Every So Often 139

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!