chiark / gitweb /
Sorted a variable initialisation issue that was giving incorrect results
authorGordon Henderson <projects@drogon.net>
Sat, 25 May 2013 12:02:02 +0000 (13:02 +0100)
committerGordon Henderson <projects@drogon.net>
Sat, 25 May 2013 12:02:02 +0000 (13:02 +0100)
with wiringPiISR.
Added the ability to readall/reset from extension modules.
Changed the build script to use /bin/sh rather than /bin/bash as it seems
some installations don't have /bin/bash (or if they do, then the people who
are telling me that ./build gives them command not found are wrong)

build
gpio/gpio.1
gpio/gpio.c
wiringPi/wiringPi.c
wiringPi/wiringPi.h

diff --git a/build b/build
index 776c756fa0703eae0ab3420c11dd09e651348e5e..f578d4a54b879fe910de6c9acd4e4f18bc8e09f5 100755 (executable)
--- a/build
+++ b/build
@@ -1,7 +1,6 @@
-#!/bin/bash
+#!/bin/sh
 
-check-make-ok()
-{
+check_make_ok() {
   if [ $? != 0 ]; then
     echo ""
     echo "Make Failed..."
@@ -51,26 +50,26 @@ fi
   cd wiringPi
   sudo make uninstall
   make
-  check-make-ok
+  check_make_ok
   sudo make install
-  check-make-ok
+  check_make_ok
 
   echo
   echo "WiringPi Devices Library"
   cd ../devLib
   sudo make uninstall
   make
-  check-make-ok
+  check_make_ok
   sudo make install
-  check-make-ok
+  check_make_ok
 
   echo
   echo "GPIO Utility"
   cd ../gpio
   make
-  check-make-ok
+  check_make_ok
   sudo make install
-  check-make-ok
+  check_make_ok
 
 # echo
 # echo "Examples"
index ae4df4e5ab73a191a913624a315262dd224499cf..e1cba322c2b768dd2972133c8ea11599f6a242f1 100644 (file)
@@ -126,11 +126,19 @@ Output a table of all GPIO pins values. The values represent the actual values r
 if the pin is in input mode, or the last value written if the pin is in output
 mode.
 
+The readall command is usable with an extension module (via the -x parameter),
+but it's unable to determine pin modes or states, so will perform both a
+digital and analog read on each pin in-turn.
+
 .TP
 .B reset
 Resets the GPIO - As much as it's possible to do. All pins are set to input
 mode and all the internal pull-up/down resistors are disconnected (tristate mode).
 
+The reset command is usable with an extension module (via the -x parameter),
+but it's limited to turning the pin into input mode (if applicable) and
+removing any pull up/down resistor.
+
 .TP
 .B pwm <pin> <value>
 Write a PWM value (0-1023) to the given pin. The pin needs to be put
@@ -212,8 +220,8 @@ Change the PWM range register. The default is 1024.
 .B load i2c [baudrate]
 This loads the i2c or drivers into the kernel and changes the permissions
 on the associated /dev/ entries so that the current user has access to
-them. Optionally it will set the I2C baudrate to that supplied (or as
-close as the Pi can manage) The default speed is 100Kb/sec.
+them. Optionally it will set the I2C baudrate to that supplied in Kb/sec
+(or as close as the Pi can manage) The default speed is 100Kb/sec.
 
 .TP
 .B load spi [buffer size in KB]
index 1933a694f2566dd581ef0cbdc411a9146395792e..7a6ef8e8332943a990a88254dfcd97cd87581416 100644 (file)
@@ -48,7 +48,7 @@ extern int wiringPiDebug ;
 #  define      FALSE   (1==2)
 #endif
 
-#define        VERSION         "2.06"
+#define        VERSION         "2.07"
 #define        I2CDETECT       "/usr/sbin/i2cdetect"
 
 static int wpMode ;
@@ -239,6 +239,10 @@ static void doI2Cdetect (int argc, char *argv [])
 /*
  * doReadall:
  *     Read all the GPIO pins
+ *     We also want to use this to read the state of pins on an externally
+ *     connected device, so we need to do some fiddling with the internal
+ *     wiringPi node structures - since the gpio command can only use
+ *     one external device at a time, we'll use that to our advantage...
  *********************************************************************************
  */
 
@@ -268,27 +272,56 @@ static int wpiToPhys [64] =
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,    // 47..63
 } ;
 
-static void doReadall (void)
+
+/*
+ * doReadallExternal:
+ *     A relatively crude way to read the pins on an external device.
+ *     We don't know the input/output mode of pins, but we can tell
+ *     if it's an analog pin or a digital one...
+ *********************************************************************************
+ */
+
+static void doReadallExternal (void)
 {
   int pin ;
 
-  printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
-  printf ("| wiringPi | GPIO | Phys | Name   | Mode | Value |\n") ;
-  printf ("+----------+------+------+--------+------+-------+\n") ;
+  printf ("+------+---------+--------+\n") ;
+  printf ("|  Pin | Digital | Analog |\n") ;
+  printf ("+------+---------+--------+\n") ;
 
-  for (pin = 0 ; pin < 64 ; ++pin)     // Crude, but effective
+  for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
+    printf ("| %4d |  %4d   |  %4d  |\n", pin, digitalRead (pin), analogRead (pin)) ;
+
+  printf ("+------+---------+--------+\n") ;
+}
+
+
+static void doReadall (void)
+{
+  int pin ;
+
+  if (wiringPiNodes != NULL)   // External readall
+    doReadallExternal () ;
+  else
   {
-    if (wpiPinToGpio (pin) == -1)
-      continue ;
+    printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
+    printf ("| wiringPi | GPIO | Phys | Name   | Mode | Value |\n") ;
+    printf ("+----------+------+------+--------+------+-------+\n") ;
 
-    printf ("| %6d   | %3d  | %3d  | %s | %s | %s  |\n",
-       pin, wpiPinToGpio (pin), wpiToPhys [pin],
-       pinNames [pin], 
-       alts [getAlt (pin)], 
-       digitalRead (pin) == HIGH ? "High" : "Low ") ;
-  }
+    for (pin = 0 ; pin < 64 ; ++pin)   // Crude, but effective
+    {
+      if (wpiPinToGpio (pin) == -1)
+       continue ;
+
+      printf ("| %6d   | %3d  | %3d  | %s | %s | %s  |\n",
+         pin, wpiPinToGpio (pin), wpiToPhys [pin],
+         pinNames [pin], 
+         alts [getAlt (pin)], 
+         digitalRead (pin) == HIGH ? "High" : "Low ") ;
+    }
 
-  printf ("+----------+------+------+--------+------+-------+\n") ;
+    printf ("+----------+------+------+--------+------+-------+\n") ;
+  }
 }
 
 
@@ -614,6 +647,24 @@ void doUnexportall (char *progName)
 }
 
 
+/*
+ * doResetExternal:
+ *     Load readallExternal, we try to do this with an external device.
+ *********************************************************************************
+ */
+
+static void doResetExternal (void)
+{
+  int pin ;
+
+  for (pin = wiringPiNodes->pinBase ; pin <= wiringPiNodes->pinMax ; ++pin)
+  {
+    pinMode         (pin, INPUT) ;
+    pullUpDnControl (pin, PUD_OFF) ;
+  }
+}
+
+
 /*
  * doReset:
  *     Reset the GPIO pins - as much as we can do
@@ -624,16 +675,21 @@ static void doReset (char *progName)
 {
   int pin ;
 
-  doUnexportall (progName) ;
-
-  for (pin = 0 ; pin < 64 ; ++pin)
+  if (wiringPiNodes != NULL)   // External reset
+    doResetExternal () ;
+  else
   {
-    if (wpiPinToGpio (pin) == -1)
-      continue ;
+    doUnexportall (progName) ;
 
-    digitalWrite    (pin, LOW) ;
-    pinMode         (pin, INPUT) ;
-    pullUpDnControl (pin, PUD_OFF) ;
+    for (pin = 0 ; pin < 64 ; ++pin)
+    {
+      if (wpiPinToGpio (pin) == -1)
+       continue ;
+
+      digitalWrite    (pin, LOW) ;
+      pinMode         (pin, INPUT) ;
+      pullUpDnControl (pin, PUD_OFF) ;
+    }
   }
 }
 
index b8e381a27cd819f809f69974473c165e57a8986a..e97a6d17f4f5694efc5f1c9a14416757de800e08 100644 (file)
@@ -88,7 +88,7 @@
 
 #define        PI_GPIO_MASK    (0xFFFFFFC0)
 
-static struct wiringPiNodeStruct *wiringPiNodes = NULL ;
+struct wiringPiNodeStruct *wiringPiNodes = NULL ;
 
 // BCM Magic
 
@@ -213,7 +213,13 @@ int wiringPiReturnCodes = FALSE ;
 // sysFds:
 //     Map a file descriptor from the /sys/class/gpio/gpioX/value
 
-static int sysFds [64] ;
+static int sysFds [64] =
+{
+  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+} ;
 
 // ISR Data
 
@@ -833,7 +839,7 @@ void gpioClockSet (int pin, int freq)
  *********************************************************************************
  */
 
-static struct wiringPiNodeStruct *wiringPiFindNode (int pin)
+struct wiringPiNodeStruct *wiringPiFindNode (int pin)
 {
   struct wiringPiNodeStruct *node = wiringPiNodes ;
 
@@ -1304,7 +1310,8 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
   char  c ;
   int   bcmGpioPin ;
 
-  pin &= 63 ;
+  if ((pin < 0) || (pin > 63))
+    return wiringPiFailure (WPI_FATAL, "wiringPiISR: pin must be 0-63 (%d)\n", pin) ;
 
   /**/ if (wiringPiMode == WPI_MODE_UNINITIALISED)
     return wiringPiFailure (WPI_FATAL, "wiringPiISR: wiringPi has not been initialised. Unable to continue.\n") ;
@@ -1333,26 +1340,26 @@ int wiringPiISR (int pin, int mode, void (*function)(void))
     sprintf (pinS, "%d", bcmGpioPin) ;
 
     if ((pid = fork ()) < 0)   // Fail
-      return pid ;
+      return wiringPiFailure (WPI_FATAL, "wiringPiISR: fork failed: %s\n", strerror (errno)) ;
 
     if (pid == 0)      // Child, exec
     {
       execl ("/usr/local/bin/gpio", "gpio", "edge", pinS, modeS, (char *)NULL) ;
-      return -1 ;      // Failure ...
+      return wiringPiFailure (WPI_FATAL, "wiringPiISR: execl failed: %s\n", strerror (errno)) ;
     }
     else               // Parent, wait
       wait (NULL) ;
   }
 
-// Now pre-open the /sys/class node - it may already be open if
-//     we are in Sys mode or if we call here twice, if-so, we'll close it.
-
-  if (sysFds [bcmGpioPin] != -1)
-    close (sysFds [bcmGpioPin]) ;
+// Now pre-open the /sys/class node - but it may already be open if
+//     we are in Sys mode...
 
-  sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
-  if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
-    return -1 ;
+  if (sysFds [bcmGpioPin] == -1)
+  {
+    sprintf (fName, "/sys/class/gpio/gpio%d/value", bcmGpioPin) ;
+    if ((sysFds [bcmGpioPin] = open (fName, O_RDWR)) < 0)
+      return wiringPiFailure (WPI_FATAL, "wiringPiISR: unable to open %s: %s\n", fName, strerror (errno)) ;
+  }
 
 // Clear any initial pending interrupt
 
index 600b318763e85eb2d452f27a6145e86c2d8fc758..84042e9176cf818fd89dd8c84ef7716f7861fb24 100644 (file)
@@ -104,6 +104,8 @@ struct wiringPiNodeStruct
   struct wiringPiNodeStruct *next ;
 } ;
 
+extern struct wiringPiNodeStruct *wiringPiNodes ;
+
 
 // Function prototypes
 //     c++ wrappers thanks to a comment by Nick Lott
@@ -119,7 +121,8 @@ extern int wiringPiFailure (int fatal, const char *message, ...) ;
 
 // Core wiringPi functions
 
-extern struct wiringPiNodeStruct *wiringPiNewNode (int pinBase, int numPins) ;
+extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
+extern struct wiringPiNodeStruct *wiringPiNewNode  (int pinBase, int numPins) ;
 
 extern int  wiringPiSetup       (void) ;
 extern int  wiringPiSetupSys    (void) ;