forked from WiringPi/WiringPi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmax31855.c
99 lines (77 loc) · 2.75 KB
/
max31855.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* max31855.c:
* Extend wiringPi with the max31855 SPI Analog to Digital convertor
* Copyright (c) 2012-2015 Gordon Henderson
***********************************************************************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
* wiringPi is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* wiringPi is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with wiringPi.
* If not, see <http://www.gnu.org/licenses/>.
***********************************************************************
*/
#include <byteswap.h>
#include <stdint.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include "max31855.h"
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
{
uint32_t spiData ;
int temp ;
int chan = pin - node->pinBase ;
wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
spiData = __bswap_32(spiData) ;
switch (chan)
{
case 0: // Existing read - return raw value * 4
spiData >>= 18 ;
temp = spiData & 0x1FFF ; // Bottom 13 bits
if ((spiData & 0x2000) != 0) // Negative
temp = -temp ;
return temp ;
case 1: // Return error bits
return spiData & 0x7 ;
case 2: // Return temp in C * 10
spiData >>= 18 ;
temp = spiData & 0x1FFF ; // Bottom 13 bits
if ((spiData & 0x2000) != 0) // Negative
temp = -temp ;
return (int)((((double)temp * 25) + 0.5) / 10.0) ;
case 3: // Return temp in F * 10
spiData >>= 18 ;
temp = spiData & 0x1FFF ; // Bottom 13 bits
if ((spiData & 0x2000) != 0) // Negative
temp = -temp ;
return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ;
default: // Who knows...
return 0 ;
}
}
/*
* max31855Setup:
* Create a new wiringPi device node for an max31855 on the Pi's
* SPI interface.
*********************************************************************************
*/
int max31855Setup (const int pinBase, int spiChannel)
{
struct wiringPiNodeStruct *node ;
if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
return FALSE ;
node = wiringPiNewNode (pinBase, 4) ;
node->fd = spiChannel ;
node->analogRead = myAnalogRead ;
return TRUE ;
}