max31855: add sensor and documentation on creation of a UPM sensor
[contrib/upm.git] / src / max31855 / max31855.cxx
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24
25 #include <iostream>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <functional>
29 #include <string.h>
30
31 #include "max31855.h"
32
33 using namespace upm;
34
35 //! [Constructor]
36 MAX31855::MAX31855(int bus, int cs)
37 {
38     // initialise chip select as a normal gpio
39     m_gpio = maa_gpio_init(cs);
40     maa_gpio_dir(m_gpio, MAA_GPIO_OUT);
41
42     // initialise the spi bus with a 2Mhz clock
43     m_sensor = maa_spi_init(bus);
44     maa_spi_frequency(m_sensor, 2000000);
45 }
46 //! [Constructor]
47
48 //! [Destructor]
49 MAX31855::~MAX31855()
50 {
51     // close both m_sensor & m_gpio cleanly
52     maa_result_t error;
53     error = maa_spi_stop(m_sensor);
54     if (error != MAA_SUCCESS) {
55         maa_result_print(error);
56     }
57     error = maa_gpio_close(m_gpio);
58     if (error != MAA_SUCCESS) {
59         maa_result_print(error);
60     }
61 }
62 //! [Destructor]
63
64 double
65 MAX31855::getTemp()
66 {
67 //! [spi]
68     // set chip select low
69     maa_gpio_write(m_gpio, 0);
70
71     uint8_t buf[4];
72
73     // set our input buffer to 0, this is clean but not required
74     memset(buf, 0, sizeof(uint8_t)*4);
75
76     // Write buffer to the spi slave
77     uint8_t* x = maa_spi_write_buf(m_sensor, buf, 4);
78 //! [spi]
79
80 //! [conversion]
81     // Endian correct way of making our char array into an 32bit int
82     int32_t temp = (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3];;
83
84     // maa_spi_write_buf does not free the return buffer
85     free(x);
86
87     if (temp & 0x7) {
88         std::cerr << "Something went very wrong!" << std::endl;
89     }
90
91     // scrap all the data we dont care about
92     temp >>= 18;
93
94     // LSB = 0.25 degrees C
95     double c = (double) temp;
96     c *= 0.25;
97 //! [conversion]
98
99     // set chip select high
100     maa_gpio_write(m_gpio, 1);
101
102     return c;
103 }