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.

wiringpi_test6_isr.c 4.2 KiB

il y a 6 mois
il y a 5 mois
il y a 6 mois
il y a 5 mois
il y a 6 mois
il y a 5 mois
il y a 6 mois
il y a 5 mois
il y a 6 mois
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // WiringPi test program: Kernel char device interface / sysfs successor
  2. // Compile: gcc -Wall wiringpi_test1_device.c -o wiringpi_test1_device -lwiringPi
  3. #include "wpi_test.h"
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <unistd.h>
  7. #include <sys/time.h>
  8. int GPIO = 19;
  9. int GPIOIN = 26;
  10. const int ToggleValue = 4;
  11. static volatile int globalCounter;
  12. volatile long long gStartTime, gEndTime;
  13. static void wfi (void) {
  14. struct timeval now;
  15. gettimeofday(&now, 0);
  16. if (0==gStartTime) {
  17. gStartTime = now.tv_sec*1000000LL + now.tv_usec;
  18. } else {
  19. gEndTime = now.tv_sec*1000000LL + now.tv_usec;
  20. }
  21. globalCounter++;
  22. }
  23. double StartSequence (int Enge, int OUTpin) {
  24. int expected;
  25. double timeExpected;
  26. gStartTime = 0;
  27. gEndTime = 0;
  28. globalCounter = 0;
  29. printf("Start\n");
  30. digitalWrite(OUTpin, HIGH);
  31. delay(200);
  32. digitalWrite(OUTpin, LOW);
  33. delay(100);
  34. digitalWrite(OUTpin, HIGH);
  35. delay(200);
  36. digitalWrite(OUTpin, LOW);
  37. delay(100);
  38. printf("Stop\n");
  39. int globalCounterCopy = globalCounter;
  40. if (INT_EDGE_BOTH == Enge) {
  41. expected = 4;
  42. timeExpected = 500;
  43. } else {
  44. expected = 2;
  45. timeExpected = 300;
  46. }
  47. if (globalCounter==expected) {
  48. double fTime = (gEndTime - gStartTime) / 1000000.0;
  49. printf("IRQ worked %g sec (~%gs expected)", fTime, timeExpected/1000.0);
  50. double diff = fTime-(timeExpected/1000.0);
  51. if(diff<0.05 && diff>0) {
  52. printf(" -> %spassed%s\n", COLORGRN, COLORDEF);
  53. } else {
  54. printf(" -> %sfailed%s\n", COLORRED, COLORDEF);
  55. }
  56. return fTime;
  57. } else {
  58. printf("IRQ not worked got %d iterations (%d exprected)\n\n", globalCounterCopy, expected);
  59. return 0;
  60. }
  61. }
  62. double DurationTime(int Enge, int OUTpin, int IRQpin) {
  63. struct timeval now;
  64. double fTime = 0.0;
  65. gStartTime = 0;
  66. gEndTime = 0;
  67. globalCounter = 0;
  68. printf("Start\n");
  69. if (INT_EDGE_RISING == Enge) {
  70. digitalWrite(OUTpin, LOW);
  71. wiringPiISR (IRQpin, INT_EDGE_RISING, &wfi) ;
  72. sleep(1);
  73. gettimeofday(&now, 0);
  74. gStartTime = now.tv_sec*1000000LL + now.tv_usec;
  75. digitalWrite(OUTpin, HIGH);
  76. delay(20);
  77. digitalWrite(OUTpin, LOW);
  78. } else if (INT_EDGE_FALLING == Enge) {
  79. digitalWrite(OUTpin, HIGH);
  80. wiringPiISR (IRQpin, INT_EDGE_FALLING, &wfi) ;
  81. sleep(1);
  82. gettimeofday(&now, 0);
  83. gStartTime = now.tv_sec*1000000LL + now.tv_usec;
  84. digitalWrite(OUTpin, LOW);
  85. }
  86. sleep(1);
  87. fTime = (gEndTime - gStartTime);
  88. printf("IRQ detect time %g usec", fTime);
  89. if (fTime<2000 && fTime>0) {
  90. printf(" -> %spassed%s\n", COLORGRN, COLORDEF);
  91. } else {
  92. printf(" -> %sfailed%s\n", COLORRED, COLORDEF);
  93. }
  94. wiringPiISRStop (IRQpin) ;
  95. return fTime;
  96. }
  97. int main (void) {
  98. int major, minor;
  99. wiringPiVersion(&major, &minor);
  100. printf("WiringPi GPIO test program 1 (using GPIO%d (output) and GPIO%d (input))\n", GPIO, GPIOIN);
  101. printf(" testing irq\n");
  102. printf("\nISR test (WiringPi %d.%d)\n", major, minor);
  103. wiringPiSetupGpio() ;
  104. if (!piBoard40Pin()) {
  105. GPIO = 23;
  106. GPIOIN = 24;
  107. }
  108. int IRQpin = GPIOIN ;
  109. int OUTpin = GPIO ;
  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 UnitTestState();
  139. }