Skip to content

Commit bf6e0d5

Browse files
authored
Merge pull request arduino#11 from bcmi-labs/fix-spi-1
Fix: Unnecessary long delay between consecutive transfers and timeout handling.
2 parents 8d7a96b + 7b97cc7 commit bf6e0d5

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

libraries/SPI/SPI.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ uint8_t ArduinoSPI::interruptMask = 0;
2525
uint8_t ArduinoSPI::interruptSave = 0;
2626

2727
static spi_event_t _spi_cb_event[13] = {SPI_EVENT_TRANSFER_ABORTED};
28-
static uint32_t timeout_ms = 1000;
2928

3029
ArduinoSPI::ArduinoSPI(spi_ctrl_t *g_spi_ctrl
3130
,const spi_cfg_t *g_spi_cfg
@@ -177,10 +176,11 @@ uint8_t ArduinoSPI::transfer(uint8_t data) {
177176
} else {
178177
R_SPI_WriteRead(_g_spi_ctrl, &data, &rxbuf, 1, SPI_BIT_WIDTH_8_BITS);
179178
}
180-
while ((SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && timeout_ms)
179+
180+
for (auto const start = millis();
181+
(SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && (millis() - start < 1000); )
181182
{
182-
timeout_ms--;
183-
delay(1);
183+
__NOP();
184184
}
185185
if (SPI_EVENT_TRANSFER_ABORTED == _spi_cb_event[_cb_event_idx])
186186
{
@@ -198,10 +198,11 @@ uint16_t ArduinoSPI::transfer16(uint16_t data) {
198198
} else {
199199
R_SPI_WriteRead(_g_spi_ctrl, &data, &rxbuf, 1, SPI_BIT_WIDTH_16_BITS);
200200
}
201-
while ((SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && timeout_ms)
201+
202+
for (auto const start = millis();
203+
(SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && (millis() - start < 1000); )
202204
{
203-
timeout_ms--;
204-
delay(1);
205+
__NOP();
205206
}
206207
if (SPI_EVENT_TRANSFER_ABORTED == _spi_cb_event[_cb_event_idx])
207208
{
@@ -218,10 +219,11 @@ void ArduinoSPI::transfer(void *buf, size_t count) {
218219
} else {
219220
R_SPI_WriteRead(_g_spi_ctrl, buf, buf, count, SPI_BIT_WIDTH_8_BITS);
220221
}
221-
while ((SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && timeout_ms)
222+
223+
for (auto const start = millis();
224+
(SPI_EVENT_TRANSFER_COMPLETE != _spi_cb_event[_cb_event_idx]) && (millis() - start < 1000); )
222225
{
223-
timeout_ms--;
224-
delay(1);
226+
__NOP();
225227
}
226228
if (SPI_EVENT_TRANSFER_ABORTED == _spi_cb_event[_cb_event_idx])
227229
{

0 commit comments

Comments
 (0)