123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- /* Copyright (c) 2019 FunKey
- * All rights reserved.
- *
- */
- #include "gpio-utils.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- /****************************************************************
- * gpio_export
- ****************************************************************/
- int gpio_export(unsigned int gpio)
- {
- int fd, len;
- char buf[MAX_BUF];
-
- fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
- if (fd < 0) {
- perror("gpio/export");
- return fd;
- }
-
- len = snprintf(buf, sizeof(buf), "%d", gpio);
- write(fd, buf, len);
- close(fd);
-
- return 0;
- }
- /****************************************************************
- * gpio_unexport
- ****************************************************************/
- int gpio_unexport(unsigned int gpio)
- {
- int fd, len;
- char buf[MAX_BUF];
-
- fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
- if (fd < 0) {
- perror("gpio/export");
- return fd;
- }
-
- len = snprintf(buf, sizeof(buf), "%d", gpio);
- write(fd, buf, len);
- close(fd);
- return 0;
- }
- /****************************************************************
- * gpio_set_dir
- ****************************************************************/
- int gpio_set_dir(unsigned int gpio, const char* dir)
- {
- int fd, len;
- char buf[MAX_BUF];
-
- len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
-
- fd = open(buf, O_WRONLY);
- if (fd < 0) {
- perror("gpio/direction");
- return fd;
- }
- write(fd, dir, sizeof(dir)+1);
- close(fd);
- return 0;
- }
- /****************************************************************
- * gpio_set_value
- ****************************************************************/
- int gpio_set_value(unsigned int gpio, unsigned int value)
- {
- int fd, len;
- char buf[MAX_BUF];
-
- len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
-
- fd = open(buf, O_WRONLY);
- if (fd < 0) {
- perror("gpio/set-value");
- return fd;
- }
-
- if (value)
- write(fd, "1", 2);
- else
- write(fd, "0", 2);
-
- close(fd);
- return 0;
- }
- /****************************************************************
- * gpio_get_value
- ****************************************************************/
- int gpio_get_value(unsigned int gpio, unsigned int *value)
- {
- int fd, len;
- char buf[MAX_BUF];
- char ch;
- len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
-
- fd = open(buf, O_RDONLY);
- if (fd < 0) {
- perror("gpio/get-value");
- return fd;
- }
-
- read(fd, &ch, 1);
- if (ch != '0') {
- *value = 1;
- } else {
- *value = 0;
- }
-
- close(fd);
- return 0;
- }
- /****************************************************************
- * gpio_set_edge
- ****************************************************************/
- int gpio_set_edge(unsigned int gpio, const char *edge)
- {
- int fd, len;
- char buf[MAX_BUF];
- len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
-
- fd = open(buf, O_WRONLY);
- if (fd < 0) {
- perror("gpio/set-edge");
- return fd;
- }
-
- write(fd, edge, strlen(edge) + 1);
- close(fd);
- return 0;
- }
- /****************************************************************
- * gpio_fd_open
- ****************************************************************/
- int gpio_fd_open(unsigned int gpio, unsigned int dir)
- {
- int fd, len;
- char buf[MAX_BUF];
- len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
-
- fd = open(buf, dir | O_NONBLOCK );
- if (fd < 0) {
- perror("gpio/fd_open");
- }
- return fd;
- }
- /****************************************************************
- * gpio_fd_close
- ****************************************************************/
- int gpio_fd_close(int fd)
- {
- return close(fd);
- }
- /****************************************************************
- * ain_get_value (from Mike McDonald)
- * https://groups.google.com/forum/#!topic/beagleboard-ece497/SLJ5nQQ_GoU
- ****************************************************************/
- int ain_get_value(unsigned int ain, unsigned int *value)
- {
- int fd, len, bytesRead;
- char buf[MAX_BUF];
- char adc_in[ADC_BUF];
- len = snprintf(buf, sizeof(buf), SYSFS_AIN_DIR "/AIN%d", ain);
-
- fd = open(buf, O_RDONLY);
- if (fd < 0) {
- perror(buf);
- return fd;
- }
-
- // Read from the
- bytesRead = read(fd, adc_in, ADC_BUF);
- // Turn the buffer value (a string) into an integer
- if (bytesRead != -1) {
- *value = atoi(adc_in);
- adc_in[bytesRead] = (int)NULL;
- lseek(fd, 0, SEEK_SET);
- }
- // Sleep for a little to ensure that we get good ADC values
- usleep(1000);
-
- close(fd);
- return bytesRead;
- }
|