throw MAX31723Exception ("GPIO failed to initilize");
}
+ CSOff ();
+
m_spi = mraa_spi_init (0);
if (m_spi == NULL) {
throw MAX31723Exception ("SPI failed to initilize");
}
- CSOff ();
+ // set spi mode to mode2 (CPOL = 1, CPHA = 0)
+ mraa_spi_mode (m_spi, MODE2);
+ // set ontinuously perform temperature conversions
+ writeRegister (R_STS_WRITE_CMD, B_CONT_READING);
}
MAX31723::~MAX31723() {
}
}
-uint16_t
+short
MAX31723::getTemperature () {
uint8_t lsb = 0;
uint8_t msb = 0;
- uint8_t buf[2] = { 0x01, 0x00};
-
- CSOn ();
-
- uint8_t* x = mraa_spi_write_buf(m_spi, buf, 2);
+ short temperature = 0;
- printf ("%d\n", (uint16_t)*x);
+ lsb = readRegister (R_TEMPERATURE_LSB);
+ msb = readRegister (R_TEMPERATURE_MSB);
- /*mraa_spi_write (m_spi, R_TEMPERATURE_LSB);
- lsb = mraa_spi_write (m_spi, lsb);
- lsb >>= 4;*/
+ if ((msb & 0x80) != 0) {
+ msb &= 0x7F;
+ temperature = 0 - msb;
- /*mraa_spi_write (m_spi, R_TEMPERATURE_MSB);
- msb = mraa_spi_write (m_spi, msb);
-
- if ((msb & 0x80) != 0)
- msb |= ~((1 << 8) - 1);*/
-
- CSOff ();
+ } else {
+ temperature = msb;
+ }
- return msb;
+ return temperature;
}
/*
* **************
*/
+uint8_t
+MAX31723::readRegister (uint8_t reg) {
+ uint8_t data[2] = { 0x00, 0x00 };
+ uint8_t* sensorData = NULL;
+
+ CSOn ();
+ data[0] = reg;
+ sensorData = mraa_spi_write_buf(m_spi, data, 2);
+ CSOff ();
+
+ return sensorData[1];
+}
+
+void
+MAX31723::writeRegister (uint8_t reg, uint8_t data) {
+ uint8_t buffer[2] = { 0x00, 0x00 };
+ uint8_t* sensorData = NULL;
+
+ CSOn ();
+ buffer[0] = reg;
+ buffer[1] = data;
+ sensorData = mraa_spi_write_buf(m_spi, buffer, 2);
+ CSOff ();
+}
+
mraa_result_t
MAX31723::CSOn () {
return mraa_gpio_write (m_csnPinCtx, HIGH);
static const uint8_t R_TEMPERATURE_LSB = 0x01;
static const uint8_t R_TEMPERATURE_MSB = 0x02;
+ static const uint8_t B_CONT_READING = 0x00;
+
/**
* Instanciates a MAX31723 object
*
/**
* Get on board temperature.
*/
- uint16_t getTemperature ();
+ short getTemperature ();
/**
* Return name of the component
mraa_spi_context m_spi;
mraa_gpio_context m_csnPinCtx;
+ uint8_t readRegister (uint8_t reg);
+ void writeRegister (uint8_t reg, uint8_t data);
+
/**
* Set chip select pin LOW
*/