Gpio_test shows the only supported way of working with GPIOs:
/usr/local/DigiEL-5.5/apps/gpio_test_c/gpio_test.c
/* Check if gpio kernel module is loaded */
if (!check_module_loaded()) {
printf("[ERROR] gpio driver not loaded, please run 'modprobe gpio'
");
exit(EXIT_FAILURE);
}
snprintf(dev_button, sizeof(dev_button) - 1, "/dev/gpio/%d",
plat_list[platform].gpio_in);
if ((fd_button = open(dev_button, O_RDWR)) < 0) {
printf("[ERROR] open (%s): %s
", dev_button, strerror(errno));
exit(EXIT_FAILURE);
}
snprintf(dev_led, sizeof(dev_led) - 1, "/dev/gpio/%d", plat_list[platform].gpio_out);
if ((fd_led = open(dev_led, O_RDWR)) < 0) {
printf("[ERROR] open (%s): %s
", dev_led, strerror(errno));
close(fd_button);
exit(EXIT_FAILURE);
}
if (GPIO_UNDEFINED != plat_list[platform].pullup_in) {
snprintf(dev_pullup, sizeof(dev_led) - 1, "/dev/gpio/%d",
plat_list[platform].pullup_in);
if ((fd_pullup = open(dev_pullup, O_RDWR)) < 0) {
printf("[ERROR] open (%s): %s
", dev_pullup, strerror(errno));
close(fd_led);
close(fd_button);
exit(EXIT_FAILURE);
}
}
fprintf(stdout, "Configuring %s as input and %s as output
", dev_button, dev_led);
if ((ret_val = ioctl(fd_button, GPIO_CONFIG_AS_INP)) < 0) {
printf("[ERROR] ioctl (button): %s
", strerror(errno));
goto err_close;
}
if ((ret_val = ioctl(fd_led, GPIO_CONFIG_AS_OUT)) < 0) {
printf("[ERROR] ioctl (led): %s
", strerror(errno));
goto err_close;
}
/* Initialize outval with the current led value */
if ((ret_val = ioctl(fd_led, GPIO_READ_PIN_VAL, &outval)) < 0) {
printf("[ERROR] ioctl (led): %s
", strerror(errno));
goto err_close;
}
fprintf(stdout, "Using ioctl system call to control the GPIOs
"
"Press the button %s 10 times and check the led %s
",
plat_list[platform].button, plat_list[platform].led);
loops = 10;
while (loops) {
do {
lastinval = inval;
if ((ret_val = ioctl(fd_button, GPIO_READ_PIN_VAL, &inval)) < 0) {
printf("[ERROR] ioctl (button): %s
", strerror(errno));
goto err_close;
}
usleep(1000);
} while (!((lastinval == 1) && (inval == 0)));
fprintf(stdout, "%s pressed
", plat_list[platform].button);
outval = outval ? 0 : 1;
if ((ret_val = ioctl(fd_led, GPIO_WRITE_PIN_VAL, &outval)) < 0) {
printf("[ERROR] ioctl (led): %s
", strerror(errno));
goto err_close;
}
loops–;
}
fprintf(stdout, "
Using read/write system calls to control the GPIOs
"
"Press the button %s 10 times and check the led %s
",
plat_list[platform].button, plat_list[platform].led);
loops = 10;
while (loops) {
do {
lastinval = inval;
if ((ret_val =
read(fd_button, (char *)&inval, sizeof(char))) != sizeof(char)) {
printf("[ERROR] read (button): %s
", strerror(errno));
goto err_close;
}
usleep(1000);
} while (!((lastinval == 1) && (inval == 0)));
fprintf(stdout, "%s pressed
", plat_list[platform].button);
outval = outval ? 0 : 1;
if ((ret_val = write(fd_led, (char *)&outval, sizeof(char))) != sizeof(char)) {
printf("[ERROR] write (led): %s
", strerror(errno));
goto err_close;
}
loops–;
}