forked from NachiketKelkar/AESD-Project_1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.c
185 lines (164 loc) · 3.46 KB
/
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/************************************************************************************************
* File name : gpio.c *
* Authors : Nachiket Kelkar and Puneet Bansal *
* Description : The functions used for gpio operations. Setting the direction of pin and *
* the value. This functions are restricted for use of only USER LED pins. *
* Tools used : GNU make, gcc, arm-linux-gcc. *
************************************************************************************************/
#define _GNU_SOURCE
/* Including standard libraries */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/gpio.h>
/* Including user libraries */
#include "gpio.h"
void gpio_init(int gpio_pin,int gpio_direction)
{
FILE *fp;
char *file = (char*)malloc(40);
if(is_pin_valid(gpio_pin))
{
fp = fopen("/sys/class/gpio/export","w");
fprintf(fp,"%d",gpio_pin);
fclose(fp);
sprintf(file,"/sys/class/gpio/gpio%d/direction",gpio_pin);
fp = fopen(file,"w");
if(gpio_direction == out)
{
fprintf(fp,"out");
}
else if(gpio_direction == in)
{
fprintf(fp,"in");
}
else
{
printf("Enter direction only as in or out");
}
fclose(fp);
}
else
{
printf("Enter valid pin number");
}
free(file);
}
void gpio_write_value(int gpio_pin, int gpio_value)
{
FILE *fp;
char *file = (char*)malloc(40);
if(is_pin_valid(gpio_pin))
{
sprintf(file,"/sys/class/gpio/gpio%d/value",gpio_pin);
fp = fopen(file,"w");
if(gpio_value == low)
{
fprintf(fp,"%d",low);
}
else if(gpio_value == high)
{
fprintf(fp,"%d",high);
}
else
{
printf("Enter value only as low or high");
}
fclose(fp);
}
else
{
printf("Enter valid pin number");
}
free(file);
}
int gpio_read_value(int gpio_pin)
{
FILE *fp;
char *file = (char*)malloc(40);
int value;
if(is_pin_valid(gpio_pin))
{
sprintf(file,"/sys/class/gpio/gpio%d/value",gpio_pin);
fp = fopen(file,"r");
fscanf(fp,"%d",&value);
fclose(fp);
}
else
{
printf("Enter valid pin number");
}
free(file);
return value;
}
bool is_pin_valid(int gpio_pin)
{
int gpio_allowed[total_gpio] = access_pin_allowed;
bool is_valid = false;
for(int i=0; i<total_gpio; i++)
{
if(gpio_pin == gpio_allowed[i])
is_valid = is_valid | true;
else
is_valid = is_valid | false;
}
return is_valid;
}
void gpio_interrupt_state(int gpio_pin, gpio_interrupt interrupt)
{
FILE *fp;
char *file = (char*)malloc(40);
if(is_pin_valid(gpio_pin))
{
gpio_init(gpio_pin,in);
sprintf(file,"/sys/class/gpio/gpio%d/edge",gpio_pin);
fp = fopen(file,"w");
switch(interrupt)
{
case rising:
fprintf(fp,"rising");
break;
case falling:
fprintf(fp,"falling");
break;
case both:
fprintf(fp,"both");
break;
case none:
fprintf(fp,"none");
break;
}
fclose(fp);
}
else
{
printf("Enter valid pin number");
}
free(file);
}
int gpio_open_value(int gpio_pin)
{
char *file = (char*)malloc(40);
int fd;
if(is_pin_valid(gpio_pin))
{
sprintf(file,"/sys/class/gpio/gpio%d/value",gpio_pin);
fd = open(file, O_RDONLY);
}
else
{
printf("Enter valid pin number");
fd = -1;
}
return fd;
}
int gpio_read_val_with_fd(int fd)
{
int value;
read(fd, &value, sizeof(value));
lseek(fd, 0, SEEK_SET);
return value & 0x1;
}