Problem with I2C.scan() on ESP8266 with 1.19.1? [Solved]

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Gorgus
Posts: 2
Joined: Sat Jan 30, 2021 7:33 pm
Location: Sweden

Problem with I2C.scan() on ESP8266 with 1.19.1? [Solved]

Post by Gorgus » Sun Aug 07, 2022 5:07 pm

First post, please be gentle.

I have problems with a ESP8266, actually a Wemos D1 Mini, with verssion 1.19.1 of the MicroPython firmware.

If I run the following to see what devices are on the I2C-bus the ESP8266 reboots when reaching the i2c.scan()-line.

Code: Select all

import machine
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))

print('Scan i2c bus...')
devices = i2c.scan()
Started reinstalling firmware, Thonny and some other things, still the same. Tried a nightly build, v1.19.1-240-g9dfabcd6d, but the problem still persisted.
If I use the 1.18 firmware it works ok so I guess it may have something to do with the firmware.

Has something new been introduced in 1.19.1 that I have missed or is it a bug in the firmware?

Any help or insights appreciated.
Last edited by Gorgus on Sun Aug 14, 2022 10:12 am, edited 1 time in total.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Problem with I2C.scan() on ESP8266 with 1.19.1?

Post by scruss » Sun Aug 07, 2022 6:19 pm

I found it crashes if you have no I2C devices attached, but runs properly if you do. MicroPython v1.19.1 on 2022-06-18 on a D1 mini v4.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with I2C.scan() on ESP8266 with 1.19.1?

Post by Roberthh » Sun Aug 07, 2022 7:45 pm

That is related to the level at the SCL pin. If the level is high, then all works fine. If the level is low, then it resets with reset cause 3, which is SOFT_WDT_RESET. scl is kept low for clock stretching, which may have be newly supported with v1.19.1.
Note that SOFT_WDT_RESET is not defined in the machine module.
Edit: The 8266 only supports SoftI2C. It should be possible to have a better exception handling in soft_i2c than causing a reset.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with I2C.scan() on ESP8266 with 1.19.1?

Post by Roberthh » Sun Aug 07, 2022 8:18 pm

You can avoid the reset by setting a timeout value in the I2C instantiation. E.g.

i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4), timeout=1000)

And that is documented, but I never noticed it.
Edit: Looking into the code, the default timeout is 50000 = 50ms. For a single i2c.write() that works and returns a Timeout error. For I2c.scan() with it's burst of 112 single byte write that's too long. The Software WDT time seems to be ~1 second. So the largest timeout that can be set is about 8000 = 8 ms. That seems long, but sometimes sensors use the clock extension to hold the requester during conversion.

Edit 2: Needless to say that with a proper wiring of the interface the i2c.scan() problem is gone. The only irritating aspect is the reset happening.

Edit 3: Adding a MICROPY_EVENT_POLL_HOOK into the scan loop of the function machine_i2c_scan() solves the "problem", but that would affect all other ports as well. So the change in favor of only ESP8266 ports seems not useful, because otherwise the exmod/machine_i2c.c module is device independent.

Gorgus
Posts: 2
Joined: Sat Jan 30, 2021 7:33 pm
Location: Sweden

Re: Problem with I2C.scan() on ESP8266 with 1.19.1?

Post by Gorgus » Mon Aug 08, 2022 1:22 pm

Thanks to both of you!

scruss: Have been using i2c.scan() to check if my wiring is correct, so I guess it is not.

Roberthh: Nice to know what it is that happens. Will investigate your solutions.

Post Reply