@@ -31,31 +31,31 @@ extern "C" {
3131#include " ets_sys.h"
3232};
3333
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+ }
5959
6060
6161// 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
126126 {
127127 esp8266::polledTimeout::oneShotFastUs timeout (twi_clockStretchLimit);
128128 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+ {
131131 if (yieldTimeout) // inner loop yields every 5ms
132+ {
132133 yield ();
134+ }
133135 }
134136 }
135137
@@ -161,23 +163,29 @@ static Twi twi;
161163void Twi::setClock (unsigned int freq)
162164{
163165 if (freq < 1000 ) // minimum freq 1000Hz to minimize slave timeouts and WDT resets
166+ {
164167 freq = 1000 ;
165-
168+ }
169+
166170 preferred_si2c_clock = freq;
167171
168172#if F_CPU == FCPU80
169173
170174 if (freq > 400000 )
175+ {
171176 freq = 400000 ;
177+ }
172178 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+
175181#else
176182
177183 if (freq > 800000 )
184+ {
178185 freq = 800000 ;
186+ }
179187 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
181189
182190#endif
183191}
@@ -221,7 +229,7 @@ void Twi::enableSlave()
221229 }
222230}
223231
224- void ICACHE_RAM_ATTR Twi::busywait (unsigned int v)
232+ void ICACHE_RAM_ATTR Twi::busywait (unsigned int v)
225233{
226234 unsigned int i;
227235 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))
463471 twi_onSlaveTransmit = function;
464472}
465473
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
467475// parts and the ICACHE_RAM_ATTR isn't propagated correctly to all parts, which of course causes crashes.
468476// TODO: test with gcc 9.x and if it still fails, disable optimization with -fdisable-ipa-fnsplit
469477void ICACHE_RAM_ATTR Twi::reply (uint8_t ack)
@@ -660,7 +668,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
660668 unsigned int scl;
661669
662670 // Store bool return in int to reduce final code size.
663-
671+
664672 sda = SDA_READ (twi.twi_sda );
665673 scl = SCL_READ (twi.twi_scl );
666674
0 commit comments