max31723 :: added new temperature sensor (HAVE ISSUES WITH SPI)
[contrib/upm.git] / src / max31723 / max31723.cxx
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@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
29 #include "max31723.h"
30
31 using namespace upm;
32
33 struct MAX31723Exception : public std::exception {
34     std::string message;
35     MAX31723Exception (std::string msg) : message (msg) { }
36     ~MAX31723Exception () throw () { }
37     const char* what() const throw () { return message.c_str(); }
38 };
39
40 MAX31723::MAX31723 (int csn) {
41     mraa_result_t error = MRAA_SUCCESS;
42     m_name = "MAX31723";
43
44     m_csnPinCtx = mraa_gpio_init (csn);
45     if (m_csnPinCtx == NULL) {
46         throw MAX31723Exception ("GPIO failed to initilize");
47     }
48
49     error = mraa_gpio_dir (m_csnPinCtx, MRAA_GPIO_OUT);
50     if (error != MRAA_SUCCESS) {
51         throw MAX31723Exception ("GPIO failed to initilize");
52     }
53
54     m_spi = mraa_spi_init (0);
55     if (m_spi == NULL) {
56         throw MAX31723Exception ("SPI failed to initilize");
57     }
58
59     CSOff ();
60 }
61
62 MAX31723::~MAX31723() {
63     mraa_result_t error = MRAA_SUCCESS;
64
65     error = mraa_spi_stop(m_spi);
66     if (error != MRAA_SUCCESS) {
67         mraa_result_print(error);
68     }
69     error = mraa_gpio_close (m_csnPinCtx);
70     if (error != MRAA_SUCCESS) {
71         mraa_result_print(error);
72     }
73 }
74
75 uint16_t
76 MAX31723::getTemperature () {
77     uint8_t lsb = 0;
78     uint8_t msb = 0;
79     uint8_t buf[2] = { 0x01, 0x00};
80
81     CSOn ();
82
83     uint8_t* x = mraa_spi_write_buf(m_spi, buf, 2);
84
85     printf ("%d\n", (uint16_t)*x);
86
87     /*mraa_spi_write (m_spi, R_TEMPERATURE_LSB);
88     lsb = mraa_spi_write (m_spi, lsb);
89     lsb >>= 4;*/
90
91     /*mraa_spi_write (m_spi, R_TEMPERATURE_MSB);
92     msb = mraa_spi_write (m_spi, msb);
93
94     if ((msb & 0x80) != 0)
95         msb |= ~((1 << 8) - 1);*/
96
97     CSOff ();
98
99     return msb;
100 }
101
102 /*
103  * **************
104  *  private area
105  * **************
106  */
107
108 mraa_result_t
109 MAX31723::CSOn () {
110     return mraa_gpio_write (m_csnPinCtx, HIGH);
111 }
112
113 mraa_result_t
114 MAX31723::CSOff () {
115     return mraa_gpio_write (m_csnPinCtx, LOW);
116 }