3 * Simplified Interrupt Service Routine handling
4 * Copyright (c) 2013 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with wiringPi.
21 * If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
33 static void (*isrFunctions [64])(void) ;
34 static int isrFds [64] ;
38 * This is a thread and gets started to wait for the interrupt we're
39 * hoping to catch. It will call the user-function when the interrupt
41 *********************************************************************************
44 static void *interruptHandler (void *arg)
46 int pin = *(int *)arg ;
52 if (waitForInterrupt (pin, -1) > 0)
53 isrFunctions [pin] () ;
61 * Take the details and create an interrupt handler that will do a call-
62 * back to the user supplied function.
63 *********************************************************************************
66 int wiringPiISR (int pin, int mode, void (*function)(void))
73 if (wiringPiMode == WPI_MODE_UNINITIALISED)
75 fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
78 else if (wiringPiMode == WPI_MODE_PINS)
79 pin = pinToGpio [pin] ;
82 isrFunctions [pin] = function ;
84 // Now export the pin and set the right edge
86 if (mode != INT_EDGE_SETUP)
88 /**/ if (mode == INT_EDGE_FALLING)
90 else if (mode == INT_EDGE_RISING)
95 sprintf (command, "/usr/local/bin/gpio edge %d %s", pin, modes) ;
99 sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
100 if ((isrFds [pin] = open (fName, O_RDWR)) < 0)
106 pthread_create (&threadId, NULL, interruptHandler, &pin) ;