max31723: made it work with SPI but the data looks like incorrect
authorKiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Wed, 6 Aug 2014 16:41:40 +0000 (16:41 +0000)
committerKiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Wed, 6 Aug 2014 16:41:40 +0000 (16:41 +0000)
Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
examples/max31723.cxx
src/max31723/max31723.cxx
src/max31723/max31723.h

index a1c413b..96082af 100644 (file)
@@ -45,11 +45,12 @@ main(int argc, char **argv)
 {
     //! [Interesting]
     sensor = new upm::MAX31723(7);
+    usleep (1000000);
 
-    // while (!doWork) {
+    while (!doWork) {
         std::cout << "Temperature " << sensor->getTemperature() << std::endl;
-        // usleep (3000000);
-    // }
+        usleep (1000000);
+    }
     //! [Interesting]
 
     std::cout << "exiting application" << std::endl;
index 69bc6d7..cf6e1ee 100644 (file)
@@ -51,12 +51,17 @@ MAX31723::MAX31723 (int csn) {
         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() {
@@ -72,31 +77,24 @@ 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;
 }
 
 /*
@@ -105,6 +103,31 @@ MAX31723::getTemperature () {
  * **************
  */
 
+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);
index e187707..e251faf 100644 (file)
@@ -46,6 +46,8 @@ class MAX31723 {
         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
          *
@@ -62,7 +64,7 @@ class MAX31723 {
         /**
          * Get on board temperature.
          */
-        uint16_t getTemperature ();
+        short getTemperature ();
 
         /**
          * Return name of the component
@@ -76,6 +78,9 @@ class MAX31723 {
         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
          */