Skip to content

Commit

Permalink
Testing GPIO
Browse files Browse the repository at this point in the history
Very rudimentary changes for testing #932
  • Loading branch information
TMRh20 committed Jan 11, 2024
1 parent 638095e commit 879432f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 14 deletions.
4 changes: 2 additions & 2 deletions utility/SPIDEV/RF24_arch_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ typedef uint16_t rf24_gpio_pin_t;
#define HIGH GPIO::OUTPUT_HIGH
#define INPUT GPIO::DIRECTION_IN
#define OUTPUT GPIO::DIRECTION_OUT
#define digitalWrite(pin, value) GPIO::write(pin, value)
#define pinMode(pin, direction) GPIO::open(pin, direction)
#define digitalWrite(pin, value) GPIO::Gwrite(pin, value)
#define pinMode(pin, direction) GPIO::Gopen(pin, direction)
#define delay(milisec) __msleep(milisec)
#define delayMicroseconds(usec) __usleep(usec)
#define millis() __millis()
Expand Down
76 changes: 70 additions & 6 deletions utility/SPIDEV/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@
*
* Patched for filedescriptor catching and error control by L Diaz 2018
*/

#include <linux/gpio.h>
#include "gpio.h"
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <getopt.h>

const char* dev_name = "/dev/gpiochip0";

#define NEWGPIO


//std::map<int, GPIOfdCache_t> GPIO::cache;


std::map<int, GPIOfdCache_t> GPIO::cache;

GPIO::GPIO()
{
Expand All @@ -27,8 +39,20 @@ GPIO::~GPIO()
{
}

void GPIO::open(int port, int DDR)
int fd;

void GPIO::Gopen(int port, int DDR)
{


fd = open(dev_name, O_RDONLY);
if (fd >= 0){

}
close(fd);
#if !defined NEWGPIO


FILE* f;
f = fopen("/sys/class/gpio/export", "w");
if (f == NULL) {
Expand Down Expand Up @@ -74,10 +98,14 @@ void GPIO::open(int port, int DDR)
cache[port] = fd; // cache the fd;
lseek(fd, SEEK_SET, 0);
}
#endif

}

void GPIO::close(int port)
void GPIO::Gclose(int port)
{
close(fd);
#if !defined NEWGPIO
std::map<int, GPIOfdCache_t>::iterator i;
i = cache.find(port);
if (i != cache.end()) {
Expand All @@ -91,10 +119,29 @@ void GPIO::close(int port)
fprintf(f, "%d\n", port);
fclose(f);
}
#endif
}

int GPIO::read(int port)
int GPIO::Gread(int port)
{

struct gpiohandle_request rq;
struct gpiohandle_data data;
int fd, ret;
fd = open(dev_name, O_RDONLY);
if (fd >= 0){
rq.lineoffsets[0] = port;
rq.flags = GPIOHANDLE_REQUEST_INPUT;
rq.lines = 1;
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq);
close(fd);
ret = ioctl(rq.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
close(rq.fd);
return ret;
}
return -1;

#if !defined NEWGPIO
std::map<int, GPIOfdCache_t>::iterator i;
int fd;
i = cache.find(port);
Expand Down Expand Up @@ -132,10 +179,26 @@ int GPIO::read(int port)
fclose(f);
return i;
*/
#endif
}

void GPIO::write(int port, int value)
void GPIO::Gwrite(int port, int value)
{

struct gpiohandle_request rq;
struct gpiohandle_data data;
int fd, ret;
fd = open(dev_name, O_RDONLY);
rq.lineoffsets[0] = port;
rq.flags = GPIOHANDLE_REQUEST_OUTPUT;
rq.lines = 1;
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq);
close(fd);
data.values[0] = value;
ret = ioctl(rq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
close(rq.fd);

#if !defined NEWGPIO
std::map<int, GPIOfdCache_t>::iterator i;
int fd;
i = cache.find(port);
Expand Down Expand Up @@ -175,4 +238,5 @@ void GPIO::write(int port, int value)
fclose(f);
*/
#endif
}
13 changes: 7 additions & 6 deletions utility/SPIDEV/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <map>
#include <stdexcept>


/** Specific excpetion for SPI errors */
class GPIOException : public std::runtime_error
{
Expand All @@ -41,19 +42,19 @@ class GPIO

GPIO();

static void open(int port, int DDR);
static void Gopen(int port, int DDR);

static void close(int port);
static void Gclose(int port);

static int read(int port);
static int Gread(int port);

static void write(int port, int value);
static void Gwrite(int port, int value);

virtual ~GPIO();

private:
/* fd cache */
static std::map<int, GPIOfdCache_t> cache;
//static std::map<int, GPIOfdCache_t> cache;
};

#endif // RF24_UTILITY_SPIDEV_GPIO_H_

0 comments on commit 879432f

Please sign in to comment.