Page 5 of 14
Article Index
Page 70
#define __ARM_PCS_VFP
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
int main(int argc, char **argv)
{
int fd;
struct gpiochip_info info;
fd = open("/dev/gpiochip0", O_RDONLY);
int ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info);
close(fd);
printf("label: %s\n name: %s\n number of lines: %u\n", info.label, info.name, info.lines);
return 0;
}
If you run this on a Pi 5 change gpiochip0 to gpiochip4.
Page 73
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
int main(int argc, char **argv)
{
int fd, ret;
struct gpiohandle_request req;
req.lineoffsets[0] = 4;
req.lineoffsets[1] = 17;
req.flags = GPIOHANDLE_REQUEST_OUTPUT;
req.default_values[0] = 0;
req.default_values[1] = 0;
strcpy(req.consumer_label, "Output test");
req.lines = 2;
fd = open("/dev/gpiochip0", O_RDONLY);
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
close(fd);
struct gpiohandle_data data;
data.values[0] = 0;
data.values[1] = 1;
while (1)
{
data.values[0] = !data.values[0];
data.values[1] = !data.values[1];
ret = ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
}
return 0;
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 74
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/gpio.h>
int main(int argc, char **argv)
{
int fd, ret;
struct gpiohandle_request req;
req.lineoffsets[0] = 4;
req.lineoffsets[1] = 17;
req.flags = GPIOHANDLE_REQUEST_INPUT;
strcpy(req.consumer_label, "Input test");
req.lines = 2;
fd = open("/dev/gpiochip0", O_RDONLY);
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
close(fd);
struct gpiohandle_data data;
ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
printf("%hhu , %hhu", data.values[0], data.values[1]);
close(req.fd);
return 0;
}
Remember to change gpiochip0 to gpiochip4 if you are running on a Pi 5.
Page 75
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/gpio.h>
#include <sys/ioctl.h>
#include <time.h>
int main(int argc, char **argv)
{
int fd, ret;
struct timespec delay = {0, 10 * 1000 * 1000};
struct timespec time1, time2;
fd = open("/dev/gpiochip0", O_RDONLY);
struct gpiohandle_request req;
req.lineoffsets[0] = 4;
strcpy(req.consumer_label, "RC Measure");
req.lines = 1;
req.flags = GPIOHANDLE_REQUEST_INPUT;
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
nanosleep(&delay, NULL);
close(req.fd);
req.flags = GPIOHANDLE_REQUEST_OUTPUT;
struct gpiohandle_data data;
data.values[0] = 0;
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
ret = ioctl(req.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
nanosleep(&delay, NULL);
close(req.fd);
req.flags = GPIOHANDLE_REQUEST_INPUT;
clock_gettime(CLOCK_REALTIME, &time1);
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &req);
ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
while (data.values[0] == 0)
{
ret = ioctl(req.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data)
}
clock_gettime(CLOCK_REALTIME, &time2);
printf("Time=%d", (time2.tv_nsec - time1.tv_nsec) / 1000);
return 0;
}