Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

6 місяці тому
6 місяці тому
6 місяці тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. const int GPIO = 19;
  9. const 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. const int IRQpin = GPIOIN ;
  99. const int OUTpin = GPIO ;
  100. int major, minor;
  101. wiringPiVersion(&major, &minor);
  102. printf("WiringPi GPIO test program 1 (using GPIO%d (output) and GPIO%d (input))\n", GPIO, GPIOIN);
  103. printf(" testing irq\n");
  104. printf("\nISR test (WiringPi %d.%d)\n", major, minor);
  105. wiringPiSetupGpio() ;
  106. pinMode(IRQpin, INPUT);
  107. pinMode(OUTpin, OUTPUT);
  108. digitalWrite (OUTpin, LOW) ;
  109. printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d rising\n", IRQpin, OUTpin);
  110. wiringPiISR (IRQpin, INT_EDGE_RISING, &wfi) ;
  111. sleep(1);
  112. StartSequence (INT_EDGE_RISING, OUTpin);
  113. printf("Testing close\n");
  114. wiringPiISRStop (IRQpin) ;
  115. printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d falling\n", IRQpin, OUTpin);
  116. wiringPiISR (IRQpin, INT_EDGE_FALLING, &wfi) ;
  117. sleep(1);
  118. StartSequence (INT_EDGE_FALLING, OUTpin);
  119. printf("Testing close\n");
  120. wiringPiISRStop (IRQpin) ;
  121. printf("Testing IRQ @ GPIO%d with trigger @ GPIO%d both\n", IRQpin, OUTpin);
  122. wiringPiISR (IRQpin, INT_EDGE_BOTH, &wfi) ;
  123. sleep(1);
  124. StartSequence (INT_EDGE_BOTH, OUTpin);
  125. printf("Testing close\n");
  126. wiringPiISRStop (IRQpin) ;
  127. for (int count=0; count<2; count++) {
  128. printf("Measuring duration IRQ @ GPIO%d with trigger @ GPIO%d rising\n", IRQpin, OUTpin);
  129. DurationTime(INT_EDGE_RISING, OUTpin, IRQpin);
  130. printf("Measuring duration IRQ @ GPIO%d with trigger @ GPIO%d falling\n", IRQpin, OUTpin);
  131. DurationTime(INT_EDGE_FALLING, OUTpin, IRQpin);
  132. }
  133. pinMode(OUTpin, INPUT);
  134. return UnitTestState();
  135. }