This commit is contained in:
mstroh76
2024-01-27 16:33:04 +01:00
parent 6f9b9ecf5c
commit 19874871de
2 changed files with 99 additions and 34 deletions

View File

@@ -88,6 +88,15 @@ char *usage = "Usage: gpio -v\n"
" gpio gbw <channel> <value>" ; // No trailing newline needed here. " gpio gbw <channel> <value>" ; // No trailing newline needed here.
int GPIOToSysFS_ExitonFail (const int pin) {
int pinFS = GPIOToSysFS(pin);
if (pinFS<0) {
fprintf (stderr, "%s: invalid sysfs pin of bcm pin %d\n", argv [0], pin) ;
exit (1) ;
}
return pinFS;
}
#ifdef NOT_FOR_NOW #ifdef NOT_FOR_NOW
/* /*
* decodePin: * decodePin:
@@ -391,16 +400,19 @@ static void doI2Cdetect (UNU int argc, char *argv [])
static void doExports (UNU int argc, UNU char *argv []) static void doExports (UNU int argc, UNU char *argv [])
{ {
int fd ; int fd ;
int i, l, first ; int pin, l, first ;
char fName [128] ; char fName [128] ;
char buf [16] ; char buf [16] ;
for (first = 0, i = 0 ; i < 64 ; ++i) // Crude, but effective for (first = 0, pin = 0 ; pin < 64 ; ++pin) // Crude, but effective
{ {
// Try to read the direction // Try to read the direction
int pinFS = GPIOToSysFS(pin);
sprintf (fName, "/sys/class/gpio/gpio%d/direction", i) ; if (pinFS<0) {
continue;
}
sprintf (fName, "/sys/class/gpio/gpio%d/direction", pinFS) ;
if ((fd = open (fName, O_RDONLY)) == -1) if ((fd = open (fName, O_RDONLY)) == -1)
continue ; continue ;
@@ -410,7 +422,11 @@ static void doExports (UNU int argc, UNU char *argv [])
printf ("GPIO Pins exported:\n") ; printf ("GPIO Pins exported:\n") ;
} }
printf ("%4d: ", i) ; if(pinFS==pin) {
printf ("%4d: ", pin) ;
} else {
printf ("%4d (%4d): ", pin, pinFS) ;
}
if ((l = read (fd, buf, 16)) == 0) if ((l = read (fd, buf, 16)) == 0)
sprintf (buf, "%s", "?") ; sprintf (buf, "%s", "?") ;
@@ -425,7 +441,7 @@ static void doExports (UNU int argc, UNU char *argv [])
// Try to Read the value // Try to Read the value
sprintf (fName, "/sys/class/gpio/gpio%d/value", i) ; sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
if ((fd = open (fName, O_RDONLY)) == -1) if ((fd = open (fName, O_RDONLY)) == -1)
{ {
printf ("No Value file (huh?)\n") ; printf ("No Value file (huh?)\n") ;
@@ -443,7 +459,7 @@ static void doExports (UNU int argc, UNU char *argv [])
// Read any edge trigger file // Read any edge trigger file
sprintf (fName, "/sys/class/gpio/gpio%d/edge", i) ; sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
if ((fd = open (fName, O_RDONLY)) == -1) if ((fd = open (fName, O_RDONLY)) == -1)
{ {
printf ("\n") ; printf ("\n") ;
@@ -485,7 +501,7 @@ void doExport (int argc, char *argv [])
} }
pin = atoi (argv [2]) ; pin = atoi (argv [2]) ;
int pinFS = GPIOToSysFS_ExitonFail(pin);
mode = argv [3] ; mode = argv [3] ;
if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL) if ((fd = fopen ("/sys/class/gpio/export", "w")) == NULL)
@@ -494,10 +510,9 @@ void doExport (int argc, char *argv [])
exit (1) ; exit (1) ;
} }
fprintf (fd, "%d\n", pin) ; fprintf (fd, "%d\n", pinFS) ;
fclose (fd) ; fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/direction", pinFS) ;
sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ;
if ((fd = fopen (fName, "w")) == NULL) if ((fd = fopen (fName, "w")) == NULL)
{ {
fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ; fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
@@ -522,10 +537,10 @@ void doExport (int argc, char *argv [])
// Change ownership so the current user can actually use it // Change ownership so the current user can actually use it
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
changeOwner (argv [0], fName) ; changeOwner (argv [0], fName) ;
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ; sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
changeOwner (argv [0], fName) ; changeOwner (argv [0], fName) ;
} }
@@ -599,6 +614,7 @@ void doEdge (int argc, char *argv [])
} }
pin = atoi (argv [2]) ; pin = atoi (argv [2]) ;
int pinFS = GPIOToSysFS_ExitonFail(pin);
mode = argv [3] ; mode = argv [3] ;
// Export the pin and set direction to input // Export the pin and set direction to input
@@ -609,10 +625,10 @@ void doEdge (int argc, char *argv [])
exit (1) ; exit (1) ;
} }
fprintf (fd, "%d\n", pin) ; fprintf (fd, "%d\n", pinFS) ;
fclose (fd) ; fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/direction", pin) ; sprintf (fName, "/sys/class/gpio/gpio%d/direction", pinFS) ;
if ((fd = fopen (fName, "w")) == NULL) if ((fd = fopen (fName, "w")) == NULL)
{ {
fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ; fprintf (stderr, "%s: Unable to open GPIO direction interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
@@ -622,7 +638,7 @@ void doEdge (int argc, char *argv [])
fprintf (fd, "in\n") ; fprintf (fd, "in\n") ;
fclose (fd) ; fclose (fd) ;
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ; sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
if ((fd = fopen (fName, "w")) == NULL) if ((fd = fopen (fName, "w")) == NULL)
{ {
fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ; fprintf (stderr, "%s: Unable to open GPIO edge interface for pin %d: %s\n", argv [0], pin, strerror (errno)) ;
@@ -641,10 +657,10 @@ void doEdge (int argc, char *argv [])
// Change ownership of the value and edge files, so the current user can actually use it! // Change ownership of the value and edge files, so the current user can actually use it!
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
changeOwner (argv [0], fName) ; changeOwner (argv [0], fName) ;
sprintf (fName, "/sys/class/gpio/gpio%d/edge", pin) ; sprintf (fName, "/sys/class/gpio/gpio%d/edge", pinFS) ;
changeOwner (argv [0], fName) ; changeOwner (argv [0], fName) ;
fclose (fd) ; fclose (fd) ;
@@ -670,6 +686,7 @@ void doUnexport (int argc, char *argv [])
} }
pin = atoi (argv [2]) ; pin = atoi (argv [2]) ;
int pinFS = GPIOToSysFS_ExitonFail(pin);
if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL) if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
{ {
@@ -677,7 +694,7 @@ void doUnexport (int argc, char *argv [])
exit (1) ; exit (1) ;
} }
fprintf (fd, "%d\n", pin) ; fprintf (fd, "%d\n", pinFS) ;
fclose (fd) ; fclose (fd) ;
} }
@@ -697,13 +714,16 @@ void doUnexportall (char *progName)
for (pin = 0 ; pin < 63 ; ++pin) for (pin = 0 ; pin < 63 ; ++pin)
{ {
if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL) int pinFS = GPIOToSysFS(pin);
{ if (pinFS>=0) {
fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ; if ((fd = fopen ("/sys/class/gpio/unexport", "w")) == NULL)
exit (1) ; {
fprintf (stderr, "%s: Unable to open GPIO export interface\n", progName) ;
exit (1) ;
}
fprintf (fd, "%d\n", pinFS) ;
fclose (fd) ;
} }
fprintf (fd, "%d\n", pin) ;
fclose (fd) ;
} }
} }

View File

@@ -464,8 +464,54 @@ static int physToGpioR2 [64] =
-1, -1, -1, -1,
} ; } ;
const int _5v=-1;
const int _0v=-1;
const int _3v=-1;
static int physToSysGPIOPi5 [41] =
{
-1, // 0
_3v, _5v, // 1, 2
401, _5v,
402, _0v,
403, 413,
_0v, 414,
416, 417,
426, _0v,
421, 422,
_3v, 423,
409, _0v,
408, 424,
410, 407,
_0v, 406,
399, 400,
404, _0v,
405, 411,
412, _0v,
418, 415,
425, 419,
_0v, 420, //39, 40
} ;
static int GPIOToSysFS(const int pin) {
int sysfspin = pin;
if (RaspberryPiModel<0) { //need to detect pi model
int model, rev, mem, maker, overVolted ;
piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
}
if (PI_MODEL_5 == RaspberryPiModel) {
sysfspin = pin + 399;
if (pin>426) { // only 399-426 supported
sysfspin = -1;
}
}
if (wiringPiDebug)
printf ("GPIOToSysFS: translate bcm gpio %d to sysfs gpio %d\n", pin, sysfspin) ;
return sysfspin;
}
// gpioToGPFSEL: // gpioToGPFSEL:
// Map a BCM_GPIO pin to it's Function Selection // Map a BCM_GPIO pin to it's Function Selection
// control port. (GPFSEL 0-5) // control port. (GPFSEL 0-5)
@@ -2075,8 +2121,9 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
if (sysFds [bcmGpioPin] == -1) if (sysFds [bcmGpioPin] == -1)
{ {
sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ; pinFS = GPIOToSysFS(bcmGpioPin);
if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0) sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
if (pinFS>=0 && (sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ; return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
} }
@@ -2525,18 +2572,16 @@ int wiringPiSetupSys (void)
physToGpio = physToGpioR2 ; physToGpio = physToGpioR2 ;
} }
if (PI_MODEL_5 == model) {
return wiringPiFailure (WPI_ALMOST, "wiringPiSetup: Raspberry Pi 5 not supported.\n"
" Unable to continue. Keep an eye of new version at https://github.com/GrazerComputerClub/WiringPi\n") ;
}
// Open and scan the directory, looking for exported GPIOs, and pre-open // Open and scan the directory, looking for exported GPIOs, and pre-open
// the 'value' interface to speed things up for later // the 'value' interface to speed things up for later
for (pin = 0 ; pin < 64 ; ++pin) for (pin = 0 ; pin < 64 ; ++pin)
{ {
sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ; pinFS = GPIOToSysFS(pin);
sysFds [pin] = open (fName, O_RDWR) ; if (pinFS>=0) {
sprintf (fName, "/sys/class/gpio/gpio%d/value", pinFS) ;
sysFds [pin] = open (fName, O_RDWR) ;
}
} }
initialiseEpoch () ; initialiseEpoch () ;