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#include <stdexcept> 29 30#include "max31723.h" 31 32using namespace upm; 33 34MAX31723::MAX31723 (int csn) : m_csnPinCtx(csn), m_spi(0) { 35 mraa::Result error = mraa::SUCCESS; 36 m_name = "MAX31723"; 37 38 error = m_csnPinCtx.dir (mraa::DIR_OUT); 39 if (error != mraa::SUCCESS) { 40 throw std::invalid_argument(std::string(__FUNCTION__) + 41 ": m_csnPinCtx.dir() failed"); 42 } 43 44 CSOff (); 45 46 // set spi mode to mode2 (CPOL = 1, CPHA = 0) 47 m_spi.mode (mraa::SPI_MODE2); 48 // set ontinuously perform temperature conversions 49 writeRegister (R_STS_WRITE_CMD, B_CONT_READING); 50} 51 52short 53MAX31723::getTemperature () { 54 uint8_t lsb = 0; 55 uint8_t msb = 0; 56 short temperature = 0; 57 58 lsb = readRegister (R_TEMPERATURE_LSB); 59 msb = readRegister (R_TEMPERATURE_MSB); 60 61 if ((msb & 0x80) != 0) { 62 msb &= 0x7F; 63 temperature = 0 - msb; 64 65 } else { 66 temperature = msb; 67 } 68 69 return temperature; 70} 71 72/* 73 * ************** 74 * private area 75 * ************** 76 */ 77 78uint8_t 79MAX31723::readRegister (uint8_t reg) { 80 uint8_t data[2] = { 0x00, 0x00 }; 81 uint8_t* sensorData = NULL; 82 83 CSOn (); 84 data[0] = reg; 85 sensorData = m_spi.write(data, 2); 86 CSOff (); 87 88 return sensorData[1]; 89} 90 91void 92MAX31723::writeRegister (uint8_t reg, uint8_t data) { 93 uint8_t buffer[2] = { 0x00, 0x00 }; 94 uint8_t* sensorData = NULL; 95 96 CSOn (); 97 buffer[0] = reg; 98 buffer[1] = data; 99 sensorData = m_spi.write(buffer, 2); 100 CSOff (); 101} 102 103mraa::Result 104MAX31723::CSOn () { 105 return m_csnPinCtx.write (HIGH); 106} 107 108mraa::Result 109MAX31723::CSOff () { 110 return m_csnPinCtx.write (LOW); 111} 112