chiark / gitweb /
More typos, added mcp3004/mcp3008
authorGordon Henderson <projects@drogon.net>
Tue, 21 May 2013 21:38:16 +0000 (22:38 +0100)
committerGordon Henderson <projects@drogon.net>
Tue, 21 May 2013 21:38:16 +0000 (22:38 +0100)
People
gpio/extensions.c
gpio/gpio.c
wiringPi/Makefile
wiringPi/mcp3004.c [new file with mode: 0644]
wiringPi/mcp3004.h [new file with mode: 0644]
wiringPi/mcp3422.c
wiringPi/mcp3422.h

diff --git a/People b/People
index 92d1ea4b1ec802c2eacd7443db63f593f1dc646f..b339494e17b7f1e7bf03d63c415be4004b9aba11 100644 (file)
--- a/People
+++ b/People
@@ -28,3 +28,6 @@ Xian Stannard
 
 Andre Crone
   Suggested the __WIRING_PI.H__ round wiringPi.h
+
+Rik Teerling
+  Pointing out some silly mistooks in the I2C code...
index dae1eacec5239040505b18e08466005ded4196c4..637dc8ccd3fae1962af95c526b22f80fccbcbe37 100644 (file)
 #include <sr595.h>
 #include <pcf8591.h>
 #include <pcf8574.h>
+#include <mcp3002.h>
+#include <mcp3004.h>
+#include <mcp4802.h>
+#include <mcp3422.h>
 
 #include "extensions.h"
 
@@ -330,6 +334,128 @@ static int doExtensionPcf8591 (char *progName, int pinBase, char *params)
 }
 
 
+/*
+ * doExtensionMcp3002:
+ *     Analog IO
+ *     mcp3002:base:spiChan
+ *********************************************************************************
+ */
+
+static int doExtensionMcp3002 (char *progName, int pinBase, char *params)
+{
+  int spi ;
+
+  if ((params = extractInt (progName, params, &spi)) == NULL)
+    return FALSE ;
+
+  if ((spi < 0) || (spi > 1))
+  {
+    fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
+    return FALSE ;
+  }
+
+  mcp3002Setup (pinBase, spi) ;
+
+  return TRUE ;
+}
+
+
+/*
+ * doExtensionMcp3004:
+ *     Analog IO
+ *     mcp3004:base:spiChan
+ *********************************************************************************
+ */
+
+static int doExtensionMcp3004 (char *progName, int pinBase, char *params)
+{
+  int spi ;
+
+  if ((params = extractInt (progName, params, &spi)) == NULL)
+    return FALSE ;
+
+  if ((spi < 0) || (spi > 1))
+  {
+    fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
+    return FALSE ;
+  }
+
+  mcp3004Setup (pinBase, spi) ;
+
+  return TRUE ;
+}
+
+
+/*
+ * doExtensionMcp4802:
+ *     Analog IO
+ *     mcp4802:base:spiChan
+ *********************************************************************************
+ */
+
+static int doExtensionMcp4802 (char *progName, int pinBase, char *params)
+{
+  int spi ;
+
+  if ((params = extractInt (progName, params, &spi)) == NULL)
+    return FALSE ;
+
+  if ((spi < 0) || (spi > 1))
+  {
+    fprintf (stderr, "%s: SPI channel (%d) out of range\n", progName, spi) ;
+    return FALSE ;
+  }
+
+  mcp4802Setup (pinBase, spi) ;
+
+  return TRUE ;
+}
+
+
+/*
+ * doExtensionMcp3422:
+ *     Analog IO
+ *     mcp3422:base:i2cAddr
+ *********************************************************************************
+ */
+
+static int doExtensionMcp3422 (char *progName, int pinBase, char *params)
+{
+  int i2c, sampleRate, gain ;
+
+  if ((params = extractInt (progName, params, &i2c)) == NULL)
+    return FALSE ;
+
+  if ((i2c < 0x03) || (i2c > 0x77))
+  {
+    fprintf (stderr, "%s: i2c address (0x%X) out of range\n", progName, i2c) ;
+    return FALSE ;
+  }
+
+  if ((params = extractInt (progName, params, &sampleRate)) == NULL)
+    return FALSE ;
+
+  if ((sampleRate < 0) || (sampleRate > 3))
+  {
+    fprintf (stderr, "%s: sample rate (%d) out of range\n", progName, sampleRate) ;
+    return FALSE ;
+  }
+
+  if ((params = extractInt (progName, params, &gain)) == NULL)
+    return FALSE ;
+
+  if ((gain < 0) || (gain > 3))
+  {
+    fprintf (stderr, "%s: gain (%d) out of range\n", progName, gain) ;
+    return FALSE ;
+  }
+
+  mcp3422Setup (pinBase, i2c, sampleRate, gain) ;
+
+  return TRUE ;
+}
+
+
 /*
  * Function list
  *********************************************************************************
@@ -345,6 +471,10 @@ struct extensionFunctionStruct extensionFunctions [] =
   { "sr595",           &doExtensionSr595       },
   { "pcf8574",         &doExtensionPcf8574     },
   { "pcf8591",         &doExtensionPcf8591     },
+  { "mcp3002",         &doExtensionMcp3002     },
+  { "mcp3004",         &doExtensionMcp3004     },
+  { "mcp4802",         &doExtensionMcp4802     },
+  { "mcp3422",         &doExtensionMcp3422     },
   { NULL,              NULL                    },
 } ;
 
index 871548ebaa7d0b445ba2f9bc0e1e81855266ddcd..1933a694f2566dd581ef0cbdc411a9146395792e 100644 (file)
@@ -48,7 +48,7 @@ extern int wiringPiDebug ;
 #  define      FALSE   (1==2)
 #endif
 
-#define        VERSION         "2.05"
+#define        VERSION         "2.06"
 #define        I2CDETECT       "/usr/sbin/i2cdetect"
 
 static int wpMode ;
index f0120bb2f001f3b857c72ddb1cdf9f25ba7991e9..16bc47628336c18d44b574d92a5251ef6633c438 100644 (file)
@@ -51,7 +51,7 @@ SRC   =       wiringPi.c                                              \
                mcp23s08.c mcp23s17.c                                   \
                sr595.c                                                 \
                pcf8574.c pcf8591.c                                     \
-               mcp3002.c mcp4802.c mcp3422.c                           \
+               mcp3002.c mcp3004.c mcp4802.c mcp3422.c                 \
                drc.c
 
 OBJ    =       $(SRC:.c=.o)
@@ -102,6 +102,7 @@ install-headers:
        @install -m 0644 mcp23s08.h             $(DESTDIR)$(PREFIX)/include
        @install -m 0644 mcp23s17.h             $(DESTDIR)$(PREFIX)/include
        @install -m 0644 mcp3002.h              $(DESTDIR)$(PREFIX)/include
+       @install -m 0644 mcp3004.h              $(DESTDIR)$(PREFIX)/include
        @install -m 0644 mcp4802.h              $(DESTDIR)$(PREFIX)/include
        @install -m 0644 mcp3422.h              $(DESTDIR)$(PREFIX)/include
        @install -m 0644 sr595.h                $(DESTDIR)$(PREFIX)/include
@@ -138,6 +139,7 @@ uninstall:
        @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h
        @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h
        @rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h
+       @rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h
        @rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h
        @rm -f $(DESTDIR)$(PREFIX)/include/mcp3422.h
        @rm -f $(DESTDIR)$(PREFIX)/include/sr595.h
diff --git a/wiringPi/mcp3004.c b/wiringPi/mcp3004.c
new file mode 100644 (file)
index 0000000..1c9357f
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * mcp3004.c:
+ *     Extend wiringPi with the MCP3004 SPI Analog to Digital convertor
+ *     Copyright (c) 2012-2013 Gordon Henderson
+ ***********************************************************************
+ * This file is part of wiringPi:
+ *     https://projects.drogon.net/raspberry-pi/wiringpi/
+ *
+ *    wiringPi is free software: you can redistribute it and/or modify
+ *    it under the terms of the GNU Lesser General Public License as
+ *    published by the Free Software Foundation, either version 3 of the
+ *    License, or (at your option) any later version.
+ *
+ *    wiringPi is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU Lesser General Public License for more details.
+ *
+ *    You should have received a copy of the GNU Lesser General Public
+ *    License along with wiringPi.
+ *    If not, see <http://www.gnu.org/licenses/>.
+ ***********************************************************************
+ */
+
+#include <wiringPi.h>
+#include <wiringPiSPI.h>
+
+#include "mcp3004.h"
+
+/*
+ * myAnalogRead:
+ *     Return the analog value of the given pin
+ *********************************************************************************
+ */
+
+static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
+{
+  unsigned char spiData [2] ;
+  unsigned char chanBits ;
+  int chan = pin - node->pinBase ;
+
+  chanBits = 0b11000000 | (chan << 3) ;
+
+  spiData [0] = chanBits ;
+  spiData [1] = 0 ;
+
+  wiringPiSPIDataRW (node->fd, spiData, 2) ;
+
+  return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
+}
+
+
+/*
+ * mcp3004Setup:
+ *     Create a new wiringPi device node for an mcp3004 on the Pi's
+ *     SPI interface.
+ *********************************************************************************
+ */
+
+int mcp3004Setup (const int pinBase, int spiChannel)
+{
+  struct wiringPiNodeStruct *node ;
+
+  if (wiringPiSPISetup (spiChannel, 1000000) < 0)
+    return -1 ;
+
+  node = wiringPiNewNode (pinBase, 8) ;
+
+  node->fd         = spiChannel ;
+  node->analogRead = myAnalogRead ;
+
+  return 0 ;
+}
diff --git a/wiringPi/mcp3004.h b/wiringPi/mcp3004.h
new file mode 100644 (file)
index 0000000..a07c0bf
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * mcp3004.c:
+ *     Extend wiringPi with the MCP3004 SPI Analog to Digital convertor
+ *     Copyright (c) 2012-2013 Gordon Henderson
+ ***********************************************************************
+ * This file is part of wiringPi:
+ *     https://projects.drogon.net/raspberry-pi/wiringpi/
+ *
+ *    wiringPi is free software: you can redistribute it and/or modify
+ *    it under the terms of the GNU Lesser General Public License as
+ *    published by the Free Software Foundation, either version 3 of the
+ *    License, or (at your option) any later version.
+ *
+ *    wiringPi is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU Lesser General Public License for more details.
+ *
+ *    You should have received a copy of the GNU Lesser General Public
+ *    License along with wiringPi.
+ *    If not, see <http://www.gnu.org/licenses/>.
+ ***********************************************************************
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int mcp3004Setup (int pinBase, int spiChannel) ;
+
+#ifdef __cplusplus
+}
+#endif
index 8e26d76f7fcd4232df0916ebbbe5390b5ee18814..4d07abcbcb256b4b9099923bf671d949726fc267 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * mcp3422.c:
  *     Extend wiringPi with the MCP3422 I2C ADC chip
+ *     Also works for the MCP3423 and MCP3224 (4 channel) chips
  *     Copyright (c) 2013 Gordon Henderson
  ***********************************************************************
  * This file is part of wiringPi:
@@ -98,7 +99,7 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
  *********************************************************************************
  */
 
-int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain)
+int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
 {
   int fd ;
   struct wiringPiNodeStruct *node ;
@@ -106,7 +107,7 @@ int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int
   if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
     return fd ;
 
-  node = wiringPiNewNode (pinBase, channels) ;
+  node = wiringPiNewNode (pinBase, 4) ;
 
   node->data0      = sampleRate ;
   node->data1      = gain ;
index 8b4e350be728e552bfcf1f198ac28ba2510eaf30..bb4692d841cc6caed92116a37479f4fad6a2d878 100644 (file)
@@ -36,7 +36,7 @@
 extern "C" {
 #endif
 
-extern int mcp3422Setup (int pinBase, int i2cAddress, int channels, int sampleRate, int gain) ;
+extern int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain) ;
 
 #ifdef __cplusplus
 }