Commit 874fee84 authored by TMRh20's avatar TMRh20 Committed by mz-fuzzy

SPIDEV fix non-root user segfault after startup

First attempt to open   /sys/class/gpio/gpio<GPIO NUMBER>/direction
fails when running as non-root user. Added loop w/delay to retry for up
to 10 seconds by default.
parent 87f3d110
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
*/ */
#include "gpio.h" #include "gpio.h"
#include <stdlib.h>
#include <unistd.h>
GPIO::GPIO() { GPIO::GPIO() {
} }
...@@ -24,12 +26,24 @@ void GPIO::open(int port, int DDR) ...@@ -24,12 +26,24 @@ void GPIO::open(int port, int DDR)
fprintf(f, "%d\n", port); fprintf(f, "%d\n", port);
fclose(f); fclose(f);
char file[128]; int counter = 0;
sprintf(file, "/sys/class/gpio/gpio%d/direction", port); char file[128];
f = fopen(file, "w"); sprintf(file, "/sys/class/gpio/gpio%d/direction", port);
if (DDR == 0) fprintf(f, "in\n");
else fprintf(f, "out\n"); while( ( f = fopen(file,"w")) == NULL ){ //Wait 10 seconds for the file to be accessible if not open on first attempt
fclose(f); sleep(1);
counter++;
if(counter > 10){
perror("Could not open /sys/class/gpio/gpio%d/direction");
exit(0);
}
}
if (DDR == 0)
fprintf(f, "in\n");
else fprintf(f, "out\n");
fclose(f);
} }
void GPIO::close(int port) void GPIO::close(int port)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment