@@ -31,31 +31,31 @@ extern "C" {
31
31
#include " ets_sys.h"
32
32
};
33
33
34
- // Inline helpers
35
- static inline __attribute__ ((always_inline)) void SDA_LOW(const int twi_sda)
36
- {
37
- GPES = (1 << twi_sda);
38
- }
39
- static inline __attribute__ ((always_inline)) void SDA_HIGH(const int twi_sda)
40
- {
41
- GPEC = (1 << twi_sda);
42
- }
43
- static inline __attribute__ ((always_inline)) bool SDA_READ(const int twi_sda)
44
- {
45
- return (GPI & (1 << twi_sda)) != 0 ;
46
- }
47
- static inline __attribute__ ((always_inline)) void SCL_LOW(const int twi_scl)
48
- {
49
- GPES = (1 << twi_scl);
50
- }
51
- static inline __attribute__ ((always_inline)) void SCL_HIGH(const int twi_scl)
52
- {
53
- GPEC = (1 << twi_scl);
54
- }
55
- static inline __attribute__ ((always_inline)) bool SCL_READ(const int twi_scl)
56
- {
57
- return (GPI & (1 << twi_scl)) != 0 ;
58
- }
34
+ // Inline helpers
35
+ static inline __attribute__ ((always_inline)) void SDA_LOW(const int twi_sda)
36
+ {
37
+ GPES = (1 << twi_sda);
38
+ }
39
+ static inline __attribute__ ((always_inline)) void SDA_HIGH(const int twi_sda)
40
+ {
41
+ GPEC = (1 << twi_sda);
42
+ }
43
+ static inline __attribute__ ((always_inline)) bool SDA_READ(const int twi_sda)
44
+ {
45
+ return (GPI & (1 << twi_sda)) != 0 ;
46
+ }
47
+ static inline __attribute__ ((always_inline)) void SCL_LOW(const int twi_scl)
48
+ {
49
+ GPES = (1 << twi_scl);
50
+ }
51
+ static inline __attribute__ ((always_inline)) void SCL_HIGH(const int twi_scl)
52
+ {
53
+ GPEC = (1 << twi_scl);
54
+ }
55
+ static inline __attribute__ ((always_inline)) bool SCL_READ(const int twi_scl)
56
+ {
57
+ return (GPI & (1 << twi_scl)) != 0 ;
58
+ }
59
59
60
60
61
61
// Implement as a class to reduce code size by allowing access to many global variables with a single base pointer
@@ -126,10 +126,12 @@ class Twi
126
126
{
127
127
esp8266::polledTimeout::oneShotFastUs timeout (twi_clockStretchLimit);
128
128
esp8266::polledTimeout::periodicFastUs yieldTimeout (5000 );
129
- while (!timeout && !SCL_READ (twi_scl)) // outer loop is stretch duration up to stretch limit
130
- {
129
+ while (!timeout && !SCL_READ (twi_scl)) // outer loop is stretch duration up to stretch limit
130
+ {
131
131
if (yieldTimeout) // inner loop yields every 5ms
132
+ {
132
133
yield ();
134
+ }
133
135
}
134
136
}
135
137
@@ -161,23 +163,29 @@ static Twi twi;
161
163
void Twi::setClock (unsigned int freq)
162
164
{
163
165
if (freq < 1000 ) // minimum freq 1000Hz to minimize slave timeouts and WDT resets
166
+ {
164
167
freq = 1000 ;
165
-
168
+ }
169
+
166
170
preferred_si2c_clock = freq;
167
171
168
172
#if F_CPU == FCPU80
169
173
170
174
if (freq > 400000 )
175
+ {
171
176
freq = 400000 ;
177
+ }
172
178
twi_dcount = (500000000 / freq); // half-cycle period in ns
173
- twi_dcount = (1000 * (twi_dcount - 1120 )) / 62500 ; // (half cycle - overhead) / busywait loop time
174
-
179
+ twi_dcount = (1000 * (twi_dcount - 1120 )) / 62500 ; // (half cycle - overhead) / busywait loop time
180
+
175
181
#else
176
182
177
183
if (freq > 800000 )
184
+ {
178
185
freq = 800000 ;
186
+ }
179
187
twi_dcount = (500000000 / freq); // half-cycle period in ns
180
- twi_dcount = (1000 * (twi_dcount - 560 )) / 31250 ; // (half cycle - overhead) / busywait loop time
188
+ twi_dcount = (1000 * (twi_dcount - 560 )) / 31250 ; // (half cycle - overhead) / busywait loop time
181
189
182
190
#endif
183
191
}
@@ -221,7 +229,7 @@ void Twi::enableSlave()
221
229
}
222
230
}
223
231
224
- void ICACHE_RAM_ATTR Twi::busywait (unsigned int v)
232
+ void ICACHE_RAM_ATTR Twi::busywait (unsigned int v)
225
233
{
226
234
unsigned int i;
227
235
for (i = 0 ; i < v; i++) // loop time is 5 machine cycles: 31.25ns @ 160MHz, 62.5ns @ 80MHz
@@ -463,7 +471,7 @@ void Twi::attachSlaveTxEvent(void (*function)(void))
463
471
twi_onSlaveTransmit = function;
464
472
}
465
473
466
- // DO NOT INLINE, inlining reply() in combination with compiler optimizations causes function breakup into
474
+ // DO NOT INLINE, inlining reply() in combination with compiler optimizations causes function breakup into
467
475
// parts and the ICACHE_RAM_ATTR isn't propagated correctly to all parts, which of course causes crashes.
468
476
// TODO: test with gcc 9.x and if it still fails, disable optimization with -fdisable-ipa-fnsplit
469
477
void ICACHE_RAM_ATTR Twi::reply (uint8_t ack)
@@ -660,7 +668,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
660
668
unsigned int scl;
661
669
662
670
// Store bool return in int to reduce final code size.
663
-
671
+
664
672
sda = SDA_READ (twi.twi_sda );
665
673
scl = SCL_READ (twi.twi_scl );
666
674
0 commit comments