Skip to content

Commit b441a19

Browse files
committed
Wire: avoid allocating slave thread while master
1 parent 1f490b7 commit b441a19

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

libraries/Wire/Wire.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#include "Wire.h"
2424
#include "pinDefinitions.h"
2525

26-
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(digitalPinToPinName(sda)), _scl(digitalPinToPinName(scl)), usedTxBuffer(0), slave_th(osPriorityNormal, 2048, nullptr, "I2CSlave") {}
26+
arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(digitalPinToPinName(sda)), _scl(digitalPinToPinName(scl)), usedTxBuffer(0) {}
2727

28-
arduino::MbedI2C::MbedI2C(PinName sda, PinName scl) : _sda(sda), _scl(scl), usedTxBuffer(0), slave_th(osPriorityNormal, 2048, nullptr, "I2CSlave") {}
28+
arduino::MbedI2C::MbedI2C(PinName sda, PinName scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {}
2929

3030
void arduino::MbedI2C::begin() {
3131
end();
@@ -37,7 +37,8 @@ void arduino::MbedI2C::begin(uint8_t slaveAddr) {
3737
end();
3838
slave = new mbed::I2CSlave((PinName)_sda, (PinName)_scl);
3939
slave->address(slaveAddr << 1);
40-
slave_th.start(mbed::callback(this, &arduino::MbedI2C::receiveThd));
40+
slave_th = new rtos::Thread(osPriorityNormal, 2048, nullptr, "I2CSlave");
41+
slave_th->start(mbed::callback(this, &arduino::MbedI2C::receiveThd));
4142
#endif
4243
}
4344

@@ -47,8 +48,9 @@ void arduino::MbedI2C::end() {
4748
}
4849
#ifdef DEVICE_I2CSLAVE
4950
if (slave != NULL) {
50-
slave_th.terminate();
51-
slave_th.free_stack();
51+
slave_th->terminate();
52+
slave_th->free_stack();
53+
delete slave_th;
5254
delete slave;
5355
}
5456
#endif

libraries/Wire/Wire.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class MbedI2C : public HardwareI2C
8282
voidFuncPtrParamInt onReceiveCb = NULL;
8383
voidFuncPtr onRequestCb = NULL;
8484
#ifdef DEVICE_I2CSLAVE
85-
rtos::Thread slave_th;
85+
rtos::Thread* slave_th;
8686
void receiveThd();
8787
#endif
8888
};

0 commit comments

Comments
 (0)