Skip to content

Commit bd3e0fd

Browse files
committed
Add a timeout to busio_i2c_scan
1 parent bed724f commit bd3e0fd

File tree

1 file changed

+6
-0
lines changed
  • shared-bindings/busio

1 file changed

+6
-0
lines changed

shared-bindings/busio/I2C.c

+6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333

3434
#include "shared/runtime/buffer_helper.h"
3535
#include "shared/runtime/context_manager_helpers.h"
36+
#include "py/mperrno.h"
3637
#include "py/runtime.h"
38+
#include "supervisor/port.h"
3739
#include "supervisor/shared/translate.h"
3840

3941
//| class I2C:
@@ -138,9 +140,13 @@ STATIC mp_obj_t busio_i2c_scan(mp_obj_t self_in) {
138140
busio_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
139141
check_for_deinit(self);
140142
check_lock(self);
143+
uint64_t deadline = port_get_raw_ticks(NULL) + 2048;
141144
mp_obj_t list = mp_obj_new_list(0, NULL);
142145
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
143146
for (int addr = 0x08; addr < 0x78; ++addr) {
147+
if (port_get_raw_ticks(NULL) > deadline) {
148+
mp_raise_OSError(MP_ETIMEDOUT);
149+
}
144150
bool success = common_hal_busio_i2c_probe(self, addr);
145151
if (success) {
146152
mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr));

0 commit comments

Comments
 (0)