Title says everything: I get an undocumented error code from Wire.endTransmission(), code 5. Here's the relevant code.
// read FIFO pointers
Wire.beginTransmission(max30105);
Wire.write(0x04);
error = Wire.endTransmission();
nbytes = Wire.requestFrom(max30105, 3, true); //read 3 bytes for the two FIFO pointers and overflow counter
if (error != 0) {
Serial.print("Could not read FIFO pointers, error code (if zero, number of bytes returned was wrong): ");
Serial.println(error);
return; // for some reason, the program crashes at this point
}
Output:
Could not read FIFO pointers, error code (if zero, number of bytes returned was wrong): 5
The code is in an interrupt routine. I did not have problems before moving it to the interrupt routine, which I did because of other problems not to discuss here. Time between interrupts is 50 ms.
My board is an Adafruit ESP32 Feather. I use Arduino IDE version 1.8.15.
What does the error code mean and how can I possibly fix it?
Thanks for reading.
PS: Referencing this documentation page: Wire - Arduino Reference
It says corrections should be posted to the forum but the link does not work so that's another correction to make besides adding error code 5.
The two wire interface operates off of interrupts. If you actually look at the twi code for the Arduino most functions wait for status that is set by the TWI interrupt. I think calling the wire functions from an interrupt is problematic. Not a 2 wire expert but I bet this is an issue, which is why it worked outside of interrupt code.
Thanks for your answers. Looks like the code snippet was sufficient after all.
By ISR, "interrupt service register" is meant? Of my sensor or of the ESP? Is it unrecommended or simply impossible to use I²C from an interrupt routine?
The 2 wire code usually sets up for an operation and performs what we usually call a "busy wait" waiting for the ISR to complete. In other words, nothing is getting done while waiting, just spinning in a loop. This is not desired in an interrupt and, in fact, depending on processor hardware might even cause the system to hang if interrupts are disabled and the interrupt you are waiting on can't happen!
There might a way to get it to work (I'm not an ESP32 expert) but it could cause a lot of other issues, like affecting timers, communications, etc. Just not a good idea!