Skip to content

Commit

Permalink
Implement detachInterrupt method. See #173
Browse files Browse the repository at this point in the history
  • Loading branch information
Oitzu committed Dec 20, 2015
1 parent e1fe438 commit b615951
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
54 changes: 50 additions & 4 deletions utility/RPi/interrupt.c
Expand Up @@ -25,9 +25,11 @@ see <http://www.gnu.org/licenses/>

static pthread_mutex_t pinMutex ;
static volatile int pinPass = -1 ;

pthread_t threadId [64];

// sysFds:
// Map a file descriptor from the /sys/class/gpio/gpioX/value

static int sysFds [64] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
Expand Down Expand Up @@ -96,6 +98,7 @@ void *interruptHandler (void *arg)
pthread_mutex_lock (&pinMutex) ;
isrFunctions [myPin] () ;
pthread_mutex_unlock (&pinMutex) ;
pthread_testcancel(); //Cancel at this point if we have an cancellation request.
}

return NULL ;
Expand All @@ -104,7 +107,6 @@ void *interruptHandler (void *arg)

int attachInterrupt (int pin, int mode, void (*function)(void))
{
pthread_t threadId ;
const char *modeS ;
char fName [64] ;
char pinS [8] ;
Expand Down Expand Up @@ -150,7 +152,7 @@ int attachInterrupt (int pin, int mode, void (*function)(void))

if (sysFds [bcmGpioPin] == -1)
{
sprintf (fName, "/sys/class/gpio/gpio%d/value",bcmGpioPin) ; //WRONG
sprintf (fName, "/sys/class/gpio/gpio%d/value",bcmGpioPin);
if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
return printf ("wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
}
Expand All @@ -163,14 +165,58 @@ int attachInterrupt (int pin, int mode, void (*function)(void))

pthread_mutex_lock (&pinMutex) ;
pinPass = pin ;
pthread_create (&threadId, NULL, interruptHandler, NULL) ;
pthread_create (&threadId[bcmGpioPin], NULL, interruptHandler, NULL) ;
while (pinPass != -1)
delay (1) ;
pthread_mutex_unlock (&pinMutex) ;

return 0 ;
}

int detachInterrupt (int pin)
{
char pinS [8];
const char *modeS = "none";
pid_t pid ;

if (!pthread_cancel(threadId[pin])) //Cancel the thread
{
return 0;
}

if (!close(sysFds[pin])) //Close filehandle
{
return 0;
}

/* Set wiringPi to 'none' interrupt mode */

sprintf (pinS, "%d", pin) ;

if ((pid = fork ()) < 0) // Fail
return printf("wiringPiISR: fork failed: %s\n", strerror (errno)) ;

if (pid == 0) // Child, exec
{
/**/ if (access ("/usr/local/bin/gpio", X_OK) == 0)
{
execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
return printf ("wiringPiISR: execl failed: %s\n", strerror (errno)) ;
}
else if (access ("/usr/bin/gpio", X_OK) == 0)
{
execl ("/usr/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
return printf ("wiringPiISR: execl failed: %s\n", strerror (errno)) ;
}
else
return printf ("wiringPiISR: Can't find gpio program\n") ;
}
else // Parent, wait
wait (NULL) ;

return 1;
}

void rfNoInterrupts(){
pthread_mutex_lock (&pinMutex) ;
}
Expand Down
12 changes: 10 additions & 2 deletions utility/RPi/interrupt.h
Expand Up @@ -52,10 +52,18 @@ extern int piHiPri (const int pri);
*********************************************************************************
*/
extern int attachInterrupt (int pin, int mode, void (*function)(void));

/*
* detachInterrupt:
* Pi Specific detachInterrupt.
* Will cancel the interrupt thread, close the filehandle and
* setting wiringPi back to 'none' mode.
*********************************************************************************
*/
extern int detachInterrupt (int pin);

extern void rfNoInterrupts();
extern void rfInterrupts();
extern void spiNoInterrupts();
extern void spiInterrupts();
#ifdef __cplusplus
}
#endif

0 comments on commit b615951

Please sign in to comment.