8000 Reduce the IRAM usage of I2C code by 600-1500 bytes by earlephilhower · Pull Request #6326 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Reduce the IRAM usage of I2C code by 600-1500 bytes #6326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
7126fbf
Reduce the IRAM (and heap) usage of I2C code
earlephilhower Jul 22, 2019
5ba2dd3
Make most variables ints, not uint8_ts
earlephilhower Jul 22, 2019
f55ab36
Make local flag vars int
earlephilhower Jul 22, 2019
37e10b9
Factor out !scl in onSdaChange
earlephilhower Jul 22, 2019
423a5bf
Make tiny twi_reply inline
earlephilhower Jul 22, 2019
62f9599
Inline additional twi_** helper functions
earlephilhower Jul 22, 2019
014f87f
Convert state machine to 1-hot for faster lookup
earlephilhower Jul 22, 2019
9ae9af1
Factor out twi_status setting
earlephilhower Jul 24, 2019
b975996
Use a struct to hold globals for TWI
earlephilhower Jul 24, 2019
52bf8ac
Use enums for states, move one more var to twi struct
earlephilhower Jul 25, 2019
31e54a0
Save 4 heap bytes by reprdering struct
earlephilhower Jul 25, 2019
fd8b6c5
Convert to C++ class, clean up code
earlephilhower Jul 26, 2019
0cdca29
Run astyle core.conf, clean up space/tab/etc.
earlephilhower Jul 26, 2019
eb7b2b9
Merge branch 'master' into reducei2ciram
earlephilhower Aug 8, 2019
3552bfc
Merge branch 'master' into reducei2ciram
earlephilhower Aug 19, 2019
51e1e34
Merge branch 'master' into reducei2ciram
earlephilhower Sep 15, 2019
1c14e9e
Merge branch 'master' into reducei2ciram
d-a-v Oct 3, 2019
f04d65c
Merge branch 'master' into reducei2ciram
earlephilhower Oct 4, 2019
f67ddf5
Add enum use comment, rename twi::delay, fix SDA/SCL_READ bool usage
earlephilhower Oct 4, 2019
dd6a854
Replace clock stretch repeated code w/inline loop
earlephilhower Oct 4, 2019
a23864a
Remove slave code when not using slave mode
earlephilhower Oct 4, 2019
991380d
Re-allmanize the i2c core and Wire library
earlephilhower Oct 14, 2019
7410bc5
Merge branch 'master' into reducei2ciram
earlephilhower Oct 14, 2019
88209b5
Add i2c and lib to restyle.sh
earlephilhower Oct 14, 2019
1012d40
Add comment about bool-as-int in slave eventhndlr
earlephilhower Oct 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
earlephilhower committed Oct 4, 2019
commit a23864a713fe17fff547497e1b32c6cb4ec7569f
27 changes: 21 additions & 6 deletions cores/esp8266/core_esp8266_si2c.cpp
8000
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ class Twi
static void eventTask(ETSEvent *e);
static void ICACHE_RAM_ATTR onTimer(void *unused);

// Allow not linking in the slave code if there is no call to setAddress
bool _slaveEnabled = false;

// Internal use functions
void ICACHE_RAM_ATTR busywait(unsigned char v);
bool write_start(void);
Expand Down Expand Up @@ -124,6 +127,7 @@ class Twi
}
}


public:
void setClock(unsigned int freq);
void setClockStretchLimit(uint32_t limit);
Expand All @@ -138,6 +142,7 @@ class Twi
inline void ICACHE_RAM_ATTR reply(uint8_t ack);
inline void ICACHE_RAM_ATTR stop(void);
inline void ICACHE_RAM_ATTR releaseBus(void);
void enableSlave();
};

static Twi twi;
Expand Down Expand Up @@ -222,6 +227,8 @@ void Twi::setClockStretchLimit(uint32_t limit)
twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER;
}



void Twi::init(unsigned char sda, unsigned char scl)
{
// set timer function
Expand All @@ -236,12 +243,6 @@ void Twi::init(unsigned char sda, unsigned char scl)
pinMode(twi_scl, INPUT_PULLUP);
twi_setClock(preferred_si2c_clock);
twi_setClockStretchLimit(230); // default value is 230 uS

if (twi_addr != 0)
{
attachInterrupt(scl, onSclChange, CHANGE);
attachInterrupt(sda, onSdaChange, CHANGE);
}
}

void Twi::setAddress(uint8_t address)
Expand All @@ -250,6 +251,15 @@ void Twi::setAddress(uint8_t address)
twi_addr = address << 1;
}

void Twi::enableSlave()
{
if (!_slaveEnabled) {
attachInterrupt(twi_scl, onSclChange, CHANGE);
attachInterrupt(twi_sda, onSdaChange, CHANGE);
_slaveEnabled = true;
}
}

void ICACHE_RAM_ATTR Twi::busywait(unsigned char v)
{
unsigned int i;
Expand Down Expand Up @@ -1044,4 +1054,9 @@ extern "C" {
twi.releaseBus();
}

void twi_enableSlaveMode(void)
{
twi.enableSlave();
}

};
1 change: 1 addition & 0 deletions cores/esp8266/twi.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ void twi_reply(uint8_t);
//void twi_stop(void);
void twi_releaseBus(void);

void twi_enableSlaveMode(void);

#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,12 @@ void TwoWire::onReceive( void (*function)(int) ) {

void TwoWire::onReceive( void (*function)(size_t) ) {
user_onReceive = function;
twi_enableSlaveMode();
}

void TwoWire::onRequest( void (*function)(void) ){
user_onRequest = function;
twi_enableSlaveMode();
}

// Preinstantiate Objects //////////////////////////////////////////////////////
Expand Down
0