-
Notifications
You must be signed in to change notification settings - Fork 0
/
mraa_gpio.c
127 lines (96 loc) · 2.52 KB
/
mraa_gpio.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* gpio example in c using mraa
*
* Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
*
* Usage: Toggles GPIO 23 and 24
*
* Compilation: gcc mraa_gpio.c -o bin -lmraa
*
* https://github.com/96boards/documentation/blob/master/consumer/guides/mraa/gpio/mraa_gpio.c
*
*/
/* standard headers */
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* mraa header */
#include "mraa/gpio.h"
/* gpio starts from 23 as per LS header pinout */
#define GPIO_1 23
#define GPIO_2 24
int flag = 1;
void sig_handler(int signum)
{
if (signum == SIGINT) {
fprintf(stdout, "Program interrupted\n");
flag = 0;
}
}
int main(void)
{
mraa_gpio_context gpio_1, gpio_2;
mraa_result_t res = MRAA_SUCCESS;
/* install signal handler */
signal(SIGINT, sig_handler);
/* initialize mraa for the platform. not needed most of the times */
mraa_init();
/* initialize gpio 23 */
gpio_1 = mraa_gpio_init(GPIO_1);
if (gpio_1 == NULL) {
fprintf(stderr, "Failed to initialize pin %d\n", GPIO_1);
}
/* initialize gpio 24 */
gpio_2 = mraa_gpio_init(GPIO_2);
if (gpio_2 == NULL) {
fprintf(stderr, "Failed to initialize pin %d\n", GPIO_2);
}
/* set gpio 23 to output */
res = mraa_gpio_dir(gpio_1, MRAA_GPIO_OUT);
if (res != MRAA_SUCCESS)
goto err;
/* set gpio 24 to output */
res = mraa_gpio_dir(gpio_2, MRAA_GPIO_OUT);
if (res != MRAA_SUCCESS)
goto err;
// start timer
clock_t t;
t = clock();
/* toggle both gpio's */
for(int i=0; i < 1000000; i++) {
res = mraa_gpio_write(gpio_1, 1);
if (res != MRAA_SUCCESS)
goto err;
// res = mraa_gpio_write(gpio_2, 0);
// if (res != MRAA_SUCCESS)
// goto err;
// sleep(1);
res = mraa_gpio_write(gpio_1, 0);
if (res != MRAA_SUCCESS)
goto err;
// res = mraa_gpio_write(gpio_2, 1);
// if (res != MRAA_SUCCESS)
// goto err;
// sleep(1);
}
// stop timer
t = clock() - t;
double seconds = ((float)t)/CLOCKS_PER_SEC;
printf("Time taken in seconds: %f, aka %f MHz", seconds, 1/seconds);
/* release gpio's */
res = mraa_gpio_close(gpio_1);
if (res != MRAA_SUCCESS) {
mraa_result_print(res);
}
res = mraa_gpio_close(gpio_2);
if (res != MRAA_SUCCESS) {
mraa_result_print(res);
}
return res;
err:
mraa_result_print(res);
return res;
}