-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.c
79 lines (64 loc) · 1.74 KB
/
benchmark.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
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int togglePin(int fd){
for (int i = 0; i < 500000; i++) {
write(fd, "1", 1);
write(fd, "0", 1);
}
}
int main(){
// export pin
int fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/export");
exit(1);
}
if (write(fd, "418", 3) != 3) {
perror("Error writing to /sys/class/gpio/export");
exit(1);
}
close(fd);
// Set the pin to be an output by writing "out" to /sys/class/gpio/gpio24/direction
fd = open("/sys/class/gpio/gpio418/direction", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/gpio24/direction");
exit(1);
}
if (write(fd, "out", 3) != 3) {
perror("Error writing to /sys/class/gpio/gpio418/direction");
exit(1);
}
close(fd);
// open pin value for writing
fd = open("/sys/class/gpio/gpio418/value", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/gpio418/value");
exit(1);
}
// start timer
clock_t t;
t = clock();
togglePin(fd);
// stop timer
t = clock() - t;
double seconds = ((float)t)/CLOCKS_PER_SEC;
printf("Time taken in seconds: %f, aka %f MHz", seconds, 1/seconds);
close(fd);
// unexport pin
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/unexport");
exit(1);
}
if (write(fd, "418", 3) != 3) {
perror("Error writing to /sys/class/gpio/unexport");
exit(1);
}
close(fd);
}