You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

153 lines
4.7 KiB

  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://github.com/WiringPi/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. #include <signal.h>
  39. //**********************************************************************************************************************
  40. // globalCounter:
  41. // Global variable to count interrupts
  42. // Should be declared volatile to make sure the compiler doesn't cache it.
  43. static volatile int globalCounter [8] ;
  44. static int terminate_process = 0;
  45. //**********************************************************************************************************************
  46. static void Signal_handler(int sig);
  47. //**********************************************************************************************************************
  48. /*
  49. * myInterrupt:
  50. *********************************************************************************
  51. */
  52. void myInterrupt0 (int pin) { ++globalCounter [0] ; }
  53. void myInterrupt1 (int pin) { ++globalCounter [1] ; }
  54. void myInterrupt2 (int pin) { ++globalCounter [2] ; }
  55. void myInterrupt3 (int pin) { ++globalCounter [3] ; }
  56. void myInterrupt4 (int pin) { ++globalCounter [4] ; }
  57. void myInterrupt5 (int pin) { ++globalCounter [5] ; }
  58. void myInterrupt6 (int pin) { ++globalCounter [6] ; }
  59. void myInterrupt7 (int pin) { ++globalCounter [7] ; }
  60. /*
  61. *********************************************************************************
  62. * main
  63. *********************************************************************************
  64. */
  65. int main (void)
  66. {
  67. int gotOne, pin ;
  68. int myCounter [8] ;
  69. for (pin = 0 ; pin < 8 ; ++pin)
  70. {
  71. globalCounter [pin] = myCounter [pin] = 0 ;
  72. }
  73. if (wiringPiSetup() < 0)
  74. {
  75. fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
  76. return 1;
  77. }
  78. wiringPiISR (0, INT_EDGE_FALLING, &myInterrupt0) ;
  79. wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt1) ;
  80. wiringPiISR (2, INT_EDGE_FALLING, &myInterrupt2) ;
  81. wiringPiISR (3, INT_EDGE_FALLING, &myInterrupt3) ;
  82. wiringPiISR (4, INT_EDGE_FALLING, &myInterrupt4) ;
  83. wiringPiISR (5, INT_EDGE_FALLING, &myInterrupt5) ;
  84. wiringPiISR (6, INT_EDGE_FALLING, &myInterrupt6) ;
  85. wiringPiISR (7, INT_EDGE_FALLING, &myInterrupt7) ;
  86. // Set the handler for SIGTERM (15)
  87. signal(SIGTERM, Signal_handler);
  88. signal(SIGHUP, Signal_handler);
  89. signal(SIGINT, Signal_handler);
  90. signal(SIGQUIT, Signal_handler);
  91. signal(SIGTRAP, Signal_handler);
  92. signal(SIGABRT, Signal_handler);
  93. signal(SIGALRM, Signal_handler);
  94. signal(SIGUSR1, Signal_handler);
  95. signal(SIGUSR2, Signal_handler);
  96. while (!terminate_process)
  97. {
  98. gotOne = 0 ;
  99. printf("Waiting ... ");
  100. fflush(stdout);
  101. while (!terminate_process)
  102. {
  103. for (pin = 0 ; pin < 8 ; ++pin)
  104. {
  105. if (globalCounter [pin] != myCounter [pin])
  106. {
  107. printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ;
  108. myCounter [pin] = globalCounter [pin] ;
  109. ++gotOne ;
  110. }
  111. }
  112. if (gotOne != 0)
  113. break ;
  114. }
  115. }
  116. return 0;
  117. }
  118. //**********************************************************************************************************************
  119. /**
  120. * Intercepts and handles signals from QNX
  121. * This function is called when the SIGTERM signal is raised by QNX
  122. */
  123. void Signal_handler(int sig) {
  124. printf("Received signal %d\n", sig);
  125. // Signal process to exit.
  126. terminate_process = 1;
  127. }
  128. //**********************************************************************************************************************