|
@@ -1,8 +1,10 @@
|
|
|
/*
|
|
|
* extensions.c:
|
|
|
- * Part of the GPIO program to test, peek, poke and otherwise
|
|
|
+ * Originally part of the GPIO program to test, peek, poke and otherwise
|
|
|
* noodle with the GPIO hardware on the Raspberry Pi.
|
|
|
- * Copyright (c) 2012-2013 Gordon Henderson
|
|
|
+ * Now used as a general purpose library to allow systems to dynamically
|
|
|
+ * add in new devices into wiringPi at program run-time.
|
|
|
+ * Copyright (c) 2012-2015 Gordon Henderson
|
|
|
***********************************************************************
|
|
|
* This file is part of wiringPi:
|
|
|
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
|
@@ -26,6 +28,7 @@
|
|
|
#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <stdint.h>
|
|
|
+#include <stdarg.h>
|
|
|
#include <ctype.h>
|
|
|
#include <string.h>
|
|
|
#include <unistd.h>
|
|
@@ -35,27 +38,31 @@
|
|
|
|
|
|
#include <wiringPi.h>
|
|
|
|
|
|
-#include <mcp23008.h>
|
|
|
-#include <mcp23016.h>
|
|
|
-#include <mcp23017.h>
|
|
|
-#include <mcp23s08.h>
|
|
|
-#include <mcp23s17.h>
|
|
|
-#include <sr595.h>
|
|
|
-#include <pcf8591.h>
|
|
|
-#include <pcf8574.h>
|
|
|
-#include <max31855.h>
|
|
|
-#include <max5322.h>
|
|
|
-#include <mcp3002.h>
|
|
|
-#include <mcp3004.h>
|
|
|
-#include <mcp4802.h>
|
|
|
-#include <mcp3422.h>
|
|
|
-#include <sn3218.h>
|
|
|
-#include <drcSerial.h>
|
|
|
-
|
|
|
-#include "extensions.h"
|
|
|
+#include "mcp23008.h"
|
|
|
+#include "mcp23016.h"
|
|
|
+#include "mcp23017.h"
|
|
|
+#include "mcp23s08.h"
|
|
|
+#include "mcp23s17.h"
|
|
|
+#include "sr595.h"
|
|
|
+#include "pcf8591.h"
|
|
|
+#include "pcf8574.h"
|
|
|
+#include "max31855.h"
|
|
|
+#include "max5322.h"
|
|
|
+#include "mcp3002.h"
|
|
|
+#include "mcp3004.h"
|
|
|
+#include "mcp4802.h"
|
|
|
+#include "mcp3422.h"
|
|
|
+#include "sn3218.h"
|
|
|
+#include "drcSerial.h"
|
|
|
+
|
|
|
+#include "wpiExtensions.h"
|
|
|
|
|
|
extern int wiringPiDebug ;
|
|
|
|
|
|
+static int verbose ;
|
|
|
+static char errorMessage [1024] ;
|
|
|
+
|
|
|
+
|
|
|
#ifndef TRUE
|
|
|
# define TRUE (1==1)
|
|
|
# define FALSE (1==2)
|
|
@@ -70,6 +77,24 @@ struct extensionFunctionStruct
|
|
|
} ;
|
|
|
|
|
|
|
|
|
+/*
|
|
|
+ * verbError:
|
|
|
+ * Convenient error handling
|
|
|
+ *********************************************************************************
|
|
|
+ */
|
|
|
+
|
|
|
+static void verbError (const char *message, ...)
|
|
|
+{
|
|
|
+ va_list argp ;
|
|
|
+ va_start (argp, message) ;
|
|
|
+ vsnprintf (errorMessage, 1023, message, argp) ;
|
|
|
+ va_end (argp) ;
|
|
|
+
|
|
|
+ if (verbose)
|
|
|
+ fprintf (stderr, "%s\n", errorMessage) ;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
/*
|
|
|
* extractInt:
|
|
|
* Check & return an integer at the given location (prefixed by a :)
|
|
@@ -80,7 +105,7 @@ static char *extractInt (char *progName, char *p, int *num)
|
|
|
{
|
|
|
if (*p != ':')
|
|
|
{
|
|
|
- fprintf (stderr, "%s: colon expected\n", progName) ;
|
|
|
+ verbError ("%s: colon expected", progName) ;
|
|
|
return NULL ;
|
|
|
}
|
|
|
|
|
@@ -88,7 +113,7 @@ static char *extractInt (char *progName, char *p, int *num)
|
|
|
|
|
|
if (!isdigit (*p))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: digit expected\n", progName) ;
|
|
|
+ verbError ("%s: digit expected", progName) ;
|
|
|
return NULL ;
|
|
|
}
|
|
|
|
|
@@ -112,7 +137,7 @@ static char *extractStr (char *progName, char *p, char **str)
|
|
|
|
|
|
if (*p != ':')
|
|
|
{
|
|
|
- fprintf (stderr, "%s: colon expected\n", progName) ;
|
|
|
+ verbError ("%s: colon expected", progName) ;
|
|
|
return NULL ;
|
|
|
}
|
|
|
|
|
@@ -120,7 +145,7 @@ static char *extractStr (char *progName, char *p, char **str)
|
|
|
|
|
|
if (!isprint (*p))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: character expected\n", progName) ;
|
|
|
+ verbError ("%s: character expected", progName) ;
|
|
|
return NULL ;
|
|
|
}
|
|
|
|
|
@@ -152,9 +177,9 @@ static int doExtensionMcp23008 (char *progName, int pinBase, char *params)
|
|
|
if ((params = extractInt (progName, params, &i2c)) == NULL)
|
|
|
return FALSE ;
|
|
|
|
|
|
- if ((i2c < 0x03) || (i2c > 0x77))
|
|
|
+ if ((i2c < 0x01) || (i2c > 0x77))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
|
|
+ verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -180,7 +205,7 @@ static int doExtensionMcp23016 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((i2c < 0x03) || (i2c > 0x77))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
|
|
+ verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -206,7 +231,7 @@ static int doExtensionMcp23017 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((i2c < 0x03) || (i2c > 0x77))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
|
|
+ verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -232,7 +257,7 @@ static int doExtensionMcp23s08 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((spi < 0) || (spi > 1))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
|
|
|
+ verbError ("%s: SPI address (%d) out of range", progName, spi) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -241,7 +266,7 @@ static int doExtensionMcp23s08 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((port < 0) || (port > 7))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
|
|
|
+ verbError ("%s: port address (%d) out of range", progName, port) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -267,7 +292,7 @@ static int doExtensionMcp23s17 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((spi < 0) || (spi > 1))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: SPI address (%d) out of range\n", progName, spi) ;
|
|
|
+ verbError ("%s: SPI address (%d) out of range", progName, spi) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -276,7 +301,7 @@ static int doExtensionMcp23s17 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((port < 0) || (port > 7))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: port address (%d) out of range\n", progName, port) ;
|
|
|
+ verbError ("%s: port address (%d) out of range", progName, port) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -304,7 +329,7 @@ static int doExtensionSr595 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((pins < 8) || (pins > 32))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: pin count (%d) out of range - 8-32 expected.\n", progName, pins) ;
|
|
|
+ verbError ("%s: pin count (%d) out of range - 8-32 expected.", progName, pins) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -339,7 +364,7 @@ static int doExtensionPcf8574 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((i2c < 0x03) || (i2c > 0x77))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
|
|
+ verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -365,7 +390,7 @@ static int doExtensionPcf8591 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((i2c < 0x03) || (i2c > 0x77))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
|
|
+ verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -391,7 +416,7 @@ static int doExtensionMax31855 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((spi < 0) || (spi > 1))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
|
|
+ verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -417,7 +442,7 @@ static int doExtensionMcp3002 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((spi < 0) || (spi > 1))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
|
|
+ verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -443,7 +468,7 @@ static int doExtensionMcp3004 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((spi < 0) || (spi > 1))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
|
|
+ verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -469,7 +494,7 @@ static int doExtensionMax5322 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((spi < 0) || (spi > 1))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
|
|
+ verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -495,7 +520,7 @@ static int doExtensionMcp4802 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((spi < 0) || (spi > 1))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
|
|
|
+ verbError ("%s: SPI channel (%d) out of range", progName, spi) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -535,7 +560,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((i2c < 0x03) || (i2c > 0x77))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
|
|
|
+ verbError ("%s: i2c address (0x%X) out of range", progName, i2c) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -544,7 +569,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((sampleRate < 0) || (sampleRate > 3))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: sample rate (%d) out of range\n", progName, sampleRate) ;
|
|
|
+ verbError ("%s: sample rate (%d) out of range", progName, sampleRate) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -553,7 +578,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((gain < 0) || (gain > 3))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: gain (%d) out of range\n", progName, gain) ;
|
|
|
+ verbError ("%s: gain (%d) out of range", progName, gain) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -562,6 +587,7 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
|
|
|
return TRUE ;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/*
|
|
|
* doExtensionDrcS:
|
|
|
* Interface to a DRC Serial system
|
|
@@ -579,7 +605,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((pins < 1) || (pins > 100))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: pins (%d) out of range (2-100)\n", progName, pins) ;
|
|
|
+ verbError ("%s: pins (%d) out of range (2-100)", progName, pins) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -588,7 +614,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if (strlen (port) == 0)
|
|
|
{
|
|
|
- fprintf (stderr, "%s: serial port device name required\n", progName) ;
|
|
|
+ verbError ("%s: serial port device name required", progName) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -597,7 +623,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params)
|
|
|
|
|
|
if ((baud < 1) || (baud > 4000000))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: baud rate (%d) out of range\n", progName, baud) ;
|
|
|
+ verbError ("%s: baud rate (%d) out of range", progName, baud) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -613,7 +639,7 @@ static int doExtensionDrcS (char *progName, int pinBase, char *params)
|
|
|
*********************************************************************************
|
|
|
*/
|
|
|
|
|
|
-struct extensionFunctionStruct extensionFunctions [] =
|
|
|
+static struct extensionFunctionStruct extensionFunctions [] =
|
|
|
{
|
|
|
{ "mcp23008", &doExtensionMcp23008 },
|
|
|
{ "mcp23016", &doExtensionMcp23016 },
|
|
@@ -636,18 +662,20 @@ struct extensionFunctionStruct extensionFunctions [] =
|
|
|
|
|
|
|
|
|
/*
|
|
|
- * doExtension:
|
|
|
+ * loadWPiExtension:
|
|
|
* Load in a wiringPi extension
|
|
|
*********************************************************************************
|
|
|
*/
|
|
|
|
|
|
-int doExtension (char *progName, char *extensionData)
|
|
|
+int loadWPiExtension (char *progName, char *extensionData, int printErrors)
|
|
|
{
|
|
|
char *p ;
|
|
|
char *extension = extensionData ;
|
|
|
struct extensionFunctionStruct *extensionFn ;
|
|
|
int pinBase = 0 ;
|
|
|
|
|
|
+ verbose = printErrors ;
|
|
|
+
|
|
|
// Get the extension extension name by finding the first colon
|
|
|
|
|
|
p = extension ;
|
|
@@ -655,7 +683,7 @@ int doExtension (char *progName, char *extensionData)
|
|
|
{
|
|
|
if (!*p) // ran out of characters
|
|
|
{
|
|
|
- fprintf (stderr, "%s: extension name not terminated by a colon\n", progName) ;
|
|
|
+ verbError ("%s: extension name not terminated by a colon", progName) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
++p ;
|
|
@@ -665,7 +693,7 @@ int doExtension (char *progName, char *extensionData)
|
|
|
|
|
|
if (!isdigit (*p))
|
|
|
{
|
|
|
- fprintf (stderr, "%s: pinBase number expected after extension name\n", progName) ;
|
|
|
+ verbError ("%s: pinBase number expected after extension name", progName) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -673,7 +701,7 @@ int doExtension (char *progName, char *extensionData)
|
|
|
{
|
|
|
if (pinBase > 1000000000)
|
|
|
{
|
|
|
- fprintf (stderr, "%s: pinBase too large\n", progName) ;
|
|
|
+ verbError ("%s: pinBase too large", progName) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -683,7 +711,7 @@ int doExtension (char *progName, char *extensionData)
|
|
|
|
|
|
if (pinBase < 64)
|
|
|
{
|
|
|
- fprintf (stderr, "%s: pinBase (%d) too small. Minimum is 64.\n", progName, pinBase) ;
|
|
|
+ verbError ("%s: pinBase (%d) too small. Minimum is 64.", progName, pinBase) ;
|
|
|
return FALSE ;
|
|
|
}
|
|
|
|
|
@@ -695,6 +723,6 @@ int doExtension (char *progName, char *extensionData)
|
|
|
return extensionFn->function (progName, pinBase, p) ;
|
|
|
}
|
|
|
|
|
|
- fprintf (stderr, "%s: extension %s not found\n", progName, extension) ;
|
|
|
+ verbError ("%s: extension %s not found", progName, extension) ;
|
|
|
return FALSE ;
|
|
|
}
|