chiark / gitweb /
Fixing a few issues - makefile goofs.
[wiringPi.git] / gpio / gpio.c
index 461274f615815242cc9a1abd6215b594354e9175..4eef19d6ced5f947d8e86bb73b17c0005e84a974 100644 (file)
@@ -116,7 +116,7 @@ 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 (not an error): File not present: %s\n", cmd, file) ;
+      fprintf (stderr, "%s: Warning (not an error, do not report): File not present: %s\n", cmd, file) ;
     else
       fprintf (stderr, "%s: Warning (not an error): Unable to change ownership of %s: %s\n", cmd, file, strerror (errno)) ;
   }
@@ -163,6 +163,22 @@ static int moduleLoaded (char *modName)
  *********************************************************************************
  */
 
+static void checkDevTree (char *argv [])
+{
+  struct stat statBuf ;
+
+  if (stat ("/proc/device-tree", &statBuf) == 0)       // We're on a devtree system ...
+  {
+    fprintf (stderr,
+"%s: Unable to load/unload modules as this Pi has the device tree enabled.\n"
+"  You need to run the raspi-config program (as root) and select the\n"
+"  modules (SPI or I2C) that you wish to load/unload there and reboot.\n"
+"  There is more information here:\n"
+"      https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314\n", argv [0]) ;
+    exit (1) ;
+  }
+}
+
 static void _doLoadUsage (char *argv [])
 {
   fprintf (stderr, "Usage: %s load <spi/i2c> [I2C baudrate in Kb/sec]\n", argv [0]) ;
@@ -176,6 +192,8 @@ static void doLoad (int argc, char *argv [])
   char *file1, *file2 ;
   char args1 [32], args2 [32] ;
 
+  checkDevTree (argv) ;
+
   if (argc < 3)
     _doLoadUsage (argv) ;
 
@@ -251,6 +269,8 @@ static void doUnLoad (int argc, char *argv [])
   char *module1, *module2 ;
   char cmd [80] ;
 
+  checkDevTree (argv) ;
+
   if (argc != 3)
     _doUnLoadUsage (argv) ;
 
@@ -434,19 +454,23 @@ void doExport (int argc, char *argv [])
     exit (1) ;
   }
 
-  /**/ if ((strcasecmp (mode, "in")  == 0) || (strcasecmp (mode, "input")  == 0))
+  /**/ if ((strcasecmp (mode, "in")   == 0) || (strcasecmp (mode, "input")  == 0))
     fprintf (fd, "in\n") ;
-  else if ((strcasecmp (mode, "out") == 0) || (strcasecmp (mode, "output") == 0))
+  else if ((strcasecmp (mode, "out")  == 0) || (strcasecmp (mode, "output") == 0))
     fprintf (fd, "out\n") ;
+  else if ((strcasecmp (mode, "high") == 0) || (strcasecmp (mode, "up")     == 0))
+    fprintf (fd, "high\n") ;
+  else if ((strcasecmp (mode, "low")  == 0) || (strcasecmp (mode, "down")   == 0))
+    fprintf (fd, "low\n") ;
   else
   {
-    fprintf (stderr, "%s: Invalid mode: %s. Should be in or out\n", argv [1], mode) ;
+    fprintf (stderr, "%s: Invalid mode: %s. Should be in, out, high or low\n", argv [1], mode) ;
     exit (1) ;
   }
 
   fclose (fd) ;
 
-// Change ownership so the current user can actually use it!
+// Change ownership so the current user can actually use it
 
   sprintf (fName, "/sys/class/gpio/gpio%d/value", pin) ;
   changeOwner (argv [0], fName) ;
@@ -1134,6 +1158,8 @@ int main (int argc, char *argv [])
 {
   int i ;
   int model, rev, mem, maker, overVolted ;
+  struct stat statBuf ;
+
 
   if (getenv ("WIRINGPI_DEBUG") != NULL)
   {
@@ -1190,6 +1216,20 @@ int main (int argc, char *argv [])
       printf ("Raspberry Pi Details:\n") ;
       printf ("  Type: %s, Revision: %s, Memory: %dMB, Maker: %s %s\n", 
          piModelNames [model], piRevisionNames [rev], mem, piMakerNames [maker], overVolted ? "[OV]" : "") ;
+
+// Check for device tree
+
+      if (stat ("/proc/device-tree", &statBuf) == 0)   // We're on a devtree system ...
+       printf ("  Device tree is enabled.\n") ;
+
+      if (stat ("/dev/gpiomem", &statBuf) == 0)                // User level GPIO is GO
+      {
+       printf ("  This Raspberry Pi supports user-level GPIO access.\n") ;
+       printf ("    -> See the man-page for more details\n") ;
+      }
+      else
+       printf ("  * Root or sudo required for GPIO access.\n") ;
+      
     }
     return 0 ;
   }