Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / wiringPi / max31855.c @ 32:cadb9025f1e0

History | View | Annotate | Download (2.75 KB)

1
/*
2
 * max31855.c:
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/
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

    
28
#include <wiringPi.h>
29
#include <wiringPiSPI.h>
30

    
31
#include "max31855.h"
32

    
33
static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
34
{
35
  uint32_t spiData ;
36
  int temp ;
37
  int chan = pin - node->pinBase ;
38

    
39
  wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
40

    
41
  spiData = __bswap_32(spiData) ;
42

    
43
  switch (chan)
44
  {
45
    case 0:                                // Existing read - return raw value * 4
46
      spiData >>= 18 ;
47
      temp = spiData & 0x1FFF ;                // Bottom 13 bits
48
      if ((spiData & 0x2000) != 0)        // Negative
49
        temp = -temp ;
50

    
51
      return temp ;
52

    
53
    case 1:                                // Return error bits
54
      return spiData & 0x7 ;
55

    
56
    case 2:                                // Return temp in C * 10
57
      spiData >>= 18 ;
58
      temp = spiData & 0x1FFF ;                // Bottom 13 bits
59
      if ((spiData & 0x2000) != 0)        // Negative
60
        temp = -temp ;
61

    
62
      return (int)((((double)temp * 25) + 0.5) / 10.0) ;
63

    
64
    case 3:                                // Return temp in F * 10
65
      spiData >>= 18 ;
66
      temp = spiData & 0x1FFF ;                // Bottom 13 bits
67
      if ((spiData & 0x2000) != 0)        // Negative
68
        temp = -temp ;
69

    
70
      return (int)((((((double)temp * 0.25 * 9.0 / 5.0) + 32.0) * 100.0) + 0.5) / 10.0) ;
71

    
72
    default:                                // Who knows...
73
      return 0 ;
74

    
75
  }
76
}
77

    
78

    
79
/*
80
 * max31855Setup:
81
 *        Create a new wiringPi device node for an max31855 on the Pi's
82
 *        SPI interface.
83
 *********************************************************************************
84
 */
85

    
86
int max31855Setup (const int pinBase, int spiChannel)
87
{
88
  struct wiringPiNodeStruct *node ;
89

    
90
  if (wiringPiSPISetup (spiChannel, 5000000) < 0)        // 5MHz - prob 4 on the Pi
91
    return FALSE ;
92

    
93
  node = wiringPiNewNode (pinBase, 4) ;
94

    
95
  node->fd         = spiChannel ;
96
  node->analogRead = myAnalogRead ;
97

    
98
  return TRUE ;
99
}