wiringPiISR.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include "wiringPi.h"
  28. static void (*isrFunctions [64])(void) ;
  29. static int isrFds [64] ;
  30. /*
  31. * interruptHandler:
  32. * This is a thread and gets started to wait for the interrupt we're
  33. * hoping to catch. It will call the user-function when the interrupt
  34. * fires.
  35. *********************************************************************************
  36. */
  37. static void *interruptHandler (void *arg)
  38. {
  39. int pin = *(int *)arg ;
  40. (void)piHiPri (55) ;
  41. for (;;)
  42. {
  43. if (waitForInterrupt (pin, -1) > 0)
  44. isrFunctions [pin] () ;
  45. }
  46. return NULL ;
  47. }
  48. /*
  49. * wiringPiISR:
  50. * Take the details and create an interrupt handler that will do a call-
  51. * back to the user supplied function.
  52. *********************************************************************************
  53. */
  54. int wiringPiISR (int pin, int mode, void (*function)(void))
  55. {
  56. pthread_t threadId ;
  57. char command [64] ;
  58. pin &= 63 ;
  59. if (wiringPiMode == WPI_MODE_UNINITIALISED)
  60. {
  61. fprintf (stderr, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
  62. exit (EXIT_FAILURE) ;
  63. }
  64. else if (wiringPiMode == WPI_MODE_PINS)
  65. pin = pinToGpio [pin] ;
  66. isrFunctions [pin] = function ;
  67. // Now export the pin and set the right edge
  68. if (mode != INT_EDGE_SETUP)
  69. {
  70. /**/ if (mode == INT_EDGE_FALLING)
  71. modes = "falling" ;
  72. else if (mode == INT_EDGE_RISING)
  73. modes = "rising" ;
  74. else
  75. modes = "both" ;
  76. sprintf (command, "/usr/local/bin/gpio edge %d %s", pin, modes) ;
  77. system (command) ;
  78. }
  79. sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
  80. if ((isrFds [pin] = open (fName, O_RDWR)) < 0)
  81. return -1 ;
  82. {
  83. fprintf ("std
  84. pthread_create (&threadId, NULL, interruptHandler, &pin) ;
  85. }