Skip to content

Commit d739b3f

Browse files
Wire: Expose 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 twi.h and exposed through Wire.h, which: - Makes it easier to change later - Allows sketches to detect the default timeout value Note that this definition is split between twi.h and Wire.h to ensure that the default values are available as compile-time constants in twi.c (which allows for more efficient initialization than e.g. writing these values from the Wire constructor or from the begin() method, where the latter has the extra downside of potentially overwriting values previously set with setWireTimeout() if begin() is called again). Additionally, since twi does not currently depend on Wire.h, defining these values in Wire.h and using them in twi.c was not feasible without breaking adding this extra dependency, so instead this commit defines the values in twi and then re-exposes them in Wire.h (with the intention of sketches referring to the Wire.h versions, not the twi.h versions). See #362 for additional discussion.
1 parent 5c2ee7c commit d739b3f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

Diff for: libraries/Wire/src/Wire.h

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <inttypes.h>
2727
#include "Stream.h"
28+
#include "utility/twi.h"
2829

2930
#define BUFFER_LENGTH 32
3031

@@ -34,6 +35,10 @@
3435
// and clearWireTimeoutFlag()
3536
#define WIRE_HAS_TIMEOUT 1
3637

38+
// When not configured, these settings are used for the timeout
39+
#define WIRE_DEFAULT_TIMEOUT TWI_DEFAULT_TIMEOUT
40+
#define WIRE_DEFAULT_RESET_ON_TIMEOUT TWI_DEFAULT_RESET_ON_TIMEOUT
41+
3742
class TwoWire : public Stream
3843
{
3944
private:

Diff for: libraries/Wire/src/utility/twi.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ static volatile uint8_t twi_inRepStart; // in the middle of a repeated start
5151
// and twi_do_reset_on_timeout could become true
5252
// to conform to the SMBus standard
5353
// http://smbus.org/specs/SMBus_3_1_20180319.pdf
54-
static volatile uint32_t twi_timeout_us = 0ul;
54+
static volatile uint32_t twi_timeout_us = TWI_DEFAULT_TIMEOUT;
5555
static volatile bool twi_timed_out_flag = false; // a timeout has been seen
56-
static volatile bool twi_do_reset_on_timeout = false; // reset the TWI registers on timeout
56+
static volatile bool twi_do_reset_on_timeout = TWI_DEFAULT_RESET_ON_TIMEOUT; // reset the TWI registers on timeout
5757

5858
static void (*twi_onSlaveTransmit)(void);
5959
static void (*twi_onSlaveReceive)(uint8_t*, int);

Diff for: libraries/Wire/src/utility/twi.h

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@
3434
#define TWI_BUFFER_LENGTH 32
3535
#endif
3636

37+
#ifndef TWI_DEFAULT_TIMEOUT
38+
#define TWI_DEFAULT_TIMEOUT 0
39+
#endif
40+
41+
#ifndef TWI_DEFAULT_RESET_ON_TIMEOUT
42+
#define TWI_DEFAULT_RESET_ON_TIMEOUT 0
43+
#endif
44+
3745
#define TWI_READY 0
3846
#define TWI_MRX 1
3947
#define TWI_MTX 2

0 commit comments

Comments
 (0)