Skip to content

Commit 00a8cf2

Browse files
committed
protecting access to rxBuffer to avoid stop of I2C communication
1 parent fe0b901 commit 00a8cf2

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

libraries/Wire/Wire.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,23 @@ size_t arduino::MbedI2C::write(const uint8_t* data, int len) {
122122
}
123123

124124
int arduino::MbedI2C::read() {
125+
int rv = -1;
126+
core_util_critical_section_enter();
127+
125128
if (rxBuffer.available()) {
126-
return rxBuffer.read_char();
129+
130+
rv = rxBuffer.read_char();
127131
}
128-
return -1;
132+
core_util_critical_section_exit();
133+
return rv;
129134
}
130135

131136
int arduino::MbedI2C::available() {
132-
return rxBuffer.available();
137+
core_util_critical_section_enter();
138+
int rv = rxBuffer.available();
139+
core_util_critical_section_exit();
140+
return rv;
141+
133142
}
134143

135144
int arduino::MbedI2C::peek() {
@@ -151,13 +160,16 @@ void arduino::MbedI2C::receiveThd() {
151160
onRequestCb();
152161
}
153162
if (usedTxBuffer != 0) {
163+
core_util_critical_section_enter();
154164
slave->write((const char *) txBuffer, usedTxBuffer);
165+
core_util_critical_section_exit();
155166
usedTxBuffer = 0;
156167
}
157168
//slave->stop();
158169
break;
159170
case mbed::I2CSlave::WriteGeneral:
160171
case mbed::I2CSlave::WriteAddressed:
172+
core_util_critical_section_enter();
161173
rxBuffer.clear();
162174
char buf[240];
163175
c = slave->read(buf, sizeof(buf));
@@ -171,6 +183,7 @@ void arduino::MbedI2C::receiveThd() {
171183
if (rxBuffer.available() > 0 && onReceiveCb != NULL) {
172184
onReceiveCb(rxBuffer.available());
173185
}
186+
core_util_critical_section_exit();
174187
//slave->stop();
175188
break;
176189
case mbed::I2CSlave::NoData:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy