cm3588-gpio-led-blink.ino
C example using sysfs interface to control a GPIO on CM3588.
Hardware: LED with resistor connected to a GPIO exposed via 100-pin connectors. Linux kernel must have GPIO sysfs support enabled. Compile with gcc blink.c -o blink. Run as root: sudo ./blink.
cm3588-gpio-led-blink.ino
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define GPIO_PIN "gpio100"
#define DIRECTION_PATH "/sys/class/gpio/%s/direction"
#define VALUE_PATH "/sys/class/gpio/%s/value"
#define EXPORT_PATH "/sys/class/gpio/export"
#define UNEXPORT_PATH "/sys/class/gpio/unexport"
void write_to_file(const char *path, const char *value) {
int fd = open(path, O_WRONLY);
if (fd < 0) {
perror("Failed to open file");
exit(EXIT_FAILURE);
}
if (write(fd, value, strlen(value)) < 0) {
perror("Failed to write");
close(fd);
exit(EXIT_FAILURE);
}
close(fd);
}
int main() {
// Export GPIO
write_to_file(EXPORT_PATH, GPIO_PIN);
sleep(1);
// Set direction to output
char dir_path[256], val_path[256];
snprintf(dir_path, sizeof(dir_path), DIRECTION_PATH, GPIO_PIN);
snprintf(val_path, sizeof(val_path), VALUE_PATH, GPIO_PIN);
write_to_file(dir_path, "out");
// Blink loop
for (int i = 0; i < 10; i++) {
write_to_file(val_path, "1");
usleep(500000);
write_to_file(val_path, "0");
usleep(500000);
}
// Unexport GPIO
write_to_file(UNEXPORT_PATH, GPIO_PIN);
return 0;
}