3 * Access routines for the SPI devices on the Gertboard
4 * Copyright (c) 2012 Gordon Henderson
8 * An MCP3002 dual-channel A to D convertor connected
9 * to the SPI bus, selected by chip-select A, and:
11 * An MCP4802 dual-channel D to A convertor connected
12 * to the SPI bus, selected via chip-select B.
14 ***********************************************************************
15 * This file is part of wiringPi:
16 * https://projects.drogon.net/raspberry-pi/wiringpi/
18 * wiringPi is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as
20 * published by the Free Software Foundation, either version 3 of the
21 * License, or (at your option) any later version.
23 * wiringPi is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
28 * You should have received a copy of the GNU Lesser General Public
29 * License along with wiringPi.
30 * If not, see <http://www.gnu.org/licenses/>.
31 ***********************************************************************
38 #include <sys/ioctl.h>
39 #include <linux/spi/spidev.h>
42 #include <wiringPiSPI.h>
44 #include "gertboard.h"
46 // The A-D convertor won't run at more than 1MHz @ 3.3v
48 #define SPI_ADC_SPEED 1000000
49 #define SPI_DAC_SPEED 1000000
55 * gertboardAnalogWrite:
56 * Write an 8-bit data value to the MCP4802 Analog to digital
57 * convertor on the Gertboard.
58 *********************************************************************************
61 void gertboardAnalogWrite (const int chan, const int value)
64 uint8_t chanBits, dataBits ;
71 chanBits |= ((value >> 4) & 0x0F) ;
72 dataBits = ((value << 4) & 0xF0) ;
74 spiData [0] = chanBits ;
75 spiData [1] = dataBits ;
77 wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
82 * gertboardAnalogRead:
83 * Return the analog value of the given channel (0/1).
84 * The A/D is a 10-bit device
85 *********************************************************************************
88 int gertboardAnalogRead (const int chan)
95 chanBits = 0b11010000 ;
97 chanBits = 0b11110000 ;
99 spiData [0] = chanBits ;
102 wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
104 return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
110 * Initialise the SPI bus, etc.
111 *********************************************************************************
114 int gertboardSPISetup (void)
116 if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
119 if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)
127 * New wiringPi node extension methods.
128 *********************************************************************************
131 static int myAnalogRead (struct wiringPiNodeStruct *node, const int chan)
133 return gertboardAnalogRead (chan - node->pinBase) ;
136 static void myAnalogWrite (struct wiringPiNodeStruct *node, const int chan, const int value)
138 gertboardAnalogWrite (chan - node->pinBase, value) ;
143 * gertboardAnalogSetup:
144 * Create a new wiringPi device node for the analog devices on the
145 * Gertboard. We create one node with 2 pins - each pin being read
146 * and write - although the operations actually go to different
148 *********************************************************************************
151 int gertboardAnalogSetup (const int pinBase)
153 struct wiringPiNodeStruct *node ;
156 if (( x = gertboardSPISetup ()) != 0)
159 node = wiringPiNewNode (pinBase, 2) ;
160 node->analogRead = myAnalogRead ;
161 node->analogWrite = myAnalogWrite ;