chiark / gitweb /
Lots of changes here. Added new I2C test code, a new serialTest program,
[wiringPi.git] / wiringPi / wiringPiISR.c
1 /*
2  * wiringPiISR.c:
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/
8  *
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.
13  *
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.
18  *
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  ***********************************************************************
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28
29 #include "wiringPi.h"
30
31
32
33 static void (*isrFunctions [64])(void) ;
34 static int    isrFds       [64] ;
35
36 /*
37  * interruptHandler:
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
40  *      fires.
41  *********************************************************************************
42  */
43
44 static void *interruptHandler (void *arg)
45 {
46   int pin = *(int *)arg ;
47
48   (void)piHiPri (55) ;
49
50   for (;;)
51   {
52     if (waitForInterrupt (pin, -1) > 0)
53       isrFunctions [pin] () ;
54   }
55
56   return NULL ;
57 }
58
59 /*
60  * wiringPiISR:
61  *      Take the details and create an interrupt handler that will do a call-
62  *      back to the user supplied function.
63  *********************************************************************************
64  */
65
66 int wiringPiISR (int pin, int mode, void (*function)(void))
67 {
68   pthread_t threadId ;
69   char command [64] ;
70
71   pin &= 63 ;
72
73   if (wiringPiMode == WPI_MODE_UNINITIALISED)
74   {
75     fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
76     exit (EXIT_FAILURE) ;
77   }
78   else if (wiringPiMode == WPI_MODE_PINS)
79     pin = pinToGpio [pin] ;
80
81
82   isrFunctions [pin] = function ;
83
84 // Now export the pin and set the right edge
85
86   if (mode != INT_EDGE_SETUP)
87   {
88     /**/ if (mode == INT_EDGE_FALLING)
89       modes = "falling" ;
90     else if (mode == INT_EDGE_RISING)
91       modes = "rising" ;
92     else
93       modes = "both" ;
94
95     sprintf (command, "/usr/local/bin/gpio edge %d %s", pin, modes) ;
96     system (command) ;
97   }
98
99   sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
100   if ((isrFds [pin] = open (fName, O_RDWR)) < 0)
101     return -1 ;
102
103   {
104     fprintf ("std
105
106   pthread_create (&threadId, NULL, interruptHandler, &pin) ;
107 }
108
109