Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

176 righe
4.2 KiB

  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. }