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