Skip to content

Commit 4046059

Browse files
giulcioffifacchinm
authored andcommitted
Rework i2c slave read
1 parent 7a8d3ee commit 4046059

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

libraries/Wire/Wire.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void arduino::MbedI2C::flush() {
124124
void arduino::MbedI2C::receiveThd() {
125125
while (1) {
126126
int i = slave->receive();
127+
int c = 0;
128+
int buf_idx = 0;
127129
switch (i) {
128130
case mbed::I2CSlave::ReadAddressed:
129131
if (onRequestCb != NULL) {
@@ -139,12 +141,11 @@ void arduino::MbedI2C::receiveThd() {
139141
case mbed::I2CSlave::WriteAddressed:
140142
rxBuffer.clear();
141143
char buf[72];
142-
while (1) {
143-
size_t c = slave->read(buf, sizeof(buf));
144-
for (size_t i = 0; i < c; i++) {
145-
rxBuffer.store_char(uint8_t(buf[i]));
146-
}
147-
if (c <= sizeof(buf)) {
144+
c = slave->read(buf, sizeof(buf));
145+
for (buf_idx = 0; buf_idx < c; buf_idx++) {
146+
if (rxBuffer.availableForStore()) {
147+
rxBuffer.store_char(uint8_t(buf[buf_idx]));
148+
} else {
148149
break;
149150
}
150151
}

0 commit comments

Comments
 (0)