Hello
I am working with an UNO r4 WiFi board and a modulino distance module on a Qwiic cable.
Randomly, the program hangs completely upon electrical noise. (I don't want to discuss the noise issues, but the hanging.)
The relevant code snippet is:
//measuring wall position
Serial.println("check availability of data");
if (distance.available()) {
// the program randomly hangs here, probably on electrical interference.
Serial.println(" measureing before get");
measure = distance.get() ;
Wallposition = measure-wallZero;
}
else {
// the program never arrives here
Serial.println("no data");
nodata=nodata+1;
}
The problem seems to be that the distance library code goes into an infinite hang, instead of returning to allow a graceful recovery.
The part of the modulino.h providing this response is:
Let me add--I just measured DC levels, and indeed the measure module is holding the SDA line of the i2c low. That is probably not supposed to happen. I guess it means digging into the api for that chip.
Continued with a solution. To diagnose this problem, I inserted a print statement in various places in the distance.available part of the modulino library. It seemed that the print statements and delays improved the behavior. I saw that in other parts of the module there were hubport resets present. Experimenting around, I found that the combination "delay(1); hubPort->clear();" inserted after each api call prevented the observed lockup. The modified code is below.
I also added timingbudget as a variable which I will add as a function when I learn how to do that.
It is interesting that the module as it exists would return the old data when check for data was false. Fixed it.
class ModulinoDistance : public Module {
public:
ModulinoDistance(ModulinoHubPort* hubPort = nullptr)
: Module(0xFF, "DISTANCE", hubPort) {}
bool begin() {
if (hubPort != nullptr) {
hubPort->select();
}
// try scanning for 0x29 since the library contains a while(true) on begin()
getWire()->beginTransmission(0x29);
if (getWire()->endTransmission() != 0) {
if (hubPort != nullptr) {
hubPort->clear();
}
return false;
}
tof_sensor = new VL53L4CD((TwoWire*)getWire(), -1);
auto ret = tof_sensor->InitSensor();
if (ret != VL53L4CD_ERROR_NONE) {
delete tof_sensor;
tof_sensor = nullptr;
tof_sensor_alt = new VL53L4ED((TwoWire*)getWire(), -1);
ret = tof_sensor_alt->InitSensor();
if (ret == VL53L4ED_ERROR_NONE) {
api = new _distance_api(tof_sensor_alt);
} else {
delete tof_sensor_alt;
tof_sensor_alt = nullptr;
if (hubPort != nullptr) {
hubPort->clear();
}
return false;
}
} else {
api = new _distance_api(tof_sensor);
}
__increaseI2CPriority();
/***************************
* Gerhard Randers-Pehrson added timingbudget as a variable which is
* declared below in private section, set to a default of 50 msec.
* Allowed Values are between 10 and 200 msec.
* It would be better to add a function to set it also.
****************************************/
api->setRangeTiming(timingbudget, 0); //sets time defined in private declarations below
api->startRanging();
if (hubPort != nullptr) {
hubPort->clear();
}
return true;
}
operator bool() {
return (api != nullptr);
}
/***********************
* Gerhard Randers-Pehrson has added some features here to avoid i2c lockups
* which are observed when using an UNO r4 WiFi board.
* First, using a timer lastcall, we return false if the time between calls
* is less than the measurement time called timingbudget.
* Secondly, the line "delay(1); hubPort->clear();" is added at the beginning
* and after each api call.
* And third, there are several Serial.print statements used for diagnostics.
* They are commented out here.
*************************************/
bool available() {
static int lastcall;
if (api == nullptr) {
return false;
}
if (hubPort != nullptr) {
hubPort->select();
}
delay(1); hubPort->clear();
long test=millis()-lastcall-timingbudget-10;
//Serial.print("time for next call = "); // do not poll too early
//Serial.println(test);
if(test<0) return false;
lastcall=millis();
uint8_t NewDataReady = 0;
api->checkForDataReady(&NewDataReady);
delay(1); hubPort->clear();//delay(1);
//Serial.print(" after checkForDataReady ");
if (NewDataReady) {
//Serial.print(" new data available ");
api->clearInterrupt();
delay(1); hubPort->clear();
//Serial.print(" cleared interrupt ");
api->getResult(&results);
delay(1); hubPort->clear();
}
else {
//Serial.println(" no data ");
return false;
}
//if (hubPort != nullptr)
hubPort->clear();
//Serial.print(" check result ");
if (results.range_status == 0) {
internal = results.distance_mm;
} else {
internal = NAN;
}
//Serial.println(internal);
return !isnan(internal);
}
float get() {
return internal;
}
private:
VL53L4CD* tof_sensor = nullptr;
VL53L4ED* tof_sensor_alt = nullptr;
VL53L4CD_Result_t results;
//VL53L4ED_ResultsData_t results;
float internal = NAN;
_distance_api* api = nullptr;
/*****************************************
* This is the integration time for the sensor
* added as a variable by Gerhard Randers-Pehrson
* ************************/
int timingbudget=50; //allowed values 10 to 200 msec.
};
Damn, crashed 12 seconds short of an hour. W
Still would like to know a recovery technique beyond rebooting.
Oh, wait. It crashed on a different modulino element in the loop--the knob. More digging in my future. G.
As I said, discussion of reducing EMF noise will detract from my real question:
How do you prevent or recover from an i2c module holding SDA low?
The noise is from a PWM controlled fan, but I don't want advice on fixing that here.
Meandering thought here... a device holding SDA LOW might be because the library/driver has determined the state of the bus to be "SDA LOW"... so if this is a condition of "something" on the command bus that is not allowing the state to change... then we would want to find what that thing is that is commanding the state, or not allowing the state to change.
Excerpts ... "Root causes for a stuck bus can come from crosstalk, electromagnetic interference, hot-insertion events, or bad power on reset situations... 3 Crosstalk... A common way a false edge can be generated is through crosstalk..."
That is a useful document. I guess, since the loop in my sketch is blocked, this reset would have to be done through some sort of heartbeat interrupt. I wonder how that is done.
OK--Status.
It is true, following the advice in this thread, twisting the power leads to my breakout board reduced crashing significantly, fot not completely.
The question of which module was responsible lead me to look at others used in the i2c string, even though the lockup occurs within the call to the distance module.
I found that the knob module also contributed (Removing it stopped the crashes.) I was able to add some lines in the knob portion of the modulino.h which completely eliminated the crashing.
The knob.get routine was modified here:
int16_t get() {
uint8_t buf[3];
auto res = read(buf, 3);
if (res == false) {
/*****************************
* These three lines are added by Gerhard Randers-Pehrson
* To prevent i2c lockup on electrical interference
***********************************/
Serial.print( "knob res=false");
hubPort->clear();
delay(1);
return 0;
}
_pressed = (buf[2] != 0);
int16_t ret = buf[0] | (buf[1] << 8);
return ret;
}
I find it odd that the Serial.print() statement is necessary for the cure. My guess is that the serial and i2c routines share some multiplexed hardware and therefore the print statement clears the jam.
So I will proceed with my project.