chiark / gitweb /
Added in a max5322 SPI D to A chip
authorGordon Henderson <projects@drogon.net>
Tue, 16 Jul 2013 09:37:26 +0000 (10:37 +0100)
committerGordon Henderson <projects@drogon.net>
Tue, 16 Jul 2013 09:37:26 +0000 (10:37 +0100)
gpio/extensions.c
wiringPi/Makefile
wiringPi/max5322.c [new file with mode: 0644]
wiringPi/max5322.h [new file with mode: 0644]

index 713fb93b9aca468b1b571279f7d3a6783e911973..8be0ec0eceb78aa809545985b3faaf5333b7759d 100644 (file)
@@ -44,6 +44,7 @@
 #include <pcf8591.h>
 #include <pcf8574.h>
 #include <max31855.h>
+#include <max5322.h>
 #include <mcp3002.h>
 #include <mcp3004.h>
 #include <mcp4802.h>
@@ -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                    },
 } ;
 
index 6f6207066aed681a4165a841a0ec42a03db71661..05dedeca1299dda841a86e3dc506f086b4cc713e 100644 (file)
@@ -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 (file)
index 0000000..c0b6264
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ ***********************************************************************
+ */
+
+#include <wiringPi.h>
+#include <wiringPiSPI.h>
+
+#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 (file)
index 0000000..a217cf8
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ ***********************************************************************
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern int max5322Setup (int pinBase, int spiChannel) ;
+
+#ifdef __cplusplus
+}
+#endif