Skip to content

Commit 470a97a

Browse files
committed
[Bugfix] nRF51 prevent endless loop in Wire library
1 parent 8915bf1 commit 470a97a

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

libraries/Wire/Wire_nRF51.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
111111
quantity = SERIAL_BUFFER_SIZE;
112112
}
113113

114+
uint32_t start;
114115
size_t byteRead = 0;
115116
rxBuffer.clear();
116117

@@ -133,8 +134,12 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
133134
}
134135

135136
_p_twi->TASKS_RESUME = 0x1UL;
136-
137-
while (!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR);
137+
start = millis();
138+
while (!_p_twi->EVENTS_RXDREADY && !_p_twi->EVENTS_ERROR) {
139+
if (millis() - start >= 3) {
140+
return 0;
141+
}
142+
}
138143

139144
if (_p_twi->EVENTS_ERROR)
140145
{
@@ -150,14 +155,24 @@ uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool stopBit)
150155
{
151156
this->suspended = false;
152157
_p_twi->TASKS_STOP = 0x1UL;
153-
while(!_p_twi->EVENTS_STOPPED);
158+
start = millis();
159+
while(!_p_twi->EVENTS_STOPPED) {
160+
if (millis() - start >= 3) {
161+
return 0;
162+
}
163+
}
154164
_p_twi->EVENTS_STOPPED = 0x0UL;
155165
}
156166
else
157167
{
158168
this->suspended = true;
159169
_p_twi->TASKS_SUSPEND = 0x1UL;
160-
while(!_p_twi->EVENTS_SUSPENDED);
170+
start = millis();
171+
while(!_p_twi->EVENTS_SUSPENDED) {
172+
if (millis() - start >= 3) {
173+
return 0;
174+
}
175+
}
161176
_p_twi->EVENTS_SUSPENDED = 0x0UL;
162177
}
163178

@@ -191,6 +206,7 @@ void TwoWire::beginTransmission(uint8_t address) {
191206
uint8_t TwoWire::endTransmission(bool stopBit)
192207
{
193208
transmissionBegun = false;
209+
uint32_t start;
194210

195211
// Start I2C transmission
196212
_p_twi->ADDRESS = txAddress;
@@ -202,7 +218,12 @@ uint8_t TwoWire::endTransmission(bool stopBit)
202218
{
203219
_p_twi->TXD = txBuffer.read_char();
204220

205-
while(!_p_twi->EVENTS_TXDSENT && !_p_twi->EVENTS_ERROR);
221+
start = millis();
222+
while(!_p_twi->EVENTS_TXDSENT && !_p_twi->EVENTS_ERROR) {
223+
if (millis() - start >= 10) {
224+
return 4;
225+
}
226+
}
206227

207228
if (_p_twi->EVENTS_ERROR)
208229
{
@@ -215,13 +236,23 @@ uint8_t TwoWire::endTransmission(bool stopBit)
215236
if (stopBit || _p_twi->EVENTS_ERROR)
216237
{
217238
_p_twi->TASKS_STOP = 0x1UL;
218-
while(!_p_twi->EVENTS_STOPPED);
239+
start = millis();
240+
while(!_p_twi->EVENTS_STOPPED) {
241+
if (millis() - start >= 3) {
242+
return 4;
243+
}
244+
}
219245
_p_twi->EVENTS_STOPPED = 0x0UL;
220246
}
221247
else
222248
{
223249
_p_twi->TASKS_SUSPEND = 0x1UL;
224-
while(!_p_twi->EVENTS_SUSPENDED);
250+
start = millis();
251+
while(!_p_twi->EVENTS_SUSPENDED) {
252+
if (millis() - start >= 3) {
253+
return 4;
254+
}
255+
}
225256
_p_twi->EVENTS_SUSPENDED = 0x0UL;
226257
}
227258

0 commit comments

Comments
 (0)