andrewm@0: /* andrewm@0: * SimpleGPIO.cpp andrewm@0: * andrewm@0: * Modifications by Derek Molloy, School of Electronic Engineering, DCU andrewm@0: * www.derekmolloy.ie andrewm@0: * Almost entirely based on Software by RidgeRun: andrewm@0: * andrewm@0: * Copyright (c) 2011, RidgeRun andrewm@0: * All rights reserved. andrewm@0: * andrewm@0: * Redistribution and use in source and binary forms, with or without andrewm@0: * modification, are permitted provided that the following conditions are met: andrewm@0: * 1. Redistributions of source code must retain the above copyright andrewm@0: * notice, this list of conditions and the following disclaimer. andrewm@0: * 2. Redistributions in binary form must reproduce the above copyright andrewm@0: * notice, this list of conditions and the following disclaimer in the andrewm@0: * documentation and/or other materials provided with the distribution. andrewm@0: * 3. All advertising materials mentioning features or use of this software andrewm@0: * must display the following acknowledgement: andrewm@0: * This product includes software developed by the RidgeRun. andrewm@0: * 4. Neither the name of the RidgeRun nor the andrewm@0: * names of its contributors may be used to endorse or promote products andrewm@0: * derived from this software without specific prior written permission. andrewm@0: * andrewm@0: * THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY andrewm@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED andrewm@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE andrewm@0: * DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY andrewm@0: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES andrewm@0: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; andrewm@0: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND andrewm@0: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT andrewm@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS andrewm@0: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. andrewm@0: */ andrewm@0: andrewm@0: #include "../include/GPIOcontrol.h" andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_setup andrewm@0: ****************************************************************/ andrewm@0: int gpio_setup(unsigned int gpio, int out_flag) andrewm@0: { andrewm@0: /* Export the GPIO pins and set their direction */ andrewm@0: if(gpio_export(gpio)) { andrewm@0: printf("Unable to export GPIO input pin\n"); andrewm@0: return -1; andrewm@0: } andrewm@0: if(gpio_set_dir(gpio, out_flag)) { andrewm@0: printf("Unable to set GPIO input direction\n"); andrewm@0: return -1; andrewm@0: } andrewm@0: andrewm@0: return gpio_fd_open(gpio, O_RDWR); andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_export andrewm@0: ****************************************************************/ andrewm@0: int gpio_export(unsigned int gpio) andrewm@0: { andrewm@0: int fd, len, result = 0; andrewm@0: char buf[MAX_BUF]; andrewm@0: andrewm@0: fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/export"); andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: len = snprintf(buf, sizeof(buf), "%d", gpio); andrewm@0: if(write(fd, buf, len) < 0) andrewm@0: result = -1; andrewm@0: close(fd); andrewm@0: andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_unexport andrewm@0: ****************************************************************/ andrewm@0: int gpio_unexport(unsigned int gpio) andrewm@0: { andrewm@0: int fd, len, result = 0; andrewm@0: char buf[MAX_BUF]; andrewm@0: andrewm@0: fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/export"); andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: len = snprintf(buf, sizeof(buf), "%d", gpio); andrewm@0: if(write(fd, buf, len) < 0) andrewm@0: result = -1; andrewm@0: close(fd); andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_set_dir andrewm@0: ****************************************************************/ andrewm@0: int gpio_set_dir(unsigned int gpio, int out_flag) andrewm@0: { andrewm@0: int fd, result = 0; andrewm@0: char buf[MAX_BUF]; andrewm@0: andrewm@0: snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio); andrewm@0: andrewm@0: fd = open(buf, O_WRONLY); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/direction"); andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: if (out_flag == OUTPUT_PIN) { andrewm@0: if(write(fd, "out", 4) < 0) andrewm@0: result = -1; andrewm@0: } andrewm@0: else { andrewm@0: if(write(fd, "in", 3) < 0) andrewm@0: result = -1; andrewm@0: } andrewm@0: andrewm@0: close(fd); andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_set_value andrewm@0: ****************************************************************/ andrewm@0: int gpio_set_value(unsigned int gpio, int value) andrewm@0: { andrewm@0: int fd, result = 0; andrewm@0: char buf[MAX_BUF]; andrewm@0: andrewm@0: snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); andrewm@0: andrewm@0: fd = open(buf, O_WRONLY); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/set-value"); andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: if (value==LOW) { andrewm@0: if(write(fd, "0", 2) < 0) andrewm@0: result = -1; andrewm@0: } andrewm@0: else { andrewm@0: if(write(fd, "1", 2) < 0) andrewm@0: result = -1; andrewm@0: } andrewm@0: andrewm@0: close(fd); andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_get_value andrewm@0: ****************************************************************/ andrewm@0: int gpio_get_value(unsigned int gpio, unsigned int *value) andrewm@0: { andrewm@0: int fd, result = 0; andrewm@0: char buf[MAX_BUF]; andrewm@0: char ch; andrewm@0: andrewm@0: snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); andrewm@0: andrewm@0: fd = open(buf, O_RDONLY); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/get-value"); andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: if(read(fd, &ch, 1) <= 0) { andrewm@0: result = -1; andrewm@0: } andrewm@0: else { andrewm@0: if (ch != '0') { andrewm@0: *value = 1; andrewm@0: } else { andrewm@0: *value = 0; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: close(fd); andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_set_edge andrewm@0: ****************************************************************/ andrewm@0: andrewm@0: int gpio_set_edge(unsigned int gpio, char *edge) andrewm@0: { andrewm@0: int fd, result = 0; andrewm@0: char buf[MAX_BUF]; andrewm@0: andrewm@0: snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio); andrewm@0: andrewm@0: fd = open(buf, O_WRONLY); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/set-edge"); andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: if(write(fd, edge, strlen(edge) + 1) < 0) andrewm@0: result = -1; andrewm@0: close(fd); andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_fd_open andrewm@0: ****************************************************************/ andrewm@0: andrewm@0: int gpio_fd_open(unsigned int gpio, int writeFlag) andrewm@0: { andrewm@0: int fd; andrewm@0: char buf[MAX_BUF]; andrewm@0: andrewm@0: snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio); andrewm@0: andrewm@0: fd = open(buf, writeFlag | O_NONBLOCK ); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/fd_open"); andrewm@0: } andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_fd_close andrewm@0: ****************************************************************/ andrewm@0: andrewm@0: int gpio_fd_close(int fd) andrewm@0: { andrewm@0: return close(fd); andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_read andrewm@0: ****************************************************************/ andrewm@0: int gpio_read(int fd, unsigned int *value) andrewm@0: { andrewm@0: int result = 0; andrewm@0: char ch; andrewm@0: andrewm@0: if(read(fd, &ch, 1) <= 0) { andrewm@0: result = -1; andrewm@0: } andrewm@0: else { andrewm@0: if (ch != '0') { andrewm@0: *value = 1; andrewm@0: } else { andrewm@0: *value = 0; andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_write andrewm@0: ****************************************************************/ andrewm@0: int gpio_write(int fd, int value) andrewm@0: { andrewm@0: int result = 0; andrewm@0: //char buf[MAX_BUF]; andrewm@0: andrewm@0: if (value==LOW) { andrewm@0: if(write(fd, "0", 2) < 0) andrewm@0: result = -1; andrewm@0: } andrewm@0: else { andrewm@0: if(write(fd, "1", 2) < 0) andrewm@0: result = -1; andrewm@0: } andrewm@0: andrewm@0: return result; andrewm@0: } andrewm@0: andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * gpio_dismiss andrewm@0: ****************************************************************/ andrewm@0: int gpio_dismiss(int fd, unsigned int gpio) andrewm@0: { andrewm@0: close(fd); andrewm@0: gpio_unexport(gpio); andrewm@0: return 0; andrewm@0: } andrewm@0: andrewm@0: /**************************************************************** andrewm@0: * led_set_trigger andrewm@0: ****************************************************************/ andrewm@0: int led_set_trigger(unsigned int lednum, const char *trigger) andrewm@0: { andrewm@0: // Set the trigger source for an onboard user LED andrewm@0: int fd, result = 0; andrewm@0: char buf[MAX_BUF]; andrewm@0: andrewm@0: snprintf(buf, sizeof(buf), SYSFS_LED_DIR "/beaglebone:green:usr%d/trigger", lednum); andrewm@0: andrewm@0: fd = open(buf, O_WRONLY); andrewm@0: if (fd < 0) { andrewm@0: perror("gpio/led-set-trigger"); andrewm@0: return fd; andrewm@0: } andrewm@0: andrewm@0: if(write(fd, trigger, strlen(trigger) + 1) < 0) andrewm@0: result = -1; andrewm@0: andrewm@0: close(fd); andrewm@0: return result; andrewm@0: } andrewm@0: