@@ -1658,19 +1658,62 @@ void pinEnableED01Pi (int pin)
16581658}
16591659#endif
16601660
1661+ #define ZeroMemory (Destination ,Length ) memset((Destination),0,(Length))
16611662
1662- const char DEV_GPIO_PI [] = "/dev/gpiochip0" ;
1663- const char DEV_GPIO_PI5 []= "/dev/gpiochip4" ;
1663+
1664+ int OpenAndCheckGpioChip (int GPIONo , const char * label , const unsigned int lines ) {
1665+ char szGPIOChip [30 ];
1666+
1667+ sprintf (szGPIOChip , "/dev/gpiochip%d" , GPIONo );
1668+ int Fd = open (szGPIOChip , O_RDWR );
1669+ if (Fd < 0 ) {
1670+ fprintf (stderr , "wiringPi: ERROR: %s open ret=%d\n" , szGPIOChip , Fd );
1671+ return Fd ;
1672+ } else {
1673+ if (wiringPiDebug ) {
1674+ printf ("wiringPi: Open chip %s succeded, fd=%d\n" , szGPIOChip , Fd ) ;
1675+ }
1676+ struct gpiochip_info chipinfo ;
1677+ ZeroMemory (& chipinfo , sizeof (chipinfo ));
1678+ int ret = ioctl (Fd , GPIO_GET_CHIPINFO_IOCTL , & chipinfo );
1679+ if (0 == ret ) {
1680+ if (wiringPiDebug ) {
1681+ printf ("%s: name=%s, label=%s, lines=%u\n" , szGPIOChip , chipinfo .name , chipinfo .label , chipinfo .lines ) ;
1682+ }
1683+ int chipOK = 1 ;
1684+ if (label [0 ]!= '\0' && NULL == strstr (chipinfo .label , label )) {
1685+ chipOK = 0 ;
1686+ }
1687+ if (lines > 0 && chipinfo .lines != lines ) {
1688+ chipOK = 0 ;
1689+ }
1690+ if (chipOK ) {
1691+ if (wiringPiDebug ) {
1692+ printf ("%s: valid, fd=%d\n" , szGPIOChip , Fd );
1693+ }
1694+ } else {
1695+ if (wiringPiDebug ) {
1696+ printf ("%s: invalid, search for '%s' with %u lines!\n" , szGPIOChip , label , lines ) ;
1697+ }
1698+ close (Fd );
1699+ return -1 ; // invalid chip
1700+ }
1701+ }
1702+ }
1703+ return Fd ;
1704+ }
16641705
16651706int wiringPiGpioDeviceGetFd () {
16661707 if (chipFd < 0 ) {
16671708 piBoard ();
1668- const char * gpiochip = PI_MODEL_5 == RaspberryPiModel ? DEV_GPIO_PI5 : DEV_GPIO_PI ;
1669- chipFd = open (gpiochip , O_RDWR );
1670- if (chipFd < 0 ) {
1671- fprintf (stderr , "wiringPi: ERROR: %s open ret=%d\n" , gpiochip , chipFd );
1672- } else if (wiringPiDebug ) {
1673- printf ("wiringPi: Open chip %s succeded, fd=%d\n" , gpiochip , chipFd ) ;
1709+ if (PI_MODEL_5 == RaspberryPiModel ) {
1710+ chipFd = OpenAndCheckGpioChip (0 , "rp1" , 54 ); // /dev/gpiochip0 @ Pi5 since Kernel 6.6.47
1711+ if (chipFd < 0 ) {
1712+ chipFd = OpenAndCheckGpioChip (4 , "rp1" , 54 ); // /dev/gpiochip4 @ Pi5 with older kernel
1713+ }
1714+ } else {
1715+ // not all Pis have same number of lines: Pi0, Pi1, Pi3, 54 lines, Pi4, 58 lines (CM ?), see #280, so this check is disabled
1716+ chipFd = OpenAndCheckGpioChip (0 , "bcm" , 0 );
16741717 }
16751718 }
16761719 return chipFd ;
0 commit comments