Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This seems to work if run with "picoc mainprog.c z2-gpio.c" */
- /* It does not run if mainprog.c is listed after z2-gpio.c. */
- /* And the main function must be defined as below (or with no args). */
- /* int main(int argc, char ** argv) */
- /*
- * GPIO user space helpers
- *
- * Copyright 2009 Analog Devices Inc.
- * Michael Hennerich (hennerich@blackfin.uclinux.org)
- *
- * Licensed under the GPL-2 or later
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <unistd.h>
- #define GPIO_DIR_IN 0
- #define GPIO_DIR_OUT 1
- int gpio_export(unsigned gpio)
- {
- FILE *fp = fopen("/sys/class/gpio/export", "w");
- if (fp == NULL) {
- perror("gpio/export");
- return -1;
- }
- fprintf(fp, "%i", gpio);
- fclose(fp);
- return 0;
- }
- int gpio_unexport(unsigned gpio)
- {
- FILE *fp = fopen("/sys/class/gpio/export", "w");
- if (fp == NULL) {
- perror("gpio/unexport");
- return -1;
- }
- fprintf(fp, "%i", gpio);
- fclose(fp);
- return 0;
- }
- int gpio_dir(unsigned gpio, unsigned dir)
- {
- FILE *fp;
- char buf[60];
- snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
- fp = fopen(buf, "w");
- if (fp == NULL) {
- perror("gpio/direction");
- return -1;
- }
- if (dir == GPIO_DIR_OUT)
- fprintf(fp, "out");
- else
- fprintf(fp, "in");
- fclose(fp);
- return 0;
- }
- int gpio_dir_out(unsigned gpio)
- {
- return gpio_dir(gpio, GPIO_DIR_OUT);
- }
- int gpio_dir_in(unsigned gpio)
- {
- return gpio_dir(gpio, GPIO_DIR_IN);
- }
- int set_gpio_value(unsigned gpio, unsigned value)
- {
- FILE *fp;
- char buf[60];
- snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
- fp = fopen(buf, "w");
- if (fp == NULL) {
- perror("gpio/value");
- return -1;
- }
- if (value)
- fprintf(fp, "1");
- else
- fprintf(fp, "0");
- fclose(fp);
- return 0;
- }
- int get_gpio_value(unsigned gpio)
- {
- FILE *fp;
- int value = 0;
- char buf[60];
- snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
- fp = fopen(buf, "r");
- if (fp == NULL) {
- perror("gpio/value");
- return -1;
- }
- fscanf(fp, "%d", &value);
- fclose(fp);
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement