Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mDotEVBM2X MTDOT-EVBDemo-DRH MTDOT-BOX-EVB-Factory-Firmware-LIB-108 MTDOT-UDKDemo_Senet ... more
Revision 5:8be678fd9e55, committed 2018-01-31
- Comitter:
- Evan Hosseini
- Date:
- Wed Jan 31 10:09:42 2018 -0600
- Parent:
- 4:b612babc2c3b
- Commit message:
- Make driver thread safe by making i2c read transactions atomic
Changed in this revision
| MPL3115A2.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- 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;
}