From f18c8f7204d6354220fd6754578b3daa43734e1b Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sat, 3 Aug 2013 23:53:35 +0100 Subject: [PATCH] Added some tweaks to gpio to set alt modes on pins (sssh!) --- gpio/gpio.c | 30 +++++++++++++++++++++++++++++- wiringPi/wiringPi.c | 27 ++++++++++++++++++++++++++- wiringPi/wiringPi.h | 1 + 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/gpio/gpio.c b/gpio/gpio.c index fab0036..b65af0e 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -51,7 +51,7 @@ extern void doReadallOld (void) ; # define FALSE (1==2) #endif -#define VERSION "2.12" +#define VERSION "2.13" #define I2CDETECT "/usr/sbin/i2cdetect" int wpMode ; @@ -75,6 +75,25 @@ char *usage = "Usage: gpio -v\n" " gpio gbw " ; // No trailing newline needed here. +/* + * decodePin: + * Decode a pin "number" which can actually be a pin name to represent + * one of the Pi's on-board pins. + ********************************************************************************* + */ + +static int decodePin (const char *str) +{ + +// The first case - see if it's a number: + + if (isdigit (str [0])) + return atoi (str) ; + + return 0 ; +} + + /* * changeOwner: * Change the ownership of the file to the real userId of the calling @@ -639,6 +658,15 @@ void doMode (int argc, char *argv []) else if (strcasecmp (mode, "down") == 0) pullUpDnControl (pin, PUD_DOWN) ; else if (strcasecmp (mode, "tri") == 0) pullUpDnControl (pin, PUD_OFF) ; else if (strcasecmp (mode, "off") == 0) pullUpDnControl (pin, PUD_OFF) ; + +// Undocumented + + else if (strcasecmp (mode, "alt0") == 0) pinModeAlt (pin, 0b100) ; + else if (strcasecmp (mode, "alt1") == 0) pinModeAlt (pin, 0b101) ; + else if (strcasecmp (mode, "alt2") == 0) pinModeAlt (pin, 0b110) ; + else if (strcasecmp (mode, "alt3") == 0) pinModeAlt (pin, 0b111) ; + else if (strcasecmp (mode, "alt4") == 0) pinModeAlt (pin, 0b011) ; + else if (strcasecmp (mode, "alt5") == 0) pinModeAlt (pin, 0b010) ; else { fprintf (stderr, "%s: Invalid mode: %s. Should be in/out/pwm/clock/up/down/tri\n", argv [1], mode) ; diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c index ba61d9f..b54ad29 100644 --- a/wiringPi/wiringPi.c +++ b/wiringPi/wiringPi.c @@ -118,7 +118,6 @@ struct wiringPiNodeStruct *wiringPiNodes = NULL ; #define FSEL_INPT 0b000 #define FSEL_OUTP 0b001 #define FSEL_ALT0 0b100 -#define FSEL_ALT0 0b100 #define FSEL_ALT1 0b101 #define FSEL_ALT2 0b110 #define FSEL_ALT3 0b111 @@ -925,6 +924,32 @@ void pinEnableED01Pi (int pin) ********************************************************************************* */ +/* + * pinModeAlt: + * This is an un-documented special to let you set any pin to any mode + ********************************************************************************* + */ + +void pinModeAlt (int pin, int mode) +{ + int fSel, shift ; + + if ((pin & PI_GPIO_MASK) == 0) // On-board pin + { + /**/ if (wiringPiMode == WPI_MODE_PINS) + pin = pinToGpio [pin] ; + else if (wiringPiMode == WPI_MODE_PHYS) + pin = physToGpio [pin] ; + else if (wiringPiMode != WPI_MODE_GPIO) + return ; + + fSel = gpioToGPFSEL [pin] ; + shift = gpioToShift [pin] ; + + *(gpio + fSel) = (*(gpio + fSel) & ~(7 << shift)) | ((mode & 0x7) << shift) ; + } +} + /* * pinMode: diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h index 84042e9..ce4680a 100644 --- a/wiringPi/wiringPi.h +++ b/wiringPi/wiringPi.h @@ -129,6 +129,7 @@ extern int wiringPiSetupSys (void) ; extern int wiringPiSetupGpio (void) ; extern int wiringPiSetupPhys (void) ; +extern void pinModeAlt (int pin, int mode) ; extern void pinMode (int pin, int mode) ; extern void pullUpDnControl (int pin, int pud) ; extern int digitalRead (int pin) ; -- 2.30.2