Skip to content

Commit 0a9e394

Browse files
committed
pulled the inline helpers out of the TWI class [issue 6875]
1 parent 4f0b0a7 commit 0a9e394

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

cores/esp8266/core_esp8266_si2c.cpp

+45-45
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,32 @@ 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+
}
59+
3460

3561
// Implement as a class to reduce code size by allowing access to many global variables with a single base pointer
3662
class Twi
@@ -95,32 +121,6 @@ class Twi
95121
unsigned char read_byte(bool nack);
96122
void ICACHE_RAM_ATTR onTwipEvent(uint8_t status);
97123

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-
124124
// Handle the case where a slave needs to stretch the clock with a time-limited busy wait
125125
inline void WAIT_CLOCK_STRETCH()
126126
{
@@ -716,8 +716,8 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
716716

717717
// Store bool return in int to reduce final code size.
718718

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);
721721

722722
twi.twip_status = 0xF8; // reset TWI status
723723

@@ -760,7 +760,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
760760
}
761761
else
762762
{
763-
twi.SDA_LOW(twi.twi_sda);
763+
SDA_LOW(twi.twi_sda);
764764
}
765765
}
766766
else
@@ -771,7 +771,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
771771
}
772772
else
773773
{
774-
twi.SDA_LOW(twi.twi_sda);
774+
SDA_LOW(twi.twi_sda);
775775
}
776776
}
777777
twi.twip_state = TWIP_WAIT_ACK;
@@ -789,13 +789,13 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
789789
{
790790
if ((twi.twi_data & 0xFE) != twi.twi_addr)
791791
{
792-
twi.SDA_HIGH(twi.twi_sda);
792+
SDA_HIGH(twi.twi_sda);
793793
twi.twip_state = TWIP_WAIT_STOP;
794794
}
795795
else
796796
{
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);
799799
twi.twip_mode = TWIPM_ADDRESSED;
800800
if (!(twi.twi_data & 0x01))
801801
{
@@ -812,8 +812,8 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
812812
}
813813
else
814814
{
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);
817817
if (!twi.twi_ack)
818818
{
819819
twi.onTwipEvent(TW_SR_DATA_NACK);
@@ -840,11 +840,11 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
840840
twi.bitCount--;
841841
if (twi.twi_data & 0x80)
842842
{
843-
twi.SDA_HIGH(twi.twi_sda);
843+
SDA_HIGH(twi.twi_sda);
844844
}
845845
else
846846
{
847-
twi.SDA_LOW(twi.twi_sda);
847+
SDA_LOW(twi.twi_sda);
848848
}
849849
twi.twi_data <<= 1;
850850

@@ -866,7 +866,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
866866
}
867867
else
868868
{
869-
twi.SDA_HIGH(twi.twi_sda);
869+
SDA_HIGH(twi.twi_sda);
870870
twi.twip_state = TWIP_READ_ACK;
871871
}
872872
}
@@ -890,7 +890,7 @@ void ICACHE_RAM_ATTR Twi::onSclChange(void)
890890
}
891891
else
892892
{
893-
twi.SCL_LOW(twi.twi_scl); // clock stretching
893+
SCL_LOW(twi.twi_scl); // clock stretching
894894
if (twi.twi_ack && twi.twi_ack_rec)
895895
{
896896
twi.onTwipEvent(TW_ST_DATA_ACK);
@@ -913,8 +913,8 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
913913
unsigned int scl;
914914

915915
// 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);
918918

919919
int twip_state_mask = S2M(twi.twip_state);
920920
if (scl) /* !DATA */
@@ -936,7 +936,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
936936
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))
937937
{
938938
// START or STOP
939-
twi.SDA_HIGH(twi.twi_sda); // Should not be necessary
939+
SDA_HIGH(twi.twi_sda); // Should not be necessary
940940
twi.onTwipEvent(TW_BUS_ERROR);
941941
twi.twip_mode = TWIPM_WAIT;
942942
twi.twip_state = TWIP_BUS_ERR;
@@ -946,11 +946,11 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
946946
if (sda)
947947
{
948948
// STOP
949-
twi.SCL_LOW(twi.twi_scl); // clock stretching
949+
SCL_LOW(twi.twi_scl); // clock stretching
950950
ets_timer_disarm(&twi.timer);
951951
twi.twip_state = TWIP_IDLE;
952952
twi.twip_mode = TWIPM_IDLE;
953-
twi.SCL_HIGH(twi.twi_scl);
953+
SCL_HIGH(twi.twi_scl);
954954
}
955955
else
956956
{
@@ -980,7 +980,7 @@ void ICACHE_RAM_ATTR Twi::onSdaChange(void)
980980
else
981981
{
982982
// during first bit in byte transfer - ok
983-
twi.SCL_LOW(twi.twi_scl); // clock stretching
983+
SCL_LOW(twi.twi_scl); // clock stretching
984984
twi.onTwipEvent(TW_SR_STOP);
985985
if (sda)
986986
{

0 commit comments

Comments
 (0)