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