chiark / gitweb /
Added some tweaks to gpio to set alt modes on pins (sssh!)
authorGordon Henderson <projects@drogon.net>
Sat, 3 Aug 2013 22:53:35 +0000 (23:53 +0100)
committerGordon Henderson <projects@drogon.net>
Sat, 3 Aug 2013 22:53:35 +0000 (23:53 +0100)
gpio/gpio.c
wiringPi/wiringPi.c
wiringPi/wiringPi.h

index fab003618e7f764b037304246bfb6c6034bcbacb..b65af0eb8d63ca3c1fc56eab46d88159d9553c96 100644 (file)
@@ -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 <channel> <value>" ;     // 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) ;
index ba61d9fab64efa3e3593c2c9b00b7ff67907096e..b54ad292cfb91c2d4908781ebdc2c538e7d1f0a6 100644 (file)
@@ -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:
index 84042e9176cf818fd89dd8c84ef7716f7861fb24..ce4680abf26b5b6d8e37cd580d5b044aa1497336 100644 (file)
@@ -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) ;