← index #13536PR #13281
Related · high · value 0.301
QUERY · ISSUE

RP2040 I2C bus breaks if pins are changed at runtime

openby desultoryopened 2024-01-27updated 2024-02-12

MicroPython v1.22.1 on 2024-01-05; Raspberry Pi Pico with RP2040

If I attempt to re-init the bus on another set of pins, things break and cannot recover until I do a full hardware reset:

>>> from machine import Pin, I2C
>>> a = I2C(1, freq=400000, scl=27, sda=26)
>>> a.readfrom_mem(118, 0xA1, 1)
b'K'
>>> a = I2C(1, freq=400000, scl=3, sda=2)
>>> a.readfrom_mem(118, 0xA1, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 5] EIO
>>> a = I2C(1, freq=400000, scl=27, sda=26)
>>> a.readfrom_mem(118, 0xA1, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 5] EIO

I'm not sure how else I can recover from this other than unplugging it and plugging it back in, soft resets do not correct the issue.

CANDIDATE · PULL REQUEST

machine_i2c: Add I2C bus clear support.

closedby PepperoniPinguopened 2023-12-27updated 2025-06-13
extmod

I have some software that is using I2C on the rp2040. Over a couple of months of continuous running there have been some failed transactions resulting in exceptions. Since these are rare, and, the i2c waveform is not too pretty when looked at with an oscilloscope, I presume my hardware implementation is at fault. Nevertheless, since this system is running 24/7 in a critical environment it is essential to be able to recover from these crashes. The proper way to do this is to send an I2C bus clear sequence (a.k.a. software reset) and retry. To do the bus clearing you simply send 10 negative pulses on SCL. This clocks out potential stuck data in the output registers of peripherals.

This PR implements:

  • API backend support for sending an I2C bus clear sequence
    • accessible from python by i2c.clear_bus()
  • said function in the:
    • soft I2C driver
    • rp2 I2C driver

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied