Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

150 lignes
4.4 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. #include <pthread.h>
  40. //**********************************************************************************************************************
  41. // What GPIO input are we using?
  42. // This is a wiringPi pin number
  43. #define BUTTON_PIN 3
  44. // LED Pin - wiringPi pin 0 is BCM_GPIO 17.
  45. #define LED 2
  46. //**********************************************************************************************************************
  47. // Interrupt state
  48. static volatile int isr_state = 0;
  49. static int terminate_process = 0;
  50. static pthread_mutex_t smb_mutex = PTHREAD_MUTEX_INITIALIZER;
  51. //**********************************************************************************************************************
  52. static void Signal_handler(int sig);
  53. //**********************************************************************************************************************
  54. /*
  55. * myInterrupt:
  56. *********************************************************************************
  57. */
  58. void myInterrupt(void) {
  59. // Only report button release on actual button release.
  60. // This interrupt sometimes fires for the wrong edge!
  61. if (!isr_state) {
  62. isr_state = 1;
  63. pthread_mutex_unlock( &smb_mutex );
  64. }
  65. }
  66. /*
  67. *********************************************************************************
  68. * main
  69. *********************************************************************************
  70. */
  71. int main(void) {
  72. int button_state = 0;
  73. int led_state = 0;
  74. if (wiringPiSetup() < 0) {
  75. fprintf(stderr, "Unable to setup wiringPi: %s\n", strerror(errno));
  76. return 1;
  77. }
  78. pinMode(LED, OUTPUT);
  79. if (wiringPiISR(BUTTON_PIN, INT_EDGE_BOTH, &myInterrupt) < 0) {
  80. fprintf(stderr, "Unable to setup ISR: %s\n", strerror(errno));
  81. return 1;
  82. }
  83. // Set the handler for SIGTERM (15)
  84. signal(SIGTERM, Signal_handler);
  85. signal(SIGHUP, Signal_handler);
  86. signal(SIGINT, Signal_handler);
  87. signal(SIGQUIT, Signal_handler);
  88. signal(SIGTRAP, Signal_handler);
  89. signal(SIGABRT, Signal_handler);
  90. signal(SIGALRM, Signal_handler);
  91. signal(SIGUSR1, Signal_handler);
  92. signal(SIGUSR2, Signal_handler);
  93. pthread_mutex_lock( &smb_mutex );
  94. while (!terminate_process) {
  95. printf("Waiting ... ");
  96. fflush(stdout);
  97. pthread_mutex_lock( &smb_mutex );
  98. delay(50);
  99. isr_state = 0;
  100. button_state = digitalRead(BUTTON_PIN);
  101. printf(" Button state: %d\n", button_state);
  102. if (button_state == 1) {
  103. digitalWrite (LED, led_state);
  104. led_state = !led_state;
  105. }
  106. }
  107. return 0;
  108. }
  109. //**********************************************************************************************************************
  110. /**
  111. * Intercepts and handles signals from QNX
  112. * This function is called when the SIGTERM signal is raised by QNX
  113. */
  114. void Signal_handler(int sig) {
  115. printf("Received signal %d\n", sig);
  116. // Signal process to exit.
  117. terminate_process = 1;
  118. pthread_mutex_unlock( &smb_mutex );
  119. }
  120. //**********************************************************************************************************************