Andre Crone
Suggested the __WIRING_PI.H__ round wiringPi.h
+
+Rik Teerling
+ Pointing out some silly mistooks in the I2C code...
#include <sr595.h>
#include <pcf8591.h>
#include <pcf8574.h>
+#include <mcp3002.h>
+#include <mcp3004.h>
+#include <mcp4802.h>
+#include <mcp3422.h>
#include "extensions.h"
}
+/*
+ * 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
*********************************************************************************
{ "sr595", &doExtensionSr595 },
{ "pcf8574", &doExtensionPcf8574 },
{ "pcf8591", &doExtensionPcf8591 },
+ { "mcp3002", &doExtensionMcp3002 },
+ { "mcp3004", &doExtensionMcp3004 },
+ { "mcp4802", &doExtensionMcp4802 },
+ { "mcp3422", &doExtensionMcp3422 },
{ NULL, NULL },
} ;
# define FALSE (1==2)
#endif
-#define VERSION "2.05"
+#define VERSION "2.06"
#define I2CDETECT "/usr/sbin/i2cdetect"
static int wpMode ;
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)
@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
@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
--- /dev/null
+/*
+ * 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 ;
+}
--- /dev/null
+/*
+ * 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
/*
* 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:
*********************************************************************************
*/
-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 ;
if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
return fd ;
- node = wiringPiNewNode (pinBase, channels) ;
+ node = wiringPiNewNode (pinBase, 4) ;
node->data0 = sampleRate ;
node->data1 = gain ;
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
}