summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2021-03-03 09:43:57 -0500
committerDan Halbert <halbert@halwitz.org>2021-03-03 09:43:57 -0500
commitfb7a0f7efcdd639ff4a14a2985f9381c426b2321 (patch)
treef9d1f78ba05ac5664f57f11ce1600268349c2298
parente3ab394cd6634aa0eefa64872461b5dc0bbfdc1a (diff)
add 1sec timeouts for I2C read and write
-rw-r--r--ports/raspberrypi/Makefile1
-rw-r--r--ports/raspberrypi/common-hal/busio/I2C.c29
2 files changed, 22 insertions, 8 deletions
diff --git a/ports/raspberrypi/Makefile b/ports/raspberrypi/Makefile
index 9d66338c7..ffd50299f 100644
--- a/ports/raspberrypi/Makefile
+++ b/ports/raspberrypi/Makefile
@@ -157,6 +157,7 @@ SRC_SDK := \
src/common/pico_sync/lock_core.c \
src/common/pico_sync/mutex.c \
src/common/pico_time/time.c \
+ src/common/pico_time/timeout_helper.c \
src/common/pico_util/pheap.c \
src/rp2_common/hardware_adc/adc.c \
src/rp2_common/hardware_claim/claim.c \
diff --git a/ports/raspberrypi/common-hal/busio/I2C.c b/ports/raspberrypi/common-hal/busio/I2C.c
index 6886b3815..cb0d73e75 100644
--- a/ports/raspberrypi/common-hal/busio/I2C.c
+++ b/ports/raspberrypi/common-hal/busio/I2C.c
@@ -38,6 +38,9 @@
#define NO_PIN 0xff
+// One second
+#define BUS_TIMEOUT_US 1000000
+
STATIC bool never_reset_i2c[2];
STATIC i2c_inst_t* i2c[2] = {i2c0, i2c1};
@@ -177,24 +180,34 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr,
return status;
}
- int result = i2c_write_blocking(self->peripheral, addr, data, len, !transmit_stop_bit);
+ int result = i2c_write_timeout_us(self->peripheral, addr, data, len, !transmit_stop_bit, BUS_TIMEOUT_US);
if (result == len) {
return 0;
- } else if (result == PICO_ERROR_GENERIC) {
- return MP_ENODEV;
}
- return MP_EIO;
+ switch (result) {
+ case PICO_ERROR_GENERIC:
+ return MP_ENODEV;
+ case PICO_ERROR_TIMEOUT:
+ return MP_ETIMEDOUT;
+ default:
+ return MP_EIO;
+ }
}
uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr,
uint8_t *data, size_t len) {
- int result = i2c_read_blocking(self->peripheral, addr, data, len, false);
+ int result = i2c_read_timeout_us(self->peripheral, addr, data, len, false, BUS_TIMEOUT_US);
if (result == len) {
return 0;
- } else if (result == PICO_ERROR_GENERIC) {
- return MP_ENODEV;
}
- return MP_EIO;
+ switch (result) {
+ case PICO_ERROR_GENERIC:
+ return MP_ENODEV;
+ case PICO_ERROR_TIMEOUT:
+ return MP_ETIMEDOUT;
+ default:
+ return MP_EIO;
+ }
}
void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {