isr.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * isr.c:
  3. * Wait for Interrupt test program - ISR method
  4. *
  5. * How to test:
  6. * Use the SoC's pull-up and pull down resistors that are avalable
  7. * on input pins. So compile & run this program (via sudo), then
  8. * in another terminal:
  9. * gpio mode 0 up
  10. * gpio mode 0 down
  11. * at which point it should trigger an interrupt. Toggle the pin
  12. * up/down to generate more interrupts to test.
  13. *
  14. * Copyright (c) 2013 Gordon Henderson.
  15. ***********************************************************************
  16. * This file is part of wiringPi:
  17. * https://projects.drogon.net/raspberry-pi/wiringpi/
  18. *
  19. * wiringPi is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Lesser General Public License as published by
  21. * the Free Software Foundation, either version 3 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * wiringPi is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public License
  30. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  31. ***********************************************************************
  32. */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #include <stdlib.h>
  37. #include <wiringPi.h>
  38. // globalCounter:
  39. // Global variable to count interrupts
  40. // Should be declared volatile to make sure the compiler doesn't cache it.
  41. static volatile int globalCounter [8] ;
  42. /*
  43. * myInterrupt:
  44. *********************************************************************************
  45. */
  46. void myInterrupt0 (void) { ++globalCounter [0] ; }
  47. void myInterrupt1 (void) { ++globalCounter [1] ; }
  48. void myInterrupt2 (void) { ++globalCounter [2] ; }
  49. void myInterrupt3 (void) { ++globalCounter [3] ; }
  50. void myInterrupt4 (void) { ++globalCounter [4] ; }
  51. void myInterrupt5 (void) { ++globalCounter [5] ; }
  52. void myInterrupt6 (void) { ++globalCounter [6] ; }
  53. void myInterrupt7 (void) { ++globalCounter [7] ; }
  54. /*
  55. *********************************************************************************
  56. * main
  57. *********************************************************************************
  58. */
  59. int main (void)
  60. {
  61. int gotOne, pin ;
  62. int myCounter [8] ;
  63. for (pin = 0 ; pin < 8 ; ++pin)
  64. globalCounter [pin] = myCounter [pin] = 0 ;
  65. wiringPiSetup () ;
  66. wiringPiISR (0, INT_EDGE_FALLING, &myInterrupt0) ;
  67. wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt1) ;
  68. wiringPiISR (2, INT_EDGE_FALLING, &myInterrupt2) ;
  69. wiringPiISR (3, INT_EDGE_FALLING, &myInterrupt3) ;
  70. wiringPiISR (4, INT_EDGE_FALLING, &myInterrupt4) ;
  71. wiringPiISR (5, INT_EDGE_FALLING, &myInterrupt5) ;
  72. wiringPiISR (6, INT_EDGE_FALLING, &myInterrupt6) ;
  73. wiringPiISR (7, INT_EDGE_FALLING, &myInterrupt7) ;
  74. for (;;)
  75. {
  76. gotOne = 0 ;
  77. printf ("Waiting ... ") ; fflush (stdout) ;
  78. for (;;)
  79. {
  80. for (pin = 0 ; pin < 8 ; ++pin)
  81. {
  82. if (globalCounter [pin] != myCounter [pin])
  83. {
  84. printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ;
  85. myCounter [pin] = globalCounter [pin] ;
  86. ++gotOne ;
  87. }
  88. }
  89. if (gotOne != 0)
  90. break ;
  91. }
  92. }
  93. return 0 ;
  94. }