chiark / gitweb /
Updates to the build process
[wiringPi.git] / gpio / gpio.c
index 7881c65466d87318d1937955f2b5e1c4659e3284..3cee59f9e99a3108fbbcc611e5ddba7c341c258a 100644 (file)
@@ -2,7 +2,7 @@
  * gpio.c:
  *     Swiss-Army-Knife, Set-UID command-line interface to the Raspberry
  *     Pi's GPIO.
- *     Copyright (c) 2012-2014 Gordon Henderson
+ *     Copyright (c) 2012-2015 Gordon Henderson
  ***********************************************************************
  * This file is part of wiringPi:
  *     https://projects.drogon.net/raspberry-pi/wiringpi/
 #include <sys/stat.h>
 
 #include <wiringPi.h>
+#include <wpiExtensions.h>
 
 #include <gertboard.h>
 #include <piFace.h>
 
-#include "extensions.h"
 
 extern int wiringPiDebug ;
 
@@ -53,7 +53,7 @@ extern void doPins       (void) ;
 #  define      FALSE   (1==2)
 #endif
 
-#define        VERSION                 "2.22"
+#define        VERSION                 "2.23"
 #define        PI_USB_POWER_CONTROL    38
 #define        I2CDETECT               "/usr/sbin/i2cdetect"
 
@@ -73,6 +73,7 @@ char *usage = "Usage: gpio -v\n"
              "       gpio pwmr <range> \n"
              "       gpio pwmc <divider> \n"
              "       gpio load spi/i2c\n"
+             "       gpio unload spi/i2c\n"
              "       gpio i2cd/i2cdetect\n"
              "       gpio usbp high/low\n"
              "       gpio gbr <channel>\n"
@@ -115,12 +116,9 @@ static void changeOwner (char *cmd, char *file)
   if (chown (file, uid, gid) != 0)
   {
     if (errno == ENOENT)       // Warn that it's not there
-      fprintf (stderr, "%s: Warning: File not present: %s\n", cmd, file) ;
+      fprintf (stderr, "%s: Warning (not an error): File not present: %s\n", cmd, file) ;
     else
-    {
-      fprintf (stderr, "%s: Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
-      exit (1) ;
-    }
+      fprintf (stderr, "%s: Warning (not an error): Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
   }
 }
 
@@ -167,7 +165,7 @@ static int moduleLoaded (char *modName)
 
 static void _doLoadUsage (char *argv [])
 {
-  fprintf (stderr, "Usage: %s load <spi/i2c> [SPI bufferSize in KB | I2C baudrate in Kb/sec]\n", argv [0]) ;
+  fprintf (stderr, "Usage: %s load <spi/i2c> [I2C baudrate in Kb/sec]\n", argv [0]) ;
   exit (1) ;
 }
 
@@ -190,7 +188,10 @@ static void doLoad (int argc, char *argv [])
     file1  = "/dev/spidev0.0" ;
     file2  = "/dev/spidev0.1" ;
     if (argc == 4)
-      sprintf (args1, " bufsiz=%d", atoi (argv [3]) * 1024) ;
+    {
+      fprintf (stderr, "%s: Unable to set the buffer size now. Load aborted. Please see the man page.\n", argv [0]) ;
+      exit (1) ;
+    }
     else if (argc > 4)
       _doLoadUsage (argv) ;
   }
@@ -210,13 +211,13 @@ static void doLoad (int argc, char *argv [])
 
   if (!moduleLoaded (module1))
   {
-    sprintf (cmd, "modprobe %s%s", module1, args1) ;
+    sprintf (cmd, "/sbin/modprobe %s%s", module1, args1) ;
     system (cmd) ;
   }
 
   if (!moduleLoaded (module2))
   {
-    sprintf (cmd, "modprobe %s%s", module2, args2) ;
+    sprintf (cmd, "/sbin/modprobe %s%s", module2, args2) ;
     system (cmd) ;
   }
 
@@ -233,6 +234,53 @@ static void doLoad (int argc, char *argv [])
 }
 
 
+/*
+ * doUnLoad:
+ *     Un-Load either the spi or i2c modules and change device ownerships, etc.
+ *********************************************************************************
+ */
+
+static void _doUnLoadUsage (char *argv [])
+{
+  fprintf (stderr, "Usage: %s unload <spi/i2c>\n", argv [0]) ;
+  exit (1) ;
+}
+
+static void doUnLoad (int argc, char *argv [])
+{
+  char *module1, *module2 ;
+  char cmd [80] ;
+
+  if (argc != 3)
+    _doUnLoadUsage (argv) ;
+
+  /**/ if (strcasecmp (argv [2], "spi") == 0)
+  {
+    module1 = "spidev" ;
+    module2 = "spi_bcm2708" ;
+  }
+  else if (strcasecmp (argv [2], "i2c") == 0)
+  {
+    module1 = "i2c_dev" ;
+    module2 = "i2c_bcm2708" ;
+  }
+  else
+    _doUnLoadUsage (argv) ;
+
+  if (moduleLoaded (module1))
+  {
+    sprintf (cmd, "/sbin/rmmod %s", module1) ;
+    system (cmd) ;
+  }
+
+  if (moduleLoaded (module2))
+  {
+    sprintf (cmd, "/sbin/rmmod %s", module2) ;
+    system (cmd) ;
+  }
+}
+
+
 /*
  * doI2Cdetect:
  *     Run the i2cdetect command with the right runes for this Pi revision
@@ -586,24 +634,6 @@ 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
@@ -612,45 +642,9 @@ static void doResetExternal (void)
 
 static void doReset (char *progName)
 {
-  int model, rev, mem, maker, overVolted ;
-  int pin, endPin ;
-
-  printf ("GPIO Reset is dangerous!\n") ;
-  printf (" - Do Not rely on this to do anything sensible!\n") ;
-
-  if (wiringPiNodes != NULL)   // External
-  {
-    doResetExternal () ;
-    return ;
-  }
-
-  piBoardId (&model, &rev, &mem, &maker, &overVolted) ;
-
-  /**/ if ((model == PI_MODEL_A) || (model == PI_MODEL_B))
-    endPin = 16 ;
-  else if (model == PI_MODEL_BP)
-    endPin = 39 ;
-  else if (model == PI_MODEL_CM)
-  {
-    printf (" - Don't know how to reset a comput module:\n") ;
-    printf ("   Write a shell-script to reset the pins to the state you need.\n") ;
-    return ;
-  }
-  else
-  {
-    printf ("Oops - unable to determine board type... model: %d\n", model) ;
-    return ;
-  }
-
-  for (pin = 0 ; pin <= endPin ; ++pin)
-  {
-    if (wpiPinToGpio (pin) == -1)
-      continue ;
-
-    digitalWrite    (pin, LOW) ;
-    pinMode         (pin, INPUT) ;
-    pullUpDnControl (pin, PUD_OFF) ;
-  }
+  printf ("GPIO Reset is dangerous and has been removed from the gpio command.\n") ;
+  printf (" - Please write a shell-script to reset the GPIO pins into the state\n") ;
+  printf ("   that you need them in for your applications.\n") ;
 }
 
 
@@ -686,9 +680,6 @@ 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) ;
@@ -1183,7 +1174,7 @@ int main (int argc, char *argv [])
   if (strcmp (argv [1], "-v") == 0)
   {
     printf ("gpio version: %s\n", VERSION) ;
-    printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
+    printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
     printf ("This is free software with ABSOLUTELY NO WARRANTY.\n") ;
     printf ("For details type: %s -warranty\n", argv [0]) ;
     printf ("\n") ;
@@ -1206,7 +1197,7 @@ int main (int argc, char *argv [])
   if (strcasecmp (argv [1], "-warranty") == 0)
   {
     printf ("gpio version: %s\n", VERSION) ;
-    printf ("Copyright (c) 2012-2014 Gordon Henderson\n") ;
+    printf ("Copyright (c) 2012-2015 Gordon Henderson\n") ;
     printf ("\n") ;
     printf ("    This program is free software; you can redistribute it and/or modify\n") ;
     printf ("    it under the terms of the GNU Leser General Public License as published\n") ;
@@ -1240,7 +1231,8 @@ int main (int argc, char *argv [])
 
 // Check for load command:
 
-  if (strcasecmp (argv [1], "load" ) == 0)     { doLoad     (argc, argv) ; return 0 ; }
+  if (strcasecmp (argv [1], "load"   ) == 0)   { doLoad   (argc, argv) ; return 0 ; }
+  if (strcasecmp (argv [1], "unload" ) == 0)   { doUnLoad (argc, argv) ; return 0 ; }
 
 // Gertboard commands
 
@@ -1301,7 +1293,7 @@ int main (int argc, char *argv [])
       exit (EXIT_FAILURE) ;
     }
 
-    if (!doExtension (argv [0], argv [2]))     // Prints its own error messages
+    if (!loadWPiExtension (argv [0], argv [2], TRUE))  // Prints its own error messages
       exit (EXIT_FAILURE) ;
 
     for (i = 3 ; i < argc ; ++i)