Skip to content

Commit a23864a

Browse files
Remove slave code when not using slave mode
Add a new twi_setSlaveMode call which actually attached the interrupts to the slave pin change code onSdaChenge/onSclChange. Don't attach interrupts in the main twi_begin. Because slave mode is only useful should a onoReceive or onRequest callback, call twi_setSlaveMode and attach interrupts on the Wire setters. This allows GCC to not link in slave code unless slave mode is used, saving over 1,000 bytes of IRAM in the common, master-only case.
1 parent dd6a854 commit a23864a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

cores/esp8266/core_esp8266_si2c.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class Twi
8181
static void eventTask(ETSEvent *e);
8282
static void ICACHE_RAM_ATTR onTimer(void *unused);
8383

84+
// Allow not linking in the slave code if there is no call to setAddress
85+
bool _slaveEnabled = false;
86+
8487
// Internal use functions
8588
void ICACHE_RAM_ATTR busywait(unsigned char v);
8689
bool write_start(void);
@@ -124,6 +127,7 @@ class Twi
124127
}
125128
}
126129

130+
127131
public:
128132
void setClock(unsigned int freq);
129133
void setClockStretchLimit(uint32_t limit);
@@ -138,6 +142,7 @@ class Twi
138142
inline void ICACHE_RAM_ATTR reply(uint8_t ack);
139143
inline void ICACHE_RAM_ATTR stop(void);
140144
inline void ICACHE_RAM_ATTR releaseBus(void);
145+
void enableSlave();
141146
};
142147

143148
static Twi twi;
@@ -222,6 +227,8 @@ void Twi::setClockStretchLimit(uint32_t limit)
222227
twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER;
223228
}
224229

230+
231+
225232
void Twi::init(unsigned char sda, unsigned char scl)
226233
{
227234
// set timer function
@@ -236,12 +243,6 @@ void Twi::init(unsigned char sda, unsigned char scl)
236243
pinMode(twi_scl, INPUT_PULLUP);
237244
twi_setClock(preferred_si2c_clock);
238245
twi_setClockStretchLimit(230); // default value is 230 uS
239-
240-
if (twi_addr != 0)
241-
{
242-
attachInterrupt(scl, onSclChange, CHANGE);
243-
attachInterrupt(sda, onSdaChange, CHANGE);
244-
}
245246
}
246247

247248
void Twi::setAddress(uint8_t address)
@@ -250,6 +251,15 @@ void Twi::setAddress(uint8_t address)
250251
twi_addr = address << 1;
251252
}
252253

254+
void Twi::enableSlave()
255+
{
256+
if (!_slaveEnabled) {
257+
attachInterrupt(twi_scl, onSclChange, CHANGE);
258+
attachInterrupt(twi_sda, onSdaChange, CHANGE);
259+
_slaveEnabled = true;
260+
}
261+
}
262+
253263
void ICACHE_RAM_ATTR Twi::busywait(unsigned char v)
254264
{
255265
unsigned int i;
@@ -1044,4 +1054,9 @@ extern "C" {
10441054
twi.releaseBus();
10451055
}
10461056

1057+
void twi_enableSlaveMode(void)
1058+
{
1059+
twi.enableSlave();
1060+
}
1061+
10471062
};

cores/esp8266/twi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void twi_reply(uint8_t);
5454
//void twi_stop(void);
5555
void twi_releaseBus(void);
5656

57+
void twi_enableSlaveMode(void);
5758

5859
#ifdef __cplusplus
5960
}

libraries/Wire/Wire.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,12 @@ void TwoWire::onReceive( void (*function)(int) ) {
274274

275275
void TwoWire::onReceive( void (*function)(size_t) ) {
276276
user_onReceive = function;
277+
twi_enableSlaveMode();
277278
}
278279

279280
void TwoWire::onRequest( void (*function)(void) ){
280281
user_onRequest = function;
282+
twi_enableSlaveMode();
281283
}
282284

283285
// Preinstantiate Objects //////////////////////////////////////////////////////

0 commit comments

Comments
 (0)