Skip to content

Commit 37e10b9

Browse files
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 (.)
1 parent f55ab36 commit 37e10b9

File tree

1 file changed

+42
-56
lines changed

1 file changed

+42
-56
lines changed

cores/esp8266/core_esp8266_si2c.cpp

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -687,12 +687,10 @@ void ICACHE_RAM_ATTR onSdaChange(void)
687687
sda = SDA_READ();
688688
scl = SCL_READ();
689689

690-
switch (twip_state)
690+
if (scl) /* !DATA */ switch (twip_state)
691691
{
692692
case TWIP_IDLE:
693-
if (!scl) {
694-
// DATA - ignore
695-
} else if (sda) {
693+
if (sda) {
696694
// STOP - ignore
697695
} else {
698696
// START
@@ -711,72 +709,60 @@ void ICACHE_RAM_ATTR onSdaChange(void)
711709
case TWIP_READ_ACK:
712710
case TWIP_RWAIT_ACK:
713711
case TWIP_WRITE:
714-
if (!scl) {
715-
// DATA - ignore
716-
} else {
717-
// START or STOP
718-
SDA_HIGH(); // Should not be necessary
719-
twip_status = TW_BUS_ERROR;
720-
twi_onTwipEvent(twip_status);
721-
twip_mode = TWIPM_WAIT;
722-
twip_state = TWIP_BUS_ERR;
723-
}
712+
// START or STOP
713+
SDA_HIGH(); // Should not be necessary
714+
twip_status = TW_BUS_ERROR;
715+
twi_onTwipEvent(twip_status);
716+
twip_mode = TWIPM_WAIT;
717+
twip_state = TWIP_BUS_ERR;
724718
break;
725719

726720
case TWIP_WAIT_STOP:
727721
case TWIP_BUS_ERR:
728-
if (!scl) {
729-
// DATA - ignore
722+
if (sda) {
723+
// STOP
724+
SCL_LOW(); // clock stretching
725+
ets_timer_disarm(&timer);
726+
twip_state = TWIP_IDLE;
727+
twip_mode = TWIPM_IDLE;
728+
SCL_HIGH();
730729
} else {
731-
if (sda) {
732-
// STOP
733-
SCL_LOW(); // clock stretching
734-
ets_timer_disarm(&timer);
735-
twip_state = TWIP_IDLE;
736-
twip_mode = TWIPM_IDLE;
737-
SCL_HIGH();
730+
// START
731+
if (twip_state == TWIP_BUS_ERR) {
732+
// ignore
738733
} else {
739-
// START
740-
if (twip_state == TWIP_BUS_ERR) {
741-
// ignore
742-
} else {
743-
bitCount = 8;
744-
twip_state = TWIP_REP_START;
745-
ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms
746-
}
734+
bitCount = 8;
735+
twip_state = TWIP_REP_START;
736+
ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms
747737
}
748738
}
749739
break;
750740

751741
case TWIP_SLA_W:
752742
case TWIP_READ:
753-
if (!scl) {
754-
// DATA - ignore
743+
// START or STOP
744+
if (bitCount != 7) {
745+
// inside byte transfer - error
746+
twip_status = TW_BUS_ERROR;
747+
twi_onTwipEvent(twip_status);
748+
twip_mode = TWIPM_WAIT;
749+
twip_state = TWIP_BUS_ERR;
755750
} else {
756-
// START or STOP
757-
if (bitCount != 7) {
758-
// inside byte transfer - error
759-
twip_status = TW_BUS_ERROR;
760-
twi_onTwipEvent(twip_status);
761-
twip_mode = TWIPM_WAIT;
762-
twip_state = TWIP_BUS_ERR;
751+
// during first bit in byte transfer - ok
752+
SCL_LOW(); // clock stretching
753+
twip_status = TW_SR_STOP;
754+
twi_onTwipEvent(twip_status);
755+
if (sda) {
756+
// STOP
757+
ets_timer_disarm(&timer);
758+
twip_state = TWIP_IDLE;
759+
twip_mode = TWIPM_IDLE;
763760
} else {
764-
// during first bit in byte transfer - ok
765-
SCL_LOW(); // clock stretching
766-
twip_status = TW_SR_STOP;
767-
twi_onTwipEvent(twip_status);
768-
if (sda) {
769-
// STOP
770-
ets_timer_disarm(&timer);
771-
twip_state = TWIP_IDLE;
772-
twip_mode = TWIPM_IDLE;
773-
} else {
774-
// START
775-
bitCount = 8;
776-
ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms
777-
twip_state = TWIP_REP_START;
778-
twip_mode = TWIPM_IDLE;
779-
}
761+
// START
762+
bitCount = 8;
763+
ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms
764+
twip_state = TWIP_REP_START;
765+
twip_mode = TWIPM_IDLE;
780766
}
781767
}
782768
break;

0 commit comments

Comments
 (0)