Skip to content

Commit 79532ba

Browse files
committed
TWI slave callbacks parameter for target object (needed for Wire library)
1 parent 7d9b474 commit 79532ba

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

cores/esp8266/core_esp8266_si2c.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ class TwiMasterOrSlave : public TwiMaster
8888
uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
8989
volatile int twi_rxBufferIndex = 0;
9090

91-
void (*twi_onSlaveTransmit)(void);
92-
void (*twi_onSlaveReceive)(uint8_t*, size_t);
91+
void* twi_SlaveTargetObject;
92+
void (*twi_onSlaveTransmit)(void*);
93+
void (*twi_onSlaveReceive)(uint8_t*, size_t, void*);
9394

9495
// ETS queue/timer interfaces
9596
enum { EVENTTASK_QUEUE_SIZE = 1, EVENTTASK_QUEUE_PRIO = 2 };
@@ -103,9 +104,9 @@ class TwiMasterOrSlave : public TwiMaster
103104
static void eventTask(ETSEvent *e);
104105
static void ICACHE_RAM_ATTR onTimer(void *unused);
105106

106-
// Allow not linking in the slave code if there is no call to setAddress
107+
// Allow not linking in the slave code if there is no call to enableSlave
107108
bool _slaveEnabled = false;
108-
109+
109110
// Internal use functions
110111
void ICACHE_RAM_ATTR onTwipEvent(uint8_t status);
111112

@@ -115,11 +116,11 @@ class TwiMasterOrSlave : public TwiMaster
115116

116117
void setAddress(uint8_t address);
117118
uint8_t transmit(const uint8_t* data, uint8_t length);
118-
void attachSlaveRxEvent(void (*function)(uint8_t*, size_t));
119-
void attachSlaveTxEvent(void (*function)(void));
119+
void attachSlaveRxEvent(void (*function)(uint8_t*, size_t, void*));
120+
void attachSlaveTxEvent(void (*function)(void*));
120121
void ICACHE_RAM_ATTR reply(uint8_t ack);
121122
void ICACHE_RAM_ATTR releaseBus(void);
122-
void enableSlave();
123+
void enableSlave(void* targetObject);
123124
};
124125

125126
static TwiMasterOrSlave twi;
@@ -211,10 +212,11 @@ void TwiMasterOrSlave::setAddress(uint8_t address)
211212
twi_addr = address << 1;
212213
}
213214

214-
void TwiMasterOrSlave::enableSlave()
215+
void TwiMasterOrSlave::enableSlave(void* targetObject)
215216
{
216217
if (!_slaveEnabled)
217218
{
219+
twi_SlaveTargetObject = targetObject;
218220
attachInterrupt(twi_scl, onSclChange, CHANGE);
219221
attachInterrupt(twi_sda, onSdaChange, CHANGE);
220222
_slaveEnabled = true;
@@ -453,12 +455,12 @@ uint8_t TwiMasterOrSlave::transmit(const uint8_t* data, uint8_t length)
453455
return 0;
454456
}
455457

456-
void TwiMasterOrSlave::attachSlaveRxEvent(void (*function)(uint8_t*, size_t))
458+
void TwiMasterOrSlave::attachSlaveRxEvent(void (*function)(uint8_t*, size_t, void*))
457459
{
458460
twi_onSlaveReceive = function;
459461
}
460462

461-
void TwiMasterOrSlave::attachSlaveTxEvent(void (*function)(void))
463+
void TwiMasterOrSlave::attachSlaveTxEvent(void (*function)(void*))
462464
{
463465
twi_onSlaveTransmit = function;
464466
}
@@ -624,7 +626,7 @@ void TwiMasterOrSlave::eventTask(ETSEvent *e)
624626
switch (e->sig)
625627
{
626628
case TWI_SIG_TX:
627-
twi.twi_onSlaveTransmit();
629+
twi.twi_onSlaveTransmit(twi.twi_SlaveTargetObject);
628630

629631
// if they didn't change buffer & length, initialize it
630632
if (twi.twi_txBufferLength == 0)
@@ -641,7 +643,7 @@ void TwiMasterOrSlave::eventTask(ETSEvent *e)
641643
case TWI_SIG_RX:
642644
// ack future responses and leave slave receiver state
643645
twi.releaseBus();
644-
twi.twi_onSlaveReceive(twi.twi_rxBuffer, e->par);
646+
twi.twi_onSlaveReceive(twi.twi_rxBuffer, e->par, twi.twi_SlaveTargetObject);
645647
break;
646648
}
647649
}
@@ -990,16 +992,17 @@ extern "C" {
990992
return twi.transmit(buf, len);
991993
}
992994

993-
void twi_attachSlaveRxEvent(void (*cb)(uint8_t*, size_t))
995+
void twi_attachSlaveRxEventWithTarget(void (*cb)(uint8_t*, size_t, void*))
994996
{
995997
twi.attachSlaveRxEvent(cb);
996998
}
997999

998-
void twi_attachSlaveTxEvent(void (*cb)(void))
1000+
void twi_attachSlaveTxEventWithTarget(void (*cb)(void*))
9991001
{
10001002
twi.attachSlaveTxEvent(cb);
10011003
}
10021004

1005+
10031006
void twi_reply(uint8_t r)
10041007
{
10051008
twi.reply(r);
@@ -1010,9 +1013,9 @@ extern "C" {
10101013
twi.releaseBus();
10111014
}
10121015

1013-
void twi_enableSlaveMode(void)
1016+
void twi_enableSlaveModeWithTarget(void* targetObject)
10141017
{
1015-
twi.enableSlave();
1018+
twi.enableSlave(targetObject);
10161019
}
10171020

10181021
};

cores/esp8266/twi.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,32 @@ uint8_t twi_status();
4848

4949
uint8_t twi_transmit(const uint8_t*, uint8_t);
5050

51-
void twi_attachSlaveRxEvent(void (*)(uint8_t*, size_t));
52-
void twi_attachSlaveTxEvent(void (*)(void));
51+
void twi_attachSlaveRxEventWithTarget(void (*)(uint8_t*, size_t, void*));
52+
void twi_attachSlaveTxEventWithTarget(void (*)(void*));
5353
void twi_reply(uint8_t);
5454
//void twi_stop(void);
5555
void twi_releaseBus(void);
5656

57-
void twi_enableSlaveMode(void);
57+
void twi_enableSlaveModeWithTarget(void* targetObject);
58+
59+
inline void twi_attachSlaveRxEvent(void (*cb)(uint8_t*, size_t))
60+
{ // force cast to the previous version of the callback
61+
// the ESP8266 convention should be fine with that:
62+
// http://naberius.de/2015/05/14/esp8266-gpio-output-performance/
63+
// https://boredpentester.com/reversing-esp8266-firmware-part-5/
64+
twi_attachSlaveRxEventWithTarget((void (*)(uint8_t*, size_t, void*))(void*)cb);
65+
}
66+
inline void twi_attachSlaveTxEvent(void (*cb)(void))
67+
{ // force cast to the previous version of the callback
68+
// the ESP8266 convention should be fine with that:
69+
// http://naberius.de/2015/05/14/esp8266-gpio-output-performance/
70+
// https://boredpentester.com/reversing-esp8266-firmware-part-5/
71+
twi_attachSlaveTxEventWithTarget((void (*)(void*))(void*)cb);
72+
}
73+
inline void twi_enableSlaveMode(void)
74+
{
75+
twi_enableSlaveModeWithTarget(NULL);
76+
}
5877

5978
#ifdef __cplusplus
6079
}

0 commit comments

Comments
 (0)