@@ -96,36 +96,37 @@ class Twi
96
96
void ICACHE_RAM_ATTR onTwipEvent (uint8_t status);
97
97
98
98
// Inline helpers
99
- inline void SDA_LOW ()
99
+ static inline void SDA_LOW (const int twi_sda) __attribute__((always_inline) )
100
100
{
101
101
GPES = (1 << twi_sda);
102
102
}
103
- inline void SDA_HIGH ()
103
+ static inline void SDA_HIGH (const int twi_sda) __attribute__((always_inline) )
104
104
{
105
105
GPEC = (1 << twi_sda);
106
106
}
107
- inline bool SDA_READ ()
108
- {
107
+ static inline bool SDA_READ (const int twi_sda) __attribute__((always_inline) )
108
+ {
109
109
return (GPI & (1 << twi_sda)) != 0 ;
110
110
}
111
- inline void SCL_LOW ()
111
+ static inline void SCL_LOW (const int twi_scl) __attribute__((always_inline) )
112
112
{
113
113
GPES = (1 << twi_scl);
114
114
}
115
- inline void SCL_HIGH ()
115
+ static inline void SCL_HIGH (const int twi_scl) __attribute__((always_inline) )
116
116
{
117
117
GPEC = (1 << twi_scl);
118
118
}
119
- inline bool SCL_READ ()
120
- {
119
+ static inline bool SCL_READ (const int twi_scl) __attribute__((always_inline) )
120
+ {
121
121
return (GPI & (1 << twi_scl)) != 0 ;
122
122
}
123
+
123
124
// Handle the case where a slave needs to stretch the clock with a time-limited busy wait
124
125
inline void WAIT_CLOCK_STRETCH ()
125
126
{
126
127
esp8266::polledTimeout::oneShotFastUs timeout (twi_clockStretchLimit);
127
128
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
129
130
{
130
131
if (yieldTimeout) // inner loop yields every 5ms
131
132
yield ();
@@ -277,57 +278,57 @@ void ICACHE_RAM_ATTR Twi::busywait(unsigned char v)
277
278
278
279
bool Twi::write_start (void )
279
280
{
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 ))
283
284
{
284
285
return false ;
285
286
}
286
287
busywait (twi_dcount);
287
- SDA_LOW ();
288
+ SDA_LOW (twi_sda );
288
289
busywait (twi_dcount);
289
290
return true ;
290
291
}
291
292
292
293
bool Twi::write_stop (void )
293
294
{
294
- SCL_LOW ();
295
- SDA_LOW ();
295
+ SCL_LOW (twi_scl );
296
+ SDA_LOW (twi_sda );
296
297
busywait (twi_dcount);
297
- SCL_HIGH ();
298
+ SCL_HIGH (twi_scl );
298
299
WAIT_CLOCK_STRETCH ();
299
300
busywait (twi_dcount);
300
- SDA_HIGH ();
301
+ SDA_HIGH (twi_sda );
301
302
busywait (twi_dcount);
302
303
return true ;
303
304
}
304
305
305
306
bool Twi::write_bit (bool bit)
306
307
{
307
- SCL_LOW ();
308
+ SCL_LOW (twi_scl );
308
309
if (bit)
309
310
{
310
- SDA_HIGH ();
311
+ SDA_HIGH (twi_sda );
311
312
}
312
313
else
313
314
{
314
- SDA_LOW ();
315
+ SDA_LOW (twi_sda );
315
316
}
316
317
busywait (twi_dcount + 1 );
317
- SCL_HIGH ();
318
+ SCL_HIGH (twi_scl );
318
319
WAIT_CLOCK_STRETCH ();
319
320
busywait (twi_dcount);
320
321
return true ;
321
322
}
322
323
323
324
bool Twi::read_bit (void )
324
325
{
325
- SCL_LOW ();
326
- SDA_HIGH ();
326
+ SCL_LOW (twi_scl );
327
+ SDA_HIGH (twi_sda );
327
328
busywait (twi_dcount + 2 );
328
- SCL_HIGH ();
329
+ SCL_HIGH (twi_scl );
329
330
WAIT_CLOCK_STRETCH ();
330
- bool bit = SDA_READ ();
331
+ bool bit = SDA_READ (twi_sda );
331
332
busywait (twi_dcount);
332
333
return bit;
333
334
}
@@ -392,7 +393,7 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned
392
393
// busywait(twi_dcount);
393
394
}
394
395
i = 0 ;
395
- while (!SDA_READ () && (i++) < 10 )
396
+ while (!SDA_READ (twi_sda ) && (i++) < 10 )
396
397
{
397
398
twi_scl_valley ();
398
399
busywait (twi_dcount);
@@ -431,7 +432,7 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
431
432
// busywait(twi_dcount);
432
433
}
433
434
i = 0 ;
434
- while (!SDA_READ () && (i++) < 10 )
435
+ while (!SDA_READ (twi_sda ) && (i++) < 10 )
435
436
{
436
437
twi_scl_valley ();
437
438
busywait (twi_dcount);
@@ -442,21 +443,21 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned
442
443
uint8_t Twi::status ()
443
444
{
444
445
WAIT_CLOCK_STRETCH (); // wait for a slow slave to finish
445
- if (!SCL_READ ())
446
+ if (!SCL_READ (twi_scl ))
446
447
{
447
448
return I2C_SCL_HELD_LOW; // SCL held low by another device, no procedure available to recover
448
449
}
449
450
450
451
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
452
453
{
453
454
read_bit ();
454
- if (!SCL_READ ())
455
+ if (!SCL_READ (twi_scl ))
455
456
{
456
457
return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time
457
458
}
458
459
}
459
- if (!SDA_READ ())
460
+ if (!SDA_READ (twi_sda ))
460
461
{
461
462
return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits.
462
463
}
@@ -506,13 +507,13 @@ inline void ICACHE_RAM_ATTR Twi::reply(uint8_t ack)
506
507
if (ack)
507
508
{
508
509
// TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
509
- SCL_HIGH (); // _BV(TWINT)
510
+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
510
511
twi_ack = 1 ; // _BV(TWEA)
511
512
}
512
513
else
513
514
{
514
515
// TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
515
- SCL_HIGH (); // _BV(TWINT)
516
+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
516
517
twi_ack = 0 ; // ~_BV(TWEA)
517
518
}
518
519
}
@@ -521,10 +522,10 @@ inline void ICACHE_RAM_ATTR Twi::stop(void)
521
522
{
522
523
// send stop condition
523
524
// TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
524
- SCL_HIGH (); // _BV(TWINT)
525
+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
525
526
twi_ack = 1 ; // _BV(TWEA)
526
527
busywait (5 ); // Maybe this should be here
527
- SDA_HIGH (); // _BV(TWSTO)
528
+ SDA_HIGH (twi. twi_sda ); // _BV(TWSTO)
528
529
// update twi state
529
530
twi_state = TWI_READY;
530
531
}
@@ -533,9 +534,9 @@ inline void ICACHE_RAM_ATTR Twi::releaseBus(void)
533
534
{
534
535
// release bus
535
536
// TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
536
- SCL_HIGH (); // _BV(TWINT)
537
+ SCL_HIGH (twi. twi_scl ); // _BV(TWINT)
537
538
twi_ack = 1 ; // _BV(TWEA)
538
- SDA_HIGH ();
539
+ SDA_HIGH (twi. twi_sda );
539
540
540
541
// update twi state
541
542
twi_state = TWI_READY;
@@ -616,11 +617,11 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status)
616
617
bitCount--;
617
618
if (twi_data & 0x80 )
618
619
{
619
- SDA_HIGH ();
620
+ SDA_HIGH (twi. twi_sda );
620
621
}
621
622
else
622
623
{
623
- SDA_LOW ();
624
+ SDA_LOW (twi. twi_sda );
624
625
}
625
626
twi_data <<= 1 ;
626
627
@@ -652,9 +653,9 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status)
652
653
653
654
void Twi::twi_scl_valley (void )
654
655
{
655
- SCL_LOW ();
656
+ SCL_LOW (twi_scl );
656
657
busywait (twi_dcount);
657
- SCL_HIGH ();
658
+ SCL_HIGH (twi_scl );
658
659
WAIT_CLOCK_STRETCH ();
659
660
}
660
661
@@ -714,8 +715,9 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
714
715
unsigned int scl;
715
716
716
717
// 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 );
719
721
720
722
twi.twip_status = 0xF8 ; // reset TWI status
721
723
@@ -758,7 +760,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
758
760
}
759
761
else
760
762
{
761
- twi.SDA_LOW ();
763
+ twi.SDA_LOW (twi. twi_sda );
762
764
}
763
765
}
764
766
else
@@ -769,7 +771,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
769
771
}
770
772
else
771
773
{
772
- twi.SDA_LOW ();
774
+ twi.SDA_LOW (twi. twi_sda );
773
775
}
774
776
}
775
777
twi.twip_state = TWIP_WAIT_ACK;
@@ -787,13 +789,13 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
787
789
{
788
790
if ((twi.twi_data & 0xFE ) != twi.twi_addr )
789
791
{
790
- twi.SDA_HIGH ();
792
+ twi.SDA_HIGH (twi. twi_sda );
791
793
twi.twip_state = TWIP_WAIT_STOP;
792
794
}
793
795
else
794
796
{
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 );
797
799
twi.twip_mode = TWIPM_ADDRESSED;
798
800
if (!(twi.twi_data & 0x01 ))
799
801
{
@@ -810,8 +812,8 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
810
812
}
811
813
else
812
814
{
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 );
815
817
if (!twi.twi_ack )
816
818
{
817
819
twi.onTwipEvent (TW_SR_DATA_NACK);
@@ -838,11 +840,11 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
838
840
twi.bitCount --;
839
841
if (twi.twi_data & 0x80 )
840
842
{
841
- twi.SDA_HIGH ();
843
+ twi.SDA_HIGH (twi. twi_sda );
842
844
}
843
845
else
844
846
{
845
- twi.SDA_LOW ();
847
+ twi.SDA_LOW (twi. twi_sda );
846
848
}
847
849
twi.twi_data <<= 1 ;
848
850
@@ -864,7 +866,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
864
866
}
865
867
else
866
868
{
867
- twi.SDA_HIGH ();
869
+ twi.SDA_HIGH (twi. twi_sda );
868
870
twi.twip_state = TWIP_READ_ACK;
869
871
}
870
872
}
@@ -888,7 +890,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
888
890
}
889
891
else
890
892
{
891
- twi.SCL_LOW (); // clock stretching
893
+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
892
894
if (twi.twi_ack && twi.twi_ack_rec )
893
895
{
894
896
twi.onTwipEvent (TW_ST_DATA_ACK);
@@ -911,8 +913,8 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
911
913
unsigned int scl;
912
914
913
915
// 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 );
916
918
917
919
int twip_state_mask = S2M (twi.twip_state );
918
920
if (scl) /* !DATA */
@@ -934,7 +936,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
934
936
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))
935
937
{
936
938
// START or STOP
937
- twi.SDA_HIGH (); // Should not be necessary
939
+ twi.SDA_HIGH (twi. twi_sda ); // Should not be necessary
938
940
twi.onTwipEvent (TW_BUS_ERROR);
939
941
twi.twip_mode = TWIPM_WAIT;
940
942
twi.twip_state = TWIP_BUS_ERR;
@@ -944,11 +946,11 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
944
946
if (sda)
945
947
{
946
948
// STOP
947
- twi.SCL_LOW (); // clock stretching
949
+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
948
950
ets_timer_disarm (&twi.timer );
949
951
twi.twip_state = TWIP_IDLE;
950
952
twi.twip_mode = TWIPM_IDLE;
951
- twi.SCL_HIGH ();
953
+ twi.SCL_HIGH (twi. twi_scl );
952
954
}
953
955
else
954
956
{
@@ -978,7 +980,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
978
980
else
979
981
{
980
982
// during first bit in byte transfer - ok
981
- twi.SCL_LOW (); // clock stretching
983
+ twi.SCL_LOW (twi. twi_scl ); // clock stretching
982
984
twi.onTwipEvent (TW_SR_STOP);
983
985
if (sda)
984
986
{
0 commit comments