From a707fe50b085ec83722106609f6fd219faf9f030 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 21 Jan 2022 14:52:03 +1100 Subject: [PATCH] rp2/machine_i2c: Use soft I2C only for len=0, and increase timeout. The RP2040 I2C hardware can do writes of length 1 and 2, just not of length 0. So only use software I2C for writes of length 0, to improve performance. Also increase the software I2C timeout for zero-length writes to accommodate the behaviour of a wider range of I2C devices. Fixes issue #8167. Signed-off-by: Damien George --- ports/rp2/machine_i2c.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/rp2/machine_i2c.c b/ports/rp2/machine_i2c.c index 56f012a5e8..f785ad7ded 100644 --- a/ports/rp2/machine_i2c.c +++ b/ports/rp2/machine_i2c.c @@ -127,12 +127,12 @@ STATIC int machine_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, si if (flags & MP_MACHINE_I2C_FLAG_READ) { ret = i2c_read_blocking(self->i2c_inst, addr, buf, len, nostop); } else { - if (len <= 2) { - // Workaround issue with hardware I2C not accepting short writes. + if (len == 0) { + // Workaround issue with hardware I2C not accepting zero-length writes. mp_machine_soft_i2c_obj_t soft_i2c = { .base = { &mp_machine_soft_i2c_type }, .us_delay = 500000 / self->freq + 1, - .us_timeout = 255, + .us_timeout = 50000, .scl = self->scl, .sda = self->sda, };