Skip to content

Commit 9ae9af1

Browse files
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
1 parent 014f87f commit 9ae9af1

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

cores/esp8266/core_esp8266_si2c.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ inline void ICACHE_RAM_ATTR twi_releaseBus(void)
391391

392392
void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status)
393393
{
394+
twip_status = status;
394395
switch(status) {
395396
// Slave Receiver
396397
case TW_SR_SLA_ACK: // addressed, returned ack
@@ -485,8 +486,7 @@ void ICACHE_RAM_ATTR onTimer(void *unused)
485486
{
486487
(void)unused;
487488
twi_releaseBus();
488-
twip_status = TW_BUS_ERROR;
489-
twi_onTwipEvent(twip_status);
489+
twi_onTwipEvent(TW_BUS_ERROR);
490490
twip_mode = TWIPM_WAIT;
491491
twip_state = TWIP_BUS_ERR;
492492
}
@@ -587,27 +587,23 @@ void ICACHE_RAM_ATTR onSclChange(void)
587587
SDA_HIGH();
588588
twip_mode = TWIPM_ADDRESSED;
589589
if (!(twi_data & 0x01)) {
590-
twip_status = TW_SR_SLA_ACK;
591-
twi_onTwipEvent(twip_status);
590+
twi_onTwipEvent(TW_SR_SLA_ACK);
592591
bitCount = 8;
593592
twip_state = TWIP_SLA_W;
594593
} else {
595-
twip_status = TW_ST_SLA_ACK;
596-
twi_onTwipEvent(twip_status);
594+
twi_onTwipEvent(TW_ST_SLA_ACK);
597595
twip_state = TWIP_SLA_R;
598596
}
599597
}
600598
} else {
601599
SCL_LOW(); // clock stretching
602600
SDA_HIGH();
603601
if (!twi_ack) {
604-
twip_status = TW_SR_DATA_NACK;
605-
twi_onTwipEvent(twip_status);
602+
twi_onTwipEvent(TW_SR_DATA_NACK);
606603
twip_mode = TWIPM_WAIT;
607604
twip_state = TWIP_WAIT_STOP;
608605
} else {
609-
twip_status = TW_SR_DATA_ACK;
610-
twi_onTwipEvent(twip_status);
606+
twi_onTwipEvent(TW_SR_DATA_ACK);
611607
bitCount = 8;
612608
twip_state = TWIP_READ;
613609
}
@@ -647,13 +643,11 @@ void ICACHE_RAM_ATTR onSclChange(void)
647643
} else {
648644
SCL_LOW(); // clock stretching
649645
if (twi_ack && twi_ack_rec) {
650-
twip_status = TW_ST_DATA_ACK;
651-
twi_onTwipEvent(twip_status);
646+
twi_onTwipEvent(TW_ST_DATA_ACK);
652647
twip_state = TWIP_WRITE;
653648
} else {
654649
// we have no more data to send and/or the master doesn't want anymore
655-
twip_status = twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK;
656-
twi_onTwipEvent(twip_status);
650+
twi_onTwipEvent(twi_ack_rec ? TW_ST_LAST_DATA : TW_ST_DATA_NACK);
657651
twip_mode = TWIPM_WAIT;
658652
twip_state = TWIP_WAIT_STOP;
659653
}
@@ -682,8 +676,7 @@ void ICACHE_RAM_ATTR onSdaChange(void)
682676
} 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)) {
683677
// START or STOP
684678
SDA_HIGH(); // Should not be necessary
685-
twip_status = TW_BUS_ERROR;
686-
twi_onTwipEvent(twip_status);
679+
twi_onTwipEvent(TW_BUS_ERROR);
687680
twip_mode = TWIPM_WAIT;
688681
twip_state = TWIP_BUS_ERR;
689682
} else IFSTATE(S2M(TWIP_WAIT_STOP)|S2M(TWIP_BUS_ERR)) {
@@ -708,15 +701,13 @@ void ICACHE_RAM_ATTR onSdaChange(void)
708701
// START or STOP
709702
if (bitCount != 7) {
710703
// inside byte transfer - error
711-
twip_status = TW_BUS_ERROR;
712-
twi_onTwipEvent(twip_status);
704+
twi_onTwipEvent(TW_BUS_ERROR);
713705
twip_mode = TWIPM_WAIT;
714706
twip_state = TWIP_BUS_ERR;
715707
} else {
716708
// during first bit in byte transfer - ok
717709
SCL_LOW(); // clock stretching
718-
twip_status = TW_SR_STOP;
719-
twi_onTwipEvent(twip_status);
710+
twi_onTwipEvent(TW_SR_STOP);
720711
if (sda) {
721712
// STOP
722713
ets_timer_disarm(&timer);

0 commit comments

Comments
 (0)