From 3bd4d11ad61960f71715516114d6d00bc3549495 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 21:16:56 +0200 Subject: [PATCH 1/3] Wire: Define WIRE_HAS_TIMEOUT In commit deea929 (Introduce non compulsory Wire timeout), some timeout-related functions were added. To allow writing portable sketches, it is important for those sketch to know whether they are using a Wire library version that is new enough to offer these functions without having to rely on version number checking (since other Arduino cores have their own versioning schemes). This adds a WIRE_HAS_TIMEOUT macro, similar to the existing WIRE_HAS_END macro, to facilitate that. This relates to #42. --- libraries/Wire/src/Wire.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index e70d72edb..c946b0486 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -30,6 +30,9 @@ // WIRE_HAS_END means Wire has end() #define WIRE_HAS_END 1 +// WIRE_HAS_TIMEOUT means Wire has setWireTimeout(), getWireTimeoutFlag +// and clearWireTimeoutFlag() +#define WIRE_HAS_TIMEOUT 1 class TwoWire : public Stream { From 4f53e51ec3313eab3e65c8df2304203259c099d5 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 21:33:44 +0200 Subject: [PATCH 2/3] Wire: Define default timeout settings in Wire.h Previously, these were implicit in the default values of some global variables. Now, the default timeout is defined in Wire.h, which: - Makes it easier to change later - Allows sketches to detect the default timeout value --- libraries/Wire/src/Wire.cpp | 2 ++ libraries/Wire/src/Wire.h | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index c407776e7..b221d4c78 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -64,6 +64,8 @@ void TwoWire::begin(void) twi_init(); twi_attachSlaveTxEvent(onRequestService); // default callback must exist twi_attachSlaveRxEvent(onReceiveService); // default callback must exist + + twi_setTimeoutInMicros(WIRE_DEFAULT_TIMEOUT, WIRE_DEFAULT_RESET_WITH_TIMEOUT); } void TwoWire::begin(uint8_t address) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index c946b0486..296f0f046 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -34,6 +34,10 @@ // and clearWireTimeoutFlag() #define WIRE_HAS_TIMEOUT 1 +// When not configured, these settings are used for the timeout +#define WIRE_DEFAULT_TIMEOUT 0 +#define WIRE_DEFAULT_RESET_WITH_TIMEOUT false + class TwoWire : public Stream { private: From b521738f65bc5b7f93dad142f89a66e61179f8a1 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 17 Sep 2020 21:53:11 +0200 Subject: [PATCH 3/3] Wire: Enable a timeout by default When no timeout is explicitly configured, Wire now uses a timeout of 25ms, resetting the Wire hardware if a timeout occurs. The timeout length matches the timeout you get when you call `Wire.setWireTimeout()` without arguments, and is loosely based on the SMBus timeout and maximum clock stretching time (though it works quite differently) and is rather long. In practice, this means that even if another master is on the bus, or slaves are using significant clock stretching, the timeout will probably not trigger unless there really is a lockup. This also enables a reset of the Wire hardware on a timeout (unlike `Wire.setWireTimeout()` without arguments), under the assumption that if a sketch has not set up timeout settings explicitly, it probably just wants things to keep running as well as possible and resetting allows recovering from most transient lockups. See #42 for earlier discussion of this. --- libraries/Wire/src/Wire.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 296f0f046..68a364961 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -35,8 +35,8 @@ #define WIRE_HAS_TIMEOUT 1 // When not configured, these settings are used for the timeout -#define WIRE_DEFAULT_TIMEOUT 0 -#define WIRE_DEFAULT_RESET_WITH_TIMEOUT false +#define WIRE_DEFAULT_TIMEOUT 25000 +#define WIRE_DEFAULT_RESET_WITH_TIMEOUT true class TwoWire : public Stream {