chiark / gitweb /
Slight change to the gpio program to fix SPI buffer size when loading
[wiringPi.git] / wiringPi / gertboard.c
1 /*
2  * gertboard.c:
3  *      Access routines for the SPI devices on the Gertboard
4  *      Copyright (c) 2012 Gordon Henderson
5  *
6  *      The Gertboard has:
7  *
8  *              An MCP3002 dual-channel A to D convertor connected
9  *              to the SPI bus, selected by chip-select A, and:
10  *
11  *              An MCP4802 dual-channel D to A convertor connected
12  *              to the SPI bus, selected via chip-select B.
13  *
14  ***********************************************************************
15  * This file is part of wiringPi:
16  *      https://projects.drogon.net/raspberry-pi/wiringpi/
17  *
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.
22  *
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.
27  *
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  ***********************************************************************
32  */
33
34
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39 #include <linux/spi/spidev.h>
40
41 #include "wiringPiSPI.h"
42
43 #include "gertboard.h"
44
45 // The A-D convertor won't run at more than 1MHz @ 3.3v
46
47 #define SPI_ADC_SPEED    1000000
48 #define SPI_DAC_SPEED    1000000
49 #define SPI_A2D         0
50 #define SPI_D2A         1
51
52
53 /*
54  * gertboardAnalogWrite:
55  *      Write an 8-bit data value to the MCP4802 Analog to digital
56  *      convertor on the Gertboard.
57  *********************************************************************************
58  */
59
60 void gertboardAnalogWrite (int chan, int value)
61 {
62   uint8_t spiData [2] ;
63   uint8_t chanBits, dataBits ;
64
65   if (chan == 0)
66     chanBits = 0x30 ;
67   else
68     chanBits = 0xB0 ;
69
70   chanBits |= ((value >> 4) & 0x0F) ;
71   dataBits  = ((value << 4) & 0xF0) ;
72
73   spiData [0] = chanBits ;
74   spiData [1] = dataBits ;
75
76   wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
77 }
78
79
80 /*
81  * gertboardAnalogRead:
82  *      Return the analog value of the given channel (0/1).
83  *      The A/D is a 10-bit device
84  *********************************************************************************
85  */
86
87 int gertboardAnalogRead (int chan)
88 {
89   uint8_t spiData [2] ;
90
91   uint8_t chanBits ;
92
93   if (chan == 0)
94     chanBits = 0b11010000 ;
95   else
96     chanBits = 0b11110000 ;
97
98   spiData [0] = chanBits ;
99   spiData [1] = 0 ;
100
101   wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
102
103   return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
104 }
105
106
107 /*
108  * gertboardSPISetup:
109  *      Initialise the SPI bus, etc.
110  *********************************************************************************
111  */
112
113 int gertboardSPISetup (void)
114 {
115   if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
116     return -1 ;
117
118   if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)
119     return -1 ;
120
121   return 0 ;
122 }