chiark / gitweb /
702d7c03226c95dea85634e3cb10a21308b036d8
[wiringPi.git] / wiringPi / max31855.c
1 /*
2  * max31855.c:
3  *      Extend wiringPi with the max31855 SPI Analog to Digital convertor
4  *      Copyright (c) 2012-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 #include <byteswap.h>
26 #include <stdint.h>
27 #include <math.h>
28
29 #include <wiringPi.h>
30 #include <wiringPiSPI.h>
31
32 #include "max31855.h"
33
34 static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
35 {
36   uint32_t spiData ;
37   int temp ;
38   int chan = pin - node->pinBase ;
39
40   wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
41
42   spiData = __bswap_32(spiData) ;
43
44   switch (chan)
45   {
46     case 0:                             // Existing read - return raw value * 4
47       spiData >>= 18 ;
48       temp = spiData & 0x1FFF ;         // Bottom 13 bits
49       if ((spiData & 0x2000) != 0)      // Negative
50         temp = -temp ;
51
52       return temp ;
53
54     case 1:                             // Return error bits
55       return spiData & 0x7 ;
56
57     case 2:                             // Return temp in C * 10
58       spiData >>= 18 ;
59       temp = spiData & 0x1FFF ;         // Bottom 13 bits
60       if ((spiData & 0x2000) != 0)      // Negative
61         temp = -temp ;
62
63       return (int)rint ((double)temp * 2.5) ;
64
65     case 3:                             // Return temp in F * 10
66       spiData >>= 18 ;
67       temp = spiData & 0x1FFF ;         // Bottom 13 bits
68       if ((spiData & 0x2000) != 0)      // Negative
69         temp = -temp ;
70
71       return (int)rint ((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 10.0) ;
72
73     default:                            // Who knows...
74       return 0 ;
75
76   }
77 }
78
79
80 /*
81  * max31855Setup:
82  *      Create a new wiringPi device node for an max31855 on the Pi's
83  *      SPI interface.
84  *********************************************************************************
85  */
86
87 int max31855Setup (const int pinBase, int spiChannel)
88 {
89   struct wiringPiNodeStruct *node ;
90
91   if (wiringPiSPISetup (spiChannel, 5000000) < 0)       // 5MHz - prob 4 on the Pi
92     return -1 ;
93
94   node = wiringPiNewNode (pinBase, 4) ;
95
96   node->fd         = spiChannel ;
97   node->analogRead = myAnalogRead ;
98
99   return 0 ;
100 }