From 7126fbf6c694af7bcae1937f75d1838c7880405a Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jul 2019 17:04:46 -0700 Subject: [PATCH 01/20] Reduce the IRAM (and heap) usage of I2C code The I2C code takes a large chunk of IRAM space. Attempt to reduce the size of the routines without impacting functionality. First, remove the `static` classifier on the sda/scl variables in the event handlers. The first instructions in the routines overwrite the last value stored in them, anyway, and their addresses are never taken. --- cores/esp8266/core_esp8266_si2c.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 6f6cd58971..0b5b6f6ed5 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -524,8 +524,8 @@ static void eventTask(ETSEvent *e) void ICACHE_RAM_ATTR onSclChange(void) { - static uint8_t sda; - static uint8_t scl; + uint8_t sda; + uint8_t scl; sda = SDA_READ(); scl = SCL_READ(); @@ -682,8 +682,8 @@ void ICACHE_RAM_ATTR onSclChange(void) void ICACHE_RAM_ATTR onSdaChange(void) { - static uint8_t sda; - static uint8_t scl; + uint8_t sda; + uint8_t scl; sda = SDA_READ(); scl = SCL_READ(); From 5ba2dd3034fac7f3b7f86b61948b5e9ded351a9f Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jul 2019 17:09:09 -0700 Subject: [PATCH 02/20] Make most variables ints, not uint8_ts Where it doesn't make a functional difference, make global variables ints and not unit8_t. Bytewide updates and extracts require multiple instructions and hence increase IRAM usage as well as runtime. --- cores/esp8266/core_esp8266_si2c.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 0b5b6f6ed5..115d7823c1 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -59,15 +59,15 @@ static unsigned char twi_addr = 0; #define TWIP_WRITE 14 #define TWIP_BUS_ERR 15 -static volatile uint8_t twip_mode = TWIPM_IDLE; -static volatile uint8_t twip_state = TWIP_IDLE; -static volatile uint8_t twip_status = TW_NO_INFO; -static volatile uint8_t bitCount = 0; +static volatile int twip_mode = TWIPM_IDLE; +static volatile int twip_state = TWIP_IDLE; +static volatile int twip_status = TW_NO_INFO; +static volatile int bitCount = 0; #define TWDR twi_data static volatile uint8_t twi_data = 0x00; -static volatile uint8_t twi_ack = 0; -static volatile uint8_t twi_ack_rec = 0; +static volatile int twi_ack = 0; +static volatile int twi_ack_rec = 0; static volatile int twi_timeout_ms = 10; #define TWI_READY 0 @@ -75,15 +75,15 @@ static volatile int twi_timeout_ms = 10; #define TWI_MTX 2 #define TWI_SRX 3 #define TWI_STX 4 -static volatile uint8_t twi_state = TWI_READY; +static volatile int twi_state = TWI_READY; static volatile uint8_t twi_error = 0xFF; static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; -static volatile uint8_t twi_txBufferIndex; -static volatile uint8_t twi_txBufferLength; +static volatile int twi_txBufferIndex; +static volatile int twi_txBufferLength; static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; -static volatile uint8_t twi_rxBufferIndex; +static volatile int twi_rxBufferIndex; static void (*twi_onSlaveTransmit)(void); static void (*twi_onSlaveReceive)(uint8_t*, size_t); From f55ab362cb549745bb391e3515f2851f576529d6 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jul 2019 18:57:45 -0700 Subject: [PATCH 03/20] Make local flag vars int Sketch uses 270855 bytes (25%) of program storage space. Maximum is 1044464 bytes. Global variables use 27940 bytes (34%) of dynamic memory, leaving 53980 bytes for local variables. Maximum is 81920 bytes. ./xtensa-lx106-elf/bin/xtensa-lx106-elf-objdump -t -j .text1 /tmp/arduino_build_9615/*elf | sort -k1 | head -20 401000cc l F .text1 00000014 twi_delay 401000ec l F .text1 00000020 twi_reply$part$1 4010010c g F .text1 00000035 twi_reply 4010014c g F .text1 00000052 twi_stop 401001a0 g F .text1 0000003b twi_releaseBus 40100204 g F .text1 000001e6 twi_onTwipEvent 40100404 l F .text1 000001f7 onSdaChange 40100608 l F .text1 000002fd onSclChange 40100908 l F .text1 0000003b onTimer --- cores/esp8266/core_esp8266_si2c.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 115d7823c1..b523418902 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -524,8 +524,8 @@ static void eventTask(ETSEvent *e) void ICACHE_RAM_ATTR onSclChange(void) { - uint8_t sda; - uint8_t scl; + unsigned int sda; + unsigned int scl; sda = SDA_READ(); scl = SCL_READ(); @@ -682,8 +682,8 @@ void ICACHE_RAM_ATTR onSclChange(void) void ICACHE_RAM_ATTR onSdaChange(void) { - uint8_t sda; - uint8_t scl; + unsigned int sda; + unsigned int scl; sda = SDA_READ(); scl = SCL_READ(); From 37e10b9aa0f424f2fb0a06144cb00d5a5e32ab21 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jul 2019 19:15:48 -0700 Subject: [PATCH 04/20] Factor out !scl in onSdaChange If SCL is low then all branches of the case are no-ops, so factor that portion outo to remove some redundant logic in each case. Sketch uses 270843 bytes (25%) of program storage space. Maximum is 1044464 bytes. Global variables use 27944 bytes (34%) of dynamic memory, leaving 53976 bytes for local variables. Maximum is 81920 bytes. 401000cc l F .text1 00000014 twi_delay 401000ec l F .text1 00000020 twi_reply$part$1 4010010c g F .text1 00000035 twi_reply 4010014c g F .text1 00000052 twi_stop 401001a0 g F .text1 0000003b twi_releaseBus 40100204 g F .text1 000001e6 twi_onTwipEvent 40100404 l F .text1 000001e7 onSdaChange 401005f8 l F .text1 000002fd onSclChange 401008f8 l F .text1 0000003b onTimer 0x0000000040107468 _text_end = ABSOLUTE (.) --- cores/esp8266/core_esp8266_si2c.cpp | 98 +++++++++++++---------------- 1 file changed, 42 insertions(+), 56 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index b523418902..d5da9ae99f 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -687,12 +687,10 @@ void ICACHE_RAM_ATTR onSdaChange(void) sda = SDA_READ(); scl = SCL_READ(); - switch (twip_state) + if (scl) /* !DATA */ switch (twip_state) { case TWIP_IDLE: - if (!scl) { - // DATA - ignore - } else if (sda) { + if (sda) { // STOP - ignore } else { // START @@ -711,72 +709,60 @@ void ICACHE_RAM_ATTR onSdaChange(void) case TWIP_READ_ACK: case TWIP_RWAIT_ACK: case TWIP_WRITE: - if (!scl) { - // DATA - ignore - } else { - // START or STOP - SDA_HIGH(); // Should not be necessary - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_BUS_ERR; - } + // START or STOP + SDA_HIGH(); // Should not be necessary + twip_status = TW_BUS_ERROR; + twi_onTwipEvent(twip_status); + twip_mode = TWIPM_WAIT; + twip_state = TWIP_BUS_ERR; break; case TWIP_WAIT_STOP: case TWIP_BUS_ERR: - if (!scl) { - // DATA - ignore + if (sda) { + // STOP + SCL_LOW(); // clock stretching + ets_timer_disarm(&timer); + twip_state = TWIP_IDLE; + twip_mode = TWIPM_IDLE; + SCL_HIGH(); } else { - if (sda) { - // STOP - SCL_LOW(); // clock stretching - ets_timer_disarm(&timer); - twip_state = TWIP_IDLE; - twip_mode = TWIPM_IDLE; - SCL_HIGH(); + // START + if (twip_state == TWIP_BUS_ERR) { + // ignore } else { - // START - if (twip_state == TWIP_BUS_ERR) { - // ignore - } else { - bitCount = 8; - twip_state = TWIP_REP_START; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms - } + bitCount = 8; + twip_state = TWIP_REP_START; + ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms } } break; case TWIP_SLA_W: case TWIP_READ: - if (!scl) { - // DATA - ignore + // START or STOP + if (bitCount != 7) { + // inside byte transfer - error + twip_status = TW_BUS_ERROR; + twi_onTwipEvent(twip_status); + twip_mode = TWIPM_WAIT; + twip_state = TWIP_BUS_ERR; } else { - // START or STOP - if (bitCount != 7) { - // inside byte transfer - error - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_BUS_ERR; + // during first bit in byte transfer - ok + SCL_LOW(); // clock stretching + twip_status = TW_SR_STOP; + twi_onTwipEvent(twip_status); + if (sda) { + // STOP + ets_timer_disarm(&timer); + twip_state = TWIP_IDLE; + twip_mode = TWIPM_IDLE; } else { - // during first bit in byte transfer - ok - SCL_LOW(); // clock stretching - twip_status = TW_SR_STOP; - twi_onTwipEvent(twip_status); - if (sda) { - // STOP - ets_timer_disarm(&timer); - twip_state = TWIP_IDLE; - twip_mode = TWIPM_IDLE; - } else { - // START - bitCount = 8; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms - twip_state = TWIP_REP_START; - twip_mode = TWIPM_IDLE; - } + // START + bitCount = 8; + ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms + twip_state = TWIP_REP_START; + twip_mode = TWIPM_IDLE; } } break; From 423a5bfb377ad66cb9afdc13e46940e4533ce9be Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jul 2019 19:36:09 -0700 Subject: [PATCH 05/20] Make tiny twi_reply inline twi_reply is a chunk of code that can be inlined and actually save IRAM space because certain conditions acan be statically evaluated by gcc. Sketch uses 270823 bytes (25%) of program storage space. Maximum is 1044464 bytes. Global variables use 27944 bytes (34%) of dynamic memory, leaving 53976 bytes for local variables. Maximum is 81920 bytes. 401000cc l F .text1 00000014 twi_delay 401000f4 g F .text1 00000052 twi_stop 40100148 g F .text1 0000003b twi_releaseBus 401001b0 g F .text1 00000206 twi_onTwipEvent 401003d0 l F .text1 000001e7 onSdaChange 401005c4 l F .text1 000002fd onSclChange 401008c4 l F .text1 0000003b onTimer 40100918 g F .text1 00000085 millis 401009a0 g F .text1 0000000f micros 401009b0 g F .text1 00000022 micros64 401009d8 g F .text1 00000013 delayMicroseconds 401009f0 g F .text1 00000034 __digitalRead 401009f0 w F .text1 00000034 digitalRead 40100a3c g F .text1 000000e4 interrupt_handler 40100b20 g F .text1 0000000f vPortFree 0x0000000040107434 _text_end = ABSOLUTE (.) --- cores/esp8266/core_esp8266_si2c.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index d5da9ae99f..aadc2adb93 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -350,7 +350,7 @@ void twi_attachSlaveTxEvent( void (*function)(void) ) twi_onSlaveTransmit = function; } -void ICACHE_RAM_ATTR twi_reply(uint8_t ack) +inline void ICACHE_RAM_ATTR twi_reply(uint8_t ack) { // transmit master read ready signal, with or without ack if (ack) { From 62f9599d14e35aed76e95a422199035a908b9324 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jul 2019 19:42:08 -0700 Subject: [PATCH 06/20] Inline additional twi_** helper functions Sketch uses 270799 bytes (25%) of program storage space. Maximum is 1044464 bytes. Global variables use 27944 bytes (34%) of dynamic memory, leaving 53976 bytes for local variables. Maximum is 81920 bytes. 401000cc l F .text1 00000014 twi_delay 401000f4 w F .text1 0000003b twi_releaseBus 4010015c g F .text1 00000246 twi_onTwipEvent 401003bc l F .text1 000001e7 onSdaChange 401005b0 l F .text1 000002f9 onSclChange 401008ac l F .text1 0000003b onTimer 0x000000004010741c _text_end = ABSOLUTE (.) --- cores/esp8266/core_esp8266_si2c.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index aadc2adb93..3d5c8a9238 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -364,7 +364,7 @@ inline void ICACHE_RAM_ATTR twi_reply(uint8_t ack) } } -void ICACHE_RAM_ATTR twi_stop(void) +inline void ICACHE_RAM_ATTR twi_stop(void) { // send stop condition //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); @@ -376,7 +376,7 @@ void ICACHE_RAM_ATTR twi_stop(void) twi_state = TWI_READY; } -void ICACHE_RAM_ATTR twi_releaseBus(void) +inline void ICACHE_RAM_ATTR twi_releaseBus(void) { // release bus //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); From 014f87f83bbb89fa56d0dde8cb8dbd7c69aad67e Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Sun, 21 Jul 2019 20:08:34 -0700 Subject: [PATCH 07/20] Convert state machine to 1-hot for faster lookup GCC won't use a lookup table for the TWI state machine, so it ends up using a series of straight line compare-jump, compare-jumps to figure out which branch of code to execute for each state. For branches that have multiple states that call them, this can expand to a lot of code. Short-circuit the whole thing by converting the FSM to a 1-hot encoding while executing it, and then just and-ing the 1-hot state with the bitmask of states with the same code. Sketch uses 270719 bytes (25%) of program storage space. Maximum is 1044464 bytes. Global variables use 27944 bytes (34%) of dynamic memory, leaving 53976 bytes for local variables. Maximum is 81920 bytes. 401000cc l F .text1 00000014 twi_delay 401000f4 w F .text1 0000003b twi_releaseBus 4010015c g F .text1 00000246 twi_onTwipEvent 401003c0 l F .text1 000001b1 onSdaChange 40100580 l F .text1 000002da onSclChange 4010085c l F .text1 0000003b onTimer 0x00000000401073cc _text_end = ABSOLUTE (.) Saves 228 bytes of IRAM vs. master, uses 32 additional bytes of heap. --- cores/esp8266/core_esp8266_si2c.cpp | 252 ++++++++++++---------------- 1 file changed, 107 insertions(+), 145 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 3d5c8a9238..5337b6e821 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -522,6 +522,14 @@ static void eventTask(ETSEvent *e) } } +// The state machine is converted from a 0...15 state to a 1-hot encoded state, and then +// compared to the logical-or of all states with the same branch. This removes the need +// for a large series of straight-line compares. The biggest win is when multiple states +// all have the same branch (onSdaChange), but for others there is some benefit, still. +#define S2M(x) (1<<(x)) +// Shorthand for if the state is any of the or'd bitmask x +#define IFSTATE(x) if (twip_state_mask & (x)) + void ICACHE_RAM_ATTR onSclChange(void) { unsigned int sda; @@ -532,151 +540,124 @@ void ICACHE_RAM_ATTR onSclChange(void) twip_status = 0xF8; // reset TWI status - switch (twip_state) - { - case TWIP_IDLE: - case TWIP_WAIT_STOP: - case TWIP_BUS_ERR: + int twip_state_mask = S2M(twip_state); + IFSTATE(S2M(TWIP_START)|S2M(TWIP_REP_START)|S2M(TWIP_SLA_W)|S2M(TWIP_READ)) { + if (!scl) { // ignore - break; + } else { + bitCount--; + twi_data <<= 1; + twi_data |= sda; - case TWIP_START: - case TWIP_REP_START: - case TWIP_SLA_W: - case TWIP_READ: - if (!scl) { - // ignore + if (bitCount != 0) { + // continue } else { - bitCount--; - twi_data <<= 1; - twi_data |= sda; - - if (bitCount != 0) { - // continue + twip_state = TWIP_SEND_ACK; + } + } + } else IFSTATE(S2M(TWIP_SEND_ACK)) { + if (scl) { + // ignore + } else { + if (twip_mode == TWIPM_IDLE) { + if ((twi_data & 0xFE) != twi_addr) { + // ignore } else { - twip_state = TWIP_SEND_ACK; + SDA_LOW(); } - } - break; - - case TWIP_SEND_ACK: - if (scl) { - // ignore } else { - if (twip_mode == TWIPM_IDLE) { - if ((twi_data & 0xFE) != twi_addr) { - // ignore - } else { - SDA_LOW(); - } + if (!twi_ack) { + // ignore } else { - if (!twi_ack) { - // ignore - } else { - SDA_LOW(); - } + SDA_LOW(); } - twip_state = TWIP_WAIT_ACK; } - break; - - case TWIP_WAIT_ACK: - if (scl) { - // ignore - } else { - if (twip_mode == TWIPM_IDLE) { - if ((twi_data & 0xFE) != twi_addr) { - SDA_HIGH(); - twip_state = TWIP_WAIT_STOP; - } else { - SCL_LOW(); // clock stretching - SDA_HIGH(); - twip_mode = TWIPM_ADDRESSED; - if (!(twi_data & 0x01)) { - twip_status = TW_SR_SLA_ACK; - twi_onTwipEvent(twip_status); - bitCount = 8; - twip_state = TWIP_SLA_W; - } else { - twip_status = TW_ST_SLA_ACK; - twi_onTwipEvent(twip_status); - twip_state = TWIP_SLA_R; - } - } + twip_state = TWIP_WAIT_ACK; + } + } else IFSTATE(S2M(TWIP_WAIT_ACK)) { + if (scl) { + // ignore + } else { + if (twip_mode == TWIPM_IDLE) { + if ((twi_data & 0xFE) != twi_addr) { + SDA_HIGH(); + twip_state = TWIP_WAIT_STOP; } else { SCL_LOW(); // clock stretching SDA_HIGH(); - if (!twi_ack) { - twip_status = TW_SR_DATA_NACK; + twip_mode = TWIPM_ADDRESSED; + if (!(twi_data & 0x01)) { + twip_status = TW_SR_SLA_ACK; twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_WAIT_STOP; + bitCount = 8; + twip_state = TWIP_SLA_W; } else { - twip_status = TW_SR_DATA_ACK; + twip_status = TW_ST_SLA_ACK; twi_onTwipEvent(twip_status); - bitCount = 8; - twip_state = TWIP_READ; + twip_state = TWIP_SLA_R; } } - } - break; - - case TWIP_SLA_R: - case TWIP_WRITE: - if (scl) { - // ignore } else { - bitCount--; - (twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); - twi_data <<= 1; - - if (bitCount != 0) { - // continue + SCL_LOW(); // clock stretching + SDA_HIGH(); + if (!twi_ack) { + twip_status = TW_SR_DATA_NACK; + twi_onTwipEvent(twip_status); + twip_mode = TWIPM_WAIT; + twip_state = TWIP_WAIT_STOP; } else { - twip_state = TWIP_REC_ACK; + twip_status = TW_SR_DATA_ACK; + twi_onTwipEvent(twip_status); + bitCount = 8; + twip_state = TWIP_READ; } } - break; - - case TWIP_REC_ACK: - if (scl) { - // ignore - } else { - SDA_HIGH(); - twip_state = TWIP_READ_ACK; - } - break; + } + } else IFSTATE(S2M(TWIP_SLA_R)|S2M(TWIP_WRITE)) { + if (scl) { + // ignore + } else { + bitCount--; + (twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); + twi_data <<= 1; - case TWIP_READ_ACK: - if (!scl) { - // ignore + if (bitCount != 0) { + // continue } else { - twi_ack_rec = !sda; - twip_state = TWIP_RWAIT_ACK; + twip_state = TWIP_REC_ACK; } - break; - - case TWIP_RWAIT_ACK: - if (scl) { - // ignore + } + } else IFSTATE(S2M(TWIP_REC_ACK)) { + if (scl) { + // ignore + } else { + SDA_HIGH(); + twip_state = TWIP_READ_ACK; + } + } else IFSTATE(S2M(TWIP_READ_ACK)) { + if (!scl) { + // ignore + } else { + twi_ack_rec = !sda; + twip_state = TWIP_RWAIT_ACK; + } + } else IFSTATE(S2M(TWIP_RWAIT_ACK)) { + if (scl) { + // ignore + } else { + SCL_LOW(); // clock stretching + if (twi_ack && twi_ack_rec) { + twip_status = TW_ST_DATA_ACK; + twi_onTwipEvent(twip_status); + twip_state = TWIP_WRITE; } else { - SCL_LOW(); // clock stretching - if (twi_ack && twi_ack_rec) { - twip_status = TW_ST_DATA_ACK; - twi_onTwipEvent(twip_status); - twip_state = TWIP_WRITE; - } else { - // we have no more data to send and/or the master doesn't want anymore - twip_status = twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK; - twi_onTwipEvent(twip_status); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_WAIT_STOP; - } + // we have no more data to send and/or the master doesn't want anymore + twip_status = twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK; + twi_onTwipEvent(twip_status); + twip_mode = TWIPM_WAIT; + twip_state = TWIP_WAIT_STOP; } - break; - - default: - break; + } } } @@ -687,9 +668,9 @@ void ICACHE_RAM_ATTR onSdaChange(void) sda = SDA_READ(); scl = SCL_READ(); - if (scl) /* !DATA */ switch (twip_state) - { - case TWIP_IDLE: + int twip_state_mask = S2M(twip_state); + if (scl) { /* !DATA */ + IFSTATE(S2M(TWIP_IDLE)) { if (sda) { // STOP - ignore } else { @@ -698,27 +679,14 @@ void ICACHE_RAM_ATTR onSdaChange(void) twip_state = TWIP_START; ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms } - break; - - case TWIP_START: - case TWIP_REP_START: - case TWIP_SEND_ACK: - case TWIP_WAIT_ACK: - case TWIP_SLA_R: - case TWIP_REC_ACK: - case TWIP_READ_ACK: - case TWIP_RWAIT_ACK: - case TWIP_WRITE: + } 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)) { // START or STOP SDA_HIGH(); // Should not be necessary twip_status = TW_BUS_ERROR; twi_onTwipEvent(twip_status); twip_mode = TWIPM_WAIT; twip_state = TWIP_BUS_ERR; - break; - - case TWIP_WAIT_STOP: - case TWIP_BUS_ERR: + } else IFSTATE(S2M(TWIP_WAIT_STOP)|S2M(TWIP_BUS_ERR)) { if (sda) { // STOP SCL_LOW(); // clock stretching @@ -736,10 +704,7 @@ void ICACHE_RAM_ATTR onSdaChange(void) ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms } } - break; - - case TWIP_SLA_W: - case TWIP_READ: + } else IFSTATE(S2M(TWIP_SLA_W)|S2M(TWIP_READ)) { // START or STOP if (bitCount != 7) { // inside byte transfer - error @@ -765,10 +730,7 @@ void ICACHE_RAM_ATTR onSdaChange(void) twip_mode = TWIPM_IDLE; } } - break; - - default: - break; + } } } From 9ae9af1ab23a5e46ae72c939c499048d0236ecc9 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 23 Jul 2019 22:21:03 -0700 Subject: [PATCH 08/20] Factor out twi_status setting twi_status is set immediately before an event handler is called, resulting in lots of duplicated code. Set the twi_status flag inside the handler itself. Saves an add'l ~100 bytes of IRAM from prior changes, for a total of ~340 bytes. earle@server:~/Arduino/hardware/esp8266com/esp8266/tools$ ./xtensa-lx106-elf/bin/xtensa-lx106-elf-objdump -t -j .text1 /tmp/arduino_build_849115/*elf | sort -k1 | head -20 401000cc l F .text1 00000014 twi_delay 401000f4 w F .text1 0000003b twi_releaseBus 40100160 g F .text1 0000024e twi_onTwipEvent 401003c8 l F .text1 00000181 onSdaChange 40100558 l F .text1 00000297 onSclChange --- cores/esp8266/core_esp8266_si2c.cpp | 31 ++++++++++------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 5337b6e821..d45425eff7 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -391,6 +391,7 @@ inline void ICACHE_RAM_ATTR twi_releaseBus(void) void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status) { + twip_status = status; switch(status) { // Slave Receiver case TW_SR_SLA_ACK: // addressed, returned ack @@ -485,8 +486,7 @@ void ICACHE_RAM_ATTR onTimer(void *unused) { (void)unused; twi_releaseBus(); - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_BUS_ERROR); twip_mode = TWIPM_WAIT; twip_state = TWIP_BUS_ERR; } @@ -587,13 +587,11 @@ void ICACHE_RAM_ATTR onSclChange(void) SDA_HIGH(); twip_mode = TWIPM_ADDRESSED; if (!(twi_data & 0x01)) { - twip_status = TW_SR_SLA_ACK; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_SR_SLA_ACK); bitCount = 8; twip_state = TWIP_SLA_W; } else { - twip_status = TW_ST_SLA_ACK; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_ST_SLA_ACK); twip_state = TWIP_SLA_R; } } @@ -601,13 +599,11 @@ void ICACHE_RAM_ATTR onSclChange(void) SCL_LOW(); // clock stretching SDA_HIGH(); if (!twi_ack) { - twip_status = TW_SR_DATA_NACK; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_SR_DATA_NACK); twip_mode = TWIPM_WAIT; twip_state = TWIP_WAIT_STOP; } else { - twip_status = TW_SR_DATA_ACK; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_SR_DATA_ACK); bitCount = 8; twip_state = TWIP_READ; } @@ -647,13 +643,11 @@ void ICACHE_RAM_ATTR onSclChange(void) } else { SCL_LOW(); // clock stretching if (twi_ack && twi_ack_rec) { - twip_status = TW_ST_DATA_ACK; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_ST_DATA_ACK); twip_state = TWIP_WRITE; } else { // we have no more data to send and/or the master doesn't want anymore - twip_status = twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); twip_mode = TWIPM_WAIT; twip_state = TWIP_WAIT_STOP; } @@ -682,8 +676,7 @@ void ICACHE_RAM_ATTR onSdaChange(void) } 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)) { // START or STOP SDA_HIGH(); // Should not be necessary - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_BUS_ERROR); twip_mode = TWIPM_WAIT; twip_state = TWIP_BUS_ERR; } else IFSTATE(S2M(TWIP_WAIT_STOP)|S2M(TWIP_BUS_ERR)) { @@ -708,15 +701,13 @@ void ICACHE_RAM_ATTR onSdaChange(void) // START or STOP if (bitCount != 7) { // inside byte transfer - error - twip_status = TW_BUS_ERROR; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_BUS_ERROR); twip_mode = TWIPM_WAIT; twip_state = TWIP_BUS_ERR; } else { // during first bit in byte transfer - ok SCL_LOW(); // clock stretching - twip_status = TW_SR_STOP; - twi_onTwipEvent(twip_status); + twi_onTwipEvent(TW_SR_STOP); if (sda) { // STOP ets_timer_disarm(&timer); From b97599604c3be39933c7c575448b5027ee147a44 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Wed, 24 Jul 2019 09:27:52 -0700 Subject: [PATCH 09/20] Use a struct to hold globals for TWI Thanks to the suggestion from @mhightower83, move all global objects into a struct. This lets a single base pointer register to be used in place of constantly reloading the address of each individual variable. This might be better expressed by moving this to a real C++ implementaion based on a class object (the twi.xxxx would go back to the old xxx-only naming for vars), but there would then need to be API wrappers since the functionality is exposed through a plain C API. Saves 168 additional code bytes, for a grand total of 550 bytes IRAM. earle@server:~/Arduino/hardware/esp8266com/esp8266/tools$ ./xtensa-lx106-elf/bin/xtensa-lx106-elf-objdump -t -j .text1 /tmp/arduino_build_849115/*elf | sort -k1 | head -20 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 00000267 onSclChange 40100704 l F .text1 00000028 onTimer --- cores/esp8266/core_esp8266_si2c.cpp | 349 ++++++++++++++-------------- 1 file changed, 178 insertions(+), 171 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index d45425eff7..e31768fa9f 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -30,10 +30,14 @@ unsigned int preferred_si2c_clock = 100000; #include "ets_sys.h" -unsigned char twi_dcount = 18; -static unsigned char twi_sda, twi_scl; -static uint32_t twi_clockStretchLimit; -static unsigned char twi_addr = 0; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" + +static struct twi { + 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 @@ -59,37 +63,34 @@ static unsigned char twi_addr = 0; #define TWIP_WRITE 14 #define TWIP_BUS_ERR 15 -static volatile int twip_mode = TWIPM_IDLE; -static volatile int twip_state = TWIP_IDLE; -static volatile int twip_status = TW_NO_INFO; -static volatile int bitCount = 0; + volatile int twip_mode;// = TWIPM_IDLE; + volatile int twip_state;// = TWIP_IDLE; + volatile int twip_status;// = TW_NO_INFO; + volatile int bitCount;// = 0; -#define TWDR twi_data -static volatile uint8_t twi_data = 0x00; -static volatile int twi_ack = 0; -static volatile int twi_ack_rec = 0; -static volatile int twi_timeout_ms = 10; +#define TWDR twi.twi_data + volatile uint8_t twi_data;// = 0x00; + volatile int twi_ack;// = 0; + 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 -static volatile int twi_state = TWI_READY; -static volatile uint8_t twi_error = 0xFF; - -static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; -static volatile int twi_txBufferIndex; -static volatile int twi_txBufferLength; + volatile int twi_state;// = TWI_READY; + volatile uint8_t twi_error;// = 0xFF; -static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; -static volatile int twi_rxBufferIndex; + uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; + volatile int twi_txBufferIndex; + volatile int twi_txBufferLength; -static void (*twi_onSlaveTransmit)(void); -static void (*twi_onSlaveReceive)(uint8_t*, size_t); + uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; + volatile int twi_rxBufferIndex; -static void onSclChange(void); -static void onSdaChange(void); + void (*twi_onSlaveTransmit)(void); + void (*twi_onSlaveReceive)(uint8_t*, size_t); #define EVENTTASK_QUEUE_SIZE 1 #define EVENTTASK_QUEUE_PRIO 2 @@ -98,17 +99,23 @@ static void onSdaChange(void); #define TWI_SIG_RX (TWI_SIG_RANGE + 0x01) #define TWI_SIG_TX (TWI_SIG_RANGE + 0x02) -static ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE]; + 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 }; +#pragma GCC diagnostic pop + +static void onSclChange(void); +static void onSdaChange(void); static void eventTask(ETSEvent *e); -static ETSTimer timer; static void onTimer(void *unused); -#define SDA_LOW() (GPES = (1 << twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low) -#define SDA_HIGH() (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high) -#define SDA_READ() ((GPI & (1 << twi_sda)) != 0) -#define SCL_LOW() (GPES = (1 << twi_scl)) -#define SCL_HIGH() (GPEC = (1 << twi_scl)) -#define SCL_READ() ((GPI & (1 << twi_scl)) != 0) +#define SDA_LOW() (GPES = (1 << twi.twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low) +#define SDA_HIGH() (GPEC = (1 << twi.twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high) +#define SDA_READ() ((GPI & (1 << twi.twi_sda)) != 0) +#define SCL_LOW() (GPES = (1 << twi.twi_scl)) +#define SCL_HIGH() (GPEC = (1 << twi.twi_scl)) +#define SCL_READ() ((GPI & (1 << twi.twi_scl)) != 0) #ifndef FCPU80 #define FCPU80 80000000L @@ -123,44 +130,44 @@ static void onTimer(void *unused); void twi_setClock(unsigned int freq){ preferred_si2c_clock = freq; #if F_CPU == FCPU80 - if(freq <= 50000) twi_dcount = 38;//about 50KHz - else if(freq <= 100000) twi_dcount = 19;//about 100KHz - else if(freq <= 200000) twi_dcount = 8;//about 200KHz - else if(freq <= 300000) twi_dcount = 3;//about 300KHz - else if(freq <= 400000) twi_dcount = 1;//about 400KHz - else twi_dcount = 1;//about 400KHz + if(freq <= 50000) twi.twi_dcount = 38;//about 50KHz + else if(freq <= 100000) twi.twi_dcount = 19;//about 100KHz + else if(freq <= 200000) twi.twi_dcount = 8;//about 200KHz + else if(freq <= 300000) twi.twi_dcount = 3;//about 300KHz + else if(freq <= 400000) twi.twi_dcount = 1;//about 400KHz + else twi.twi_dcount = 1;//about 400KHz #else - if(freq <= 50000) twi_dcount = 64;//about 50KHz - else if(freq <= 100000) twi_dcount = 32;//about 100KHz - else if(freq <= 200000) twi_dcount = 14;//about 200KHz - else if(freq <= 300000) twi_dcount = 8;//about 300KHz - else if(freq <= 400000) twi_dcount = 5;//about 400KHz - else if(freq <= 500000) twi_dcount = 3;//about 500KHz - else if(freq <= 600000) twi_dcount = 2;//about 600KHz - else twi_dcount = 1;//about 700KHz + if(freq <= 50000) twi.twi_dcount = 64;//about 50KHz + else if(freq <= 100000) twi.twi_dcount = 32;//about 100KHz + else if(freq <= 200000) twi.twi_dcount = 14;//about 200KHz + else if(freq <= 300000) twi.twi_dcount = 8;//about 300KHz + else if(freq <= 400000) twi.twi_dcount = 5;//about 400KHz + else if(freq <= 500000) twi.twi_dcount = 3;//about 500KHz + else if(freq <= 600000) twi.twi_dcount = 2;//about 600KHz + else twi.twi_dcount = 1;//about 700KHz #endif } void twi_setClockStretchLimit(uint32_t limit){ - twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER; + twi.twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER; } void twi_init(unsigned char sda, unsigned char scl) { // set timer function - ets_timer_setfn(&timer, onTimer, NULL); + ets_timer_setfn(&twi.timer, onTimer, NULL); // create event task - ets_task(eventTask, EVENTTASK_QUEUE_PRIO, eventTaskQueue, EVENTTASK_QUEUE_SIZE); + ets_task(eventTask, EVENTTASK_QUEUE_PRIO, twi.eventTaskQueue, EVENTTASK_QUEUE_SIZE); - twi_sda = sda; - twi_scl = scl; - pinMode(twi_sda, INPUT_PULLUP); - pinMode(twi_scl, INPUT_PULLUP); + twi.twi_sda = sda; + twi.twi_scl = scl; + pinMode(twi.twi_sda, INPUT_PULLUP); + pinMode(twi.twi_scl, INPUT_PULLUP); twi_setClock(preferred_si2c_clock); twi_setClockStretchLimit(230); // default value is 230 uS - if (twi_addr != 0) + if (twi.twi_addr != 0) { attachInterrupt(scl, onSclChange, CHANGE); attachInterrupt(sda, onSdaChange, CHANGE); @@ -170,7 +177,7 @@ void twi_init(unsigned char sda, unsigned char scl) void twi_setAddress(uint8_t address) { // set twi slave address (skip over R/W bit) - twi_addr = address << 1; + twi.twi_addr = address << 1; } static void ICACHE_RAM_ATTR twi_delay(unsigned char v){ @@ -191,9 +198,9 @@ static bool twi_write_start(void) { if (SDA_READ() == 0) { return false; } - twi_delay(twi_dcount); + twi_delay(twi.twi_dcount); SDA_LOW(); - twi_delay(twi_dcount); + twi_delay(twi.twi_dcount); return true; } @@ -201,12 +208,12 @@ static bool twi_write_stop(void){ uint32_t i = 0; SCL_LOW(); SDA_LOW(); - twi_delay(twi_dcount); + twi_delay(twi.twi_dcount); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching - twi_delay(twi_dcount); + while (SCL_READ() == 0 && (i++) < twi.twi_clockStretchLimit); // Clock stretching + twi_delay(twi.twi_dcount); SDA_HIGH(); - twi_delay(twi_dcount); + twi_delay(twi.twi_dcount); return true; } @@ -215,10 +222,10 @@ static bool twi_write_bit(bool bit) { SCL_LOW(); if (bit) SDA_HIGH(); else SDA_LOW(); - twi_delay(twi_dcount+1); + twi_delay(twi.twi_dcount+1); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching - twi_delay(twi_dcount); + while (SCL_READ() == 0 && (i++) < twi.twi_clockStretchLimit);// Clock stretching + twi_delay(twi.twi_dcount); return true; } @@ -226,11 +233,11 @@ static bool twi_read_bit(void) { uint32_t i = 0; SCL_LOW(); SDA_HIGH(); - twi_delay(twi_dcount+2); + twi_delay(twi.twi_dcount+2); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching + while (SCL_READ() == 0 && (i++) < twi.twi_clockStretchLimit);// Clock stretching bool bit = SDA_READ(); - twi_delay(twi_dcount); + twi_delay(twi.twi_dcount); return bit; } @@ -268,10 +275,10 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i i = 0; while(SDA_READ() == 0 && (i++) < 10){ SCL_LOW(); - twi_delay(twi_dcount); + twi_delay(twi.twi_dcount); SCL_HIGH(); - unsigned int t=0; while(SCL_READ()==0 && (t++)sig) { case TWI_SIG_TX: - twi_onSlaveTransmit(); + twi.twi_onSlaveTransmit(); // if they didn't change buffer & length, initialize it - if (twi_txBufferLength == 0) { - twi_txBufferLength = 1; - twi_txBuffer[0] = 0x00; + if (twi.twi_txBufferLength == 0) { + twi.twi_txBufferLength = 1; + twi.twi_txBuffer[0] = 0x00; } // Initiate transmission @@ -517,7 +524,7 @@ static void eventTask(ETSEvent *e) case TWI_SIG_RX: // ack future responses and leave slave receiver state twi_releaseBus(); - twi_onSlaveReceive(twi_rxBuffer, e->par); + twi.twi_onSlaveReceive(twi.twi_rxBuffer, e->par); break; } } @@ -538,74 +545,74 @@ void ICACHE_RAM_ATTR onSclChange(void) sda = SDA_READ(); scl = SCL_READ(); - twip_status = 0xF8; // reset TWI status + twi.twip_status = 0xF8; // reset TWI status - int twip_state_mask = S2M(twip_state); + int twip_state_mask = S2M(twi.twip_state); IFSTATE(S2M(TWIP_START)|S2M(TWIP_REP_START)|S2M(TWIP_SLA_W)|S2M(TWIP_READ)) { if (!scl) { // ignore } else { - bitCount--; - twi_data <<= 1; - twi_data |= sda; + twi.bitCount--; + twi.twi_data <<= 1; + twi.twi_data |= sda; - if (bitCount != 0) { + if (twi.bitCount != 0) { // continue } else { - twip_state = TWIP_SEND_ACK; + twi.twip_state = TWIP_SEND_ACK; } } } else IFSTATE(S2M(TWIP_SEND_ACK)) { if (scl) { // ignore } else { - if (twip_mode == TWIPM_IDLE) { - if ((twi_data & 0xFE) != twi_addr) { + if (twi.twip_mode == TWIPM_IDLE) { + if ((twi.twi_data & 0xFE) != twi.twi_addr) { // ignore } else { SDA_LOW(); } } else { - if (!twi_ack) { + if (!twi.twi_ack) { // ignore } else { SDA_LOW(); } } - twip_state = TWIP_WAIT_ACK; + twi.twip_state = TWIP_WAIT_ACK; } } else IFSTATE(S2M(TWIP_WAIT_ACK)) { if (scl) { // ignore } else { - if (twip_mode == TWIPM_IDLE) { - if ((twi_data & 0xFE) != twi_addr) { + if (twi.twip_mode == TWIPM_IDLE) { + if ((twi.twi_data & 0xFE) != twi.twi_addr) { SDA_HIGH(); - twip_state = TWIP_WAIT_STOP; + twi.twip_state = TWIP_WAIT_STOP; } else { SCL_LOW(); // clock stretching SDA_HIGH(); - twip_mode = TWIPM_ADDRESSED; - if (!(twi_data & 0x01)) { + twi.twip_mode = TWIPM_ADDRESSED; + if (!(twi.twi_data & 0x01)) { twi_onTwipEvent(TW_SR_SLA_ACK); - bitCount = 8; - twip_state = TWIP_SLA_W; + twi.bitCount = 8; + twi.twip_state = TWIP_SLA_W; } else { twi_onTwipEvent(TW_ST_SLA_ACK); - twip_state = TWIP_SLA_R; + twi.twip_state = TWIP_SLA_R; } } } else { SCL_LOW(); // clock stretching SDA_HIGH(); - if (!twi_ack) { + if (!twi.twi_ack) { twi_onTwipEvent(TW_SR_DATA_NACK); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_WAIT_STOP; + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_WAIT_STOP; } else { twi_onTwipEvent(TW_SR_DATA_ACK); - bitCount = 8; - twip_state = TWIP_READ; + twi.bitCount = 8; + twi.twip_state = TWIP_READ; } } } @@ -613,14 +620,14 @@ void ICACHE_RAM_ATTR onSclChange(void) if (scl) { // ignore } else { - bitCount--; - (twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); - twi_data <<= 1; + twi.bitCount--; + (twi.twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); + twi.twi_data <<= 1; - if (bitCount != 0) { + if (twi.bitCount != 0) { // continue } else { - twip_state = TWIP_REC_ACK; + twi.twip_state = TWIP_REC_ACK; } } } else IFSTATE(S2M(TWIP_REC_ACK)) { @@ -628,28 +635,28 @@ void ICACHE_RAM_ATTR onSclChange(void) // ignore } else { SDA_HIGH(); - twip_state = TWIP_READ_ACK; + twi.twip_state = TWIP_READ_ACK; } } else IFSTATE(S2M(TWIP_READ_ACK)) { if (!scl) { // ignore } else { - twi_ack_rec = !sda; - twip_state = TWIP_RWAIT_ACK; + twi.twi_ack_rec = !sda; + twi.twip_state = TWIP_RWAIT_ACK; } } else IFSTATE(S2M(TWIP_RWAIT_ACK)) { if (scl) { // ignore } else { SCL_LOW(); // clock stretching - if (twi_ack && twi_ack_rec) { + if (twi.twi_ack && twi.twi_ack_rec) { twi_onTwipEvent(TW_ST_DATA_ACK); - twip_state = TWIP_WRITE; + twi.twip_state = TWIP_WRITE; } else { // we have no more data to send and/or the master doesn't want anymore - twi_onTwipEvent(twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_WAIT_STOP; + twi_onTwipEvent(twi.twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_WAIT_STOP; } } } @@ -662,63 +669,63 @@ void ICACHE_RAM_ATTR onSdaChange(void) sda = SDA_READ(); scl = SCL_READ(); - int twip_state_mask = S2M(twip_state); + int twip_state_mask = S2M(twi.twip_state); if (scl) { /* !DATA */ IFSTATE(S2M(TWIP_IDLE)) { if (sda) { // STOP - ignore } else { // START - bitCount = 8; - twip_state = TWIP_START; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms + twi.bitCount = 8; + twi.twip_state = TWIP_START; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms } } 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)) { // START or STOP SDA_HIGH(); // Should not be necessary twi_onTwipEvent(TW_BUS_ERROR); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_BUS_ERR; + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; } else IFSTATE(S2M(TWIP_WAIT_STOP)|S2M(TWIP_BUS_ERR)) { if (sda) { // STOP SCL_LOW(); // clock stretching - ets_timer_disarm(&timer); - twip_state = TWIP_IDLE; - twip_mode = TWIPM_IDLE; + ets_timer_disarm(&twi.timer); + twi.twip_state = TWIP_IDLE; + twi.twip_mode = TWIPM_IDLE; SCL_HIGH(); } else { // START - if (twip_state == TWIP_BUS_ERR) { + if (twi.twip_state == TWIP_BUS_ERR) { // ignore } else { - bitCount = 8; - twip_state = TWIP_REP_START; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms + twi.bitCount = 8; + twi.twip_state = TWIP_REP_START; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms } } } else IFSTATE(S2M(TWIP_SLA_W)|S2M(TWIP_READ)) { // START or STOP - if (bitCount != 7) { + if (twi.bitCount != 7) { // inside byte transfer - error twi_onTwipEvent(TW_BUS_ERROR); - twip_mode = TWIPM_WAIT; - twip_state = TWIP_BUS_ERR; + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; } else { // during first bit in byte transfer - ok SCL_LOW(); // clock stretching twi_onTwipEvent(TW_SR_STOP); if (sda) { // STOP - ets_timer_disarm(&timer); - twip_state = TWIP_IDLE; - twip_mode = TWIPM_IDLE; + ets_timer_disarm(&twi.timer); + twi.twip_state = TWIP_IDLE; + twi.twip_mode = TWIPM_IDLE; } else { // START - bitCount = 8; - ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms - twip_state = TWIP_REP_START; - twip_mode = TWIPM_IDLE; + twi.bitCount = 8; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms + twi.twip_state = TWIP_REP_START; + twi.twip_mode = TWIPM_IDLE; } } } From 52bf8ac21e8c490018f2f258e155a96ae9e0e58a Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Thu, 25 Jul 2019 09:22:19 -0700 Subject: [PATCH 10/20] 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. --- cores/esp8266/core_esp8266_si2c.cpp | 55 ++++++++++------------------- 1 file changed, 18 insertions(+), 37 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index e31768fa9f..9e97ececbe 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -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" +// 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; @@ -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]; @@ -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); @@ -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 @@ -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) From 31e54a024daea1dad99bfd205639667be9b1a3b3 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Thu, 25 Jul 2019 09:29:28 -0700 Subject: [PATCH 11/20] Save 4 heap bytes by reprdering struct --- cores/esp8266/core_esp8266_si2c.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 9e97ececbe..a4f5343fc0 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -44,9 +44,10 @@ 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_sda; + unsigned char twi_scl; unsigned char twi_addr;// = 0; + uint32_t twi_clockStretchLimit; volatile twipModeType twip_mode;// = TWIPM_IDLE; From fd8b6c503fb25e4a548162f48f3da5234fb46e45 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 26 Jul 2019 10:26:02 -0700 Subject: [PATCH 12/20] Convert to C++ class, clean up code Convert the entire file into a C++ class (with C wrappers to preserve the ABI). This allows for setting individual values of the global struct(class) in-situ instead of a cryptic list at the end of the struct definition. It also removes a lot of redundant `twi.`s from most class members. Clean up the code by converting from `#defines` to inline functions, get rid of ternarys-as-ifs, use real enums, etc. For slave_receiver.ino, the numbers are: GIT Master IRAM: 0x723c This push IRAM: 0x6fc0 For a savings of 636 total IRAM bytes (note, there may be a slight flash text increase, but we have 1MB of flash to work with and only 32K of IRAM so the tradeoff makes sense. --- cores/esp8266/core_esp8266_si2c.cpp | 502 ++++++++++++++++------------ 1 file changed, 285 insertions(+), 217 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index a4f5343fc0..21689e358b 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -27,77 +27,90 @@ extern "C" { #include "twi_util.h" - #include "ets_sys.h" +}; -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmissing-field-initializers" - -// 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; - unsigned char twi_scl; - unsigned char twi_addr;// = 0; - uint32_t twi_clockStretchLimit; - +// Implement as a class to reduce code size by allowing access to many global variables with a single base pointer +class Twi { +private: + unsigned int preferred_si2c_clock = 100000; + unsigned char twi_dcount = 18; + unsigned char twi_sda = 0; + unsigned char twi_scl = 0; + unsigned char twi_addr = 0; + uint32_t twi_clockStretchLimit = 0; - volatile twipModeType twip_mode;// = TWIPM_IDLE; - volatile twipStateType twip_state;// = TWIP_IDLE; - volatile int twip_status;// = TW_NO_INFO; - volatile int bitCount;// = 0; + volatile enum { TWIPM_UNKNOWN = 0, TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twip_mode = TWIPM_IDLE; + volatile 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 } twip_state = TWIP_IDLE; + volatile int twip_status = TW_NO_INFO; + volatile int bitCount = 0; -#define TWDR twi.twi_data - volatile uint8_t twi_data;// = 0x00; - volatile int twi_ack;// = 0; - volatile int twi_ack_rec;// = 0; - volatile int twi_timeout_ms;// = 10; + volatile uint8_t twi_data = 0x00; + volatile int twi_ack = 0; + volatile int twi_ack_rec = 0; + volatile int twi_timeout_ms = 10; - volatile twiStateType twi_state;// = TWI_READY; - volatile uint8_t twi_error;// = 0xFF; + volatile enum { TWI_READY=0, TWI_MRX, TWI_MTX, TWI_SRX, TWI_STX } twi_state = TWI_READY; + volatile uint8_t twi_error = 0xFF; uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; - volatile int twi_txBufferIndex; - volatile int twi_txBufferLength; + volatile int twi_txBufferIndex = 0; + volatile int twi_txBufferLength = 0; uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; - volatile int twi_rxBufferIndex; + volatile int twi_rxBufferIndex = 0; void (*twi_onSlaveTransmit)(void); void (*twi_onSlaveReceive)(uint8_t*, size_t); -#define EVENTTASK_QUEUE_SIZE 1 -#define EVENTTASK_QUEUE_PRIO 2 - -#define TWI_SIG_RANGE 0x00000100 -#define TWI_SIG_RX (TWI_SIG_RANGE + 0x01) -#define TWI_SIG_TX (TWI_SIG_RANGE + 0x02) - + // ETS queue/timer interfaces + enum { EVENTTASK_QUEUE_SIZE = 1, EVENTTASK_QUEUE_PRIO = 2 }; + enum { TWI_SIG_RANGE = 0x00000100, TWI_SIG_RX = 0x00000101, TWI_SIG_TX = 0x00000102 }; ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE]; ETSTimer timer; -} 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); -static void onSdaChange(void); -static void eventTask(ETSEvent *e); -static void onTimer(void *unused); + // Event/IRQ callbacks, so they can't use "this" and need to be static + static void ICACHE_RAM_ATTR onSclChange(void); + static void ICACHE_RAM_ATTR onSdaChange(void); + static void eventTask(ETSEvent *e); + static void ICACHE_RAM_ATTR onTimer(void *unused); + + // Internal use functions + void ICACHE_RAM_ATTR delay(unsigned char v); + bool write_start(void); + bool write_stop(void); + bool write_bit(bool bit); + bool read_bit(void); + bool write_byte(unsigned char byte); + unsigned char read_byte(bool nack); + void ICACHE_RAM_ATTR onTwipEvent(uint8_t status); + + // Inline helpers + inline void SDA_LOW() { GPES = (1 << twi_sda); } + inline void SDA_HIGH() { GPEC = (1 << twi_sda); } + inline bool SDA_READ() { return (GPI & (1 << twi_sda)) != 0; } + inline void SCL_LOW() { GPES = (1 << twi_scl); } + inline void SCL_HIGH() { GPEC = (1 << twi_scl); } + inline bool SCL_READ() { return (GPI & (1 << twi_scl)) != 0; } + +public: + void setClock(unsigned int freq); + void setClockStretchLimit(uint32_t limit); + void init(unsigned char sda, unsigned char scl); + void setAddress(uint8_t address); + unsigned char writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop); + unsigned char readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop); + uint8_t status(); + uint8_t transmit(const uint8_t* data, uint8_t length); + void attachSlaveRxEvent( void (*function)(uint8_t*, size_t) ); + void attachSlaveTxEvent( void (*function)(void) ); + inline void ICACHE_RAM_ATTR reply(uint8_t ack); + inline void ICACHE_RAM_ATTR stop(void); + inline void ICACHE_RAM_ATTR releaseBus(void); +}; -#define SDA_LOW() (GPES = (1 << twi.twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low) -#define SDA_HIGH() (GPEC = (1 << twi.twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high) -#define SDA_READ() ((GPI & (1 << twi.twi_sda)) != 0) -#define SCL_LOW() (GPES = (1 << twi.twi_scl)) -#define SCL_HIGH() (GPEC = (1 << twi.twi_scl)) -#define SCL_READ() ((GPI & (1 << twi.twi_scl)) != 0) +static Twi twi; #ifndef FCPU80 #define FCPU80 80000000L @@ -109,60 +122,59 @@ static void onTimer(void *unused); #define TWI_CLOCK_STRETCH_MULTIPLIER 6 #endif -void twi_setClock(unsigned int freq){ - twi.preferred_si2c_clock = freq; +void Twi::setClock(unsigned int freq) { + 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 - else if(freq <= 200000) twi.twi_dcount = 8;//about 200KHz - else if(freq <= 300000) twi.twi_dcount = 3;//about 300KHz - else if(freq <= 400000) twi.twi_dcount = 1;//about 400KHz - else twi.twi_dcount = 1;//about 400KHz + if(freq <= 50000) twi_dcount = 38;//about 50KHz + else if(freq <= 100000) twi_dcount = 19;//about 100KHz + else if(freq <= 200000) twi_dcount = 8;//about 200KHz + else if(freq <= 300000) twi_dcount = 3;//about 300KHz + else if(freq <= 400000) twi_dcount = 1;//about 400KHz + else twi_dcount = 1;//about 400KHz #else - if(freq <= 50000) twi.twi_dcount = 64;//about 50KHz - else if(freq <= 100000) twi.twi_dcount = 32;//about 100KHz - else if(freq <= 200000) twi.twi_dcount = 14;//about 200KHz - else if(freq <= 300000) twi.twi_dcount = 8;//about 300KHz - else if(freq <= 400000) twi.twi_dcount = 5;//about 400KHz - else if(freq <= 500000) twi.twi_dcount = 3;//about 500KHz - else if(freq <= 600000) twi.twi_dcount = 2;//about 600KHz - else twi.twi_dcount = 1;//about 700KHz + if(freq <= 50000) twi_dcount = 64;//about 50KHz + else if(freq <= 100000) twi_dcount = 32;//about 100KHz + else if(freq <= 200000) twi_dcount = 14;//about 200KHz + else if(freq <= 300000) twi_dcount = 8;//about 300KHz + else if(freq <= 400000) twi_dcount = 5;//about 400KHz + else if(freq <= 500000) twi_dcount = 3;//about 500KHz + else if(freq <= 600000) twi_dcount = 2;//about 600KHz + else twi_dcount = 1;//about 700KHz #endif } -void twi_setClockStretchLimit(uint32_t limit){ - twi.twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER; +void Twi::setClockStretchLimit(uint32_t limit) { + twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER; } -void twi_init(unsigned char sda, unsigned char scl) -{ +void Twi::init(unsigned char sda, unsigned char scl) { // set timer function - ets_timer_setfn(&twi.timer, onTimer, NULL); + ets_timer_setfn(&timer, onTimer, NULL); // create event task - ets_task(eventTask, EVENTTASK_QUEUE_PRIO, twi.eventTaskQueue, EVENTTASK_QUEUE_SIZE); + ets_task(eventTask, EVENTTASK_QUEUE_PRIO, eventTaskQueue, EVENTTASK_QUEUE_SIZE); - twi.twi_sda = sda; - twi.twi_scl = scl; - pinMode(twi.twi_sda, INPUT_PULLUP); - pinMode(twi.twi_scl, INPUT_PULLUP); - twi_setClock(twi.preferred_si2c_clock); + twi_sda = sda; + twi_scl = scl; + pinMode(twi_sda, INPUT_PULLUP); + pinMode(twi_scl, INPUT_PULLUP); + twi_setClock(preferred_si2c_clock); twi_setClockStretchLimit(230); // default value is 230 uS - if (twi.twi_addr != 0) + if (twi_addr != 0) { attachInterrupt(scl, onSclChange, CHANGE); attachInterrupt(sda, onSdaChange, CHANGE); } } -void twi_setAddress(uint8_t address) +void Twi::setAddress(uint8_t address) { // set twi slave address (skip over R/W bit) - twi.twi_addr = address << 1; + twi_addr = address << 1; } -static void ICACHE_RAM_ATTR twi_delay(unsigned char v){ +void ICACHE_RAM_ATTR Twi::delay(unsigned char v) { unsigned int i; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-but-set-variable" @@ -174,125 +186,125 @@ static void ICACHE_RAM_ATTR twi_delay(unsigned char v){ #pragma GCC diagnostic pop } -static bool twi_write_start(void) { +bool Twi::write_start(void) { SCL_HIGH(); SDA_HIGH(); if (SDA_READ() == 0) { return false; } - twi_delay(twi.twi_dcount); + delay(twi_dcount); SDA_LOW(); - twi_delay(twi.twi_dcount); + delay(twi_dcount); return true; } -static bool twi_write_stop(void){ +bool Twi::write_stop(void) { uint32_t i = 0; SCL_LOW(); SDA_LOW(); - twi_delay(twi.twi_dcount); + delay(twi_dcount); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi.twi_clockStretchLimit); // Clock stretching - twi_delay(twi.twi_dcount); + while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching + delay(twi_dcount); SDA_HIGH(); - twi_delay(twi.twi_dcount); + delay(twi_dcount); return true; } -static bool twi_write_bit(bool bit) { +bool Twi::write_bit(bool bit) { uint32_t i = 0; SCL_LOW(); if (bit) SDA_HIGH(); else SDA_LOW(); - twi_delay(twi.twi_dcount+1); + delay(twi_dcount+1); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi.twi_clockStretchLimit);// Clock stretching - twi_delay(twi.twi_dcount); + while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching + delay(twi_dcount); return true; } -static bool twi_read_bit(void) { +bool Twi::read_bit(void) { uint32_t i = 0; SCL_LOW(); SDA_HIGH(); - twi_delay(twi.twi_dcount+2); + delay(twi_dcount+2); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi.twi_clockStretchLimit);// Clock stretching + while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching bool bit = SDA_READ(); - twi_delay(twi.twi_dcount); + delay(twi_dcount); return bit; } -static bool twi_write_byte(unsigned char byte) { +bool Twi::write_byte(unsigned char byte) { unsigned char bit; for (bit = 0; bit < 8; bit++) { - twi_write_bit(byte & 0x80); + write_bit(byte & 0x80); byte <<= 1; } - return !twi_read_bit();//NACK/ACK + return !read_bit();//NACK/ACK } -static unsigned char twi_read_byte(bool nack) { +unsigned char Twi::read_byte(bool nack) { unsigned char byte = 0; unsigned char bit; - for (bit = 0; bit < 8; bit++) byte = (byte << 1) | twi_read_bit(); - twi_write_bit(nack); + for (bit = 0; bit < 8; bit++) byte = (byte << 1) | read_bit(); + write_bit(nack); return byte; } -unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){ +unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) { unsigned int i; - if(!twi_write_start()) return 4;//line busy - if(!twi_write_byte(((address << 1) | 0) & 0xFF)) { - if (sendStop) twi_write_stop(); + if(!write_start()) return 4;//line busy + if(!write_byte(((address << 1) | 0) & 0xFF)) { + if (sendStop) write_stop(); return 2; //received NACK on transmit of address } for(i=0; i 0) { // if SDA low, read the bits slaves have to sent to a max - twi_read_bit(); + read_bit(); if (SCL_READ() == 0) { return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time } @@ -300,14 +312,13 @@ uint8_t twi_status() { if (SDA_READ() == 0) return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. - if (!twi_write_start()) + if (!write_start()) return I2C_SDA_HELD_LOW_AFTER_INIT; // line busy. SDA again held low by another device. 2nd master? return I2C_OK; } -uint8_t twi_transmit(const uint8_t* data, uint8_t length) -{ +uint8_t Twi::transmit(const uint8_t* data, uint8_t length) { uint8_t i; // ensure data will fit into buffer @@ -316,71 +327,65 @@ uint8_t twi_transmit(const uint8_t* data, uint8_t length) } // ensure we are currently a slave transmitter - if (twi.twi_state != TWI_STX) { + if (twi_state != TWI_STX) { return 2; } // set length and copy data into tx buffer - twi.twi_txBufferLength = length; + twi_txBufferLength = length; for (i = 0; i < length; ++i) { - twi.twi_txBuffer[i] = data[i]; + twi_txBuffer[i] = data[i]; } return 0; } -void twi_attachSlaveRxEvent( void (*function)(uint8_t*, size_t) ) -{ - twi.twi_onSlaveReceive = function; +void Twi::attachSlaveRxEvent( void (*function)(uint8_t*, size_t) ) { + twi_onSlaveReceive = function; } -void twi_attachSlaveTxEvent( void (*function)(void) ) -{ - twi.twi_onSlaveTransmit = function; +void Twi::attachSlaveTxEvent( void (*function)(void) ) { + twi_onSlaveTransmit = function; } -inline void ICACHE_RAM_ATTR twi_reply(uint8_t ack) -{ +inline void ICACHE_RAM_ATTR Twi::reply(uint8_t ack) { // transmit master read ready signal, with or without ack if (ack) { //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); SCL_HIGH(); // _BV(TWINT) - twi.twi_ack = 1; // _BV(TWEA) + twi_ack = 1; // _BV(TWEA) } else { //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); SCL_HIGH(); // _BV(TWINT) - twi.twi_ack = 0; // ~_BV(TWEA) + twi_ack = 0; // ~_BV(TWEA) } } -inline void ICACHE_RAM_ATTR twi_stop(void) -{ +inline void ICACHE_RAM_ATTR Twi::stop(void) { // send stop condition //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); SCL_HIGH(); // _BV(TWINT) - twi.twi_ack = 1; // _BV(TWEA) - twi_delay(5); // Maybe this should be here + twi_ack = 1; // _BV(TWEA) + delay(5); // Maybe this should be here SDA_HIGH(); // _BV(TWSTO) // update twi state - twi.twi_state = TWI_READY; + twi_state = TWI_READY; } -inline void ICACHE_RAM_ATTR twi_releaseBus(void) -{ +inline void ICACHE_RAM_ATTR Twi::releaseBus(void) { // release bus //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); SCL_HIGH(); // _BV(TWINT) - twi.twi_ack = 1; // _BV(TWEA) + twi_ack = 1; // _BV(TWEA) SDA_HIGH(); // update twi state - twi.twi_state = TWI_READY; + twi_state = TWI_READY; } -void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status) -{ - twi.twip_status = status; +void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status) { + twip_status = status; switch(status) { // Slave Receiver case TW_SR_SLA_ACK: // addressed, returned ack @@ -388,51 +393,51 @@ void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status) case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack // enter slave receiver mode - twi.twi_state = TWI_SRX; + twi_state = TWI_SRX; // indicate that rx buffer can be overwritten and ack - twi.twi_rxBufferIndex = 0; - twi_reply(1); + twi_rxBufferIndex = 0; + reply(1); break; case TW_SR_DATA_ACK: // data received, returned ack case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack // if there is still room in the rx buffer - if(twi.twi_rxBufferIndex < TWI_BUFFER_LENGTH){ + if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ // put byte in buffer and ack - twi.twi_rxBuffer[twi.twi_rxBufferIndex++] = TWDR; - twi_reply(1); + twi_rxBuffer[twi_rxBufferIndex++] = twi_data; + reply(1); }else{ // otherwise nack - twi_reply(0); + reply(0); } break; case TW_SR_STOP: // stop or repeated start condition received // put a null char after data if there's room - if(twi.twi_rxBufferIndex < TWI_BUFFER_LENGTH){ - twi.twi_rxBuffer[twi.twi_rxBufferIndex] = '\0'; + if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ + twi_rxBuffer[twi_rxBufferIndex] = '\0'; } // callback to user-defined callback over event task to allow for non-RAM-residing code //twi_rxBufferLock = true; // This may be necessary - ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_RX, twi.twi_rxBufferIndex); + ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_RX, twi_rxBufferIndex); // since we submit rx buffer to "wire" library, we can reset it - twi.twi_rxBufferIndex = 0; + twi_rxBufferIndex = 0; break; case TW_SR_DATA_NACK: // data received, returned nack case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack // nack back at master - twi_reply(0); + reply(0); break; // Slave Transmitter case TW_ST_SLA_ACK: // addressed, returned ack case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack // enter slave transmitter mode - twi.twi_state = TWI_STX; + twi_state = TWI_STX; // ready the tx buffer index for iteration - twi.twi_txBufferIndex = 0; + twi_txBufferIndex = 0; // set tx buffer length to be zero, to verify if user changes it - twi.twi_txBufferLength = 0; + twi_txBufferLength = 0; // callback to user-defined callback over event task to allow for non-RAM-residing code // request for txBuffer to be filled and length to be set // note: user must call twi_transmit(bytes, length) to do this @@ -441,46 +446,50 @@ void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status) case TW_ST_DATA_ACK: // byte sent, ack returned // copy data to output register - TWDR = twi.twi_txBuffer[twi.twi_txBufferIndex++]; + twi_data = twi_txBuffer[twi_txBufferIndex++]; - twi.bitCount = 8; - twi.bitCount--; - (twi.twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); - twi.twi_data <<= 1; + bitCount = 8; + bitCount--; + if (twi_data & 0x80) { + SDA_HIGH(); + } else { + SDA_LOW(); + } + twi_data <<= 1; // if there is more to send, ack, otherwise nack - if(twi.twi_txBufferIndex < twi.twi_txBufferLength){ - twi_reply(1); + if(twi_txBufferIndex < twi_txBufferLength){ + reply(1); }else{ - twi_reply(0); + reply(0); } break; case TW_ST_DATA_NACK: // received nack, we are done case TW_ST_LAST_DATA: // received ack, but we are done already! // leave slave receiver state - twi_releaseBus(); + releaseBus(); break; // All case TW_NO_INFO: // no state information break; case TW_BUS_ERROR: // bus error, illegal stop/start - twi.twi_error = TW_BUS_ERROR; - twi_stop(); + twi_error = TW_BUS_ERROR; + stop(); break; } } -void ICACHE_RAM_ATTR onTimer(void *unused) +void ICACHE_RAM_ATTR Twi::onTimer(void *unused) { (void)unused; - twi_releaseBus(); - twi_onTwipEvent(TW_BUS_ERROR); + twi.releaseBus(); + twi.onTwipEvent(TW_BUS_ERROR); twi.twip_mode = TWIPM_WAIT; twi.twip_state = TWIP_BUS_ERR; } -static void eventTask(ETSEvent *e) +void Twi::eventTask(ETSEvent *e) { if (e == NULL) { @@ -499,13 +508,13 @@ static void eventTask(ETSEvent *e) } // Initiate transmission - twi_onTwipEvent(TW_ST_DATA_ACK); + twi.onTwipEvent(TW_ST_DATA_ACK); break; case TWI_SIG_RX: // ack future responses and leave slave receiver state - twi_releaseBus(); + twi.releaseBus(); twi.twi_onSlaveReceive(twi.twi_rxBuffer, e->par); break; } @@ -519,13 +528,13 @@ static void eventTask(ETSEvent *e) // Shorthand for if the state is any of the or'd bitmask x #define IFSTATE(x) if (twip_state_mask & (x)) -void ICACHE_RAM_ATTR onSclChange(void) +void ICACHE_RAM_ATTR Twi::onSclChange(void) { unsigned int sda; unsigned int scl; - sda = SDA_READ(); - scl = SCL_READ(); + sda = twi.SDA_READ(); + scl = twi.SCL_READ(); twi.twip_status = 0xF8; // reset TWI status @@ -552,13 +561,13 @@ void ICACHE_RAM_ATTR onSclChange(void) if ((twi.twi_data & 0xFE) != twi.twi_addr) { // ignore } else { - SDA_LOW(); + twi.SDA_LOW(); } } else { if (!twi.twi_ack) { // ignore } else { - SDA_LOW(); + twi.SDA_LOW(); } } twi.twip_state = TWIP_WAIT_ACK; @@ -569,30 +578,30 @@ void ICACHE_RAM_ATTR onSclChange(void) } else { if (twi.twip_mode == TWIPM_IDLE) { if ((twi.twi_data & 0xFE) != twi.twi_addr) { - SDA_HIGH(); + twi.SDA_HIGH(); twi.twip_state = TWIP_WAIT_STOP; } else { - SCL_LOW(); // clock stretching - SDA_HIGH(); + twi.SCL_LOW(); // clock stretching + twi.SDA_HIGH(); twi.twip_mode = TWIPM_ADDRESSED; if (!(twi.twi_data & 0x01)) { - twi_onTwipEvent(TW_SR_SLA_ACK); + twi.onTwipEvent(TW_SR_SLA_ACK); twi.bitCount = 8; twi.twip_state = TWIP_SLA_W; } else { - twi_onTwipEvent(TW_ST_SLA_ACK); + twi.onTwipEvent(TW_ST_SLA_ACK); twi.twip_state = TWIP_SLA_R; } } } else { - SCL_LOW(); // clock stretching - SDA_HIGH(); + twi.SCL_LOW(); // clock stretching + twi.SDA_HIGH(); if (!twi.twi_ack) { - twi_onTwipEvent(TW_SR_DATA_NACK); + twi.onTwipEvent(TW_SR_DATA_NACK); twi.twip_mode = TWIPM_WAIT; twi.twip_state = TWIP_WAIT_STOP; } else { - twi_onTwipEvent(TW_SR_DATA_ACK); + twi.onTwipEvent(TW_SR_DATA_ACK); twi.bitCount = 8; twi.twip_state = TWIP_READ; } @@ -603,7 +612,11 @@ void ICACHE_RAM_ATTR onSclChange(void) // ignore } else { twi.bitCount--; - (twi.twi_data & 0x80) ? SDA_HIGH() : SDA_LOW(); + if (twi.twi_data & 0x80) { + twi.SDA_HIGH(); + } else { + twi.SDA_LOW(); + } twi.twi_data <<= 1; if (twi.bitCount != 0) { @@ -616,7 +629,7 @@ void ICACHE_RAM_ATTR onSclChange(void) if (scl) { // ignore } else { - SDA_HIGH(); + twi.SDA_HIGH(); twi.twip_state = TWIP_READ_ACK; } } else IFSTATE(S2M(TWIP_READ_ACK)) { @@ -630,13 +643,13 @@ void ICACHE_RAM_ATTR onSclChange(void) if (scl) { // ignore } else { - SCL_LOW(); // clock stretching + twi.SCL_LOW(); // clock stretching if (twi.twi_ack && twi.twi_ack_rec) { - twi_onTwipEvent(TW_ST_DATA_ACK); + twi.onTwipEvent(TW_ST_DATA_ACK); twi.twip_state = TWIP_WRITE; } else { // we have no more data to send and/or the master doesn't want anymore - twi_onTwipEvent(twi.twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); + twi.onTwipEvent(twi.twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); twi.twip_mode = TWIPM_WAIT; twi.twip_state = TWIP_WAIT_STOP; } @@ -644,12 +657,12 @@ void ICACHE_RAM_ATTR onSclChange(void) } } -void ICACHE_RAM_ATTR onSdaChange(void) +void ICACHE_RAM_ATTR Twi::onSdaChange(void) { unsigned int sda; unsigned int scl; - sda = SDA_READ(); - scl = SCL_READ(); + sda = twi.SDA_READ(); + scl = twi.SCL_READ(); int twip_state_mask = S2M(twi.twip_state); if (scl) { /* !DATA */ @@ -664,18 +677,18 @@ void ICACHE_RAM_ATTR onSdaChange(void) } } 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)) { // START or STOP - SDA_HIGH(); // Should not be necessary - twi_onTwipEvent(TW_BUS_ERROR); + twi.SDA_HIGH(); // Should not be necessary + twi.onTwipEvent(TW_BUS_ERROR); twi.twip_mode = TWIPM_WAIT; twi.twip_state = TWIP_BUS_ERR; } else IFSTATE(S2M(TWIP_WAIT_STOP)|S2M(TWIP_BUS_ERR)) { if (sda) { // STOP - SCL_LOW(); // clock stretching + twi.SCL_LOW(); // clock stretching ets_timer_disarm(&twi.timer); twi.twip_state = TWIP_IDLE; twi.twip_mode = TWIPM_IDLE; - SCL_HIGH(); + twi.SCL_HIGH(); } else { // START if (twi.twip_state == TWIP_BUS_ERR) { @@ -690,13 +703,13 @@ void ICACHE_RAM_ATTR onSdaChange(void) // START or STOP if (twi.bitCount != 7) { // inside byte transfer - error - twi_onTwipEvent(TW_BUS_ERROR); + twi.onTwipEvent(TW_BUS_ERROR); twi.twip_mode = TWIPM_WAIT; twi.twip_state = TWIP_BUS_ERR; } else { // during first bit in byte transfer - ok - SCL_LOW(); // clock stretching - twi_onTwipEvent(TW_SR_STOP); + twi.SCL_LOW(); // clock stretching + twi.onTwipEvent(TW_SR_STOP); if (sda) { // STOP ets_timer_disarm(&twi.timer); @@ -714,4 +727,59 @@ void ICACHE_RAM_ATTR onSdaChange(void) } } +// C wrappers for the object, since API is exposed only as C +extern "C" { + +void twi_init(unsigned char sda, unsigned char scl) { + return twi.init(sda, scl); +} + +void twi_setAddress(uint8_t a) { + return twi.setAddress(a); +} + +void twi_stop(void) { + twi.stop(); +} + +void twi_setClock(unsigned int freq) { + twi.setClock(freq); +} + +void twi_setClockStretchLimit(uint32_t limit) { + twi.setClockStretchLimit(limit); +} + +uint8_t twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) { + return twi.writeTo(address, buf, len, sendStop); +} + +uint8_t twi_readFrom(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) { + return twi.readFrom(address, buf, len, sendStop); +} + +uint8_t twi_status() { + return twi.status(); +} + +uint8_t twi_transmit(const uint8_t * buf, uint8_t len) { + return twi.transmit(buf, len); +} + +void twi_attachSlaveRxEvent( void (*cb)(uint8_t*, size_t) ) { + twi.attachSlaveRxEvent(cb); +} + +void twi_attachSlaveTxEvent( void (*cb)(void) ) { + twi.attachSlaveTxEvent(cb); +} + +void twi_reply(uint8_t r) { + twi.reply(r); +} + +void twi_releaseBus(void) { + twi.releaseBus(); +} + }; From 0cdca29d6087552e521f54a09364a3f55ad07b97 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 26 Jul 2019 10:40:46 -0700 Subject: [PATCH 13/20] Run astyle core.conf, clean up space/tab/etc. Since the C++ version has significant text differences anyway, now is a good time to clean up the mess of spaces, tabs, and differing cuddles. --- cores/esp8266/core_esp8266_si2c.cpp | 1479 ++++++++++++++++----------- 1 file changed, 866 insertions(+), 613 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 21689e358b..8971a422cc 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -1,23 +1,23 @@ /* - si2c.c - Software I2C library for esp8266 + si2c.c - Software I2C library for esp8266 - Copyright (c) 2015 Hristo Gochkov. All rights reserved. - This file is part of the esp8266 core for Arduino environment. + Copyright (c) 2015 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Modified January 2017 by Bjorn Hammarberg (bjoham@esp8266.com) - i2c slave support */ #include "twi.h" #include "pins_arduino.h" @@ -32,82 +32,101 @@ extern "C" { // Implement as a class to reduce code size by allowing access to many global variables with a single base pointer -class Twi { +class Twi +{ private: - unsigned int preferred_si2c_clock = 100000; - unsigned char twi_dcount = 18; - unsigned char twi_sda = 0; - unsigned char twi_scl = 0; - unsigned char twi_addr = 0; - uint32_t twi_clockStretchLimit = 0; - - volatile enum { TWIPM_UNKNOWN = 0, TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twip_mode = TWIPM_IDLE; - volatile 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 } twip_state = TWIP_IDLE; - volatile int twip_status = TW_NO_INFO; - volatile int bitCount = 0; - - volatile uint8_t twi_data = 0x00; - volatile int twi_ack = 0; - volatile int twi_ack_rec = 0; - volatile int twi_timeout_ms = 10; - - volatile enum { TWI_READY=0, TWI_MRX, TWI_MTX, TWI_SRX, TWI_STX } twi_state = TWI_READY; - volatile uint8_t twi_error = 0xFF; - - uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; - volatile int twi_txBufferIndex = 0; - volatile int twi_txBufferLength = 0; - - uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; - volatile int twi_rxBufferIndex = 0; - - void (*twi_onSlaveTransmit)(void); - void (*twi_onSlaveReceive)(uint8_t*, size_t); - - // ETS queue/timer interfaces - enum { EVENTTASK_QUEUE_SIZE = 1, EVENTTASK_QUEUE_PRIO = 2 }; - enum { TWI_SIG_RANGE = 0x00000100, TWI_SIG_RX = 0x00000101, TWI_SIG_TX = 0x00000102 }; - ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE]; - ETSTimer timer; - - // Event/IRQ callbacks, so they can't use "this" and need to be static - static void ICACHE_RAM_ATTR onSclChange(void); - static void ICACHE_RAM_ATTR onSdaChange(void); - static void eventTask(ETSEvent *e); - static void ICACHE_RAM_ATTR onTimer(void *unused); - - // Internal use functions - void ICACHE_RAM_ATTR delay(unsigned char v); - bool write_start(void); - bool write_stop(void); - bool write_bit(bool bit); - bool read_bit(void); - bool write_byte(unsigned char byte); - unsigned char read_byte(bool nack); - void ICACHE_RAM_ATTR onTwipEvent(uint8_t status); - - // Inline helpers - inline void SDA_LOW() { GPES = (1 << twi_sda); } - inline void SDA_HIGH() { GPEC = (1 << twi_sda); } - inline bool SDA_READ() { return (GPI & (1 << twi_sda)) != 0; } - inline void SCL_LOW() { GPES = (1 << twi_scl); } - inline void SCL_HIGH() { GPEC = (1 << twi_scl); } - inline bool SCL_READ() { return (GPI & (1 << twi_scl)) != 0; } + unsigned int preferred_si2c_clock = 100000; + unsigned char twi_dcount = 18; + unsigned char twi_sda = 0; + unsigned char twi_scl = 0; + unsigned char twi_addr = 0; + uint32_t twi_clockStretchLimit = 0; + + volatile enum { TWIPM_UNKNOWN = 0, TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twip_mode = TWIPM_IDLE; + volatile 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 } twip_state = TWIP_IDLE; + volatile int twip_status = TW_NO_INFO; + volatile int bitCount = 0; + + volatile uint8_t twi_data = 0x00; + volatile int twi_ack = 0; + volatile int twi_ack_rec = 0; + volatile int twi_timeout_ms = 10; + + volatile enum { TWI_READY = 0, TWI_MRX, TWI_MTX, TWI_SRX, TWI_STX } twi_state = TWI_READY; + volatile uint8_t twi_error = 0xFF; + + uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; + volatile int twi_txBufferIndex = 0; + volatile int twi_txBufferLength = 0; + + uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; + volatile int twi_rxBufferIndex = 0; + + void (*twi_onSlaveTransmit)(void); + void (*twi_onSlaveReceive)(uint8_t*, size_t); + + // ETS queue/timer interfaces + enum { EVENTTASK_QUEUE_SIZE = 1, EVENTTASK_QUEUE_PRIO = 2 }; + enum { TWI_SIG_RANGE = 0x00000100, TWI_SIG_RX = 0x00000101, TWI_SIG_TX = 0x00000102 }; + ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE]; + ETSTimer timer; + + // Event/IRQ callbacks, so they can't use "this" and need to be static + static void ICACHE_RAM_ATTR onSclChange(void); + static void ICACHE_RAM_ATTR onSdaChange(void); + static void eventTask(ETSEvent *e); + static void ICACHE_RAM_ATTR onTimer(void *unused); + + // Internal use functions + void ICACHE_RAM_ATTR delay(unsigned char v); + bool write_start(void); + bool write_stop(void); + bool write_bit(bool bit); + bool read_bit(void); + bool write_byte(unsigned char byte); + unsigned char read_byte(bool nack); + void ICACHE_RAM_ATTR onTwipEvent(uint8_t status); + + // Inline helpers + inline void SDA_LOW() + { + GPES = (1 << twi_sda); + } + inline void SDA_HIGH() + { + GPEC = (1 << twi_sda); + } + inline bool SDA_READ() + { + return (GPI & (1 << twi_sda)) != 0; + } + inline void SCL_LOW() + { + GPES = (1 << twi_scl); + } + inline void SCL_HIGH() + { + GPEC = (1 << twi_scl); + } + inline bool SCL_READ() + { + return (GPI & (1 << twi_scl)) != 0; + } public: - void setClock(unsigned int freq); - void setClockStretchLimit(uint32_t limit); - void init(unsigned char sda, unsigned char scl); - void setAddress(uint8_t address); - unsigned char writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop); - unsigned char readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop); - uint8_t status(); - uint8_t transmit(const uint8_t* data, uint8_t length); - void attachSlaveRxEvent( void (*function)(uint8_t*, size_t) ); - void attachSlaveTxEvent( void (*function)(void) ); - inline void ICACHE_RAM_ATTR reply(uint8_t ack); - inline void ICACHE_RAM_ATTR stop(void); - inline void ICACHE_RAM_ATTR releaseBus(void); + void setClock(unsigned int freq); + void setClockStretchLimit(uint32_t limit); + void init(unsigned char sda, unsigned char scl); + void setAddress(uint8_t address); + unsigned char writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop); + unsigned char readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop); + uint8_t status(); + uint8_t transmit(const uint8_t* data, uint8_t length); + void attachSlaveRxEvent(void (*function)(uint8_t*, size_t)); + void attachSlaveTxEvent(void (*function)(void)); + inline void ICACHE_RAM_ATTR reply(uint8_t ack); + inline void ICACHE_RAM_ATTR stop(void); + inline void ICACHE_RAM_ATTR releaseBus(void); }; static Twi twi; @@ -122,402 +141,533 @@ static Twi twi; #define TWI_CLOCK_STRETCH_MULTIPLIER 6 #endif -void Twi::setClock(unsigned int freq) { - preferred_si2c_clock = freq; +void Twi::setClock(unsigned int freq) +{ + preferred_si2c_clock = freq; #if F_CPU == FCPU80 - if(freq <= 50000) twi_dcount = 38;//about 50KHz - else if(freq <= 100000) twi_dcount = 19;//about 100KHz - else if(freq <= 200000) twi_dcount = 8;//about 200KHz - else if(freq <= 300000) twi_dcount = 3;//about 300KHz - else if(freq <= 400000) twi_dcount = 1;//about 400KHz - else twi_dcount = 1;//about 400KHz + if (freq <= 50000) + { + twi_dcount = 38; //about 50KHz + } + else if (freq <= 100000) + { + twi_dcount = 19; //about 100KHz + } + else if (freq <= 200000) + { + twi_dcount = 8; //about 200KHz + } + else if (freq <= 300000) + { + twi_dcount = 3; //about 300KHz + } + else if (freq <= 400000) + { + twi_dcount = 1; //about 400KHz + } + else + { + twi_dcount = 1; //about 400KHz + } #else - if(freq <= 50000) twi_dcount = 64;//about 50KHz - else if(freq <= 100000) twi_dcount = 32;//about 100KHz - else if(freq <= 200000) twi_dcount = 14;//about 200KHz - else if(freq <= 300000) twi_dcount = 8;//about 300KHz - else if(freq <= 400000) twi_dcount = 5;//about 400KHz - else if(freq <= 500000) twi_dcount = 3;//about 500KHz - else if(freq <= 600000) twi_dcount = 2;//about 600KHz - else twi_dcount = 1;//about 700KHz + if (freq <= 50000) + { + twi_dcount = 64; //about 50KHz + } + else if (freq <= 100000) + { + twi_dcount = 32; //about 100KHz + } + else if (freq <= 200000) + { + twi_dcount = 14; //about 200KHz + } + else if (freq <= 300000) + { + twi_dcount = 8; //about 300KHz + } + else if (freq <= 400000) + { + twi_dcount = 5; //about 400KHz + } + else if (freq <= 500000) + { + twi_dcount = 3; //about 500KHz + } + else if (freq <= 600000) + { + twi_dcount = 2; //about 600KHz + } + else + { + twi_dcount = 1; //about 700KHz + } #endif } -void Twi::setClockStretchLimit(uint32_t limit) { - twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER; +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 - ets_timer_setfn(&timer, onTimer, NULL); - - // create event task - ets_task(eventTask, EVENTTASK_QUEUE_PRIO, eventTaskQueue, EVENTTASK_QUEUE_SIZE); - - twi_sda = sda; - twi_scl = scl; - pinMode(twi_sda, INPUT_PULLUP); - 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::init(unsigned char sda, unsigned char scl) +{ + // set timer function + ets_timer_setfn(&timer, onTimer, NULL); + + // create event task + ets_task(eventTask, EVENTTASK_QUEUE_PRIO, eventTaskQueue, EVENTTASK_QUEUE_SIZE); + + twi_sda = sda; + twi_scl = scl; + pinMode(twi_sda, INPUT_PULLUP); + 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) { - // set twi slave address (skip over R/W bit) - twi_addr = address << 1; + // set twi slave address (skip over R/W bit) + twi_addr = address << 1; } -void ICACHE_RAM_ATTR Twi::delay(unsigned char v) { - unsigned int i; +void ICACHE_RAM_ATTR Twi::delay(unsigned char v) +{ + unsigned int i; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-but-set-variable" - unsigned int reg; - for (i = 0; i < v; i++) { - reg = GPI; - } - (void)reg; + unsigned int reg; + for (i = 0; i < v; i++) + { + reg = GPI; + } + (void)reg; #pragma GCC diagnostic pop } -bool Twi::write_start(void) { - SCL_HIGH(); - SDA_HIGH(); - if (SDA_READ() == 0) { - return false; - } - delay(twi_dcount); - SDA_LOW(); - delay(twi_dcount); - return true; +bool Twi::write_start(void) +{ + SCL_HIGH(); + SDA_HIGH(); + if (SDA_READ() == 0) + { + return false; + } + delay(twi_dcount); + SDA_LOW(); + delay(twi_dcount); + return true; } -bool Twi::write_stop(void) { - uint32_t i = 0; - SCL_LOW(); - SDA_LOW(); - delay(twi_dcount); - SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching - delay(twi_dcount); - SDA_HIGH(); - delay(twi_dcount); - return true; +bool Twi::write_stop(void) +{ + uint32_t i = 0; + SCL_LOW(); + SDA_LOW(); + delay(twi_dcount); + SCL_HIGH(); + while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching + delay(twi_dcount); + SDA_HIGH(); + delay(twi_dcount); + return true; } -bool Twi::write_bit(bool bit) { - uint32_t i = 0; - SCL_LOW(); - if (bit) SDA_HIGH(); - else SDA_LOW(); - delay(twi_dcount+1); - SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching - delay(twi_dcount); - return true; +bool Twi::write_bit(bool bit) +{ + uint32_t i = 0; + SCL_LOW(); + if (bit) + { + SDA_HIGH(); + } + else + { + SDA_LOW(); + } + delay(twi_dcount + 1); + SCL_HIGH(); + while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching + delay(twi_dcount); + return true; } -bool Twi::read_bit(void) { - uint32_t i = 0; - SCL_LOW(); - SDA_HIGH(); - delay(twi_dcount+2); - SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching - bool bit = SDA_READ(); - delay(twi_dcount); - return bit; +bool Twi::read_bit(void) +{ + uint32_t i = 0; + SCL_LOW(); + SDA_HIGH(); + delay(twi_dcount + 2); + SCL_HIGH(); + while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching + bool bit = SDA_READ(); + delay(twi_dcount); + return bit; } -bool Twi::write_byte(unsigned char byte) { - unsigned char bit; - for (bit = 0; bit < 8; bit++) { - write_bit(byte & 0x80); - byte <<= 1; - } - return !read_bit();//NACK/ACK +bool Twi::write_byte(unsigned char byte) +{ + unsigned char bit; + for (bit = 0; bit < 8; bit++) + { + write_bit(byte & 0x80); + byte <<= 1; + } + return !read_bit();//NACK/ACK } -unsigned char Twi::read_byte(bool nack) { - unsigned char byte = 0; - unsigned char bit; - for (bit = 0; bit < 8; bit++) byte = (byte << 1) | read_bit(); - write_bit(nack); - return byte; +unsigned char Twi::read_byte(bool nack) +{ + unsigned char byte = 0; + unsigned char bit; + for (bit = 0; bit < 8; bit++) + { + byte = (byte << 1) | read_bit(); + } + write_bit(nack); + return byte; } -unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) { - unsigned int i; - if(!write_start()) return 4;//line busy - if(!write_byte(((address << 1) | 0) & 0xFF)) { - if (sendStop) write_stop(); - return 2; //received NACK on transmit of address - } - for(i=0; i 0) { // if SDA low, read the bits slaves have to sent to a max + while (SDA_READ() == 0 && clockCount-- > 0) // if SDA low, read the bits slaves have to sent to a max + { read_bit(); - if (SCL_READ() == 0) { - return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time + if (SCL_READ() == 0) + { + return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time } } if (SDA_READ() == 0) - return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. - + { + return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. + } + if (!write_start()) - return I2C_SDA_HELD_LOW_AFTER_INIT; // line busy. SDA again held low by another device. 2nd master? + { + return I2C_SDA_HELD_LOW_AFTER_INIT; // line busy. SDA again held low by another device. 2nd master? + } return I2C_OK; } -uint8_t Twi::transmit(const uint8_t* data, uint8_t length) { - uint8_t i; +uint8_t Twi::transmit(const uint8_t* data, uint8_t length) +{ + uint8_t i; - // ensure data will fit into buffer - if (length > TWI_BUFFER_LENGTH) { - return 1; - } + // ensure data will fit into buffer + if (length > TWI_BUFFER_LENGTH) + { + return 1; + } - // ensure we are currently a slave transmitter - if (twi_state != TWI_STX) { - return 2; - } + // ensure we are currently a slave transmitter + if (twi_state != TWI_STX) + { + return 2; + } - // set length and copy data into tx buffer - twi_txBufferLength = length; - for (i = 0; i < length; ++i) { - twi_txBuffer[i] = data[i]; - } + // set length and copy data into tx buffer + twi_txBufferLength = length; + for (i = 0; i < length; ++i) + { + twi_txBuffer[i] = data[i]; + } - return 0; + return 0; } -void Twi::attachSlaveRxEvent( void (*function)(uint8_t*, size_t) ) { - twi_onSlaveReceive = function; +void Twi::attachSlaveRxEvent(void (*function)(uint8_t*, size_t)) +{ + twi_onSlaveReceive = function; } -void Twi::attachSlaveTxEvent( void (*function)(void) ) { - twi_onSlaveTransmit = function; +void Twi::attachSlaveTxEvent(void (*function)(void)) +{ + twi_onSlaveTransmit = function; } -inline void ICACHE_RAM_ATTR Twi::reply(uint8_t ack) { - // transmit master read ready signal, with or without ack - if (ack) { - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 1; // _BV(TWEA) - } else { - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 0; // ~_BV(TWEA) - } +inline void ICACHE_RAM_ATTR Twi::reply(uint8_t ack) +{ + // transmit master read ready signal, with or without ack + if (ack) + { + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 1; // _BV(TWEA) + } + else + { + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 0; // ~_BV(TWEA) + } } -inline void ICACHE_RAM_ATTR Twi::stop(void) { - // send stop condition - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 1; // _BV(TWEA) - delay(5); // Maybe this should be here - SDA_HIGH(); // _BV(TWSTO) - // update twi state - twi_state = TWI_READY; +inline void ICACHE_RAM_ATTR Twi::stop(void) +{ + // send stop condition + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 1; // _BV(TWEA) + delay(5); // Maybe this should be here + SDA_HIGH(); // _BV(TWSTO) + // update twi state + twi_state = TWI_READY; } -inline void ICACHE_RAM_ATTR Twi::releaseBus(void) { - // release bus - //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); - SCL_HIGH(); // _BV(TWINT) - twi_ack = 1; // _BV(TWEA) - SDA_HIGH(); - - // update twi state - twi_state = TWI_READY; +inline void ICACHE_RAM_ATTR Twi::releaseBus(void) +{ + // release bus + //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); + SCL_HIGH(); // _BV(TWINT) + twi_ack = 1; // _BV(TWEA) + SDA_HIGH(); + + // update twi state + twi_state = TWI_READY; } -void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status) { - twip_status = status; - switch(status) { +void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status) +{ + twip_status = status; + switch (status) + { // Slave Receiver case TW_SR_SLA_ACK: // addressed, returned ack case TW_SR_GCALL_ACK: // addressed generally, returned ack case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack - // enter slave receiver mode - twi_state = TWI_SRX; - // indicate that rx buffer can be overwritten and ack - twi_rxBufferIndex = 0; - reply(1); - break; + // enter slave receiver mode + twi_state = TWI_SRX; + // indicate that rx buffer can be overwritten and ack + twi_rxBufferIndex = 0; + reply(1); + break; case TW_SR_DATA_ACK: // data received, returned ack case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack - // if there is still room in the rx buffer - if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ - // put byte in buffer and ack - twi_rxBuffer[twi_rxBufferIndex++] = twi_data; - reply(1); - }else{ - // otherwise nack - reply(0); - } - break; + // if there is still room in the rx buffer + if (twi_rxBufferIndex < TWI_BUFFER_LENGTH) + { + // put byte in buffer and ack + twi_rxBuffer[twi_rxBufferIndex++] = twi_data; + reply(1); + } + else + { + // otherwise nack + reply(0); + } + break; case TW_SR_STOP: // stop or repeated start condition received - // put a null char after data if there's room - if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ - twi_rxBuffer[twi_rxBufferIndex] = '\0'; - } - // callback to user-defined callback over event task to allow for non-RAM-residing code - //twi_rxBufferLock = true; // This may be necessary - ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_RX, twi_rxBufferIndex); - - // since we submit rx buffer to "wire" library, we can reset it - twi_rxBufferIndex = 0; - break; + // put a null char after data if there's room + if (twi_rxBufferIndex < TWI_BUFFER_LENGTH) + { + twi_rxBuffer[twi_rxBufferIndex] = '\0'; + } + // callback to user-defined callback over event task to allow for non-RAM-residing code + //twi_rxBufferLock = true; // This may be necessary + ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_RX, twi_rxBufferIndex); + + // since we submit rx buffer to "wire" library, we can reset it + twi_rxBufferIndex = 0; + break; case TW_SR_DATA_NACK: // data received, returned nack case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack - // nack back at master - reply(0); - break; + // nack back at master + reply(0); + break; // Slave Transmitter case TW_ST_SLA_ACK: // addressed, returned ack case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack - // enter slave transmitter mode - twi_state = TWI_STX; - // ready the tx buffer index for iteration - twi_txBufferIndex = 0; - // set tx buffer length to be zero, to verify if user changes it - twi_txBufferLength = 0; - // callback to user-defined callback over event task to allow for non-RAM-residing code - // request for txBuffer to be filled and length to be set - // note: user must call twi_transmit(bytes, length) to do this - ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_TX, 0); - break; - - case TW_ST_DATA_ACK: // byte sent, ack returned - // copy data to output register - twi_data = twi_txBuffer[twi_txBufferIndex++]; - - bitCount = 8; - bitCount--; - if (twi_data & 0x80) { + // enter slave transmitter mode + twi_state = TWI_STX; + // ready the tx buffer index for iteration + twi_txBufferIndex = 0; + // set tx buffer length to be zero, to verify if user changes it + twi_txBufferLength = 0; + // callback to user-defined callback over event task to allow for non-RAM-residing code + // request for txBuffer to be filled and length to be set + // note: user must call twi_transmit(bytes, length) to do this + ets_post(EVENTTASK_QUEUE_PRIO, TWI_SIG_TX, 0); + break; + + case TW_ST_DATA_ACK: // byte sent, ack returned + // copy data to output register + twi_data = twi_txBuffer[twi_txBufferIndex++]; + + bitCount = 8; + bitCount--; + if (twi_data & 0x80) + { SDA_HIGH(); - } else { + } + else + { SDA_LOW(); - } - twi_data <<= 1; + } + twi_data <<= 1; - // if there is more to send, ack, otherwise nack - if(twi_txBufferIndex < twi_txBufferLength){ - reply(1); - }else{ - reply(0); - } - break; + // if there is more to send, ack, otherwise nack + if (twi_txBufferIndex < twi_txBufferLength) + { + reply(1); + } + else + { + reply(0); + } + break; case TW_ST_DATA_NACK: // received nack, we are done case TW_ST_LAST_DATA: // received ack, but we are done already! - // leave slave receiver state - releaseBus(); - break; + // leave slave receiver state + releaseBus(); + break; // All case TW_NO_INFO: // no state information - break; + break; case TW_BUS_ERROR: // bus error, illegal stop/start - twi_error = TW_BUS_ERROR; - stop(); - break; - } + twi_error = TW_BUS_ERROR; + stop(); + break; + } } void ICACHE_RAM_ATTR Twi::onTimer(void *unused) { - (void)unused; - twi.releaseBus(); - twi.onTwipEvent(TW_BUS_ERROR); - twi.twip_mode = TWIPM_WAIT; - twi.twip_state = TWIP_BUS_ERR; + (void)unused; + twi.releaseBus(); + twi.onTwipEvent(TW_BUS_ERROR); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; } void Twi::eventTask(ETSEvent *e) { - if (e == NULL) { - return; - } + if (e == NULL) + { + return; + } - switch (e->sig) - { - case TWI_SIG_TX: - twi.twi_onSlaveTransmit(); + switch (e->sig) + { + case TWI_SIG_TX: + twi.twi_onSlaveTransmit(); - // if they didn't change buffer & length, initialize it - if (twi.twi_txBufferLength == 0) { - twi.twi_txBufferLength = 1; - twi.twi_txBuffer[0] = 0x00; - } + // if they didn't change buffer & length, initialize it + if (twi.twi_txBufferLength == 0) + { + twi.twi_txBufferLength = 1; + twi.twi_txBuffer[0] = 0x00; + } - // Initiate transmission - twi.onTwipEvent(TW_ST_DATA_ACK); + // Initiate transmission + twi.onTwipEvent(TW_ST_DATA_ACK); - break; + break; - case TWI_SIG_RX: - // ack future responses and leave slave receiver state - twi.releaseBus(); - twi.twi_onSlaveReceive(twi.twi_rxBuffer, e->par); - break; - } + case TWI_SIG_RX: + // ack future responses and leave slave receiver state + twi.releaseBus(); + twi.twi_onSlaveReceive(twi.twi_rxBuffer, e->par); + break; + } } // The state machine is converted from a 0...15 state to a 1-hot encoded state, and then @@ -530,256 +680,359 @@ void Twi::eventTask(ETSEvent *e) void ICACHE_RAM_ATTR Twi::onSclChange(void) { - unsigned int sda; - unsigned int scl; - - sda = twi.SDA_READ(); - scl = twi.SCL_READ(); - - twi.twip_status = 0xF8; // reset TWI status - - int twip_state_mask = S2M(twi.twip_state); - IFSTATE(S2M(TWIP_START)|S2M(TWIP_REP_START)|S2M(TWIP_SLA_W)|S2M(TWIP_READ)) { - if (!scl) { - // ignore - } else { - twi.bitCount--; - twi.twi_data <<= 1; - twi.twi_data |= sda; - - if (twi.bitCount != 0) { - // continue - } else { - twi.twip_state = TWIP_SEND_ACK; - } - } - } else IFSTATE(S2M(TWIP_SEND_ACK)) { - if (scl) { - // ignore - } else { - if (twi.twip_mode == TWIPM_IDLE) { - if ((twi.twi_data & 0xFE) != twi.twi_addr) { - // ignore - } else { - twi.SDA_LOW(); - } - } else { - if (!twi.twi_ack) { - // ignore - } else { - twi.SDA_LOW(); - } - } - twi.twip_state = TWIP_WAIT_ACK; - } - } else IFSTATE(S2M(TWIP_WAIT_ACK)) { - if (scl) { - // ignore - } else { - if (twi.twip_mode == TWIPM_IDLE) { - if ((twi.twi_data & 0xFE) != twi.twi_addr) { - twi.SDA_HIGH(); - twi.twip_state = TWIP_WAIT_STOP; - } else { - twi.SCL_LOW(); // clock stretching - twi.SDA_HIGH(); - twi.twip_mode = TWIPM_ADDRESSED; - if (!(twi.twi_data & 0x01)) { - twi.onTwipEvent(TW_SR_SLA_ACK); - twi.bitCount = 8; - twi.twip_state = TWIP_SLA_W; - } else { - twi.onTwipEvent(TW_ST_SLA_ACK); - twi.twip_state = TWIP_SLA_R; - } - } - } else { - twi.SCL_LOW(); // clock stretching - twi.SDA_HIGH(); - if (!twi.twi_ack) { - twi.onTwipEvent(TW_SR_DATA_NACK); - twi.twip_mode = TWIPM_WAIT; - twi.twip_state = TWIP_WAIT_STOP; - } else { - twi.onTwipEvent(TW_SR_DATA_ACK); - twi.bitCount = 8; - twi.twip_state = TWIP_READ; - } - } - } - } else IFSTATE(S2M(TWIP_SLA_R)|S2M(TWIP_WRITE)) { - if (scl) { - // ignore - } else { - twi.bitCount--; - if (twi.twi_data & 0x80) { - twi.SDA_HIGH(); - } else { - twi.SDA_LOW(); - } - twi.twi_data <<= 1; - - if (twi.bitCount != 0) { - // continue - } else { - twi.twip_state = TWIP_REC_ACK; - } - } - } else IFSTATE(S2M(TWIP_REC_ACK)) { - if (scl) { - // ignore - } else { - twi.SDA_HIGH(); - twi.twip_state = TWIP_READ_ACK; - } - } else IFSTATE(S2M(TWIP_READ_ACK)) { - if (!scl) { - // ignore - } else { - twi.twi_ack_rec = !sda; - twi.twip_state = TWIP_RWAIT_ACK; - } - } else IFSTATE(S2M(TWIP_RWAIT_ACK)) { - if (scl) { - // ignore - } else { - twi.SCL_LOW(); // clock stretching - if (twi.twi_ack && twi.twi_ack_rec) { - twi.onTwipEvent(TW_ST_DATA_ACK); - twi.twip_state = TWIP_WRITE; - } else { - // we have no more data to send and/or the master doesn't want anymore - twi.onTwipEvent(twi.twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); - twi.twip_mode = TWIPM_WAIT; - twi.twip_state = TWIP_WAIT_STOP; - } - } - } + unsigned int sda; + unsigned int scl; + + sda = twi.SDA_READ(); + scl = twi.SCL_READ(); + + twi.twip_status = 0xF8; // reset TWI status + + int twip_state_mask = S2M(twi.twip_state); + IFSTATE(S2M(TWIP_START) | S2M(TWIP_REP_START) | S2M(TWIP_SLA_W) | S2M(TWIP_READ)) + { + if (!scl) + { + // ignore + } + else + { + twi.bitCount--; + twi.twi_data <<= 1; + twi.twi_data |= sda; + + if (twi.bitCount != 0) + { + // continue + } + else + { + twi.twip_state = TWIP_SEND_ACK; + } + } + } + else IFSTATE(S2M(TWIP_SEND_ACK)) + { + if (scl) + { + // ignore + } + else + { + if (twi.twip_mode == TWIPM_IDLE) + { + if ((twi.twi_data & 0xFE) != twi.twi_addr) + { + // ignore + } + else + { + twi.SDA_LOW(); + } + } + else + { + if (!twi.twi_ack) + { + // ignore + } + else + { + twi.SDA_LOW(); + } + } + twi.twip_state = TWIP_WAIT_ACK; + } + } + else IFSTATE(S2M(TWIP_WAIT_ACK)) + { + if (scl) + { + // ignore + } + else + { + if (twi.twip_mode == TWIPM_IDLE) + { + if ((twi.twi_data & 0xFE) != twi.twi_addr) + { + twi.SDA_HIGH(); + twi.twip_state = TWIP_WAIT_STOP; + } + else + { + twi.SCL_LOW(); // clock stretching + twi.SDA_HIGH(); + twi.twip_mode = TWIPM_ADDRESSED; + if (!(twi.twi_data & 0x01)) + { + twi.onTwipEvent(TW_SR_SLA_ACK); + twi.bitCount = 8; + twi.twip_state = TWIP_SLA_W; + } + else + { + twi.onTwipEvent(TW_ST_SLA_ACK); + twi.twip_state = TWIP_SLA_R; + } + } + } + else + { + twi.SCL_LOW(); // clock stretching + twi.SDA_HIGH(); + if (!twi.twi_ack) + { + twi.onTwipEvent(TW_SR_DATA_NACK); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_WAIT_STOP; + } + else + { + twi.onTwipEvent(TW_SR_DATA_ACK); + twi.bitCount = 8; + twi.twip_state = TWIP_READ; + } + } + } + } + else IFSTATE(S2M(TWIP_SLA_R) | S2M(TWIP_WRITE)) + { + if (scl) + { + // ignore + } + else + { + twi.bitCount--; + if (twi.twi_data & 0x80) + { + twi.SDA_HIGH(); + } + else + { + twi.SDA_LOW(); + } + twi.twi_data <<= 1; + + if (twi.bitCount != 0) + { + // continue + } + else + { + twi.twip_state = TWIP_REC_ACK; + } + } + } + else IFSTATE(S2M(TWIP_REC_ACK)) + { + if (scl) + { + // ignore + } + else + { + twi.SDA_HIGH(); + twi.twip_state = TWIP_READ_ACK; + } + } + else IFSTATE(S2M(TWIP_READ_ACK)) + { + if (!scl) + { + // ignore + } + else + { + twi.twi_ack_rec = !sda; + twi.twip_state = TWIP_RWAIT_ACK; + } + } + else IFSTATE(S2M(TWIP_RWAIT_ACK)) + { + if (scl) + { + // ignore + } + else + { + twi.SCL_LOW(); // clock stretching + if (twi.twi_ack && twi.twi_ack_rec) + { + twi.onTwipEvent(TW_ST_DATA_ACK); + twi.twip_state = TWIP_WRITE; + } + else + { + // we have no more data to send and/or the master doesn't want anymore + twi.onTwipEvent(twi.twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_WAIT_STOP; + } + } + } } void ICACHE_RAM_ATTR Twi::onSdaChange(void) { - unsigned int sda; - unsigned int scl; - sda = twi.SDA_READ(); - scl = twi.SCL_READ(); - - int twip_state_mask = S2M(twi.twip_state); - if (scl) { /* !DATA */ - IFSTATE(S2M(TWIP_IDLE)) { - if (sda) { - // STOP - ignore - } else { - // START - twi.bitCount = 8; - twi.twip_state = TWIP_START; - ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms - } - } 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)) { - // START or STOP - twi.SDA_HIGH(); // Should not be necessary - twi.onTwipEvent(TW_BUS_ERROR); - twi.twip_mode = TWIPM_WAIT; - twi.twip_state = TWIP_BUS_ERR; - } else IFSTATE(S2M(TWIP_WAIT_STOP)|S2M(TWIP_BUS_ERR)) { - if (sda) { - // STOP - twi.SCL_LOW(); // clock stretching - ets_timer_disarm(&twi.timer); - twi.twip_state = TWIP_IDLE; - twi.twip_mode = TWIPM_IDLE; - twi.SCL_HIGH(); - } else { - // START - if (twi.twip_state == TWIP_BUS_ERR) { - // ignore - } else { - twi.bitCount = 8; - twi.twip_state = TWIP_REP_START; - ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms - } - } - } else IFSTATE(S2M(TWIP_SLA_W)|S2M(TWIP_READ)) { - // START or STOP - if (twi.bitCount != 7) { - // inside byte transfer - error - twi.onTwipEvent(TW_BUS_ERROR); - twi.twip_mode = TWIPM_WAIT; - twi.twip_state = TWIP_BUS_ERR; - } else { - // during first bit in byte transfer - ok - twi.SCL_LOW(); // clock stretching - twi.onTwipEvent(TW_SR_STOP); - if (sda) { - // STOP - ets_timer_disarm(&twi.timer); - twi.twip_state = TWIP_IDLE; - twi.twip_mode = TWIPM_IDLE; - } else { - // START - twi.bitCount = 8; - ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms - twi.twip_state = TWIP_REP_START; - twi.twip_mode = TWIPM_IDLE; - } - } - } - } + unsigned int sda; + unsigned int scl; + sda = twi.SDA_READ(); + scl = twi.SCL_READ(); + + int twip_state_mask = S2M(twi.twip_state); + if (scl) /* !DATA */ + { + IFSTATE(S2M(TWIP_IDLE)) + { + if (sda) + { + // STOP - ignore + } + else + { + // START + twi.bitCount = 8; + twi.twip_state = TWIP_START; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms + } + } + 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)) + { + // START or STOP + twi.SDA_HIGH(); // Should not be necessary + twi.onTwipEvent(TW_BUS_ERROR); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; + } + else IFSTATE(S2M(TWIP_WAIT_STOP) | S2M(TWIP_BUS_ERR)) + { + if (sda) + { + // STOP + twi.SCL_LOW(); // clock stretching + ets_timer_disarm(&twi.timer); + twi.twip_state = TWIP_IDLE; + twi.twip_mode = TWIPM_IDLE; + twi.SCL_HIGH(); + } + else + { + // START + if (twi.twip_state == TWIP_BUS_ERR) + { + // ignore + } + else + { + twi.bitCount = 8; + twi.twip_state = TWIP_REP_START; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms + } + } + } + else IFSTATE(S2M(TWIP_SLA_W) | S2M(TWIP_READ)) + { + // START or STOP + if (twi.bitCount != 7) + { + // inside byte transfer - error + twi.onTwipEvent(TW_BUS_ERROR); + twi.twip_mode = TWIPM_WAIT; + twi.twip_state = TWIP_BUS_ERR; + } + else + { + // during first bit in byte transfer - ok + twi.SCL_LOW(); // clock stretching + twi.onTwipEvent(TW_SR_STOP); + if (sda) + { + // STOP + ets_timer_disarm(&twi.timer); + twi.twip_state = TWIP_IDLE; + twi.twip_mode = TWIPM_IDLE; + } + else + { + // START + twi.bitCount = 8; + ets_timer_arm_new(&twi.timer, twi.twi_timeout_ms, false, true); // Once, ms + twi.twip_state = TWIP_REP_START; + twi.twip_mode = TWIPM_IDLE; + } + } + } + } } // C wrappers for the object, since API is exposed only as C extern "C" { -void twi_init(unsigned char sda, unsigned char scl) { - return twi.init(sda, scl); -} + void twi_init(unsigned char sda, unsigned char scl) + { + return twi.init(sda, scl); + } -void twi_setAddress(uint8_t a) { - return twi.setAddress(a); -} + void twi_setAddress(uint8_t a) + { + return twi.setAddress(a); + } -void twi_stop(void) { - twi.stop(); -} + void twi_stop(void) + { + twi.stop(); + } -void twi_setClock(unsigned int freq) { - twi.setClock(freq); -} + void twi_setClock(unsigned int freq) + { + twi.setClock(freq); + } -void twi_setClockStretchLimit(uint32_t limit) { - twi.setClockStretchLimit(limit); -} + void twi_setClockStretchLimit(uint32_t limit) + { + twi.setClockStretchLimit(limit); + } -uint8_t twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) { - return twi.writeTo(address, buf, len, sendStop); -} + uint8_t twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) + { + return twi.writeTo(address, buf, len, sendStop); + } -uint8_t twi_readFrom(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) { - return twi.readFrom(address, buf, len, sendStop); -} + uint8_t twi_readFrom(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop) + { + return twi.readFrom(address, buf, len, sendStop); + } -uint8_t twi_status() { - return twi.status(); -} + uint8_t twi_status() + { + return twi.status(); + } -uint8_t twi_transmit(const uint8_t * buf, uint8_t len) { - return twi.transmit(buf, len); -} + uint8_t twi_transmit(const uint8_t * buf, uint8_t len) + { + return twi.transmit(buf, len); + } -void twi_attachSlaveRxEvent( void (*cb)(uint8_t*, size_t) ) { - twi.attachSlaveRxEvent(cb); -} + void twi_attachSlaveRxEvent(void (*cb)(uint8_t*, size_t)) + { + twi.attachSlaveRxEvent(cb); + } -void twi_attachSlaveTxEvent( void (*cb)(void) ) { - twi.attachSlaveTxEvent(cb); -} + void twi_attachSlaveTxEvent(void (*cb)(void)) + { + twi.attachSlaveTxEvent(cb); + } -void twi_reply(uint8_t r) { - twi.reply(r); -} + void twi_reply(uint8_t r) + { + twi.reply(r); + } -void twi_releaseBus(void) { - twi.releaseBus(); -} + void twi_releaseBus(void) + { + twi.releaseBus(); + } }; From 1c633582389bba3c7ecd8bc78f8ee50631a53466 Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Thu, 3 Oct 2019 14:36:10 +0200 Subject: [PATCH 14/20] Double I2C read in one transaction skips a clock pulse (#5528) See https://github.com/esp8266/Arduino/issues/5528 and the more elaborate [description](https://github.com/maarten-pennings/I2C-tool/blob/master/I2Ctest8266/README.md#how-to-fix) where @maarten-pennings did all the hard work, but as far as I could see, no PR was made. --- cores/esp8266/core_esp8266_si2c.cpp | 52 +++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 8971a422cc..f62ddbb240 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -113,6 +113,12 @@ class Twi return (GPI & (1 << twi_scl)) != 0; } + // ICACHE_RAM_ATTR used to have a constant delay. + void ICACHE_RAM_ATTR twi_wait_clockStretchLimit(void); + + // Generate a clock "valley" (at the end of a segment, just before a repeated start) + void twi_scl_valley(void); + public: void setClock(unsigned int freq); void setClockStretchLimit(uint32_t limit); @@ -268,12 +274,11 @@ bool Twi::write_start(void) bool Twi::write_stop(void) { - uint32_t i = 0; SCL_LOW(); SDA_LOW(); delay(twi_dcount); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching + twi_wait_clockStretchLimit(); delay(twi_dcount); SDA_HIGH(); delay(twi_dcount); @@ -282,7 +287,6 @@ bool Twi::write_stop(void) bool Twi::write_bit(bool bit) { - uint32_t i = 0; SCL_LOW(); if (bit) { @@ -294,19 +298,18 @@ bool Twi::write_bit(bool bit) } delay(twi_dcount + 1); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching + twi_wait_clockStretchLimit(); delay(twi_dcount); return true; } bool Twi::read_bit(void) { - uint32_t i = 0; SCL_LOW(); SDA_HIGH(); delay(twi_dcount + 2); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching + twi_wait_clockStretchLimit(); bool bit = SDA_READ(); delay(twi_dcount); return bit; @@ -364,14 +367,18 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned if (sendStop) { write_stop(); + } + else + { + twi_scl_valley(); + twi_wait_clockStretchLimit(); + // TD-er: Also twi_delay here? + // delay(twi_dcount); } i = 0; while (SDA_READ() == 0 && (i++) < 10) { - SCL_LOW(); - delay(twi_dcount); - SCL_HIGH(); - unsigned int t = 0; while (SCL_READ() == 0 && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit + twi_scl_valley(); delay(twi_dcount); } return 0; @@ -401,13 +408,17 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned { write_stop(); } + else + { + twi_scl_valley(); + twi_wait_clockStretchLimit(); + // TD-er: Also twi_delay here? + // delay(twi_dcount); + } i = 0; while (SDA_READ() == 0 && (i++) < 10) { - SCL_LOW(); - delay(twi_dcount); - SCL_HIGH(); - unsigned int t = 0; while (SCL_READ() == 0 && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit + twi_scl_valley(); delay(twi_dcount); } return 0; @@ -628,6 +639,19 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status) } } +void ICACHE_RAM_ATTR Twi::twi_wait_clockStretchLimit(void) +{ + uint32_t t=0; + while(SCL_READ() == 0 && (t++) < twi_clockStretchLimit); // Clock stretching +} + +void Twi::twi_scl_valley(void) +{ + SCL_LOW(); + delay(twi_dcount); + SCL_HIGH(); +} + void ICACHE_RAM_ATTR Twi::onTimer(void *unused) { (void)unused; From f67ddf516ce846de648032aea1a4eade3df8b0d5 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 4 Oct 2019 08:45:18 -0700 Subject: [PATCH 15/20] Add enum use comment, rename twi::delay, fix SDA/SCL_READ bool usage Per review comments --- cores/esp8266/core_esp8266_si2c.cpp | 60 +++++++++++++++-------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 8971a422cc..13a91483e7 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -42,6 +42,10 @@ class Twi unsigned char twi_addr = 0; uint32_t twi_clockStretchLimit = 0; + // These are int-wide, even though they could all fit in a byte, to reduce code size and avoid any potential + // issues about RmW on packed bytes. The int-wide variations of asm instructions are smaller than the equivalent + // byte-wide ones, and since these emums are used everywhere, the difference adds up fast. There is only a single + // instance of the class, though, so the extra 12 bytes of RAM used here saves a lot more IRAM. volatile enum { TWIPM_UNKNOWN = 0, TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twip_mode = TWIPM_IDLE; volatile 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 } twip_state = TWIP_IDLE; volatile int twip_status = TW_NO_INFO; @@ -78,7 +82,7 @@ class Twi static void ICACHE_RAM_ATTR onTimer(void *unused); // Internal use functions - void ICACHE_RAM_ATTR delay(unsigned char v); + void ICACHE_RAM_ATTR busywait(unsigned char v); bool write_start(void); bool write_stop(void); bool write_bit(bool bit); @@ -238,7 +242,7 @@ void Twi::setAddress(uint8_t address) twi_addr = address << 1; } -void ICACHE_RAM_ATTR Twi::delay(unsigned char v) +void ICACHE_RAM_ATTR Twi::busywait(unsigned char v) { unsigned int i; #pragma GCC diagnostic push @@ -256,13 +260,13 @@ bool Twi::write_start(void) { SCL_HIGH(); SDA_HIGH(); - if (SDA_READ() == 0) + if (!SDA_READ()) { return false; } - delay(twi_dcount); + busywait(twi_dcount); SDA_LOW(); - delay(twi_dcount); + busywait(twi_dcount); return true; } @@ -271,12 +275,12 @@ bool Twi::write_stop(void) uint32_t i = 0; SCL_LOW(); SDA_LOW(); - delay(twi_dcount); + busywait(twi_dcount); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit); // Clock stretching - delay(twi_dcount); + while (!SCL_READ() && (i++) < twi_clockStretchLimit); // Clock stretching + busywait(twi_dcount); SDA_HIGH(); - delay(twi_dcount); + busywait(twi_dcount); return true; } @@ -292,10 +296,10 @@ bool Twi::write_bit(bool bit) { SDA_LOW(); } - delay(twi_dcount + 1); + busywait(twi_dcount + 1); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching - delay(twi_dcount); + while (!SCL_READ() && (i++) < twi_clockStretchLimit);// Clock stretching + busywait(twi_dcount); return true; } @@ -304,11 +308,11 @@ bool Twi::read_bit(void) uint32_t i = 0; SCL_LOW(); SDA_HIGH(); - delay(twi_dcount + 2); + busywait(twi_dcount + 2); SCL_HIGH(); - while (SCL_READ() == 0 && (i++) < twi_clockStretchLimit);// Clock stretching + while (!SCL_READ() && (i++) < twi_clockStretchLimit);// Clock stretching bool bit = SDA_READ(); - delay(twi_dcount); + busywait(twi_dcount); return bit; } @@ -366,13 +370,13 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned write_stop(); } i = 0; - while (SDA_READ() == 0 && (i++) < 10) + while (!SDA_READ() && (i++) < 10) { SCL_LOW(); - delay(twi_dcount); + busywait(twi_dcount); SCL_HIGH(); - unsigned int t = 0; while (SCL_READ() == 0 && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit - delay(twi_dcount); + unsigned int t = 0; while (!SCL_READ() && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit + busywait(twi_dcount); } return 0; } @@ -402,34 +406,34 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned write_stop(); } i = 0; - while (SDA_READ() == 0 && (i++) < 10) + while (!SDA_READ() && (i++) < 10) { SCL_LOW(); - delay(twi_dcount); + busywait(twi_dcount); SCL_HIGH(); - unsigned int t = 0; while (SCL_READ() == 0 && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit - delay(twi_dcount); + unsigned int t = 0; while (!SCL_READ() && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit + busywait(twi_dcount); } return 0; } uint8_t Twi::status() { - if (SCL_READ() == 0) + if (!SCL_READ()) { return I2C_SCL_HELD_LOW; // SCL held low by another device, no procedure available to recover } int clockCount = 20; - while (SDA_READ() == 0 && clockCount-- > 0) // if SDA low, read the bits slaves have to sent to a max + while (!SDA_READ() && clockCount-- > 0) // if SDA low, read the bits slaves have to sent to a max { read_bit(); - if (SCL_READ() == 0) + if (!SCL_READ()) { return I2C_SCL_HELD_LOW_AFTER_READ; // I2C bus error. SCL held low beyond slave clock stretch time } } - if (SDA_READ() == 0) + if (!SDA_READ()) { return I2C_SDA_HELD_LOW; // I2C bus error. SDA line held low by slave/another_master after n bits. } @@ -501,7 +505,7 @@ inline void ICACHE_RAM_ATTR Twi::stop(void) //TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); SCL_HIGH(); // _BV(TWINT) twi_ack = 1; // _BV(TWEA) - delay(5); // Maybe this should be here + busywait(5); // Maybe this should be here SDA_HIGH(); // _BV(TWSTO) // update twi state twi_state = TWI_READY; From dd6a854801ab497ac22a6f1a438c1dd05834de36 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 4 Oct 2019 08:59:14 -0700 Subject: [PATCH 16/20] Replace clock stretch repeated code w/inline loop There were multiple places where the code was waiting for a slave to finish stretching the clock. Factor them out to an *inline* function to reduce code smell. --- cores/esp8266/core_esp8266_si2c.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 13a91483e7..1c40564881 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -116,6 +116,13 @@ class Twi { return (GPI & (1 << twi_scl)) != 0; } + // Handle the case where a slave needs to stretch the clock with a time-limited busy wait + inline void WAIT_CLOCK_STRETCH() + { + for (unsigned int t = 0; !SCL_READ() && (t < twi_clockStretchLimit); t++) { + /* noop */ + } + } public: void setClock(unsigned int freq); @@ -145,6 +152,7 @@ static Twi twi; #define TWI_CLOCK_STRETCH_MULTIPLIER 6 #endif + void Twi::setClock(unsigned int freq) { preferred_si2c_clock = freq; @@ -272,12 +280,11 @@ bool Twi::write_start(void) bool Twi::write_stop(void) { - uint32_t i = 0; SCL_LOW(); SDA_LOW(); busywait(twi_dcount); SCL_HIGH(); - while (!SCL_READ() && (i++) < twi_clockStretchLimit); // Clock stretching + WAIT_CLOCK_STRETCH(); busywait(twi_dcount); SDA_HIGH(); busywait(twi_dcount); @@ -286,7 +293,6 @@ bool Twi::write_stop(void) bool Twi::write_bit(bool bit) { - uint32_t i = 0; SCL_LOW(); if (bit) { @@ -298,19 +304,18 @@ bool Twi::write_bit(bool bit) } busywait(twi_dcount + 1); SCL_HIGH(); - while (!SCL_READ() && (i++) < twi_clockStretchLimit);// Clock stretching + WAIT_CLOCK_STRETCH(); busywait(twi_dcount); return true; } bool Twi::read_bit(void) { - uint32_t i = 0; SCL_LOW(); SDA_HIGH(); busywait(twi_dcount + 2); SCL_HIGH(); - while (!SCL_READ() && (i++) < twi_clockStretchLimit);// Clock stretching + WAIT_CLOCK_STRETCH(); bool bit = SDA_READ(); busywait(twi_dcount); return bit; @@ -375,7 +380,7 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned SCL_LOW(); busywait(twi_dcount); SCL_HIGH(); - unsigned int t = 0; while (!SCL_READ() && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit + WAIT_CLOCK_STRETCH(); busywait(twi_dcount); } return 0; @@ -411,7 +416,7 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned SCL_LOW(); busywait(twi_dcount); SCL_HIGH(); - unsigned int t = 0; while (!SCL_READ() && (t++) < twi_clockStretchLimit); // twi_clockStretchLimit + WAIT_CLOCK_STRETCH(); busywait(twi_dcount); } return 0; From a23864a713fe17fff547497e1b32c6cb4ec7569f Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 4 Oct 2019 10:39:27 -0700 Subject: [PATCH 17/20] 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. --- cores/esp8266/core_esp8266_si2c.cpp | 27 +++++++++++++++++++++------ cores/esp8266/twi.h | 1 + libraries/Wire/Wire.cpp | 2 ++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 1c40564881..5ab88ffa08 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -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); @@ -124,6 +127,7 @@ class Twi } } + public: void setClock(unsigned int freq); void setClockStretchLimit(uint32_t limit); @@ -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; @@ -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 @@ -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) @@ -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; @@ -1044,4 +1054,9 @@ extern "C" { twi.releaseBus(); } + void twi_enableSlaveMode(void) + { + twi.enableSlave(); + } + }; diff --git a/cores/esp8266/twi.h b/cores/esp8266/twi.h index 685221820b..af91786563 100644 --- a/cores/esp8266/twi.h +++ b/cores/esp8266/twi.h @@ -54,6 +54,7 @@ void twi_reply(uint8_t); //void twi_stop(void); void twi_releaseBus(void); +void twi_enableSlaveMode(void); #ifdef __cplusplus } diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 88103650d1..c8151d9c01 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -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 ////////////////////////////////////////////////////// From eb85c68f399cbb4ef6d93cae1c4fc72782ff45b4 Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Thu, 3 Oct 2019 14:36:10 +0200 Subject: [PATCH 18/20] Double I2C read in one transaction skips a clock pulse (#5528) See https://github.com/esp8266/Arduino/issues/5528 and the more elaborate [description](https://github.com/maarten-pennings/I2C-tool/blob/master/I2Ctest8266/README.md#how-to-fix) where @maarten-pennings did all the hard work, but as far as I could see, no PR was made. --- cores/esp8266/core_esp8266_si2c.cpp | 40 ++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 5ab88ffa08..f180a06e7d 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -127,7 +127,8 @@ class Twi } } - + // Generate a clock "valley" (at the end of a segment, just before a repeated start) + void twi_scl_valley(void); public: void setClock(unsigned int freq); void setClockStretchLimit(uint32_t limit); @@ -383,14 +384,18 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned if (sendStop) { write_stop(); + } + else + { + twi_scl_valley(); + twi_wait_clockStretchLimit(); + // TD-er: Also twi_delay here? + // delay(twi_dcount); } i = 0; while (!SDA_READ() && (i++) < 10) { - SCL_LOW(); - busywait(twi_dcount); - SCL_HIGH(); - WAIT_CLOCK_STRETCH(); + twi_scl_valley(); busywait(twi_dcount); } return 0; @@ -420,13 +425,17 @@ unsigned char Twi::readFrom(unsigned char address, unsigned char* buf, unsigned { write_stop(); } + else + { + twi_scl_valley(); + twi_wait_clockStretchLimit(); + // TD-er: Also twi_delay here? + // delay(twi_dcount); + } i = 0; while (!SDA_READ() && (i++) < 10) { - SCL_LOW(); - busywait(twi_dcount); - SCL_HIGH(); - WAIT_CLOCK_STRETCH(); + twi_scl_valley(); busywait(twi_dcount); } return 0; @@ -647,6 +656,19 @@ void ICACHE_RAM_ATTR Twi::onTwipEvent(uint8_t status) } } +void ICACHE_RAM_ATTR Twi::twi_wait_clockStretchLimit(void) +{ + uint32_t t=0; + while(SCL_READ() == 0 && (t++) < twi_clockStretchLimit); // Clock stretching +} + +void Twi::twi_scl_valley(void) +{ + SCL_LOW(); + busywait(twi_dcount); + SCL_HIGH(); +} + void ICACHE_RAM_ATTR Twi::onTimer(void *unused) { (void)unused; From 138e1ec2b3535edc72b178808b77719cff24301c Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Mon, 14 Oct 2019 15:00:51 +0200 Subject: [PATCH 19/20] Restyle core_esp8266_si2c.cpp As requested by @d-a-v https://github.com/esp8266/Arduino/pull/6592#issuecomment-541632492 --- cores/esp8266/core_esp8266_si2c.cpp | 8 +++++--- tests/restyle.sh | 25 +++++++++++++++++++------ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/cores/esp8266/core_esp8266_si2c.cpp b/cores/esp8266/core_esp8266_si2c.cpp index 4a1b59f363..2ad2016d2c 100644 --- a/cores/esp8266/core_esp8266_si2c.cpp +++ b/cores/esp8266/core_esp8266_si2c.cpp @@ -122,7 +122,8 @@ class Twi // Handle the case where a slave needs to stretch the clock with a time-limited busy wait inline void WAIT_CLOCK_STRETCH() { - for (unsigned int t = 0; !SCL_READ() && (t < twi_clockStretchLimit); t++) { + for (unsigned int t = 0; !SCL_READ() && (t < twi_clockStretchLimit); t++) + { /* noop */ } } @@ -254,7 +255,8 @@ void Twi::setAddress(uint8_t address) void Twi::enableSlave() { - if (!_slaveEnabled) { + if (!_slaveEnabled) + { attachInterrupt(twi_scl, onSclChange, CHANGE); attachInterrupt(twi_sda, onSdaChange, CHANGE); _slaveEnabled = true; @@ -384,7 +386,7 @@ unsigned char Twi::writeTo(unsigned char address, unsigned char * buf, unsigned if (sendStop) { write_stop(); - } + } else { twi_scl_valley(); diff --git a/tests/restyle.sh b/tests/restyle.sh index 513a891900..9f6eb13a4e 100755 --- a/tests/restyle.sh +++ b/tests/restyle.sh @@ -8,12 +8,25 @@ pwd test -d cores/esp8266 test -d libraries -# in a near future, restyle-all.sh will be renamed to restyle.sh -# and will be checked against CI +#all="cores/esp8266 libraries" +all=" +libraries/ESP8266mDNS +cores/esp8266/core_esp8266_si2c.cpp +" + +for d in $all; do + for e in c cpp h; do + find $d -name "*.$e" -exec \ + astyle \ + --suffix=none \ + --options=${org}/astyle_core.conf {} \; + done +done for d in libraries; do - find $d -name "*.ino" -exec \ - astyle \ - --suffix=none \ - --options=${org}/astyle_examples.conf {} \; + find $d -name "*.ino" -exec \ + astyle \ + --suffix=none \ + --options=${org}/astyle_examples.conf {} \; done + From a6c37009865ca64186a743f621336d3c0ab52263 Mon Sep 17 00:00:00 2001 From: TD-er Date: Tue, 15 Oct 2019 23:02:44 +0200 Subject: [PATCH 20/20] Add libraries/Wire to restyle.sh As suggested here: https://github.com/esp8266/Arduino/pull/6592#issuecomment-542164759 --- tests/restyle.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/restyle.sh b/tests/restyle.sh index 9f6eb13a4e..1204ad818c 100755 --- a/tests/restyle.sh +++ b/tests/restyle.sh @@ -12,6 +12,7 @@ test -d libraries all=" libraries/ESP8266mDNS cores/esp8266/core_esp8266_si2c.cpp +libraries/Wire " for d in $all; do