From 27afc017b9d8f08f2f67b990c078d3127f7c4ef1 Mon Sep 17 00:00:00 2001 From: Gordon Henderson Date: Tue, 16 Jul 2013 10:37:26 +0100 Subject: [PATCH] Added in a max5322 SPI D to A chip --- gpio/extensions.c | 28 ++++++++++++++++ wiringPi/Makefile | 5 ++- wiringPi/max5322.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++ wiringPi/max5322.h | 33 ++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 wiringPi/max5322.c create mode 100644 wiringPi/max5322.h diff --git a/gpio/extensions.c b/gpio/extensions.c index 713fb93..8be0ec0 100644 --- a/gpio/extensions.c +++ b/gpio/extensions.c @@ -44,6 +44,7 @@ #include #include #include +#include #include #include #include @@ -413,6 +414,32 @@ static int doExtensionMcp3004 (char *progName, int pinBase, char *params) } +/* + * doExtensionMax5322: + * Analog O + * max5322:base:spiChan + ********************************************************************************* + */ + +static int doExtensionMax5322 (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 ; + } + + max5322Setup (pinBase, spi) ; + + return TRUE ; +} + + /* * doExtensionMcp4802: * Analog IO @@ -503,6 +530,7 @@ struct extensionFunctionStruct extensionFunctions [] = { "mcp4802", &doExtensionMcp4802 }, { "mcp3422", &doExtensionMcp3422 }, { "max31855", &doExtensionMax31855 }, + { "max5322", &doExtensionMax5322 }, { NULL, NULL }, } ; diff --git a/wiringPi/Makefile b/wiringPi/Makefile index 6f62070..05dedec 100644 --- a/wiringPi/Makefile +++ b/wiringPi/Makefile @@ -53,7 +53,7 @@ SRC = wiringPi.c \ sr595.c \ pcf8574.c pcf8591.c \ mcp3002.c mcp3004.c mcp4802.c mcp3422.c \ - max31855.c \ + max31855.c max5322.c \ drc.c OBJ = $(SRC:.c=.o) @@ -104,6 +104,7 @@ install-headers: @install -m 0644 mcp23s08.h $(DESTDIR)$(PREFIX)/include @install -m 0644 mcp23s17.h $(DESTDIR)$(PREFIX)/include @install -m 0644 max31855.h $(DESTDIR)$(PREFIX)/include + @install -m 0644 max5322.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 @@ -142,6 +143,7 @@ uninstall: @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s08.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp23s17.h @rm -f $(DESTDIR)$(PREFIX)/include/max31855.h + @rm -f $(DESTDIR)$(PREFIX)/include/max5322.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3002.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp3004.h @rm -f $(DESTDIR)$(PREFIX)/include/mcp4802.h @@ -181,4 +183,5 @@ mcp3004.o: wiringPi.h wiringPiSPI.h mcp3004.h mcp4802.o: wiringPi.h wiringPiSPI.h mcp4802.h mcp3422.o: wiringPi.h wiringPiI2C.h mcp3422.h max31855.o: wiringPi.h wiringPiSPI.h max31855.h +max5322.o: wiringPi.h wiringPiSPI.h max5322.h drc.o: wiringPi.h wiringSerial.h drc.h diff --git a/wiringPi/max5322.c b/wiringPi/max5322.c new file mode 100644 index 0000000..c0b6264 --- /dev/null +++ b/wiringPi/max5322.c @@ -0,0 +1,84 @@ +/* + * max5322.c: + * Extend wiringPi with the MAX5322 SPI Digital to Analog 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 "max5322.h" + +/* + * myAnalogWrite: + * Write analog value on the given pin + ********************************************************************************* + */ + +static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value) +{ + unsigned char spiData [2] ; + unsigned char chanBits, dataBits ; + int chan = pin - node->pinBase ; + + if (chan == 0) + chanBits = 0b01000000 ; + else + chanBits = 0b01010000 ; + + chanBits |= ((value >> 4) & 0x0F) ; + dataBits = ((value << 4) & 0xF0) ; + + spiData [0] = chanBits ; + spiData [1] = dataBits ; + + wiringPiSPIDataRW (node->fd, spiData, 2) ; +} + +/* + * max5322Setup: + * Create a new wiringPi device node for an max5322 on the Pi's + * SPI interface. + ********************************************************************************* + */ + +int max5322Setup (const int pinBase, int spiChannel) +{ + struct wiringPiNodeStruct *node ; + unsigned char spiData [2] ; + + if (wiringPiSPISetup (spiChannel, 8000000) < 0) // 10MHz Max + return -1 ; + + node = wiringPiNewNode (pinBase, 2) ; + + node->fd = spiChannel ; + node->analogWrite = myAnalogWrite ; + +// Enable both DACs + + spiData [0] = 0b11100000 ; + spiData [1] = 0 ; + + wiringPiSPIDataRW (node->fd, spiData, 2) ; + + return 0 ; +} diff --git a/wiringPi/max5322.h b/wiringPi/max5322.h new file mode 100644 index 0000000..a217cf8 --- /dev/null +++ b/wiringPi/max5322.h @@ -0,0 +1,33 @@ +/* + * max5322.h: + * Extend wiringPi with the MAX5322 SPI Digital to Analog 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 max5322Setup (int pinBase, int spiChannel) ; + +#ifdef __cplusplus +} +#endif -- 2.30.2