@@ -31,6 +31,32 @@ 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
+ }
59
+
34
60
35
61
// Implement as a class to reduce code size by allowing access to many global variables with a single base pointer
36
62
class Twi
@@ -95,32 +121,6 @@ class Twi
95
121
unsigned char read_byte (bool nack);
96
122
void ICACHE_RAM_ATTR onTwipEvent (uint8_t status);
97
123
98
- // Inline helpers
99
- static inline void SDA_LOW (const int twi_sda) __attribute__((always_inline))
100
- {
101
- GPES = (1 << twi_sda);
102
- }
103
- static inline void SDA_HIGH (const int twi_sda) __attribute__((always_inline))
104
- {
105
- GPEC = (1 << twi_sda);
106
- }
107
- static inline bool SDA_READ (const int twi_sda) __attribute__((always_inline))
108
- {
109
- return (GPI & (1 << twi_sda)) != 0 ;
110
- }
111
- static inline void SCL_LOW (const int twi_scl) __attribute__((always_inline))
112
- {
113
- GPES = (1 << twi_scl);
114
- }
115
- static inline void SCL_HIGH (const int twi_scl) __attribute__((always_inline))
116
- {
117
- GPEC = (1 << twi_scl);
118
- }
119
- static inline bool SCL_READ (const int twi_scl) __attribute__((always_inline))
120
- {
121
- return (GPI & (1 << twi_scl)) != 0 ;
122
- }
123
-
124
124
// Handle the case where a slave needs to stretch the clock with a time-limited busy wait
125
125
inline void WAIT_CLOCK_STRETCH ()
126
126
{
@@ -716,8 +716,8 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
716
716
717
717
// Store bool return in int to reduce final code size.
718
718
719
- sda = twi. SDA_READ (twi.twi_sda );
720
- scl = twi. SCL_READ (twi.twi_scl );
719
+ sda = SDA_READ (twi.twi_sda );
720
+ scl = SCL_READ (twi.twi_scl );
721
721
722
722
twi.twip_status = 0xF8 ; // reset TWI status
723
723
@@ -760,7 +760,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
760
760
}
761
761
else
762
762
{
763
- twi. SDA_LOW (twi.twi_sda );
763
+ SDA_LOW (twi.twi_sda );
764
764
}
765
765
}
766
766
else
@@ -771,7 +771,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
771
771
}
772
772
else
773
773
{
774
- twi. SDA_LOW (twi.twi_sda );
774
+ SDA_LOW (twi.twi_sda );
775
775
}
776
776
}
777
777
twi.twip_state = TWIP_WAIT_ACK;
@@ -789,13 +789,13 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
789
789
{
790
790
if ((twi.twi_data & 0xFE ) != twi.twi_addr )
791
791
{
792
- twi. SDA_HIGH (twi.twi_sda );
792
+ SDA_HIGH (twi.twi_sda );
793
793
twi.twip_state = TWIP_WAIT_STOP;
794
794
}
795
795
else
796
796
{
797
- twi. SCL_LOW (twi.twi_scl ); // clock stretching
798
- twi. SDA_HIGH (twi.twi_sda );
797
+ SCL_LOW (twi.twi_scl ); // clock stretching
798
+ SDA_HIGH (twi.twi_sda );
799
799
twi.twip_mode = TWIPM_ADDRESSED;
800
800
if (!(twi.twi_data & 0x01 ))
801
801
{
@@ -812,8 +812,8 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
812
812
}
813
813
else
814
814
{
815
- twi. SCL_LOW (twi.twi_scl ); // clock stretching
816
- twi. SDA_HIGH (twi.twi_sda );
815
+ SCL_LOW (twi.twi_scl ); // clock stretching
816
+ SDA_HIGH (twi.twi_sda );
817
817
if (!twi.twi_ack )
818
818
{
819
819
twi.onTwipEvent (TW_SR_DATA_NACK);
@@ -840,11 +840,11 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
840
840
twi.bitCount --;
841
841
if (twi.twi_data & 0x80 )
842
842
{
843
- twi. SDA_HIGH (twi.twi_sda );
843
+ SDA_HIGH (twi.twi_sda );
844
844
}
845
845
else
846
846
{
847
- twi. SDA_LOW (twi.twi_sda );
847
+ SDA_LOW (twi.twi_sda );
848
848
}
849
849
twi.twi_data <<= 1 ;
850
850
@@ -866,7 +866,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
866
866
}
867
867
else
868
868
{
869
- twi. SDA_HIGH (twi.twi_sda );
869
+ SDA_HIGH (twi.twi_sda );
870
870
twi.twip_state = TWIP_READ_ACK;
871
871
}
872
872
}
@@ -890,7 +890,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
890
890
}
891
891
else
892
892
{
893
- twi. SCL_LOW (twi.twi_scl ); // clock stretching
893
+ SCL_LOW (twi.twi_scl ); // clock stretching
894
894
if (twi.twi_ack && twi.twi_ack_rec )
895
895
{
896
896
twi.onTwipEvent (TW_ST_DATA_ACK);
@@ -913,8 +913,8 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
913
913
unsigned int scl;
914
914
915
915
// Store bool return in int to reduce final code size.
916
- sda = twi. SDA_READ (twi.twi_sda );
917
- scl = twi. SCL_READ (twi.twi_scl );
916
+ sda = SDA_READ (twi.twi_sda );
917
+ scl = SCL_READ (twi.twi_scl );
918
918
919
919
int twip_state_mask = S2M (twi.twip_state );
920
920
if (scl) /* !DATA */
@@ -936,7 +936,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
936
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))
937
937
{
938
938
// START or STOP
939
- twi. SDA_HIGH (twi.twi_sda ); // Should not be necessary
939
+ SDA_HIGH (twi.twi_sda ); // Should not be necessary
940
940
twi.onTwipEvent (TW_BUS_ERROR);
941
941
twi.twip_mode = TWIPM_WAIT;
942
942
twi.twip_state = TWIP_BUS_ERR;
@@ -946,11 +946,11 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
946
946
if (sda)
947
947
{
948
948
// STOP
949
- twi. SCL_LOW (twi.twi_scl ); // clock stretching
949
+ SCL_LOW (twi.twi_scl ); // clock stretching
950
950
ets_timer_disarm (&twi.timer );
951
951
twi.twip_state = TWIP_IDLE;
952
952
twi.twip_mode = TWIPM_IDLE;
953
- twi. SCL_HIGH (twi.twi_scl );
953
+ SCL_HIGH (twi.twi_scl );
954
954
}
955
955
else
956
956
{
@@ -980,7 +980,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
980
980
else
981
981
{
982
982
// during first bit in byte transfer - ok
983
- twi. SCL_LOW (twi.twi_scl ); // clock stretching
983
+ SCL_LOW (twi.twi_scl ); // clock stretching
984
984
twi.onTwipEvent (TW_SR_STOP);
985
985
if (sda)
986
986
{
0 commit comments