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 ***********************************************************************
26 #include <wiringPiSPI.h>
32 * Return the analog value of the given pin
33 * Note: The chip really only has one read "channel", but we're faking it
34 * here so we can read the error registers. Channel 0 will be the data
35 * channel, and 1 is the error register code.
36 * Note: Temperature returned is temp in C * 4, so divide result by 4
37 *********************************************************************************
40 static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
42 unsigned int spiData ;
44 int chan = pin - node->pinBase ;
46 wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
48 if (chan == 0) // Read temp in C
51 temp = spiData & 0x3FFF ; // Bottom 13 bits
52 if ((spiData & 0x2000) != 0) // Negative
56 else // Return error bits
57 return spiData & 0x7 ;
63 * Create a new wiringPi device node for an max31855 on the Pi's
65 *********************************************************************************
68 int max31855Setup (const int pinBase, int spiChannel)
70 struct wiringPiNodeStruct *node ;
72 if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
75 node = wiringPiNewNode (pinBase, 2) ;
77 node->fd = spiChannel ;
78 node->analogRead = myAnalogRead ;