Skip to content

Wire: Define WIRE_HAS_TIMEOUT #362

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 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,23 @@ void TwoWire::setClock(uint32_t clock)
* master that has claimed the bus).
*
* When a timeout is triggered, a flag is set that can be queried with `getWireTimeoutFlag()` and is cleared
* when `clearWireTimeoutFlag()` or `setWireTimeoutUs()` is called.
* when `clearWireTimeoutFlag()` or `setWireTimeout()` is called.
*
* Note that this timeout can also trigger while waiting for clock stretching or waiting for a second master
* to complete its transaction. So make sure to adapt the timeout to accomodate for those cases if needed.
* A typical timeout would be 25ms (which is the maximum clock stretching allowed by the SMBus protocol),
* but (much) shorter values will usually also work.
*
* In the future, a timeout will be enabled by default, so if you require the timeout to be disabled, it is
* recommended you disable it by default using `setWireTimeoutUs(0)`, even though that is currently
* recommended you disable it by default using `setWireTimeout(0)`, even though that is currently
* the default.
*
* @param timeout a timeout value in microseconds, if zero then timeout checking is disabled
* @param reset_with_timeout if true then TWI interface will be automatically reset on timeout
* @param reset_on_timeout if true then TWI interface will be automatically reset on timeout
* if false then TWI interface will not be reset on timeout

*/
void TwoWire::setWireTimeout(uint32_t timeout, bool reset_with_timeout){
twi_setTimeoutInMicros(timeout, reset_with_timeout);
void TwoWire::setWireTimeout(uint32_t timeout, bool reset_on_timeout){
twi_setTimeoutInMicros(timeout, reset_on_timeout);
}

/***
Expand Down
10 changes: 9 additions & 1 deletion libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@

#include <inttypes.h>
#include "Stream.h"
#include "utility/twi.h"

#define BUFFER_LENGTH 32

// 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

// When not configured, these settings are used for the timeout
#define WIRE_DEFAULT_TIMEOUT TWI_DEFAULT_TIMEOUT
#define WIRE_DEFAULT_RESET_ON_TIMEOUT TWI_DEFAULT_RESET_ON_TIMEOUT

class TwoWire : public Stream
{
Expand All @@ -55,7 +63,7 @@ class TwoWire : public Stream
void begin(int);
void end();
void setClock(uint32_t);
void setWireTimeout(uint32_t timeout = 25000, bool reset_with_timeout = false);
void setWireTimeout(uint32_t timeout = 25000, bool reset_on_timeout = false);
bool getWireTimeoutFlag(void);
void clearWireTimeoutFlag(void);
void beginTransmission(uint8_t);
Expand Down
10 changes: 5 additions & 5 deletions libraries/Wire/src/utility/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ static volatile uint8_t twi_inRepStart; // in the middle of a repeated start
// and twi_do_reset_on_timeout could become true
// to conform to the SMBus standard
// http://smbus.org/specs/SMBus_3_1_20180319.pdf
static volatile uint32_t twi_timeout_us = 0ul;
static volatile uint32_t twi_timeout_us = TWI_DEFAULT_TIMEOUT;
static volatile bool twi_timed_out_flag = false; // a timeout has been seen
static volatile bool twi_do_reset_on_timeout = false; // reset the TWI registers on timeout
static volatile bool twi_do_reset_on_timeout = TWI_DEFAULT_RESET_ON_TIMEOUT; // reset the TWI registers on timeout

static void (*twi_onSlaveTransmit)(void);
static void (*twi_onSlaveReceive)(uint8_t*, int);
Expand Down Expand Up @@ -451,13 +451,13 @@ void twi_releaseBus(void)
* Function twi_setTimeoutInMicros
* Desc set a timeout for while loops that twi might get stuck in
* Input timeout value in microseconds (0 means never time out)
* Input reset_with_timeout: true causes timeout events to reset twi
* Input reset_on_timeout: true causes timeout events to reset twi
* Output none
*/
void twi_setTimeoutInMicros(uint32_t timeout, bool reset_with_timeout){
void twi_setTimeoutInMicros(uint32_t timeout, bool reset_on_timeout){
twi_timed_out_flag = false;
twi_timeout_us = timeout;
twi_do_reset_on_timeout = reset_with_timeout;
twi_do_reset_on_timeout = reset_on_timeout;
}

/*
Expand Down
8 changes: 8 additions & 0 deletions libraries/Wire/src/utility/twi.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
#define TWI_BUFFER_LENGTH 32
#endif

#ifndef TWI_DEFAULT_TIMEOUT
#define TWI_DEFAULT_TIMEOUT 0
#endif

#ifndef TWI_DEFAULT_RESET_ON_TIMEOUT
#define TWI_DEFAULT_RESET_ON_TIMEOUT 0
#endif

#define TWI_READY 0
#define TWI_MRX 1
#define TWI_MTX 2
Expand Down