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
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
Use enums for states, move one more var to twi struct
Make the TWI states enums and not #defines, in the hope that it will
allow GCC to more easily flag problems and general good code
organization.

401000cc l     F .text1	00000014 twi_delay
401000e8  w    F .text1	00000032 twi_releaseBus
40100128 g     F .text1	00000217 twi_onTwipEvent
4010034c l     F .text1	00000149 onSdaChange
4010049c l     F .text1	00000257 onSclChange
401006f4 l     F .text1	00000028 onTimer

Looks like another 16 bytes IRAM saved from the prior push.

Sketch uses 267079 bytes (25%) of program storage space. Maximum is 1044464 bytes.
Global variables use 27696 bytes (33%) of dynamic memory, leaving 54224 bytes for local variables. Maximum is 81920 bytes.
  • Loading branch information
earlephilhower committed Jul 25, 2019
commit 52bf8ac21e8c490018f2f258e155a96ae9e0e58a
55 changes: 18 additions & 37 deletions cores/esp8266/core_esp8266_si2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,34 @@
#include "pins_arduino.h"
#include "wiring_private.h"

extern "C" {

unsigned int preferred_si2c_clock = 100000;

extern "C" {
#include "twi_util.h"

#include "ets_sys.h"


#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"

10000
// modes (private)
typedef enum { TWIPM_UNKNOWN = 0, TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twipModeType;

// states (private)
typedef enum { TWIP_UNKNOWN = 0, TWIP_IDLE, TWIP_START, TWIP_SEND_ACK, TWIP_WAIT_ACK, TWIP_WAIT_STOP, TWIP_SLA_W, TWIP_SLA_R, TWIP_REP_START, TWIP_READ, TWIP_STOP, TWIP_REC_ACK, TWIP_READ_ACK, TWIP_RWAIT_ACK, TWIP_WRITE, TWIP_BUS_ERR } twipStateType;
typedef enum { TWI_READY=0, TWI_MRX, TWI_MTX, TWI_SRX, TWI_STX } twiStateType;

static struct twi {
unsigned int preferred_si2c_clock; // = 100000;
unsigned char twi_dcount;// = 18;
unsigned char twi_sda, twi_scl;
uint32_t twi_clockStretchLimit;
unsigned char twi_addr;// = 0;

// modes (private)
#define TWIPM_UNKNOWN 0
#define TWIPM_IDLE 1
#define TWIPM_ADDRESSED 2
#define TWIPM_WAIT 3

// states (private)
#define TWIP_UNKNOWN 0
#define TWIP_IDLE 1
#define TWIP_START 2
#define TWIP_SEND_ACK 3
#define TWIP_WAIT_ACK 4
#define TWIP_WAIT_STOP 5
#define TWIP_SLA_W 6
#define TWIP_SLA_R 7
#define TWIP_REP_START 8
#define TWIP_READ 9
#define TWIP_STOP 10
#define TWIP_REC_ACK 11
#define TWIP_READ_ACK 12
#define TWIP_RWAIT_ACK 13
#define TWIP_WRITE 14
#define TWIP_BUS_ERR 15

volatile int twip_mode;// = TWIPM_IDLE;
volatile int twip_state;// = TWIP_IDLE;
volatile twipModeType twip_mode;// = TWIPM_IDLE;
volatile twipStateType twip_state;// = TWIP_IDLE;
volatile int twip_status;// = TW_NO_INFO;
volatile int bitCount;// = 0;

Expand All @@ -74,12 +60,7 @@ static struct twi {
volatile int twi_ack_rec;// = 0;
volatile int twi_timeout_ms;// = 10;

#define TWI_READY 0
#define TWI_MRX 1
#define TWI_MTX 2
#define TWI_SRX 3
#define TWI_STX 4
volatile int twi_state;// = TWI_READY;
volatile twiStateType twi_state;// = TWI_READY;
volatile uint8_t twi_error;// = 0xFF;

uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
Expand All @@ -101,8 +82,8 @@ static struct twi {

ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE];
ETSTimer timer;
} twi = { 18, 0, 0, 0, 0, TWIPM_IDLE, TWIP_IDLE, TW_NO_INFO, 0, 0, 0, 0, 10, TWI_READY, 0xff, {0}, 0, 0, {0}, 0, NULL, NULL, {{0}}, {0,0,0,0}};
// = { twi_dcount: 18, twi_addr: 0, twip_mode: TWIPM_IDLE, twip_state: TWIP_IDLE, twip_status: TW_NO_INFO, bitCount: 0, twi_data: 0x00, twi_ack: 0, twi_ack_rec: 0, twi_timeout_ms: 10, twi_state: TWI_READY, twi_error: 0xFF };
} twi = { 100000, 18, 0, 0, 0, 0, TWIPM_IDLE, TWIP_IDLE, TW_NO_INFO, 0, 0, 0, 0, 10, TWI_READY, 0xff, {0}, 0, 0, {0}, 0, NULL, NULL, {{0}}, {0,0,0,0}};
// = { preferred_si2c_clock: 100000, twi_dcount: 18, twi_addr: 0, twip_mode: TWIPM_IDLE, twip_state: TWIP_IDLE, twip_status: TW_NO_INFO, bitCount: 0, twi_data: 0x00, twi_ack: 0, twi_ack_rec: 0, twi_timeout_ms: 10, twi_state: TWI_READY, twi_error: 0xFF };
#pragma GCC diagnostic pop

static void onSclChange(void);
Expand All @@ -128,7 +109,7 @@ static void onTimer(void *unused);
#endif

void twi_setClock(unsigned int freq){
preferred_si2c_clock = freq;
twi.preferred_si2c_clock = freq;
#if F_CPU == FCPU80
if(freq <= 50000) twi.twi_dcount = 38;//about 50KHz
else if(freq <= 100000) twi.twi_dcount = 19;//about 100KHz
Expand Down Expand Up @@ -164,7 +145,7 @@ void twi_init(unsigned char sda, unsigned char scl)
twi.twi_scl = scl;
pinMode(twi.twi_sda, INPUT_PULLUP);
pinMode(twi.twi_scl, INPUT_PULLUP);
twi_setClock(preferred_si2c_clock);
twi_setClock(twi.preferred_si2c_clock);
twi_setClockStretchLimit(230); // default value is 230 uS

if (twi.twi_addr != 0)
Expand Down
0