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>
41 #include "wiringPiSPI.h"
43 #include "gertboard.h"
45 // The A-D convertor won't run at more than 1MHz @ 3.3v
47 #define SPI_ADC_SPEED 1000000
48 #define SPI_DAC_SPEED 1000000
54 * gertboardAnalogWrite:
55 * Write an 8-bit data value to the MCP4802 Analog to digital
56 * convertor on the Gertboard.
57 *********************************************************************************
60 void gertboardAnalogWrite (int chan, int value)
63 uint8_t chanBits, dataBits ;
70 chanBits |= ((value >> 4) & 0x0F) ;
71 dataBits = ((value << 4) & 0xF0) ;
73 spiData [0] = chanBits ;
74 spiData [1] = dataBits ;
76 wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
81 * gertboardAnalogRead:
82 * Return the analog value of the given channel (0/1).
83 * The A/D is a 10-bit device
84 *********************************************************************************
87 int gertboardAnalogRead (int chan)
94 chanBits = 0b11010000 ;
96 chanBits = 0b11110000 ;
98 spiData [0] = chanBits ;
101 wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
103 return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
109 * Initialise the SPI bus, etc.
110 *********************************************************************************
113 int gertboardSPISetup (void)
115 if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
118 if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)