From 711017ec363de7bfc012e84afcbf2270ef147dc2 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 21 May 2013 22:38:16 +0100 Subject: [PATCH] More typos, added mcp3004/mcp3008 --- People | 3 ++ gpio/extensions.c | 130 +++++++++++++++++++++++++++++++++++++++++++++ gpio/gpio.c | 2 +- wiringPi/Makefile | 4 +- wiringPi/mcp3004.c | 73 +++++++++++++++++++++++++ wiringPi/mcp3004.h | 33 ++++++++++++ wiringPi/mcp3422.c | 5 +- wiringPi/mcp3422.h | 2 +- 8 files changed, 247 insertions(+), 5 deletions(-) create mode 100644 wiringPi/mcp3004.c create mode 100644 wiringPi/mcp3004.h diff --git a/People b/People index 92d1ea4..b339494 100644 --- 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... diff --git a/gpio/extensions.c b/gpio/extensions.c index dae1eac..637dc8c 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -43,6 +43,10 @@ #include #include #include +#include +#include +#include +#include #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 }, } ; diff --git a/gpio/gpio.c b/gpio/gpio.c index 871548e..1933a69 100644 --- a/gpio/gpio.c +++ b/gpio/gpio.c @@ -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 ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index f0120bb..16bc476 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -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 index 0000000..1c9357f --- /dev/null +++ b/wiringPi/mcp3004.c @@ -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 . + *********************************************************************** + */ + +#include +#include + +#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 index 0000000..a07c0bf --- /dev/null +++ b/wiringPi/mcp3004.h @@ -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 . + *********************************************************************** + */ + +#ifdef __cplusplus +extern "C" { +#endif + +extern int mcp3004Setup (int pinBase, int spiChannel) ; + +#ifdef __cplusplus +} +#endif diff --git a/wiringPi/mcp3422.c b/wiringPi/mcp3422.c index 8e26d76..4d07abc 100644 --- a/wiringPi/mcp3422.c +++ b/wiringPi/mcp3422.c @@ -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 ; diff --git a/wiringPi/mcp3422.h b/wiringPi/mcp3422.h index 8b4e350..bb4692d 100644 --- a/wiringPi/mcp3422.h +++ b/wiringPi/mcp3422.h @@ -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 } -- 2.30.2