@@ -96,36 +96,37 @@ class Twi
9696 void ICACHE_RAM_ATTR onTwipEvent (uint8_t status);
9797
9898 // Inline helpers
99- inline void SDA_LOW ()
99+ static inline void SDA_LOW (const int twi_sda) __attribute__((always_inline) )
100100 {
101101 GPES = (1 << twi_sda);
102102 }
103- inline void SDA_HIGH ()
103+ static inline void SDA_HIGH (const int twi_sda) __attribute__((always_inline) )
104104 {
105105 GPEC = (1 << twi_sda);
106106 }
107- inline bool SDA_READ ()
108- {
107+ static inline bool SDA_READ (const int twi_sda) __attribute__((always_inline) )
108+ {
109109 return (GPI & (1 << twi_sda)) != 0 ;
110110 }
111- inline void SCL_LOW ()
111+ static inline void SCL_LOW (const int twi_scl) __attribute__((always_inline) )
112112 {
113113 GPES = (1 << twi_scl);
114114 }
115- inline void SCL_HIGH ()
115+ static inline void SCL_HIGH (const int twi_scl) __attribute__((always_inline) )
116116 {
117117 GPEC = (1 << twi_scl);
118118 }
119- inline bool SCL_READ ()
120- {
119+ static inline bool SCL_READ (const int twi_scl) __attribute__((always_inline) )
120+ {
121121 return (GPI & (1 << twi_scl)) != 0 ;
122122 }
123+
123124 // Handle the case where a slave needs to stretch the clock with a time-limited busy wait
124125 inline void WAIT_CLOCK_STRETCH ()
125126 {
126127 esp8266::polledTimeout::oneShotFastUs timeout (twi_clockStretchLimit);
127128 esp8266::polledTimeout::periodicFastUs yieldTimeout (5000 );
128- while (!timeout && !SCL_READ ()) // outer loop is stretch duration up to stretch limit
129+ while (!timeout && !SCL_READ (twi_scl )) // outer loop is stretch duration up to stretch limit
129130 {
130131 if (yieldTimeout) // inner loop yields every 5ms
131132 yield ();
@@ -277,57 +278,57 @@ void ICACHE_RAM_ATTR Twi::busywait(unsigned char v)
277278
278279bool Twi::write_start (void )
279280{
280- SCL_HIGH ();
281- SDA_HIGH ();
282- if (!SDA_READ ())
281+ SCL_HIGH (twi_scl );
282+ SDA_HIGH (twi_sda );
283+ if (!SDA_READ (twi_sda ))
283284 {
284285 return false ;
285286 }
286287 busywait (twi_dcount);
287- SDA_LOW ();
288+ SDA_LOW (twi_sda );
288289 busywait (twi_dcount);
289290 return true ;
290291}
291292
292293bool Twi::write_stop (void )
293294{
294- SCL_LOW ();
295- SDA_LOW ();
295+ SCL_LOW (twi_scl );
296+ SDA_LOW (twi_sda );
296297 busywait (twi_dcount);
297- SCL_HIGH ();
298+ SCL_HIGH (twi_scl );
298299 WAIT_CLOCK_STRETCH ();
299300 busywait (twi_dcount);
300- SDA_HIGH ();
301+ SDA_HIGH (twi_sda );
301302 busywait (twi_dcount);
302303 return true ;
303304}
304305
305306bool Twi::write_bit (bool bit)
306307{
307- SCL_LOW ();
308+ SCL_LOW (twi_scl );
308309 if (bit)
309310 {
310- SDA_HIGH ();
311+ SDA_HIGH (twi_sda );
311312 }
312313 else
313314 {
314- SDA_LOW ();
315+ SDA_LOW (twi_sda );
315316 }
316317 busywait (twi_dcount + 1 );
317- SCL_HIGH ();
318+ SCL_HIGH (twi_scl );
318319 WAIT_CLOCK_STRETCH ();
319320 busywait (twi_dcount);
320321 return true ;
321322}
322323
323324bool Twi::read_bit (void )
324325{
325- SCL_LOW ();
326- SDA_HIGH ();
326+ SCL_LOW (twi_scl );
327+ SDA_HIGH (twi_sda );
327328 busywait (twi_dcount + 2 );
328- SCL_HIGH ();
329+ SCL_HIGH (twi_scl );
329330 WAIT_CLOCK_STRETCH ();
330- bool bit = SDA_READ ();
331+ bool bit = SDA_READ (twi_sda );
331332 busywait (twi_dcount);
332333 return bit;
333334}
@@ -392,7 +393,7 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned
392393 // busywait(twi_dcount);
393394 }
394395 i = 0 ;
395- while (!SDA_READ () && (i++) < 10 )
396+ while (!SDA_READ (twi_sda ) && (i++) < 10 )
396397 {
397398 twi_scl_valley ();
398399 busywait (twi_dcount);
@@ -431,7 +432,7 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
431432 // busywait(twi_dcount);
432433 }
433434 i = 0 ;
434- while (!SDA_READ () && (i++) < 10 )
435+ while (!SDA_READ (twi_sda ) && (i++) < 10 )
435436 {
436437 twi_scl_valley ();
437438 busywait (twi_dcount);
@@ -442,21 +443,21 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
442443uint8_t Twi::status ()
443444{
444445 WAIT_CLOCK_STRETCH (); // wait for a slow slave to finish
445- if (!SCL_READ ())
446+ if (!SCL_READ (twi_scl ))
446447 {
447448 return I2C_SCL_HELD_LOW; // SCL held low by another device, no procedure available to recover
448449 }
449450
450451 int clockCount = 20 ;
451- while (!SDA_READ () && clockCount-- > 0 ) // if SDA low, read the bits slaves have to sent to a max
452+ while (!SDA_READ (twi_sda ) && clockCount-- > 0 ) // if SDA low, read the bits slaves have to sent to a max
452453 {
453454 read_bit ();
454- if (!SCL_READ ())
455+ if (!SCL_READ (twi_scl ))
455456 {
456457 return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time
457458 }
458459 }
459- if (!SDA_READ ())
460+ if (!SDA_READ (twi_sda ))
460461 {
461462 return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits.
462463 }
@@ -506,13 +507,13 @@ inline void ICACHE_RAM_ATTR Twi::reply(uint8_t ack)
506507 if (ack)
507508 {
508509 // TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
509- SCL_HIGH (); // _BV(TWINT)
510+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
510511 twi_ack = 1 ; // _BV(TWEA)
511512 }
512513 else
513514 {
514515 // TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
515- SCL_HIGH (); // _BV(TWINT)
516+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
516517 twi_ack = 0 ; // ~_BV(TWEA)
517518 }
518519}
@@ -521,10 +522,10 @@ inline void ICACHE_RAM_ATTR Twi::stop(void)
521522{
522523 // send stop condition
523524 // TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
524- SCL_HIGH (); // _BV(TWINT)
525+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
525526 twi_ack = 1 ; // _BV(TWEA)
526527 busywait (5 ); // Maybe this should be here
527- SDA_HIGH (); // _BV(TWSTO)
528+ SDA_HIGH (twi. twi_sda ); // _BV(TWSTO)
528529 // update twi state
529530 twi_state = TWI_READY;
530531}
@@ -533,9 +534,9 @@ inline void ICACHE_RAM_ATTR Twi::releaseBus(void)
533534{
534535 // release bus
535536 // TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
536- SCL_HIGH (); // _BV(TWINT)
537+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
537538 twi_ack = 1 ; // _BV(TWEA)
538- SDA_HIGH ();
539+ SDA_HIGH (twi. twi_sda );
539540
540541 // update twi state
541542 twi_state = TWI_READY;
@@ -616,11 +617,11 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status)
616617 bitCount--;
617618 if (twi_data & 0x80 )
618619 {
619- SDA_HIGH ();
620+ SDA_HIGH (twi. twi_sda );
620621 }
621622 else
622623 {
623- SDA_LOW ();
624+ SDA_LOW (twi. twi_sda );
624625 }
625626 twi_data <<= 1 ;
626627
@@ -652,9 +653,9 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status)
652653
653654void Twi::twi_scl_valley (void )
654655{
655- SCL_LOW ();
656+ SCL_LOW (twi_scl );
656657 busywait (twi_dcount);
657- SCL_HIGH ();
658+ SCL_HIGH (twi_scl );
658659 WAIT_CLOCK_STRETCH ();
659660}
660661
@@ -714,8 +715,9 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
714715 unsigned int scl;
715716
716717 // Store bool return in int to reduce final code size.
717- sda = twi.SDA_READ ();
718- scl = twi.SCL_READ ();
718+
719+ sda = twi.SDA_READ (twi.twi_sda );
720+ scl = twi.SCL_READ (twi.twi_scl );
719721
720722 twi.twip_status = 0xF8 ; // reset TWI status
721723
@@ -758,7 +760,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
758760 }
759761 else
760762 {
761- twi.SDA_LOW ();
763+ twi.SDA_LOW (twi. twi_sda );
762764 }
763765 }
764766 else
@@ -769,7 +771,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
769771 }
770772 else
771773 {
772- twi.SDA_LOW ();
774+ twi.SDA_LOW (twi. twi_sda );
773775 }
774776 }
775777 twi.twip_state = TWIP_WAIT_ACK;
@@ -787,13 +789,13 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
787789 {
788790 if ((twi.twi_data & 0xFE ) != twi.twi_addr )
789791 {
790- twi.SDA_HIGH ();
792+ twi.SDA_HIGH (twi. twi_sda );
791793 twi.twip_state = TWIP_WAIT_STOP;
792794 }
793795 else
794796 {
795- twi.SCL_LOW (); // clock stretching
796- twi.SDA_HIGH ();
797+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
798+ twi.SDA_HIGH (twi. twi_sda );
797799 twi.twip_mode = TWIPM_ADDRESSED;
798800 if (!(twi.twi_data & 0x01 ))
799801 {
@@ -810,8 +812,8 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
810812 }
811813 else
812814 {
813- twi.SCL_LOW (); // clock stretching
814- twi.SDA_HIGH ();
815+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
816+ twi.SDA_HIGH (twi. twi_sda );
815817 if (!twi.twi_ack )
816818 {
817819 twi.onTwipEvent (TW_SR_DATA_NACK);
@@ -838,11 +840,11 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
838840 twi.bitCount --;
839841 if (twi.twi_data & 0x80 )
840842 {
841- twi.SDA_HIGH ();
843+ twi.SDA_HIGH (twi. twi_sda );
842844 }
843845 else
844846 {
845- twi.SDA_LOW ();
847+ twi.SDA_LOW (twi. twi_sda );
846848 }
847849 twi.twi_data <<= 1 ;
848850
@@ -864,7 +866,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
864866 }
865867 else
866868 {
867- twi.SDA_HIGH ();
869+ twi.SDA_HIGH (twi. twi_sda );
868870 twi.twip_state = TWIP_READ_ACK;
869871 }
870872 }
@@ -888,7 +890,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
888890 }
889891 else
890892 {
891- twi.SCL_LOW (); // clock stretching
893+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
892894 if (twi.twi_ack && twi.twi_ack_rec )
893895 {
894896 twi.onTwipEvent (TW_ST_DATA_ACK);
@@ -911,8 +913,8 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
911913 unsigned int scl;
912914
913915 // Store bool return in int to reduce final code size.
914- sda = twi.SDA_READ ();
915- scl = twi.SCL_READ ();
916+ sda = twi.SDA_READ (twi. twi_sda );
917+ scl = twi.SCL_READ (twi. twi_scl );
916918
917919 int twip_state_mask = S2M (twi.twip_state );
918920 if (scl) /* !DATA */
@@ -934,7 +936,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
934936 else IFSTATE (S2M (TWIP_START) | S2M (TWIP_REP_START) | S2M (TWIP_SEND_ACK) | S2M (TWIP_WAIT_ACK) | S2M (TWIP_SLA_R) | S2M (TWIP_REC_ACK) | S2M (TWIP_READ_ACK) | S2M (TWIP_RWAIT_ACK) | S2M (TWIP_WRITE))
935937 {
936938 // START or STOP
937- twi.SDA_HIGH (); // Should not be necessary
939+ twi.SDA_HIGH (twi. twi_sda ); // Should not be necessary
938940 twi.onTwipEvent (TW_BUS_ERROR);
939941 twi.twip_mode = TWIPM_WAIT;
940942 twi.twip_state = TWIP_BUS_ERR;
@@ -944,11 +946,11 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
944946 if (sda)
945947 {
946948 // STOP
947- twi.SCL_LOW (); // clock stretching
949+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
948950 ets_timer_disarm (&twi.timer );
949951 twi.twip_state = TWIP_IDLE;
950952 twi.twip_mode = TWIPM_IDLE;
951- twi.SCL_HIGH ();
953+ twi.SCL_HIGH (twi. twi_scl );
952954 }
953955 else
954956 {
@@ -978,7 +980,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
978980 else
979981 {
980982 // during first bit in byte transfer - ok
981- twi.SCL_LOW (); // clock stretching
983+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
982984 twi.onTwipEvent (TW_SR_STOP);
983985 if (sda)
984986 {
0 commit comments