-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled-driver.c
More file actions
137 lines (98 loc) · 3.04 KB
/
led-driver.c
File metadata and controls
137 lines (98 loc) · 3.04 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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define int fd;
void exportpins(void);
void setoutput(void);
void LED1H(void);
void LED1L(void);
void unexport(void);
int main()
{
exportpins();
setoutput();
LED1H();
sleep(20);
unexport();
return 0;
}
void exportpins(void){
// Export the desired pin by writing to /sys/class/gpio/export
int fd = open("/sys/class/gpio/export", O_WRONLY);
for (int i = 0; i < 8; i++) {
char gpioNumber[2];
sprintf(gpioNumber, "%d", i);
if (write(fd, gpioNumber, 2) != 2) {
perror("Error writing to /sys/class/gpio/export");
exit(1);
}
printf("Exported GPIO %d\n", i);
// Additional operations on the GPIO can be performed here
sleep(1); // Wait for 1 second before exporting the next GPIO
}
close(fd);
}
void setoutput(void){
for (int i = 0; i < 8; i++) {
char gpioPath[50];
sprintf(gpioPath, "/sys/class/gpio/gpio%d/direction", i);
FILE* directionFile = fopen(gpioPath, "w");
if (directionFile == NULL) {
perror("Unable to open GPIO direction file");
exit(1);
}
if (fprintf(directionFile, "out") < 0) {
perror("Error writing to GPIO direction file");
exit(1);
}
fclose(directionFile);
printf("Set GPIO %d direction to out\n", i);
// Additional operations on the GPIO can be performed here
sleep(1); // Wait for 1 second before setting the direction for the next GPIO
}
}
void LED1H(void) {
fd = open("/sys/class/gpio/gpio0/value", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/gpio0/value");
exit(1);
}
// Toggle LED 500 ms on, 50ms off, 100 times (10 seconds)
if (write(fd, "1", 1) != 1) {
perror("Error writing to /sys/class/gpio/gpio0/value");
}
close(fd);
}
void LED1L(void) {
fd = open("/sys/class/gpio/gpio0/value", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/gpio0/value");
exit(1);
}
// Toggle LED 500 ms on, 50ms off, 100 times (10 seconds)
if (write(fd, "0", 1) != 1) {
perror("Error writing to /sys/class/gpio/gpio0/value");
}
close(fd);
}
void unexport(void) {
for (int i = 0; i <= 24; i++) {
char gpioNumber[3];
sprintf(gpioNumber, "%d", i);
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd == -1) {
perror("Unable to open /sys/class/gpio/unexport");
exit(1);
}
if (write(fd, gpioNumber, 2) != 2) {
perror("Error writing to /sys/class/gpio/unexport");
exit(1);
}
close(fd);
printf("Unexported GPIO %d\n", i);
}
}