3 * Extend wiringPi with the max31855 SPI Analog to Digital convertor
4 * Copyright (c) 2012-2015 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 ***********************************************************************
29 #include <wiringPiSPI.h>
33 static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
37 int chan = pin - node->pinBase ;
39 wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
41 spiData = __bswap_32(spiData) ;
45 case 0: // Existing read - return raw value * 4
47 temp = spiData & 0x1FFF ; // Bottom 13 bits
48 if ((spiData & 0x2000) != 0) // Negative
53 case 1: // Return error bits
54 return spiData & 0x7 ;
56 case 2: // Return temp in C * 10
58 temp = spiData & 0x1FFF ; // Bottom 13 bits
59 if ((spiData & 0x2000) != 0) // Negative
62 return (int)((((double)temp * 25) + 0.5) / 10.0) ;
64 case 3: // Return temp in F * 10
66 temp = spiData & 0x1FFF ; // Bottom 13 bits
67 if ((spiData & 0x2000) != 0) // Negative
70 return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ;
72 default: // Who knows...
81 * Create a new wiringPi device node for an max31855 on the Pi's
83 *********************************************************************************
86 int max31855Setup (const int pinBase, int spiChannel)
88 struct wiringPiNodeStruct *node ;
90 if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
93 node = wiringPiNewNode (pinBase, 4) ;
95 node->fd = spiChannel ;
96 node->analogRead = myAnalogRead ;