Skip to content

Added support with 3v3 Devices and provision safe future reset by timeout #401

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 6 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
1 change: 1 addition & 0 deletions libraries/Wire/keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ endTransmission KEYWORD2
requestFrom KEYWORD2
onReceive KEYWORD2
onRequest KEYWORD2
disablePullups KEYWORD2

#######################################
# Instances (KEYWORD2)
Expand Down
2 changes: 1 addition & 1 deletion libraries/Wire/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Wire
version=1.0
version=1.1
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=This library allows you to communicate with I2C and Two Wire Interface devices.
Expand Down
6 changes: 6 additions & 0 deletions libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
Modified 2017 by Chuck Todd (ctodd@cableone.net) to correct Unconfigured Slave Mode reboot
Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts
Modified 2021 by Fernando Rubio (frubio@techdev.cl) to support I2C 3v3 devices
*/

extern "C" {
Expand Down Expand Up @@ -82,6 +83,11 @@ void TwoWire::end(void)
twi_disable();
}

void TwoWire::disablePullups(void)
{
twi_disablePullups();
}

void TwoWire::setClock(uint32_t clock)
{
twi_setFrequency(clock);
Expand Down
2 changes: 2 additions & 0 deletions libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts
Modified 2021 by Fernando Rubio (frubio@techdev.cl) to support I2C 3v3 devices
*/

#ifndef TwoWire_h
Expand Down Expand Up @@ -54,6 +55,7 @@ class TwoWire : public Stream
void begin(uint8_t);
void begin(int);
void end();
void disablePullups();
void setClock(uint32_t);
void setWireTimeout(uint32_t timeout = 25000, bool reset_with_timeout = false);
bool getWireTimeoutFlag(void);
Expand Down
22 changes: 19 additions & 3 deletions libraries/Wire/src/utility/twi.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts
Modified 2021 by Fernando Rubio (frubio@techdev.cl) to support I2C 3v3 devices
*/

#include <math.h>
Expand Down Expand Up @@ -55,6 +56,9 @@ static volatile uint32_t twi_timeout_us = 0ul;
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

//3v3 Compatibility need the feature of disable pullups and allow external pullups
static volatile uint8_t twi_pullups_state = 1; //Pull-ups Enabled by default as previous Lib versions, state is needed to support future resets from timeouts

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

Expand Down Expand Up @@ -84,9 +88,9 @@ void twi_init(void)
twi_sendStop = true; // default value
twi_inRepStart = false;

// activate internal pullups for twi.
digitalWrite(SDA, 1);
digitalWrite(SCL, 1);
// activate/deactivate internal pullups for twi according to default value.
digitalWrite(SDA, twi_pullups_state);
digitalWrite(SCL, twi_pullups_state);

// initialize twi prescaler and bit rate
cbi(TWSR, TWPS0);
Expand Down Expand Up @@ -118,6 +122,18 @@ void twi_disable(void)
digitalWrite(SCL, 0);
}

/*
* Function twi_disablePullups
* Desc disables internal pullups on init
* Input none
* Output none
*/
void twi_disablePullups(void)
{
// deactivate internal pullups for twi.
twi_pullups_state = 0;
}

/*
* Function twi_slaveInit
* Desc sets slave address and enables interrupt
Expand Down
2 changes: 2 additions & 0 deletions libraries/Wire/src/utility/twi.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Modified 2020 by Greyson Christoforo (grey@christoforo.net) to implement timeouts
Modified 2021 by Fernando Rubio (frubio@techdev.cl) to support I2C 3v3 devices
*/

#ifndef twi_h
Expand All @@ -42,6 +43,7 @@

void twi_init(void);
void twi_disable(void);
void twi_disablePullups(void);
void twi_setAddress(uint8_t);
void twi_setFrequency(uint32_t);
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t);
Expand Down