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.

isr3.c 4.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * isr3.c:
  3. * Wait for Interrupt test program WiringPi >=3.2 - ISR method
  4. *
  5. *
  6. */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <stdlib.h>
  11. #include <wiringPi.h>
  12. #include <wpiExtensions.h>
  13. #include <unistd.h>
  14. #include <sys/time.h>
  15. // globalCounter:
  16. // Global variable to count interrupts
  17. // Should be declared volatile to make sure the compiler doesn't cache it.
  18. static volatile int globalCounter;
  19. volatile long long gStartTime, gEndTime;
  20. /*
  21. * myInterrupt:
  22. *********************************************************************************
  23. */
  24. static void wfi (void) {
  25. struct timeval now;
  26. gettimeofday(&now, 0);
  27. if (0==gStartTime) {
  28. gStartTime = now.tv_sec*1000000LL + now.tv_usec;
  29. } else {
  30. gEndTime = now.tv_sec*1000000LL + now.tv_usec;
  31. }
  32. globalCounter++;
  33. }
  34. /*
  35. *********************************************************************************
  36. * main
  37. *********************************************************************************
  38. */
  39. double StartSequence (int Enge, int OUTpin) {
  40. int expected;
  41. double timeExpected;
  42. gStartTime = 0;
  43. gEndTime = 0;
  44. globalCounter = 0;
  45. printf("Start\n");
  46. digitalWrite(OUTpin, HIGH);
  47. delay(200);
  48. digitalWrite(OUTpin, LOW);
  49. delay(100);
  50. digitalWrite(OUTpin, HIGH);
  51. delay(200);
  52. digitalWrite(OUTpin, LOW);
  53. delay(100);
  54. printf("Stop\n");
  55. int globalCounterCopy = globalCounter;
  56. if (INT_EDGE_BOTH == Enge) {
  57. expected = 4;
  58. timeExpected = 500;
  59. } else {
  60. expected = 2;
  61. timeExpected = 300;
  62. }
  63. if(globalCounter==expected) {
  64. double fTime = (gEndTime - gStartTime) / 1000000.0;
  65. printf("IRQ worked %g sec (~%gs expected)\n\n", fTime, timeExpected/1000.0);
  66. return fTime;
  67. } else {
  68. printf("IRQ not worked got %d iterations (%d exprected)\n\n", globalCounterCopy, expected);
  69. return 0;
  70. }
  71. }
  72. double DurationTime(int Enge, int OUTpin, int IRQpin) {
  73. struct timeval now;
  74. double fTime = 0.0;
  75. gStartTime = 0;
  76. gEndTime = 0;
  77. globalCounter = 0;
  78. printf("Start\n");
  79. if (INT_EDGE_RISING == Enge) {
  80. digitalWrite(OUTpin, LOW);
  81. wiringPiISR (IRQpin, INT_EDGE_RISING, &wfi) ;
  82. sleep(1);
  83. gettimeofday(&now, 0);
  84. gStartTime = now.tv_sec*1000000LL + now.tv_usec;
  85. digitalWrite(OUTpin, HIGH);
  86. delay(20);
  87. digitalWrite(OUTpin, LOW);
  88. } else if (INT_EDGE_FALLING == Enge) {
  89. digitalWrite(OUTpin, HIGH);
  90. wiringPiISR (IRQpin, INT_EDGE_FALLING, &wfi) ;
  91. sleep(1);
  92. gettimeofday(&now, 0);
  93. gStartTime = now.tv_sec*1000000LL + now.tv_usec;
  94. digitalWrite(OUTpin, LOW);
  95. }
  96. sleep(1);
  97. fTime = (gEndTime - gStartTime);
  98. printf("IRQ detect time %g usec\n\n", fTime);
  99. wiringPiISRStop (IRQpin) ;
  100. return fTime;
  101. }
  102. int main (void)
  103. {
  104. const int IRQpin = 22 ;
  105. const int OUTpin = 25 ;
  106. int major, minor;
  107. wiringPiVersion(&major, &minor);
  108. printf("\nISR test (WiringPi %d.%d)\n", major, minor);
  109. wiringPiSetupGpio() ;
  110. pinMode(IRQpin, INPUT);
  111. pinMode(OUTpin, OUTPUT);
  112. digitalWrite (OUTpin, LOW) ;
  113. printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d rising\n", IRQpin, OUTpin);
  114. wiringPiISR (IRQpin, INT_EDGE_RISING, &wfi) ;
  115. sleep(1);
  116. StartSequence (INT_EDGE_RISING, OUTpin);
  117. printf("Testing close\n");
  118. wiringPiISRStop (IRQpin) ;
  119. printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d falling\n", IRQpin, OUTpin);
  120. wiringPiISR (IRQpin, INT_EDGE_FALLING, &wfi) ;
  121. sleep(1);
  122. StartSequence (INT_EDGE_FALLING, OUTpin);
  123. printf("Testing close\n");
  124. wiringPiISRStop (IRQpin) ;
  125. printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d both\n", IRQpin, OUTpin);
  126. wiringPiISR (IRQpin, INT_EDGE_BOTH, &wfi) ;
  127. sleep(1);
  128. StartSequence (INT_EDGE_BOTH, OUTpin);
  129. printf("Testing close\n");
  130. wiringPiISRStop (IRQpin) ;
  131. for (int count=0; count<2; count++) {
  132. printf("Measuring duration IRQ @ GPIO%d with trigger @ GPIO%d rising\n", IRQpin, OUTpin);
  133. DurationTime(INT_EDGE_RISING, OUTpin, IRQpin);
  134. printf("Measuring duration IRQ @ GPIO%d with trigger @ GPIO%d falling\n", IRQpin, OUTpin);
  135. DurationTime(INT_EDGE_FALLING, OUTpin, IRQpin);
  136. }
  137. pinMode(OUTpin, INPUT);
  138. return 0 ;
  139. }