mirror of
https://github.com/clockworkpi/WiringPi.git
synced 2025-12-13 08:18:48 +01:00
Updated wiringPi to check the the hardware board revision, to
accomodate the changes in pin that are present on the Revision 2 Raspberry Pi board. Added in simple debugging too - set the environment variable WIRINGPI_DEBUG to get some more debug messages out
This commit is contained in:
parent
99095e3fa0
commit
728cdc4c23
@ -56,6 +56,8 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -169,6 +171,14 @@ static volatile uint32_t *timer ;
|
|||||||
|
|
||||||
static volatile uint32_t *timerIrqRaw ;
|
static volatile uint32_t *timerIrqRaw ;
|
||||||
|
|
||||||
|
// Raspberry Pi board revision
|
||||||
|
|
||||||
|
static int boardRevision = -1 ;
|
||||||
|
|
||||||
|
// Debugging
|
||||||
|
|
||||||
|
static int wiringPiDebug = FALSE ;
|
||||||
|
|
||||||
// The BCM2835 has 54 GPIO pins.
|
// The BCM2835 has 54 GPIO pins.
|
||||||
// BCM2835 data sheet, Page 90 onwards.
|
// BCM2835 data sheet, Page 90 onwards.
|
||||||
// There are 6 control registers, each control the functions of a block
|
// There are 6 control registers, each control the functions of a block
|
||||||
@ -198,8 +208,11 @@ static int sysFds [64] ;
|
|||||||
|
|
||||||
// pinToGpio:
|
// pinToGpio:
|
||||||
// Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
|
// Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
|
||||||
|
// Cope for 2 different board revieions here
|
||||||
|
|
||||||
static int pinToGpio [64] =
|
static int *pinToGpio ;
|
||||||
|
|
||||||
|
static int pinToGpioR1 [64] =
|
||||||
{
|
{
|
||||||
17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
|
17, 18, 21, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
|
||||||
0, 1, // I2C - SDA0, SCL0
|
0, 1, // I2C - SDA0, SCL0
|
||||||
@ -209,7 +222,23 @@ static int pinToGpio [64] =
|
|||||||
|
|
||||||
// Padding:
|
// Padding:
|
||||||
|
|
||||||
-1, -1, -1,-1,-1,-1,-1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
|
||||||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
|
||||||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
|
||||||
|
} ;
|
||||||
|
|
||||||
|
static int pinToGpioR2 [64] =
|
||||||
|
{
|
||||||
|
17, 18, 27, 22, 23, 24, 25, 4, // From the Original Wiki - GPIO 0 through 7
|
||||||
|
2, 3, // I2C - SDA0, SCL0
|
||||||
|
8, 7, // SPI - CE1, CE0
|
||||||
|
10, 9, 11, // SPI - MOSI, MISO, SCLK
|
||||||
|
14, 15, // UART - Tx, Rx
|
||||||
|
28, 29, 30, 31, // New GPIOs 8 though 11
|
||||||
|
|
||||||
|
// Padding:
|
||||||
|
|
||||||
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 31
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 47
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // ... 63
|
||||||
} ;
|
} ;
|
||||||
@ -800,9 +829,19 @@ unsigned int millis (void)
|
|||||||
int wiringPiSetup (void)
|
int wiringPiSetup (void)
|
||||||
{
|
{
|
||||||
int fd ;
|
int fd ;
|
||||||
|
FILE *cpuFd ;
|
||||||
|
char line [80] ;
|
||||||
|
char *c ;
|
||||||
|
int revision = -1 ;
|
||||||
uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
|
uint8_t *gpioMem, *pwmMem, *clkMem, *padsMem, *timerMem ;
|
||||||
struct timeval tv ;
|
struct timeval tv ;
|
||||||
|
|
||||||
|
if (getenv ("WIRINGPI_DEBUG") != NULL)
|
||||||
|
wiringPiDebug = TRUE ;
|
||||||
|
|
||||||
|
if (wiringPiDebug)
|
||||||
|
printf ("wiringPiSetup called\n") ;
|
||||||
|
|
||||||
pinMode = pinModeWPi ;
|
pinMode = pinModeWPi ;
|
||||||
pullUpDnControl = pullUpDnControlWPi ;
|
pullUpDnControl = pullUpDnControlWPi ;
|
||||||
digitalWrite = digitalWriteWPi ;
|
digitalWrite = digitalWriteWPi ;
|
||||||
@ -814,6 +853,52 @@ int wiringPiSetup (void)
|
|||||||
pwmSetMode = pwmSetModeWPi ;
|
pwmSetMode = pwmSetModeWPi ;
|
||||||
pwmSetRange = pwmSetRangeWPi ;
|
pwmSetRange = pwmSetRangeWPi ;
|
||||||
|
|
||||||
|
// Find board revision
|
||||||
|
|
||||||
|
if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "wiringPiSetup: Unable to open /proc/cpuinfo: %s\n", strerror (errno)) ;
|
||||||
|
return -1 ;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (fgets (line, 80, cpuFd) != NULL)
|
||||||
|
if (strncmp (line, "Revision", 8) == 0)
|
||||||
|
for (c = line ; *c ; ++c)
|
||||||
|
{
|
||||||
|
if (!isdigit (*c))
|
||||||
|
continue ;
|
||||||
|
revision = atoi (c) ;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose (cpuFd) ;
|
||||||
|
if (revision == -1)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "wiringPiSetup: Unable to determine board revision\n") ;
|
||||||
|
errno = 0 ;
|
||||||
|
return -1 ;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**/ if ((revision == 2) || (revision == 3))
|
||||||
|
boardRevision = 1 ;
|
||||||
|
else if ((revision == 4) || (revision == 5) || (revision == 6))
|
||||||
|
boardRevision = 2 ;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf (stderr, "wiringPiSetup: Unable to determine board revision: %d\n", revision) ;
|
||||||
|
errno = 0 ;
|
||||||
|
return -1 ;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (boardRevision == 1)
|
||||||
|
pinToGpio = pinToGpioR1 ;
|
||||||
|
else
|
||||||
|
pinToGpio = pinToGpioR2 ;
|
||||||
|
|
||||||
|
if (wiringPiDebug)
|
||||||
|
printf ("Revision: %d, board revision: %d\n", revision, boardRevision) ;
|
||||||
|
|
||||||
// Open the master /dev/memory device
|
// Open the master /dev/memory device
|
||||||
|
|
||||||
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
|
if ((fd = open ("/dev/mem", O_RDWR | O_SYNC) ) < 0)
|
||||||
@ -954,9 +1039,12 @@ int wiringPiSetup (void)
|
|||||||
|
|
||||||
int wiringPiSetupGpio (void)
|
int wiringPiSetupGpio (void)
|
||||||
{
|
{
|
||||||
int x = wiringPiSetup () ;
|
int x ;
|
||||||
|
|
||||||
if (x != 0)
|
if (wiringPiDebug)
|
||||||
|
printf ("wiringPiSetupGpio called\n") ;
|
||||||
|
|
||||||
|
if ((x = wiringPiSetup ()) < 0)
|
||||||
return x ;
|
return x ;
|
||||||
|
|
||||||
pinMode = pinModeGpio ;
|
pinMode = pinModeGpio ;
|
||||||
@ -989,6 +1077,9 @@ int wiringPiSetupSys (void)
|
|||||||
struct timeval tv ;
|
struct timeval tv ;
|
||||||
char fName [128] ;
|
char fName [128] ;
|
||||||
|
|
||||||
|
if (wiringPiDebug)
|
||||||
|
printf ("wiringPiSetupSys called\n") ;
|
||||||
|
|
||||||
pinMode = pinModeSys ;
|
pinMode = pinModeSys ;
|
||||||
pullUpDnControl = pullUpDnControlSys ;
|
pullUpDnControl = pullUpDnControlSys ;
|
||||||
digitalWrite = digitalWriteSys ;
|
digitalWrite = digitalWriteSys ;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user