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.

Writing interrupt handlers<br />

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

Using InterruptAttachEvent()<br />

With the volatile keyword, we’re telling the compiler not to cache the value of any<br />

of these variables, because they can change at any point during execution.<br />

The next thing we notice is the prototype for the interrupt service routine itself. It’s<br />

marked as const struct sigevent *. This says that the routine intHandler()<br />

returns a struct sigevent pointer. This is standard for all interrupt service<br />

routines.<br />

Finally, notice that the ISR decides if the thread will or won’t be sent an event. Only in<br />

the case of a Modem Status Register (MSR) interrupt do we want the event to be<br />

delivered (the event is identified by the variable event, which was conveniently passed<br />

to the ISR when we attached it). In all other cases, we ignore the interrupt (and update<br />

some global variables). In all cases, however, we clear the source of the interrupt. This<br />

is done by reading the I/O port via in8().<br />

If we were to recode the example above to use InterruptAttachEvent(), it would look<br />

like this:<br />

/*<br />

* part of int2.c<br />

*/<br />

#include <br />

#include <br />

#define HW_SERIAL_IRQ 3<br />

#define REG_RX 0<br />

#define REG_II 2<br />

#define REG_LS 5<br />

#define REG_MS 6<br />

#define IIR_MASK<br />

0x07<br />

#define IIR_MSR<br />

0x00<br />

#define IIR_THE<br />

0x02<br />

#define IIR_RX<br />

0x04<br />

#define IIR_LSR<br />

0x06<br />

#define IIR_MASK<br />

0x07<br />

static int base_reg = 0x2f8;<br />

int<br />

main (int argc, char **argv)<br />

{<br />

int intId; // interrupt id<br />

int iir; // interrupt identification register<br />

int serial_msr; // saved contents of Modem Status Reg<br />

int serial_rx; // saved contents of RX register<br />

int serial_lsr; // saved contents of Line Status Reg<br />

struct sigevent event;<br />

// usual main() setup stuff...<br />

// set up the event<br />

intId = InterruptAttachEvent (HW_SERIAL_IRQ, &event, 0);<br />

for (;;) {<br />

// wait for an interrupt event (could use MsgReceive instead)<br />

180 Chapter 4 • Interrupts April 30, 2009

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

Saved successfully!

Ooh no, something went wrong!