2 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
36 MAX31855::MAX31855(int bus, int cs)
38 // initialise chip select as a normal gpio
39 m_gpio = maa_gpio_init(cs);
40 maa_gpio_dir(m_gpio, MAA_GPIO_OUT);
42 // initialise the spi bus with a 2Mhz clock
43 m_sensor = maa_spi_init(bus);
44 maa_spi_frequency(m_sensor, 2000000);
51 // close both m_sensor & m_gpio cleanly
53 error = maa_spi_stop(m_sensor);
54 if (error != MAA_SUCCESS) {
55 maa_result_print(error);
57 error = maa_gpio_close(m_gpio);
58 if (error != MAA_SUCCESS) {
59 maa_result_print(error);
68 // set chip select low
69 maa_gpio_write(m_gpio, 0);
73 // set our input buffer to 0, this is clean but not required
74 memset(buf, 0, sizeof(uint8_t)*4);
76 // Write buffer to the spi slave
77 uint8_t* x = maa_spi_write_buf(m_sensor, buf, 4);
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];;
84 // maa_spi_write_buf does not free the return buffer
88 std::cerr << "Something went very wrong!" << std::endl;
91 // scrap all the data we dont care about
94 // LSB = 0.25 degrees C
95 double c = (double) temp;
99 // set chip select high
100 maa_gpio_write(m_gpio, 1);