Solution
Install and Check GPIO Drivers
Navigate to the GPIO directory and ensure the GPIO drivers are working:
$ cd /sys/class/gpio/
$ ls
export gpiochip0 gpiochip64 unexport
$ echo 44 > export
$ ls
export gpio44 gpiochip0 gpiochip64 unexport
$ cd gpio44/
$ ls
active_low direction subsystem value
$ cat direction
in
$ cat value
1
- Create LED Blinking C program -
gpioLEDBlink.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#define GPIO44 44
#define FISCV_GPIO GPIO44
#define MAX_BUF 128 // Define array size
#define StarF_Gpio_Dir "/sys/class/gpio" // GPIO control paths
int StarF_gpio_export(unsigned int gpio) {
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/export
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0) {
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
int StarF_gpio_unexport(unsigned int gpio) {
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/unexport
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0) {
perror("gpio/unexport");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
int StarF_gpio_set_dir(unsigned int gpio, unsigned int out_flag) {
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/gpioN/direction
len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror(buf);
return fd;
}
if (out_flag) //'1' set to output
write(fd, "out", 4);
else // ‘0’ set input
write(fd, "in", 3);
close(fd);
return 0;
}
int StarF_gpio_set_value(unsigned int gpio, unsigned int value) {
int fd, len;
char buf[MAX_BUF];
// /sys/class/gpio/gpioN/value
len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror(buf);
return fd;
}
if (value) //'1' output is high level
write(fd, "1", 2);
else //'0' output is Low level
write(fd, "0", 2);
close(fd);
return 0;
}
int StarF_gpio_get_value(unsigned int gpio, unsigned int *value) {
int fd, len;
char buf[MAX_BUF];
char ch;
// /sys/class/gpio/gpioN/value
len = snprintf(buf, sizeof(buf), StarF_Gpio_Dir "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1); // Read the external input level
if (ch != '0') { //'1' Input is high level
*value = 1;
} else { //'0' Input is Low level
*value = 0;
}
close(fd);
return 0;
}
int main(int argc, char **argv) {
unsigned int i;
unsigned int value1, value2;
printf("\t*************************************\n");
printf("\t******** GPIO_LED_BLINK ************\n");
printf("\t*************************************\n");
printf("Gpio begin to init\r\n");
StarF_gpio_export(FISCV_GPIO); // export gpio Gpio
StarF_gpio_set_dir(FISCV_GPIO, 1); // set as output
printf("Gpio init ok\r\n");
while (1) {
StarF_gpio_set_value(FISCV_GPIO, 1); // output high
printf("Gpio off\r\n");
usleep(500000); // delay
StarF_gpio_set_value(FISCV_GPIO, 0); // output low
printf("Gpio on\r\n");
usleep(500000); // delay
}
StarF_gpio_unexport(FISCV_GPIO); // unexport gpio Gpio
return 0;
}
-
Compile and Run the Program
$ gcc gpioLEDBlink.c -o gpioLEDBlink
$ ./gpioLEDBlink
-
Connect GPIO pin 40 to the positive terminal of the LED and link pin 6 (ground) to the LED’s negative terminal.
-
This program initiates LED blinking at a 0.5-second interval.