Skip to content

Commit 3b2ea52

Browse files
committed
Merge pull request #1563 from mvdbro/master
Configurable I2C clock stretching limit
2 parents f3a4c0a + 3f15903 commit 3b2ea52

File tree

5 files changed

+22
-8
lines changed

5 files changed

+22
-8
lines changed

cores/esp8266/core_esp8266_si2c.c

+15-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
unsigned char twi_dcount = 18;
2626
static unsigned char twi_sda, twi_scl;
27+
static uint32_t twi_clockStretchLimit;
2728

2829
#define SDA_LOW() (GPES = (1 << twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low)
2930
#define SDA_HIGH() (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high)
@@ -37,9 +38,9 @@ static unsigned char twi_sda, twi_scl;
3738
#endif
3839

3940
#if F_CPU == FCPU80
40-
#define TWI_CLOCK_STRETCH 800
41+
#define TWI_CLOCK_STRETCH_MULTIPLIER 3
4142
#else
42-
#define TWI_CLOCK_STRETCH 1600
43+
#define TWI_CLOCK_STRETCH_MULTIPLIER 6
4344
#endif
4445

4546
void twi_setClock(unsigned int freq){
@@ -60,14 +61,20 @@ void twi_setClock(unsigned int freq){
6061
#endif
6162
}
6263

64+
void twi_setClockStretchLimit(uint32_t limit){
65+
twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER;
66+
}
67+
6368
void twi_init(unsigned char sda, unsigned char scl){
6469
twi_sda = sda;
6570
twi_scl = scl;
6671
pinMode(twi_sda, INPUT_PULLUP);
6772
pinMode(twi_scl, INPUT_PULLUP);
6873
twi_setClock(100000);
74+
twi_setClockStretchLimit(230); // default value is 230 uS
6975
}
7076

77+
7178
void twi_stop(void){
7279
pinMode(twi_sda, INPUT);
7380
pinMode(twi_scl, INPUT);
@@ -93,12 +100,12 @@ static bool twi_write_start(void) {
93100
}
94101

95102
static bool twi_write_stop(void){
96-
unsigned int i = 0;
103+
uint32_t i = 0;
97104
SCL_LOW();
98105
SDA_LOW();
99106
twi_delay(twi_dcount);
100107
SCL_HIGH();
101-
while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
108+
while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching
102109
twi_delay(twi_dcount);
103110
SDA_HIGH();
104111
twi_delay(twi_dcount);
@@ -107,24 +114,24 @@ static bool twi_write_stop(void){
107114
}
108115

109116
static bool twi_write_bit(bool bit) {
110-
unsigned int i = 0;
117+
uint32_t i = 0;
111118
SCL_LOW();
112119
if (bit) SDA_HIGH();
113120
else SDA_LOW();
114121
twi_delay(twi_dcount+1);
115122
SCL_HIGH();
116-
while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
123+
while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching
117124
twi_delay(twi_dcount);
118125
return true;
119126
}
120127

121128
static bool twi_read_bit(void) {
122-
unsigned int i = 0;
129+
uint32_t i = 0;
123130
SCL_LOW();
124131
SDA_HIGH();
125132
twi_delay(twi_dcount+2);
126133
SCL_HIGH();
127-
while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
134+
while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching
128135
bool bit = SDA_READ();
129136
twi_delay(twi_dcount);
130137
return bit;

cores/esp8266/twi.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ extern "C" {
2929
void twi_init(unsigned char sda, unsigned char scl);
3030
void twi_stop(void);
3131
void twi_setClock(unsigned int freq);
32+
void twi_setClockStretchLimit(uint32_t limit);
3233
uint8_t twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop);
3334
uint8_t twi_readFrom(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop);
3435

libraries/Wire/Wire.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ void TwoWire::setClock(uint32_t frequency){
8585
twi_setClock(frequency);
8686
}
8787

88+
void TwoWire::setClockStretchLimit(uint32_t limit){
89+
twi_setClockStretchLimit(limit);
90+
}
91+
8892
size_t TwoWire::requestFrom(uint8_t address, size_t size, bool sendStop){
8993
if(size > BUFFER_LENGTH){
9094
size = BUFFER_LENGTH;

libraries/Wire/Wire.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class TwoWire : public Stream
5656
void begin(uint8_t);
5757
void begin(int);
5858
void setClock(uint32_t);
59+
void setClockStretchLimit(uint32_t);
5960
void beginTransmission(uint8_t);
6061
void beginTransmission(int);
6162
uint8_t endTransmission(void);

libraries/Wire/keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
begin KEYWORD2
1414
setClock KEYWORD2
15+
setClockStretchLimit KEYWORD2
1516
beginTransmission KEYWORD2
1617
endTransmission KEYWORD2
1718
requestFrom KEYWORD2

0 commit comments

Comments
 (0)