-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpiosniffint3.c
107 lines (90 loc) · 2.31 KB
/
gpiosniffint3.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <wiringPi.h>
#define MAX_BUFFER_LEN 1024 /* circular buffer size for timing events */
#define DISPLAY_DELAY 10 /* main display loop in miliseconds */
struct sample {
int value;
unsigned long uslen;
};
/* volatile keeps variables in memory for interrupts */
static volatile struct sample sbuf[MAX_BUFFER_LEN];
static volatile int gpio, intr, buflck, isrblk, rptr, wptr;
static struct timeval tstart;
/* Show help */
void help(char *progname)
{
printf("Usage:\n\t%s {gpio} {seconds} \n\n", progname);
puts("Where:");
puts("\tgpio\t - GPIO pin to scan for state changes (mandatory)");
puts("\tseconds\t - duration of scan in seconds (optional)");
}
/* ************* */
/* * Interrupt * */
/* ************* */
void handleGpioInt(void)
{
int v;
struct timeval t;
intr++;
if (buflck) {
isrblk++;
return;
}
gettimeofday(&t, NULL);
buflck = 1;
sbuf[wptr].value = digitalRead(gpio);
sbuf[wptr].uslen = (t.tv_sec - tstart.tv_sec) * 1000000 + t.tv_usec - tstart.tv_usec;
wptr = (wptr + 1) % MAX_BUFFER_LEN;
buflck = 0;
}
/* ********** */
/* * MAIN * */
/* ********** */
int main(int argc, char *argv[])
{
int i, dur;
struct timeval t;
/* show help */
if (argc < 2) {
help(argv[0]);
exit(0);
}
/* get parameters */
sscanf(argv[1], "%d", &gpio);
if (argc > 2)
sscanf(argv[2], "%d", &dur);
else
dur = 0;
/* initialize global variables */
intr = 0; /* number of interrupts */
buflck = 0; /* buffer write lock */
isrblk = 0; /* blocked interrupt counter*/
rptr = 0; /* read pointer */
wptr = 0; /* write pointer */
gettimeofday(&tstart, NULL);
/* initialize WiringPi library - use BCM GPIO numbers */
wiringPiSetupGpio();
pinMode(gpio, INPUT);
wiringPiISR(gpio, INT_EDGE_BOTH, handleGpioInt);
for(;;) {
if (dur > 0 && time(NULL) - tstart.tv_sec > dur)
break;
while(rptr != wptr) {
printf("%lu : %d\n", sbuf[rptr].uslen, sbuf[rptr].value);
rptr = (rptr + 1) % MAX_BUFFER_LEN;
}
delay(DISPLAY_DELAY);
}
gettimeofday(&t, NULL);
printf("Total time: %.3lf second(s), total interrupts: %d (%d blocked)\n",
t.tv_sec - tstart.tv_sec + (t.tv_usec - tstart.tv_usec) / 1000000.0,
intr, isrblk);
}