From edab1cd0f0fdc7be3e0e379791d433f2a213e217 Mon Sep 17 00:00:00 2001 From: TMRh20 Date: Tue, 13 Feb 2024 05:56:36 -0600 Subject: [PATCH] Minor update - Try using gpiochip4 then 0 if that fails to support RPi5 - Remove old GPIO code --- utility/SPIDEV/gpio.cpp | 168 +++------------------------------------- utility/SPIDEV/gpio.h | 3 +- 2 files changed, 10 insertions(+), 161 deletions(-) diff --git a/utility/SPIDEV/gpio.cpp b/utility/SPIDEV/gpio.cpp index 2d6f8707..b6bac03a 100644 --- a/utility/SPIDEV/gpio.cpp +++ b/utility/SPIDEV/gpio.cpp @@ -22,14 +22,7 @@ #include #include -const char* dev_name = "/dev/gpiochip0"; - -#define NEWGPIO - - -//std::map GPIO::cache; - - +char* dev_name = "/dev/gpiochip4"; GPIO::GPIO() { @@ -42,86 +35,23 @@ GPIO::~GPIO() int fd; void GPIO::Gopen(int port, int DDR) -{ - - +{ fd = open(dev_name, O_RDONLY); if (fd >= 0){ close(fd); }else{ - throw GPIOException("Can't open device"); - } - - #if !defined NEWGPIO - - - FILE* f; - f = fopen("/sys/class/gpio/export", "w"); - if (f == NULL) { - throw GPIOException("can't export GPIO pin .check access rights"); - } - fprintf(f, "%d\n", port); - fclose(f); - - int counter = 0; - char file[128]; - sprintf(file, "/sys/class/gpio/gpio%d/direction", port); - - while ((f = fopen(file, "w")) == NULL) { //Wait 10 seconds for the file to be accessible if not open on first attempt - sleep(1); - counter++; - if (counter > 10) { - throw GPIOException("can't access /sys/class/gpio/gpio%d/direction GPIO pin. check access rights"); - /*perror("Could not open /sys/class/gpio/gpio%d/direction"); - exit(0); */ - } - } - int l = (DDR == 0) ? fprintf(f, "in\n") : fprintf(f, "out\n"); - if (!(l == 3 || l == 4)) { - fclose(f); - throw GPIOException("can't set direction on GPIO pin. check access rights"); - } - /* - if (DDR == 0) - fprintf(f, "in\n"); - else - printf(f, "out\n"); - */ - fclose(f); - - // Caches the GPIO descriptor; - sprintf(file, "/sys/class/gpio/gpio%d/value", port); - int flags = (DDR == 0) ? O_RDONLY : O_WRONLY; - int fd = ::open(file, flags); - if (fd < 0) { - throw GPIOException("Can't open the GPIO"); - } - else { - cache[port] = fd; // cache the fd; - lseek(fd, SEEK_SET, 0); + dev_name = "/dev/gpiochip0"; + fd = open(dev_name, O_RDONLY); + if (fd >= 0){ + close(fd); + }else{ + throw GPIOException("can't open /dev/gpiochip"); } - #endif - + } } void GPIO::Gclose(int port) { - close(fd); - #if !defined NEWGPIO - std::map::iterator i; - i = cache.find(port); - if (i != cache.end()) { - close(i->second); // close the cached fd - cache.erase(i); // Delete cache entry - } - // Do unexport - FILE* f; - f = fopen("/sys/class/gpio/unexport", "w"); - if (f != NULL) { - fprintf(f, "%d\n", port); - fclose(f); - } - #endif } int GPIO::Gread(int port) @@ -151,45 +81,6 @@ int GPIO::Gread(int port) } return -1; - #if !defined NEWGPIO - std::map::iterator i; - int fd; - i = cache.find(port); - if (i == cache.end()) { // Fallback to open the gpio - GPIO::open(port, GPIO::DIRECTION_IN); - i = cache.find(port); - if (i == cache.end()) { - throw GPIOException("can't access to GPIO"); - } - else { - fd = i->second; - } - } - else { - fd = i->second; - } - - char c; - if (lseek(fd, 0, SEEK_SET) == 0 && ::read(fd, &c, 1) == 1) { - return (c == '0') ? 0 : 1; - } - else { - throw GPIOException("can't access to GPIO"); - } - - /* - FILE *f; - - char file[128]; - sprintf(file, "/sys/class/gpio/gpio%d/value", port); - f = fopen(file, "r"); - - int i; - fscanf(f, "%d", &i); - fclose(f); - return i; - */ - #endif } void GPIO::Gwrite(int port, int value) @@ -219,46 +110,5 @@ void GPIO::Gwrite(int port, int value) return; } close(rq.fd); - - #if !defined NEWGPIO - std::map::iterator i; - int fd; - i = cache.find(port); - if (i == cache.end()) { // Fallback to open the gpio - GPIO::open(port, GPIO::DIRECTION_OUT); - i = cache.find(port); - if (i == cache.end()) { - throw GPIOException("can't access to GPIO"); - } - else { - fd = i->second; - } - } - else { - fd = i->second; - } - - if (lseek(fd, 0, SEEK_SET) != 0) { - throw GPIOException("can't access to GPIO"); - } - int l = (value == 0) ? ::write(fd, "0\n", 2) : ::write(fd, "1\n", 2); - if (l != 2) { - throw GPIOException("can't access to GPIO"); - } - - /* - FILE *f; - - char file[128]; - sprintf(file, "/sys/class/gpio/gpio%d/value", port); - f = fopen(file, "w"); - - if (value == 0) - fprintf(f, "0\n"); - else - fprintf(f, "1\n"); - fclose(f); - */ - #endif } diff --git a/utility/SPIDEV/gpio.h b/utility/SPIDEV/gpio.h index aa7cd629..6b7f5809 100644 --- a/utility/SPIDEV/gpio.h +++ b/utility/SPIDEV/gpio.h @@ -53,8 +53,7 @@ class GPIO virtual ~GPIO(); private: - /* fd cache */ - //static std::map cache; + }; #endif // RF24_UTILITY_SPIDEV_GPIO_H_