chiark / gitweb /
Updated the Debian build system thanks to Ian Jackson for the
[wiringPi.git] / wiringPi / mcp3422.c
1 /*
2  * mcp3422.c:
3  *      Extend wiringPi with the MCP3422 I2C ADC chip
4  *      Also works for the MCP3423 and MCP3224 (4 channel) chips
5  *      Copyright (c) 2013 Gordon Henderson
6  ***********************************************************************
7  * This file is part of wiringPi:
8  *      https://projects.drogon.net/raspberry-pi/wiringpi/
9  *
10  *    wiringPi is free software: you can redistribute it and/or modify
11  *    it under the terms of the GNU Lesser General Public License as
12  *    published by the Free Software Foundation, either version 3 of the
13  *    License, or (at your option) any later version.
14  *
15  *    wiringPi is distributed in the hope that it will be useful,
16  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *    GNU Lesser General Public License for more details.
19  *
20  *    You should have received a copy of the GNU Lesser General Public
21  *    License along with wiringPi.
22  *    If not, see <http://www.gnu.org/licenses/>.
23  ***********************************************************************
24  */
25
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdint.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <linux/spi/spidev.h>
33
34 #include <wiringPi.h>
35 #include <wiringPiI2C.h>
36
37 #include "mcp3422.h"
38
39
40 /*
41  * myAnalogRead:
42  *      Read a channel from the device
43  *********************************************************************************
44  */
45
46 int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
47 {
48   unsigned char config ;
49   unsigned char buffer [4] ;
50   int value = 0 ;
51
52 // One-shot mode, trigger plus the other configs.
53
54   config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ;
55   
56   wiringPiI2CWrite (node->fd, config) ;
57
58   switch (node->data0)  // Sample rate
59   {
60     case MCP3422_SR_3_75:                       // 18 bits
61       delay (270) ;
62       read (node->fd, buffer, 4) ;
63       value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ;
64       break ;
65
66     case MCP3422_SR_15:                         // 16 bits
67       delay ( 70) ;
68       read (node->fd, buffer, 3) ;
69       value = (buffer [0] << 8) | buffer [1] ;
70       break ;
71
72     case MCP3422_SR_60:                         // 14 bits
73       delay ( 17) ;
74       read (node->fd, buffer, 3) ;
75       value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
76       break ;
77
78     case MCP3422_SR_240:                        // 12 bits
79       delay (  5) ;
80       read (node->fd, buffer, 3) ;
81       value = ((buffer [0] & 0x0F) << 8) | buffer [0] ;
82       break ;
83   }
84
85   return value ;
86 }
87
88
89 /*
90  * mcp3422Setup:
91  *      Create a new wiringPi device node for the mcp3422
92  *********************************************************************************
93  */
94
95 int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
96 {
97   int fd ;
98   struct wiringPiNodeStruct *node ;
99
100   if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
101     return fd ;
102
103   node = wiringPiNewNode (pinBase, 4) ;
104
105   node->data0      = sampleRate ;
106   node->data1      = gain ;
107   node->analogRead = myAnalogRead ;
108
109   return 0 ;
110 }