Skip to content

Keep i2c error as bit flag which TwoWire::endTransmission() returns #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ This library is intended to provide a quicker and easier way to get started usin
* `bool timeoutOccurred()`<br>
Indicates whether a read timeout has occurred since the last call to `timeoutOccurred()`.

* `uint8_t getWireErrorBits(void)`<br>
Indicates what I&sup2;C transmission errors have occurred since the last call to VL53L0X API. Returns 0 if `last_status` was success, but returns value whose bit is set corresponding to error status written on [`Wire.endTransmission()` documentation](http://arduino.cc/en/Reference/WireEndTransmission).

## Version history

* 1.3.0 (2020 Sep 24): Added support for alternative I&sup2;C buses (thanks KurtE).
Expand Down
18 changes: 18 additions & 0 deletions VL53L0X.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ VL53L0X::VL53L0X()
, address(ADDRESS_DEFAULT)
, io_timeout(0) // no timeout
, did_timeout(false)
, wire_error(0)
{
}

Expand Down Expand Up @@ -289,6 +290,7 @@ void VL53L0X::writeReg(uint8_t reg, uint8_t value)
bus->write(reg);
bus->write(value);
last_status = bus->endTransmission();
setWireErrorBit();
}

// Write a 16-bit register
Expand All @@ -299,6 +301,7 @@ void VL53L0X::writeReg16Bit(uint8_t reg, uint16_t value)
bus->write((value >> 8) & 0xFF); // value high byte
bus->write( value & 0xFF); // value low byte
last_status = bus->endTransmission();
setWireErrorBit();
}

// Write a 32-bit register
Expand All @@ -311,6 +314,7 @@ void VL53L0X::writeReg32Bit(uint8_t reg, uint32_t value)
bus->write((value >> 8) & 0xFF);
bus->write( value & 0xFF); // value lowest byte
last_status = bus->endTransmission();
setWireErrorBit();
}

// Read an 8-bit register
Expand All @@ -321,6 +325,7 @@ uint8_t VL53L0X::readReg(uint8_t reg)
bus->beginTransmission(address);
bus->write(reg);
last_status = bus->endTransmission();
setWireErrorBit();

bus->requestFrom(address, (uint8_t)1);
value = bus->read();
Expand All @@ -336,6 +341,7 @@ uint16_t VL53L0X::readReg16Bit(uint8_t reg)
bus->beginTransmission(address);
bus->write(reg);
last_status = bus->endTransmission();
setWireErrorBit();

bus->requestFrom(address, (uint8_t)2);
value = (uint16_t)bus->read() << 8; // value high byte
Expand All @@ -352,6 +358,7 @@ uint32_t VL53L0X::readReg32Bit(uint8_t reg)
bus->beginTransmission(address);
bus->write(reg);
last_status = bus->endTransmission();
setWireErrorBit();

bus->requestFrom(address, (uint8_t)4);
value = (uint32_t)bus->read() << 24; // value highest byte
Expand All @@ -375,6 +382,7 @@ void VL53L0X::writeMulti(uint8_t reg, uint8_t const * src, uint8_t count)
}

last_status = bus->endTransmission();
setWireErrorBit();
}

// Read an arbitrary number of bytes from the sensor, starting at the given
Expand All @@ -384,6 +392,7 @@ void VL53L0X::readMulti(uint8_t reg, uint8_t * dst, uint8_t count)
bus->beginTransmission(address);
bus->write(reg);
last_status = bus->endTransmission();
setWireErrorBit();

bus->requestFrom(address, count);

Expand Down Expand Up @@ -873,6 +882,15 @@ bool VL53L0X::timeoutOccurred()
return tmp;
}

// Did I2C transmission error occur in one of the read/write functions since
// the last call to getWireErrorBits()?
uint8_t VL53L0X::getWireErrorBits()
{
const uint8_t tmp = wire_error;
wire_error = 0;
return tmp;
}

// Private Methods /////////////////////////////////////////////////////////////

// Get reference SPAD (single photon avalanche diode) count and type
Expand Down
8 changes: 8 additions & 0 deletions VL53L0X.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class VL53L0X
inline void setTimeout(uint16_t timeout) { io_timeout = timeout; }
inline uint16_t getTimeout() { return io_timeout; }
bool timeoutOccurred();
uint8_t getWireErrorBits(void);

private:
// TCC: Target CentreCheck
Expand All @@ -158,6 +159,7 @@ class VL53L0X
uint16_t io_timeout;
bool did_timeout;
uint16_t timeout_start_ms;
uint8_t wire_error;

uint8_t stop_variable; // read by init and used when starting measurement; is StopVariable field of VL53L0X_DevData_t structure in API
uint32_t measurement_timing_budget_us;
Expand All @@ -173,6 +175,12 @@ class VL53L0X
static uint16_t encodeTimeout(uint32_t timeout_mclks);
static uint32_t timeoutMclksToMicroseconds(uint16_t timeout_period_mclks, uint8_t vcsel_period_pclks);
static uint32_t timeoutMicrosecondsToMclks(uint32_t timeout_period_us, uint8_t vcsel_period_pclks);

inline void setWireErrorBit(void) {
if (last_status) {
bitSet(wire_error, last_status);
}
}
};

#endif
Expand Down