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/
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.
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.
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 ***********************************************************************
30 #include <wiringPiSPI.h>
34 static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
38 int chan = pin - node->pinBase ;
40 wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
42 spiData = __bswap_32(spiData) ;
46 case 0: // Existing read - return raw value * 4
48 temp = spiData & 0x1FFF ; // Bottom 13 bits
49 if ((spiData & 0x2000) != 0) // Negative
54 case 1: // Return error bits
55 return spiData & 0x7 ;
57 case 2: // Return temp in C * 10
59 temp = spiData & 0x1FFF ; // Bottom 13 bits
60 if ((spiData & 0x2000) != 0) // Negative
63 return (int)rint ((double)temp * 2.5) ;
65 case 3: // Return temp in F * 10
67 temp = spiData & 0x1FFF ; // Bottom 13 bits
68 if ((spiData & 0x2000) != 0) // Negative
71 return (int)rint ((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 10.0) ;
73 default: // Who knows...
82 * Create a new wiringPi device node for an max31855 on the Pi's
84 *********************************************************************************
87 int max31855Setup (const int pinBase, int spiChannel)
89 struct wiringPiNodeStruct *node ;
91 if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
94 node = wiringPiNewNode (pinBase, 4) ;
96 node->fd = spiChannel ;
97 node->analogRead = myAnalogRead ;