From dca8a19fb816b1314008d84c092b11148a85715c Mon Sep 17 00:00:00 2001
From: Gordon Henderson <projects@drogon.net>
Date: Mon, 14 Jul 2014 08:39:38 +0100
Subject: [PATCH] Updated mostly to the gpio readall command to support the Raspberry Pi B+

---
 gpio/gpio.c         |   52 ++++++++++++++++++++-------
 gpio/readall.c      |  101 +++++++++++++++++++++++++++++++++++++++------------
 wiringPi/wiringPi.c |   67 ++++++++++++++++++++++++++++------
 wiringPi/wiringPi.h |    6 ++-
 4 files changed, 176 insertions(+), 50 deletions(-)

diff --git a/gpio/gpio.c b/gpio/gpio.c
index b9d1799..be22e0e 100644
--- a/gpio/gpio.c
+++ b/gpio/gpio.c
@@ -55,7 +55,7 @@ extern void doPins       (void) ;
 #  define	FALSE	(1==2)
 #endif
 
-#define	VERSION		"2.16"
+#define	VERSION		"2.18"
 #define	I2CDETECT	"/usr/sbin/i2cdetect"
 
 int wpMode ;
@@ -654,16 +654,17 @@ void doMode (int argc, char *argv [])
 
   mode = argv [3] ;
 
-  /**/ if (strcasecmp (mode, "in")     == 0) pinMode         (pin, INPUT) ;
-  else if (strcasecmp (mode, "input")  == 0) pinMode         (pin, INPUT) ;
-  else if (strcasecmp (mode, "out")    == 0) pinMode         (pin, OUTPUT) ;
-  else if (strcasecmp (mode, "output") == 0) pinMode         (pin, OUTPUT) ;
-  else if (strcasecmp (mode, "pwm")    == 0) pinMode         (pin, PWM_OUTPUT) ;
-  else if (strcasecmp (mode, "clock")  == 0) pinMode         (pin, GPIO_CLOCK) ;
-  else if (strcasecmp (mode, "up")     == 0) pullUpDnControl (pin, PUD_UP) ;
-  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) ;
+  /**/ if (strcasecmp (mode, "in")      == 0) pinMode         (pin, INPUT) ;
+  else if (strcasecmp (mode, "input")   == 0) pinMode         (pin, INPUT) ;
+  else if (strcasecmp (mode, "out")     == 0) pinMode         (pin, OUTPUT) ;
+  else if (strcasecmp (mode, "output")  == 0) pinMode         (pin, OUTPUT) ;
+  else if (strcasecmp (mode, "pwm")     == 0) pinMode         (pin, PWM_OUTPUT) ;
+  else if (strcasecmp (mode, "pwmTone") == 0) pinMode         (pin, PWM_TONE_OUTPUT) ;
+  else if (strcasecmp (mode, "clock")   == 0) pinMode         (pin, GPIO_CLOCK) ;
+  else if (strcasecmp (mode, "up")      == 0) pullUpDnControl (pin, PUD_UP) ;
+  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
 
@@ -742,7 +743,7 @@ static void doGbw (int argc, char *argv [])
     exit (1) ;
   }
 
-  if ((value < 0) || (value > 1023))
+  if ((value < 0) || (value > 255))
   {
     fprintf (stderr, "%s: gbw: Value must be from 0 to 255\n", argv [0]) ;
     exit (1) ;
@@ -933,6 +934,30 @@ void doToggle (int argc, char *argv [])
   digitalWrite (pin, !digitalRead (pin)) ;
 }
 
+
+/*
+ * doPwmTone:
+ *	Output a tone in a PWM pin
+ *********************************************************************************
+ */
+
+void doPwmTone (int argc, char *argv [])
+{
+  int pin, freq ;
+
+  if (argc != 4)
+  {
+    fprintf (stderr, "Usage: %s pwmTone <pin> <freq>\n", argv [0]) ;
+    exit (1) ;
+  }
+
+  pin = atoi (argv [2]) ;
+  freq = atoi (argv [3]) ;
+
+  pwmToneWrite (pin, freq) ;
+}
+
+
 /*
  * doClock:
  *	Output a clock on a pin
@@ -1100,7 +1125,7 @@ int main (int argc, char *argv [])
   if (strcasecmp (argv [1], "-warranty") == 0)
   {
     printf ("gpio version: %s\n", VERSION) ;
-    printf ("Copyright (c) 2012-2013 Gordon Henderson\n") ;
+    printf ("Copyright (c) 2012-2014 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") ;
@@ -1228,6 +1253,7 @@ int main (int argc, char *argv [])
   else if (strcasecmp (argv [1], "pwm-ms"   ) == 0) doPwmMode    (PWM_MODE_MS) ;
   else if (strcasecmp (argv [1], "pwmr"     ) == 0) doPwmRange   (argc, argv) ;
   else if (strcasecmp (argv [1], "pwmc"     ) == 0) doPwmClock   (argc, argv) ;
+  else if (strcasecmp (argv [1], "pwmTone"  ) == 0) doPwmTone    (argc, argv) ;
   else if (strcasecmp (argv [1], "drive"    ) == 0) doPadDrive   (argc, argv) ;
   else if (strcasecmp (argv [1], "readall"  ) == 0) doReadallOld () ;
   else if (strcasecmp (argv [1], "nreadall" ) == 0) doReadall    () ;
diff --git a/gpio/readall.c b/gpio/readall.c
index b363d3b..a92e58c 100644
--- a/gpio/readall.c
+++ b/gpio/readall.c
@@ -119,11 +119,16 @@ static int physToWpi [64] =
   13,  6,
   14, 10,
   -1, 11,       // 25, 26
-
-                                              -1, -1, -1, -1, -1,       // ... 31
-  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,       // ... 47
+  30, 31,	// Actually I2C, but not used
+  21, -1,
+  22, 26,
+  23, -1,
+  24, 27,
+  25, 28,
+  -1, 29,
+                                      -1, -1, -1, -1, -1, -1, -1,       // ... 47
   -1, -1, -1, -1, -1,							// ... 52
-  28, 29, 30, 31,							// ... 53, 54, 55, 56 - P5
+  17, 18, 19, 20,							// ... 53, 54, 55, 56 - P5
   -1, -1, -1, -1, -1, -1, -1,						// ... 63
 } ;
 
@@ -131,25 +136,34 @@ static char *physNames [64] =
 {
   NULL,
 
-  "3.3v",  "5v",
-  "SDA",   "5V",
-  "SCL",   "0v",
-  "GPIO7", "TxD",
-  "0v",    "RxD",
-  "GPIO0", "GPIO1",
-  "GPIO2", "0v",
-  "GPIO3", "GPIO4",
-  "3.3v",  "GPIO5",
-  "MOSI",  "0v",
-  "MISO",  "GPIO6",
-  "SCLK",  "CE0",
-  "0v",    "CE1",
-
-                                                         NULL,NULL,NULL,NULL,NULL,	// ... 31
-  NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,	// ... 47
-  NULL,NULL,NULL,NULL,NULL,								// ... 52
-  "GPIO8", "GPIO9", "GPIO10", "GPIO11",							// ... 53, 54, 55, 56 - P5
-  NULL,NULL,NULL,NULL,NULL,NULL,							// ... 63
+  "   3.3v", "5v     ",
+  "  SDA.1", "5V     ",
+  "  SCL.1", "0v     ",
+  "GPIO. 7", "TxD    ",
+  "     0v", "RxD    ",
+  "GPIO. 0", "GPIO. 1",
+  "GPIO. 2", "0v     ",
+  "GPIO. 3", "GPIO. 4",
+  "   3.3v", "GPIO. 5",
+  "   MOSI", "0v     ",
+  "   MISO", "GPIO. 6",
+  "   SCLK", "CE0    ",
+  "     0v", "CE1    ",
+  "  SDA.0", "SCL0   ",
+  "GPIO.21", "0v     ",
+  "GPIO.22", "GPIO.26",
+  "GPIO.23", "0v     ",
+  "GPIO.24", "GPIO.27",
+  "GPIO.25", "GPIO.28",
+  "     0v", "GPIO.29",
+       NULL, NULL,
+       NULL, NULL,
+       NULL, NULL,
+       NULL, NULL,
+       NULL, NULL,
+  "GPIO.17", "GPIO.18",
+  "GPIO.19", "GPIO.20",
+   NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
 } ;
 
 static void readallPhys (int physPin)
@@ -171,7 +185,7 @@ static void readallPhys (int physPin)
       printf (" | %3d", physToWpi     [physPin]) ;
   }
 
-  printf (" | %5s", physNames [physPin]) ;
+  printf (" | %s", physNames [physPin]) ;
 
   if (physToWpi [physPin] == -1)
     printf (" |      |   ") ;
@@ -264,6 +278,39 @@ int cmReadall (void)
 }
 
 
+/*
+ * bPlusReadall:
+ *	Read all the pins on the model B+
+ *********************************************************************************
+ */
+
+int bPlusReadall (void)
+{
+  int model, rev, mem ;
+  int pin ;
+  char *maker ;
+  char *name ;
+
+  piBoardId (&model, &rev, &mem, &maker) ;
+  if (model != PI_MODEL_BPLUS)
+    return FALSE ;
+
+  /**/ if (wpMode == WPI_MODE_GPIO)
+    name = "BCM" ;
+  else
+    name = "wPi" ;
+
+  printf (" +-----+---------+------+----+--B Plus--+----+------+---------+-----+\n") ;
+  printf (" | %s |   Name  | Mode | Val| Physical |Val | Mode | Name    | %s |\n", name, name) ;
+  printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ;
+  for (pin = 1 ; pin <= 40 ; pin += 2)
+    readallPhys (pin) ;
+  printf (" +-----+---------+------+----+----++----+----+------+---------+-----+\n") ;
+
+  return TRUE ;
+}
+
+
 void doReadall (void)
 {
   int pin ;
@@ -277,6 +324,9 @@ void doReadall (void)
   if (cmReadall ())
     return ;
 
+  if (bPlusReadall ())
+    return ;
+
   /**/ if (wpMode == WPI_MODE_GPIO)
   {
     printf (" +-----+-------+------+----+-Rev%d-----+----+------+-------+-----+\n", piBoardRev ()) ;
@@ -320,6 +370,9 @@ void doReadallOld (void)
   if (cmReadall ())
     return ;
 
+  if (bPlusReadall ())
+    return ;
+
   printf ("+----------+-Rev%d-+------+--------+------+-------+\n", piBoardRev ()) ;
   printf ("| wiringPi | GPIO | Phys | Name   | Mode | Value |\n") ;
   printf ("+----------+------+------+--------+------+-------+\n") ;
diff --git a/wiringPi/wiringPi.c b/wiringPi/wiringPi.c
index f0e8282..1e1cfe4 100644
--- a/wiringPi/wiringPi.c
+++ b/wiringPi/wiringPi.c
@@ -234,14 +234,16 @@ static void (*isrFunctions [64])(void) ;
 
 // pinToGpio:
 //	Take a Wiring pin (0 through X) and re-map it to the BCM_GPIO pin
-//	Cope for 2 different board revisions here.
+//	Cope for 3 different board revisions here.
 
 static int *pinToGpio ;
 
+// Revision 1, 1.1:
+
 static int pinToGpioR1 [64] =
 {
   17, 18, 21, 22, 23, 24, 25, 4,	// From the Original Wiki - GPIO 0 through 7:	wpi  0 -  7
-   0,  1,				// I2C  - SDA0, SCL0				wpi  8 -  9
+   0,  1,				// I2C  - SDA1, SCL1				wpi  8 -  9
    8,  7,				// SPI  - CE1, CE0				wpi 10 - 11
   10,  9, 11, 				// SPI  - MOSI, MISO, SCLK			wpi 12 - 14
   14, 15,				// UART - Tx, Rx				wpi 15 - 16
@@ -253,6 +255,8 @@ static int pinToGpioR1 [64] =
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	// ... 63
 } ;
 
+// Revision 2:
+
 static int pinToGpioR2 [64] =
 {
   17, 18, 27, 22, 23, 24, 25, 4,	// From the Original Wiki - GPIO 0 through 7:	wpi  0 -  7
@@ -260,11 +264,13 @@ static int pinToGpioR2 [64] =
    8,  7,				// SPI  - CE1, CE0				wpi 10 - 11
   10,  9, 11, 				// SPI  - MOSI, MISO, SCLK			wpi 12 - 14
   14, 15,				// UART - Tx, Rx				wpi 15 - 16
-  28, 29, 30, 31,			// New GPIOs 8 though 11			wpi 17 - 20
+  28, 29, 30, 31,			// Rev 2: New GPIOs 8 though 11			wpi 17 - 20
+   5,  6, 13, 19, 26,			// B+						wpi 21, 22, 23, 24, 25
+  12, 16, 20, 21,			// B+						wpi 26, 27, 28, 29
+   0,  1,				// B+						wpi 30, 31
 
 // Padding:
 
-                      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	// ... 31
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	// ... 47
   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	// ... 63
 } ;
@@ -316,16 +322,24 @@ static int physToGpioR2 [64] =
   11,  8,
   -1,  7,	// 25, 26
 
+// B+
+
+   0,  1,
+   5, -1,
+   6, 12,
+  13, -1,
+  19, 16,
+  26, 20,
+  -1, 21,
+
 // the P5 connector on the Rev 2 boards:
 
-                                              -1, -1, -1, -1, -1,	// ... 31
-  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,	// ... 47
+                           -1, -1, -1, -1, -1, -1, -1,			// ... 47
   -1, -1, -1, -1, -1,							// ... 52
   28, 29, 30, 31,							// ... 53, 54, 55, 56 - P5
   -1, -1, -1, -1, -1, -1, -1,						// ... 63
 } ;
 
-
 // gpioToGPFSEL:
 //	Map a BCM_GPIO pin to it's Function Selection
 //	control port. (GPFSEL 0-5)
@@ -395,7 +409,7 @@ static uint8_t gpioToEDS [] =
 } ;
 
 // gpioToREN
-//	(Word) offset to the Rising edgde ENable register
+//	(Word) offset to the Rising edge ENable register
 
 static uint8_t gpioToREN [] =
 {
@@ -563,6 +577,7 @@ int wiringPiFailure (int fatal, const char *message, ...)
  *	000d - Model B, Rev 2, 512MB, Egoman
  *	000e - Model B, Rev 2, 512MB, Sony
  *	000f - Model B, Rev 2, 512MB, Qisda
+ *	0010 - Model B+        512MB, Sony
  *	0011 - Pi compute Module
  *
  *	A small thorn is the olde style overvolting - that will add in
@@ -655,6 +670,7 @@ const char *piModelNames [] =
 {
   "Model A",
   "Model B",
+  "Model B+",
   "Compute Module",
 } ;
 
@@ -663,6 +679,7 @@ const char *piRevisionNames[] =
   "1",
   "1.1",
   "2",
+  "1.2",
 } ;
 
 void piBoardId (int *model, int *rev, int *mem, char **maker)
@@ -721,7 +738,8 @@ void piBoardId (int *model, int *rev, int *mem, char **maker)
   else if (strcmp (c, "000d") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; }
   else if (strcmp (c, "000e") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Sony"   ; }
   else if (strcmp (c, "000f") == 0) { *model = 1 ; *rev = 2 ; *mem = 512 ; *maker = "Egoman" ; }
-  else if (strcmp (c, "0011") == 0) { *model = 2 ; *rev = 1 ; *mem = 512 ; *maker = "Sony"   ; }
+  else if (strcmp (c, "0010") == 0) { *model = 2 ; *rev = 3 ; *mem = 512 ; *maker = "Sony"   ; }
+  else if (strcmp (c, "0011") == 0) { *model = 3 ; *rev = 1 ; *mem = 512 ; *maker = "Sony"   ; }
   else                              { *model = 0 ; *rev = 0 ; *mem =   0 ; *maker = "Unkn"   ; }
 }
  
@@ -1081,6 +1099,11 @@ void pinMode (int pin, int mode)
       softPwmCreate (origPin, 0, 100) ;
     else if (mode == SOFT_TONE_OUTPUT)
       softToneCreate (origPin) ;
+    else if (mode == PWM_TONE_OUTPUT)
+    {
+      pinMode (origPin, PWM_OUTPUT) ;	// Call myself to enable PWM mode
+      pwmSetMode (PWM_MODE_MS) ;
+    }
     else if (mode == PWM_OUTPUT)
     {
       if ((alt = gpioToPwmALT [pin]) == 0)	// Not a hardware capable PWM pin
@@ -1306,6 +1329,28 @@ void analogWrite (int pin, int value)
 }
 
 
+/*
+ * pwmToneWrite:
+ *	Pi Specific.
+ *      Output the given frequency on the Pi's PWM pin
+ *********************************************************************************
+ */
+
+void pwmToneWrite (int pin, int freq)
+{
+  int range ;
+
+  if (freq == 0)
+    pwmWrite (pin, 0) ;             // Off
+  else
+  {
+    range = 600000 / freq ;
+    pwmSetRange (range) ;
+    pwmWrite    (pin, freq / 2) ;
+  }
+}
+
+
 
 /*
  * digitalWriteByte:
@@ -1670,12 +1715,12 @@ int wiringPiSetup (void)
 
   boardRev = piBoardRev () ;
 
-  if (boardRev == 1)
+  /**/ if (boardRev == 1)	// A, B, Rev 1, 1.1
   {
      pinToGpio =  pinToGpioR1 ;
     physToGpio = physToGpioR1 ;
   }
-  else
+  else 				// A, B, Rev 2, B+, CM
   {
      pinToGpio =  pinToGpioR2 ;
     physToGpio = physToGpioR2 ;
diff --git a/wiringPi/wiringPi.h b/wiringPi/wiringPi.h
index 5117cc9..78ff87d 100644
--- a/wiringPi/wiringPi.h
+++ b/wiringPi/wiringPi.h
@@ -44,6 +44,7 @@
 #define	GPIO_CLOCK		 3
 #define	SOFT_PWM_OUTPUT		 4
 #define	SOFT_TONE_OUTPUT	 5
+#define	PWM_TONE_OUTPUT		 6
 
 #define	LOW			 0
 #define	HIGH			 1
@@ -70,7 +71,8 @@
 
 #define	PI_MODEL_A		0
 #define	PI_MODEL_B		1
-#define	PI_MODEL_CM		2
+#define	PI_MODEL_BPLUS		2
+#define	PI_MODEL_CM		3
 
 
 // Threads
@@ -128,7 +130,6 @@ extern "C" {
 
 extern const char *piModelNames [] ;
 extern const char *piRevisionNames[] ;
-extern const char *piComputeRevisionNames[] ;
 
 // Internal
 
@@ -167,6 +168,7 @@ extern int  wpiPinToGpio        (int wpiPin) ;
 extern int  physPinToGpio       (int physPin) ;
 extern void setPadDrive         (int group, int value) ;
 extern int  getAlt              (int pin) ;
+extern void pwmToneWrite        (int pin, int freq) ;
 extern void digitalWriteByte    (int value) ;
 extern void pwmSetMode          (int mode) ;
 extern void pwmSetRange         (unsigned int range) ;
-- 
1.7.2.5

