Skip to content

Commit

Permalink
protecting access to rxBuffer to avoid stop of I2C communication
Browse files Browse the repository at this point in the history
  • Loading branch information
maidnl committed Jan 23, 2025
1 parent fe0b901 commit 00a8cf2
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,23 @@ size_t arduino::MbedI2C::write(const uint8_t* data, int len) {
}

int arduino::MbedI2C::read() {
int rv = -1;
core_util_critical_section_enter();

if (rxBuffer.available()) {
return rxBuffer.read_char();

rv = rxBuffer.read_char();
}
return -1;
core_util_critical_section_exit();
return rv;
}

int arduino::MbedI2C::available() {
return rxBuffer.available();
core_util_critical_section_enter();
int rv = rxBuffer.available();
core_util_critical_section_exit();
return rv;

}

int arduino::MbedI2C::peek() {
Expand All @@ -151,13 +160,16 @@ void arduino::MbedI2C::receiveThd() {
onRequestCb();
}
if (usedTxBuffer != 0) {
core_util_critical_section_enter();
slave->write((const char *) txBuffer, usedTxBuffer);
core_util_critical_section_exit();
usedTxBuffer = 0;
}
//slave->stop();
break;
case mbed::I2CSlave::WriteGeneral:
case mbed::I2CSlave::WriteAddressed:
core_util_critical_section_enter();
rxBuffer.clear();
char buf[240];
c = slave->read(buf, sizeof(buf));
Expand All @@ -171,6 +183,7 @@ void arduino::MbedI2C::receiveThd() {
if (rxBuffer.available() > 0 && onReceiveCb != NULL) {
onReceiveCb(rxBuffer.available());
}
core_util_critical_section_exit();
//slave->stop();
break;
case mbed::I2CSlave::NoData:
Expand Down

0 comments on commit 00a8cf2

Please sign in to comment.