max31723: use new prefixed spi MODE enum
[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     CSOff ();
55
56     m_spi = mraa_spi_init (0);
57     if (m_spi == NULL) {
58         throw MAX31723Exception ("SPI failed to initilize");
59     }
60
61     // set spi mode to mode2 (CPOL = 1, CPHA = 0)
62     mraa_spi_mode (m_spi, MRAA_SPI_MODE2);
63     // set ontinuously perform temperature conversions
64     writeRegister (R_STS_WRITE_CMD, B_CONT_READING);
65 }
66
67 MAX31723::~MAX31723() {
68     mraa_result_t error = MRAA_SUCCESS;
69
70     error = mraa_spi_stop(m_spi);
71     if (error != MRAA_SUCCESS) {
72         mraa_result_print(error);
73     }
74     error = mraa_gpio_close (m_csnPinCtx);
75     if (error != MRAA_SUCCESS) {
76         mraa_result_print(error);
77     }
78 }
79
80 short
81 MAX31723::getTemperature () {
82     uint8_t lsb = 0;
83     uint8_t msb = 0;
84     short temperature = 0;
85
86     lsb = readRegister (R_TEMPERATURE_LSB);
87     msb = readRegister (R_TEMPERATURE_MSB);
88
89     if ((msb & 0x80) != 0) {
90         msb &= 0x7F;
91         temperature = 0 - msb;
92
93     } else {
94         temperature = msb;
95     }
96
97     return temperature;
98 }
99
100 /*
101  * **************
102  *  private area
103  * **************
104  */
105
106 uint8_t
107 MAX31723::readRegister (uint8_t reg) {
108     uint8_t     data[2]     = { 0x00, 0x00 };
109     uint8_t*    sensorData  = NULL;
110
111     CSOn ();
112     data[0] = reg;
113     sensorData = mraa_spi_write_buf(m_spi, data, 2);
114     CSOff ();
115
116     return sensorData[1];
117 }
118
119 void
120 MAX31723::writeRegister (uint8_t reg, uint8_t data) {
121     uint8_t     buffer[2]   = { 0x00, 0x00 };
122     uint8_t*    sensorData  = NULL;
123
124     CSOn ();
125     buffer[0] = reg;
126     buffer[1] = data;
127     sensorData = mraa_spi_write_buf(m_spi, buffer, 2);
128     CSOff ();
129 }
130
131 mraa_result_t
132 MAX31723::CSOn () {
133     return mraa_gpio_write (m_csnPinCtx, HIGH);
134 }
135
136 mraa_result_t
137 MAX31723::CSOff () {
138     return mraa_gpio_write (m_csnPinCtx, LOW);
139 }