Skip to content

Commit cbdb7ef

Browse files
committed
wiringPi: Add wiringPiISRCancel function
Signed-off-by: Joshua Yang <[email protected]>
1 parent 68a09cb commit cbdb7ef

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

wiringPi/wiringPi.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,18 +721,52 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
721721
fprintf(stderr, "Unable to read from the sysfs GPIO node: %s \n", strerror(errno));
722722
}
723723

724-
libwiring.isrFunctions [PIN_NUM_CALC_SYSFD(GpioPin)] = function ;
725-
726724
pthread_mutex_lock (&pinMutex) ;
727725
pinPass = GpioPin ;
728726
pthread_create (&threadId, NULL, interruptHandler, NULL) ;
729727
while (pinPass != -1)
730728
delay (1) ;
731729
pthread_mutex_unlock (&pinMutex) ;
732730

731+
libwiring.isrFunctions [PIN_NUM_CALC_SYSFD(GpioPin)] = function ;
732+
libwiring.isrThreadIds [PIN_NUM_CALC_SYSFD(GpioPin)] = threadId ;
733+
733734
return 0 ;
734735
}
735736

737+
/*----------------------------------------------------------------------------*/
738+
int wiringPiISRCancel(int pin) {
739+
int GpioPin = -1;
740+
741+
if (libwiring.mode == MODE_UNINITIALISED)
742+
return wiringPiFailure (
743+
WPI_FATAL,
744+
"wiringPiISRCancel: wiringPi has not been initialised. " \
745+
"Unable to continue.\n") ;
746+
747+
if (libwiring.getModeToGpio)
748+
GpioPin = libwiring.getModeToGpio(libwiring.mode, pin);
749+
else
750+
return wiringPiFailure (
751+
WPI_FATAL,
752+
"%s: getModeToGpio function not initialize!\n",
753+
__func__);
754+
755+
pthread_t threadId = libwiring.isrThreadIds[PIN_NUM_CALC_SYSFD(GpioPin)];
756+
757+
if (pthread_cancel(threadId) < 0)
758+
return wiringPiFailure (
759+
WPI_FATAL,
760+
"%s: wiringPiISRCancel: Unregister for the interrupt pin failed!\n",
761+
__func__);
762+
else {
763+
libwiring.isrFunctions[PIN_NUM_CALC_SYSFD(GpioPin)] = NULL;
764+
libwiring.isrThreadIds[PIN_NUM_CALC_SYSFD(GpioPin)] = 0;
765+
}
766+
767+
return 0;
768+
}
769+
736770
/*----------------------------------------------------------------------------*/
737771
static void initialiseEpoch (void)
738772
{

wiringPi/wiringPi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <stdarg.h>
3030
#include <stdint.h>
3131
#include <stdlib.h>
32+
#include <pthread.h>
3233

3334
/*----------------------------------------------------------------------------*/
3435

@@ -139,6 +140,7 @@ struct libodroid
139140

140141
/* ISR Function pointer */
141142
void (*isrFunctions[256])(void);
143+
pthread_t isrThreadIds[256];
142144

143145
/* GPIO sysfs file discripter */
144146
int sysFds[256];
@@ -283,6 +285,7 @@ extern void digitalWriteByte2 (int value) UNU;
283285
// Interrupt
284286
extern int waitForInterrupt (int pin, int mS);
285287
extern int wiringPiISR (int pin, int mode, void (*function)(void));
288+
extern int wiringPiISRCancel (int pin);
286289

287290
// Threads
288291
extern int piThreadCreate (void *(*fn)(void *));

0 commit comments

Comments
 (0)