具体gpio编号需要根据系统产生gpio来确定,本例只以一个固定编号的gpio898来说明:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
//eg 根据ps内gpio来控制操作
int main()
{
int valuefd, exportfd, directionfd;
printf("GPIO test running...\n");
// The GPIO has to be exported to be able to see it
// in sysfs
exportfd = open("/sys/class/gpio/export", O_WRONLY);
if (exportfd < 0)
{
printf("Cannot open GPIO to export it\n");
exit(1);
}
write(exportfd, "898", 4);
close(exportfd);
printf("GPIO exported successfully\n");
// Update the direction of the GPIO to be an output
directionfd = open("/sys/class/gpio/gpio898/direction", O_RDWR);
if (directionfd < 0)
{
printf("Cannot open GPIO direction it\n");
exit(1);
}
write(directionfd, "out", 4);
close(directionfd);
printf("GPIO direction set as output successfully\n");
// Get the GPIO value ready to be toggled
valuefd = open("/sys/class/gpio/gpio898/value", O_RDWR);
if (valuefd < 0)
{
printf("Cannot open GPIO value\n");
exit(1);
}
printf("GPIO value opened, now toggling...\n");
// toggle the GPIO as fast a possible forever, a control c is needed
// to stop it
while (1)
{
write(valuefd,"1", 2);
sleep(1);
write(valuefd,"0", 2);
sleep(1);
}
return 0;
}