8000 rp2/machine_i2c: Implement I2C bus clear on rp2. · micropython/micropython@418653a · GitHub
[go: up one dir, main page]

Skip to content

Commit 418653a

Browse files
rp2/machine_i2c: Implement I2C bus clear on rp2.
Since the rp2040 hardware I2C block does not support sending a bus clear sequence this is accomplished by using the soft I2C implementation of bus clearing. Signed-off-by: Hugo Frisk <hugopaf@outlook.com>
1 parent a0db231 commit 418653a

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

ports/rp2/machine_i2c.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,26 @@ STATIC int machine_i2c_transfer_single(mp_obj_base_t *self_in, uint16_t addr, si
189189
}
190190
}
191191

192+
STATIC int machine_i2c_clear_bus(mp_obj_base_t *self_in) {
193+
machine_i2c_obj_t *self = (machine_i2c_obj_t *)self_in;
194+
// Workaround issue with hardware I2C not supporting bus clear
195+
mp_machine_soft_i2c_obj_t soft_i2c = {
196+
.base = { &mp_machine_soft_i2c_type },
197+
.us_delay = 500000 / self->freq + 1,
198+
.us_timeout = self->timeout,
199+
.scl = self->scl,
200+
.sda = self->sda,
201+
};
202+
mp_hal_pin_open_drain(self->scl);
203+
mp_hal_pin_open_drain(self->sda);
204+
int ret = mp_machine_soft_i2c_clear_bus(&soft_i2c.base);
205+
gpio_set_function(self->scl, GPIO_FUNC_I2C);
206+
gpio_set_function(self->sda, GPIO_FUNC_I2C);
207+
return ret;
208+
}
209+
192210
STATIC const mp_machine_i2c_p_t machine_i2c_p = {
211+
.clear_bus = machine_i2c_clear_bus,
193212
.transfer = mp_machine_i2c_transfer_adaptor,
194213
.transfer_single = machine_i2c_transfer_single,
195214
};

0 commit comments

Comments
 (0)
0