Class module for MPL3115A2 I2C Barometric Sensor

Dependents:   mDotEVBM2X MTDOT-EVBDemo-DRH MTDOT-BOX-EVB-Factory-Firmware-LIB-108 MTDOT-UDKDemo_Senet ... more

Revision:
5:8be678fd9e55
Parent:
4:b612babc2c3b
diff -r b612babc2c3b -r 8be678fd9e55 MPL3115A2.cpp
--- a/MPL3115A2.cpp	Mon Jul 31 17:34:55 2017 -0500
+++ b/MPL3115A2.cpp	Wed Jan 31 10:09:42 2018 -0600
@@ -52,8 +52,6 @@
     uint8_t i = 0;
     char reg_val[1];
 
-    _i2c->frequency(400000);
-
     // Reset all registers to POR values
     reg_val[0] = 0x04;
     result = MPL3115A2::writeRegister(CTRL_REG1, reg_val);
@@ -78,10 +76,10 @@
         result = 1;
     }
 
-    if(result != 0) {
+    if (result != 0) {
         debug("MPL3115A2:init failed\n\r");
     }
-    _i2c->stop();
+    
     return result;
 }
 
@@ -343,8 +341,8 @@
 
     result |= _i2c->write(_i2c_addr, buf, (count + 1));
 
-    if(result != 0) {
-        debug("MPL3115A2:writeRegister failed r-%d\n\r",result);
+    if (result != 0) {
+        debug("MPL3115A2::writeRegister failed r-%d\n\r",result);
     }
 
     return result;
@@ -356,18 +354,23 @@
     char reg_out[1];
 
     reg_out[0] = reg;
+    _i2c->lock();
+
+    // MPL3115A2 expects a repeated start from the master
     result |= _i2c->write(_i2c_addr,reg_out,1,true);
 
-    if(result != 0) {
+    if (result != 0) {
         debug("MPL3115A2::readRegister failed write\n\r");
-        return result;
+        goto exit;
     }
 
     result |= _i2c->read(_i2c_addr,data,count,false);
 
-    if(result != 0) {
+    if (result != 0) {
         debug("MPL3115A2::readRegister failed read\n\r");
     }
 
+exit:
+    _i2c->unlock();
     return result;
 }