From 5e16e15a59437fc93d75f31d9c37abfe9febd479 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Sun, 28 Jul 2013 10:54:32 +0100 Subject: [PATCH] tidied and tested DRC Serial (renamed it it drcSerial too) Tweaked the mcp3422 code altered the build script to let me build static --- build | 24 +++++-- gpio/Makefile | 7 +- gpio/extensions.c | 84 ++++++++++++++++++++++ wiringPi/Makefile | 4 +- wiringPi/drc.c | 203 ----------------------------------------------------- wiringPi/drc.h | 34 --------- wiringPi/mcp3422.c | 29 +++----- 7 files changed, 120 insertions(+), 265 deletions(-) delete mode 100644 wiringPi/drc.c delete mode 100644 wiringPi/drc.h diff --git a/build b/build index 01fa706..e17cf18 100755 --- a/build +++ b/build @@ -51,18 +51,30 @@ fi echo "WiringPi Library" cd wiringPi sudo make uninstall - make - check_make_ok - sudo make install + if [ x$1 = "xstatic" ]; then + make static + check_make_ok + sudo make install-static + else + make + check_make_ok + sudo make install + fi check_make_ok echo echo "WiringPi Devices Library" cd ../devLib sudo make uninstall - make - check_make_ok - sudo make install + if [ x$1 = "xstatic" ]; then + make static + check_make_ok + sudo make install-static + else + make + check_make_ok + sudo make install + fi check_make_ok echo diff --git a/gpio/Makefile b/gpio/Makefile index b4519fa..8fa12d5 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -1,9 +1,10 @@ # # Makefile: -# wiringPi - Wiring Compatable library for the Raspberry Pi +# The gpio command: +# A swiss-army knige of GPIO shenanigans. # https://projects.drogon.net/wiring-pi # -# Copyright (c) 2012 Gordon Henderson +# Copyright (c) 2012-2013 Gordon Henderson ################################################################################# # This file is part of wiringPi: # Wiring Compatable library for the Raspberry Pi @@ -28,7 +29,7 @@ PREFIX=/local #DEBUG = -g -O0 DEBUG = -O2 CC = gcc -INCLUDE = -I$(DESTDIR)$(PREFIX)/usr/local/include +INCLUDE = -I$(DESTDIR)$(PREFIX)/include CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe LDFLAGS = -L$(DESTDIR)$(PREFIX)/lib diff --git a/gpio/extensions.c b/gpio/extensions.c index c7e5308..96d6255 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -50,6 +50,7 @@ #include #include #include +#include #include "extensions.h" @@ -99,6 +100,43 @@ static char *extractInt (char *progName, char *p, int *num) } +/* + * extractStr: + * Check & return a string at the given location (prefixed by a :) + ********************************************************************************* + */ + +static char *extractStr (char *progName, char *p, char **str) +{ + char *q, *r ; + + if (*p != ':') + { + fprintf (stderr, "%s: colon expected\n", progName) ; + return NULL ; + } + + ++p ; + + if (!isprint (*p)) + { + fprintf (stderr, "%s: character expected\n", progName) ; + return NULL ; + } + + q = p ; + while ((*q != 0) && (*q != ':')) + ++q ; + + *str = r = calloc (q - p + 2, 1) ; // Zeros it + + while (p != q) + *r++ = *p++ ; + + return p ; +} + + /* * doExtensionMcp23008: @@ -524,6 +562,51 @@ static int doExtensionMcp3422 (char *progName, int pinBase, char *params) return TRUE ; } +/* + * doExtensionDrcS: + * Interface to a DRC Serial system + * drcs:base:pins:serialPort:baud + ********************************************************************************* + */ + +static int doExtensionDrcS (char *progName, int pinBase, char *params) +{ + char *port ; + int pins, baud ; + + if ((params = extractInt (progName, params, &pins)) == NULL) + return FALSE ; + + if ((pins < 1) || (pins > 100)) + { + fprintf (stderr, "%s: pins (%d) out of range (2-100)\n", progName, pins) ; + return FALSE ; + } + + if ((params = extractStr (progName, params, &port)) == NULL) + return FALSE ; + + if (strlen (port) == 0) + { + fprintf (stderr, "%s: serial port device name required\n", progName) ; + return FALSE ; + } + + if ((params = extractInt (progName, params, &baud)) == NULL) + return FALSE ; + + if ((baud < 1) || (baud > 4000000)) + { + fprintf (stderr, "%s: baud rate (%d) out of range\n", progName, baud) ; + return FALSE ; + } + + drcSetupSerial (pinBase, pins, port, baud) ; + + return TRUE ; +} + + /* * Function list @@ -547,6 +630,7 @@ struct extensionFunctionStruct extensionFunctions [] = { "max31855", &doExtensionMax31855 }, { "max5322", &doExtensionMax5322 }, { "sn3218", &doExtensionSn3218 }, + { "drcs", &doExtensionDrcS }, { NULL, NULL }, } ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index ed4f6be..619a3dc 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -55,7 +55,7 @@ SRC = wiringPi.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ max31855.c max5322.c \ sn3218.c \ - drc.c + drcSerial.c OBJ = $(SRC:.c=.o) @@ -99,6 +99,7 @@ install-headers: @install -m 0644 softTone.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiSPI.h $(DESTDIR)$(PREFIX)/include @install -m 0644 wiringPiI2C.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 drcSerial.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23008.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23016.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23017.h $(DESTDIR)$(PREFIX)/include @@ -139,6 +140,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/softTone.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiSPI.h @rm -f $(DESTDIR)$(PREFIX)/include/wiringPiI2C.h + @rm -f $(DESTDIR)$(PREFIX)/include/drcSerial.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23008.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23016.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23017.h diff --git a/wiringPi/drc.c b/wiringPi/drc.c deleted file mode 100644 index 07baf17..0000000 --- a/wiringPi/drc.c +++ /dev/null @@ -1,203 +0,0 @@ -/* - * drc.c: - * Extend wiringPi with the DRC control protocll to Arduino - * Copyright (c) 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 -#include - -#include "wiringPi.h" -#include "wiringSerial.h" - -#include "drc.h" - -#ifndef TRUE -# define TRUE (1==1) -# define FALSE (1==2) -#endif - - -/* - * myPinMode: - * Change the pin mode on the remote DRC device - ********************************************************************************* - */ - -static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode) -{ - /**/ if (mode == OUTPUT) - serialPutchar (node->fd, 'o') ; // Input - else if (mode == PWM_OUTPUT) - serialPutchar (node->fd, 'p') ; // PWM - else - serialPutchar (node->fd, 'i') ; // Default to input - - serialPutchar (node->fd, pin - node->pinBase) ; -} - - -/* - * myPullUpDnControl: - * ATmegas only have pull-up's on of off. No pull-downs. - ********************************************************************************* - */ - -static void myPullUpDnControl (struct wiringPiNodeStruct *node, int pin, int mode) -{ - -// Force pin into input mode - - serialPutchar (node->fd, 'i' ) ; - serialPutchar (node->fd, pin) ; - - /**/ if (mode == PUD_UP) - { - serialPutchar (node->fd, '1') ; - serialPutchar (node->fd, pin - node->pinBase) ; - } - else if (mode == PUD_OFF) - { - serialPutchar (node->fd, '0') ; - serialPutchar (node->fd, pin - node->pinBase) ; - } -} - - -/* - * myDigitalWrite: - ********************************************************************************* - */ - -static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value) -{ - serialPutchar (node->fd, value == 0 ? '0' : '1') ; - serialPutchar (node->fd, pin - node->pinBase) ; -} - - -/* - * myPwmWrite: - ********************************************************************************* - */ - -static void myPwmWrite (struct wiringPiNodeStruct *node, int pin, int value) -{ - serialPutchar (node->fd, 'v') ; - serialPutchar (node->fd, pin - node->pinBase) ; - serialPutchar (node->fd, value & 0xFF) ; -} - - -/* - * myAnalogRead: - ********************************************************************************* - */ - -static int myAnalogRead (struct wiringPiNodeStruct *node, int pin) -{ - int vHi, vLo ; - - serialPutchar (node->fd, 'a') ; - serialPutchar (node->fd, pin - node->pinBase) ; - vHi = serialGetchar (node->fd) ; - vLo = serialGetchar (node->fd) ; - - return (vHi << 8) | vLo ; -} - - -/* - * myDigitalRead: - ********************************************************************************* - */ - -static int myDigitalRead (struct wiringPiNodeStruct *node, int pin) -{ - serialPutchar (node->fd, 'r') ; // Send read command - serialPutchar (node->fd, pin - node->pinBase) ; - return (serialGetchar (node->fd) == '0') ? 0 : 1 ; -} - - -/* - * drcSetup: - * Create a new instance of an DRC GPIO interface. - * Could be a variable nunber of pins here - we might not know in advance - * if it's an ATmega with 14 pins, or something with less or more! - ********************************************************************************* - */ - -int drcSetup (const int pinBase, const int numPins, const char *device) -{ - int fd ; - int ok, tries ; - time_t then ; - struct wiringPiNodeStruct *node ; - - if ((fd = serialOpen (device, 115200)) < 0) - return wiringPiFailure (WPI_ALMOST, "Unable to open DRC device (%s): %s", device, strerror (errno)) ; - - delay (10) ; // May need longer if it's an Uno that reboots on the open... - -// Flush any pending input - - while (serialDataAvail (fd)) - (void)serialGetchar (fd) ; - - ok = FALSE ; - for (tries = 1 ; tries < 5 ; ++tries) - { - serialPutchar (fd, '@') ; - then = time (NULL) + 2 ; - while (time (NULL) < then) - if (serialDataAvail (fd)) - { - if (serialGetchar (fd) == '@') - { - ok = TRUE ; - break ; - } - } - if (ok) - break ; - } - - if (!ok) - { - serialClose (fd) ; - return wiringPiFailure (WPI_FATAL, "Unable to communidate with DRC device") ; - } - - node = wiringPiNewNode (pinBase, numPins) ; - - node->fd = fd ; - node->pinMode = myPinMode ; - node->pullUpDnControl = myPullUpDnControl ; - node->analogRead = myAnalogRead ; - node->digitalRead = myDigitalRead ; - node->digitalWrite = myDigitalWrite ; - node->pwmWrite = myPwmWrite ; - - return 0 ; -} diff --git a/wiringPi/drc.h b/wiringPi/drc.h deleted file mode 100644 index c2c4ff3..0000000 --- a/wiringPi/drc.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * drc.h: - * Extend wiringPi with the DRC control protocll to Arduino - * Copyright (c) 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 - -int drcSetup (const int pinBase, const int numPins, const char *device) ; - - -#ifdef __cplusplus -} -#endif diff --git a/wiringPi/mcp3422.c b/wiringPi/mcp3422.c index 4d07abc..831aece 100644 --- a/wiringPi/mcp3422.c +++ b/wiringPi/mcp3422.c @@ -25,6 +25,7 @@ #include +#include #include #include #include @@ -44,7 +45,8 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan) { - unsigned char config, b0, b1, b2, b3 ; + unsigned char config ; + unsigned char buffer [4] ; int value = 0 ; // One-shot mode, trigger plus the other configs. @@ -57,35 +59,26 @@ int myAnalogRead (struct wiringPiNodeStruct *node, int chan) { case MCP3422_SR_3_75: // 18 bits delay (270) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - b3 = wiringPiI2CRead (node->fd) ; - value = ((b0 & 3) << 16) | (b1 << 8) | b2 ; + read (node->fd, buffer, 4) ; + value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ; break ; case MCP3422_SR_15: // 16 bits delay ( 70) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - value = (b0 << 8) | b1 ; + read (node->fd, buffer, 3) ; + value = (buffer [0] << 8) | buffer [1] ; break ; case MCP3422_SR_60: // 14 bits delay ( 17) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - value = ((b0 & 0x3F) << 8) | b1 ; + read (node->fd, buffer, 3) ; + value = ((buffer [0] & 0x3F) << 8) | buffer [1] ; break ; case MCP3422_SR_240: // 12 bits delay ( 5) ; - b0 = wiringPiI2CRead (node->fd) ; - b1 = wiringPiI2CRead (node->fd) ; - b2 = wiringPiI2CRead (node->fd) ; - value = ((b0 & 0x0F) << 8) | b1 ; + read (node->fd, buffer, 3) ; + value = ((buffer [0] & 0x0F) << 8) | buffer [0] ; break ; }