Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix I2C slave communication stuck #1018

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,22 @@ 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 +159,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 +182,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
Loading