Skip to content

Commit

Permalink
Add error handling for GPIO in SPIDEV
Browse files Browse the repository at this point in the history
  • Loading branch information
TMRh20 committed Jan 14, 2024
1 parent 879432f commit 6a78ecf
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions utility/SPIDEV/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ 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");
}
close(fd);

#if !defined NEWGPIO


Expand Down Expand Up @@ -134,8 +136,16 @@ int GPIO::Gread(int port)
rq.flags = GPIOHANDLE_REQUEST_INPUT;
rq.lines = 1;
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq);
if(ret == -1){
throw GPIOException("Can't get line handle from IOCTL");
return ret;
}
close(fd);
ret = ioctl(rq.fd, GPIOHANDLE_GET_LINE_VALUES_IOCTL, &data);
if(ret == -1){
throw GPIOException("Can't get line value from IOCTL");
return ret;
}
close(rq.fd);
return ret;
}
Expand Down Expand Up @@ -189,13 +199,25 @@ void GPIO::Gwrite(int port, int value)
struct gpiohandle_data data;
int fd, ret;
fd = open(dev_name, O_RDONLY);
if(fd < 0){
throw GPIOException("Can't open dev");
return;
}
rq.lineoffsets[0] = port;
rq.flags = GPIOHANDLE_REQUEST_OUTPUT;
rq.lines = 1;
ret = ioctl(fd, GPIO_GET_LINEHANDLE_IOCTL, &rq);
if(ret == -1){
throw GPIOException("Can't get line handle from IOCTL");
return;
}
close(fd);
data.values[0] = value;
ret = ioctl(rq.fd, GPIOHANDLE_SET_LINE_VALUES_IOCTL, &data);
if(ret == -1){
throw GPIOException("Can't set line value from IOCTL");
return;
}
close(rq.fd);

#if !defined NEWGPIO
Expand Down

0 comments on commit 6a78ecf

Please sign in to comment.