Skip to content

Added code to demonstrate use of new I2C timeout feature #356

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 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions libraries/Wire/examples/i2c_scanner/i2c_scanner.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// A sensor seems to use address 120.
// Version 6, November 27, 2015.
// Added waiting for the Leonardo serial communication.
// Version 6, July 10, 2020 by G. Frank Paynter
// Demonstrates the use of the new Wire library timeout feature
//
//
// This sketch tests the standard 7-bit addresses
Expand All @@ -29,15 +31,29 @@

#include <Wire.h>

uint16_t wireTimeoutCount; //capture I2C bus timeout events

void setup() {
Wire.begin();

Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("\nI2C Scanner");

Wire.setWireTimeout(3000, true); //timeout value in uSec, true to reset I2C bus on timeout
wireTimeoutCount = 0;
Wire.clearWireTimeoutFlag();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not put this directly after the Wire.begin()? Seems more clear? Also, is clearing the timeout flag really needed here? We can rely on it being cleared on startup, I think? Same for initializing the count to 0, why not just do that in the variable declaration?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment; I moved the anti-lockup code to just under 'Wire.begin()' as you suggested. I wanted to put the timeout flag in to expose its existence to a new user; otherwise they might never know the timeout flag existed at all or how to query it.

I could initialize the variable in the declaration, but I have learned that assuming a variable has been initialized to zero 'somewhere else' can lead to confusing/incorrect behavior. I like making things obvious and explicit, even at the cost of an extra line or two of code. YMMV ;-).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to put the timeout flag in to expose its existence to a new user; otherwise they might never know the timeout flag existed at all or how to query it.

Yeah, that's fine (and I think this might be a good example for it), I was just suggesting not doing the timeout flag handling in all examples (OTOH, maybe it should be).

I like making things obvious and explicit, even at the cost of an extra line or two of code. YMMV ;-).

It does (on this particular thing, I do like explicit in general, but also concise), but it's ok :-)

What about (not) calling clearWireTimeoutFlag() on startup? You've now added a comment that seems less than helpful?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I removed the call to clearWireTimeoutFlag() as it is explicitly cleared in Wire.setWireTimeout()


}

void loop() {
if (Wire.getWireTimeoutFlag())
{
wireTimeoutCount++;
Wire.clearWireTimeoutFlag();
Serial.print("Wire timeout detected; count now "); Serial.println(wireTimeoutCount);
}

int nDevices = 0;

Serial.println("Scanning...");
Expand Down