Skip to content

Commit 8808458

Browse files
committed
add common_hal_busio_spi_wait_for_lock()
1 parent 76aa0c7 commit 8808458

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

shared-bindings/busio/SPI.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99

1010
#include <string.h>
1111

12+
#include "py/binary.h"
13+
#include "py/mperrno.h"
14+
#include "py/objproperty.h"
15+
#include "py/runtime.h"
1216
#include "shared-bindings/microcontroller/Pin.h"
1317
#include "shared-bindings/busio/SPI.h"
1418
#include "shared-bindings/util.h"
15-
1619
#include "shared/runtime/buffer_helper.h"
1720
#include "shared/runtime/context_manager_helpers.h"
18-
#include "py/binary.h"
19-
#include "py/mperrno.h"
20-
#include "py/objproperty.h"
21-
#include "py/runtime.h"
21+
#include "supervisor/shared/tick.h"
2222

2323

2424
//| class SPI:
@@ -494,3 +494,16 @@ MP_DEFINE_CONST_OBJ_TYPE(
494494
busio_spi_obj_t *validate_obj_is_spi_bus(mp_obj_t obj, qstr arg_name) {
495495
return mp_arg_validate_type(obj, &busio_spi_type, arg_name);
496496
}
497+
498+
// Wait as long as needed for the lock. This is used by SD card access from USB.
499+
// The default implementation is to busy-wait while running the background tasks. espressif is different.
500+
bool common_hal_busio_spi_wait_for_lock(busio_spi_obj_t *self, uint32_t timeout_ms) {
501+
uint64_t deadline = supervisor_ticks_ms64() + timeout_ms;
502+
while (supervisor_ticks_ms64() < deadline) {
503+
if (common_hal_busio_spi_try_lock(self)) {
504+
return true;
505+
}
506+
RUN_BACKGROUND_TASKS;
507+
}
508+
return false;
509+
}

shared-bindings/busio/SPI.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,7 @@ uint8_t common_hal_busio_spi_get_polarity(busio_spi_obj_t *self);
5454
extern void common_hal_busio_spi_never_reset(busio_spi_obj_t *self);
5555

5656
extern busio_spi_obj_t *validate_obj_is_spi_bus(mp_obj_t obj_in, qstr arg_name);
57+
58+
// Wait as long as needed for the lock. This is used by SD card access from USB.
59+
// For most ports, busy-wait while running the background tasks.
60+
MP_WEAK bool common_hal_busio_spi_wait_for_lock(busio_spi_obj_t *self, uint32_t timeout_ms);

0 commit comments

Comments
 (0)