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. Writing interrupt handlers<br />

}<br />

sizeof (event),<br />

0);<br />

if (interruptID == -1) {<br />

fprintf (stderr, "%s: can’t attach to IRQ %d\n",<br />

progname, HW_SERIAL_IRQ);<br />

perror (NULL);<br />

exit (EXIT_FAILURE);<br />

}<br />

...<br />

return (EXIT_SUCCESS);<br />

Detaching an interrupt handler<br />

This creates the association between the ISR (the routine called intHandler(); see<br />

below for details) and the hardware interrupt vector HW_SERIAL_IRQ.<br />

At this point, if an interrupt occurs on that interrupt vector, our ISR will be dispatched.<br />

When we call InterruptAttach(), the kernel unmasks the interrupt source at the PIC<br />

level (unless it’s already unmasked, which would be the case if multiple ISRs were<br />

sharing the same interrupt).<br />

When done <strong>with</strong> the ISR, we may wish to break the association between the ISR and<br />

the interrupt vector:<br />

int<br />

InterruptDetach (int id);<br />

I said “may” because threads that handle interrupts are generally found in servers, and<br />

servers generally hang around forever. It’s therefore conceivable that a<br />

well-constructed server wouldn’t ever issue the InterruptDetach() function call. Also,<br />

the OS will remove any interrupt handlers that a thread or process may have associated<br />

<strong>with</strong> it when the thread or process dies. So, simply falling off the end of main(),<br />

calling exit(), or exiting due to a SIGSEGV, will dissociate your ISR from the interrupt<br />

vector, automagically. (Of course, you’ll probably want to handle this a little better,<br />

and stop your device from generating interrupts. If another device is sharing the<br />

interrupt, then there are no two ways about it — you must clean up, otherwise you<br />

won’t get any more interrupts if running edge-sensitive mode, or you’ll get a constant<br />

flood of ISR dispatches if running in level-sensitive mode.)<br />

Continuing the above example, if we want to detach, we’d use the following code:<br />

void<br />

terminateInterrupts (void)<br />

{<br />

InterruptDetach (interruptID);<br />

}<br />

If this was the last ISR associated <strong>with</strong> that interrupt vector, the kernel would<br />

automatically mask the interrupt source at the PIC level so that it doesn’t generate<br />

interrupts.<br />

April 30, 2009 Chapter 4 • Interrupts 177

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

Saved successfully!

Ooh no, something went wrong!