[nRF52] I2C reconstruct issues

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
User avatar
mcauser
Posts: 491
Joined: Mon Jun 15, 2015 8:03 am

[nRF52] I2C reconstruct issues

Post by mcauser » Sat Dec 05, 2020 10:17 am

Found two few issues with HW I2C on the nRF52832.

1) Constructing a new I2C instance does not deinit the previous peripheral.
2) A soft reboot does not deinit the previous HW I2C.

Running latest MicroPython on the Ebyte E73-TBB dev board (E73-2G4M04S1B module)
MicroPython v1.13-221-gc8b055717 on 2020-12-05; E73-Demo with NRF52832

1) Constructing a new I2C instance does not deinit the previous peripheral.

* construct a HW I2C on the wrong pins
* scan() does not find an I2C sensor
* reconstruct on the right pins (pins seem to be ignored)
* scan() does not find an I2C sensor

Code: Select all

>>> from machine import Pin, I2C
>>> i2c = I2C(0, scl=Pin(4), sda=Pin(3)) # oops scl/sda backwards
>>> i2c.scan()
[] # as expected

>>> i2c = I2C(0, scl=Pin(3), sda=Pin(4)) # correct pins
>>> i2c.scan()
[] # was expecting [30]

# hard reset
>>> import machine
>>> machine.reset()

>>> from machine import Pin, I2C
>>> i2c = I2C(0, scl=Pin(3), sda=Pin(4)) # correct pins
>>> i2c.scan()
[30] # as expected
* construct a HW I2C on the right pins
* scan() finds the sensor
* reconstruct on the wrong pins (pins seem to be ignored)
* scan() still finds the sensor (using previous config?)

Code: Select all

>>> from machine import Pin, I2C
>>> i2c = I2C(0, scl=Pin(3), sda=Pin(4)) # correct pins
>>> i2c.scan()
[30] # as expected

# reconstruct
>>> i2c = I2C(0, scl=Pin(4), sda=Pin(3)) # oops scl/sda backwards
>>> i2c.scan()
[30] # was expecting [] - it's using the previous configured pins (scl=3, sda=4)
2) A soft reboot does not deinit the previous HW I2C.

* construct a HW I2C on the right pins
* scan() finds the sensor
* soft reset
* construct a HW I2C on the wrong pins (pins seem to be ignored)
* scan() still finds the sensor (using previous config?)

Code: Select all

>>> from machine import Pin, I2C
>>> i2c = I2C(0, scl=Pin(3), sda=Pin(4)) # correct pins
>>> i2c.scan()
[30] # as expected

CTRL+D
MPY: soft reboot

>>> from machine import Pin, I2C
>>> i2c = I2C(0, scl=Pin(4), sda=Pin(3)) # oops scl/sda backwards
>>> i2c.scan()
[30] # was expecting [] - it's using the pins defined before the reboot
* construct a HW I2C on the wrong pins
* scan() does not find the sensor
* hard reset
* construct a HW I2C on the right pins
* scan() finds the sensor

Code: Select all

>>> from machine import Pin, I2C
>>> i2c = I2C(0, scl=Pin(4), sda=Pin(3)) # oops scl/sda backwards
>>> i2c.scan()
[] # as expected

# hard reset
>>> import machine
>>> machine.reset()

>>> from machine import Pin, I2C
>>> i2c = I2C(0, scl=Pin(3), sda=Pin(4)) # correct pins
>>> i2c.scan()
[30] # as expected - the hard reboot cleared the previous incorrect config

User avatar
mcauser
Posts: 491
Joined: Mon Jun 15, 2015 8:03 am

Re: [nRF52] I2C reconstruct issues

Post by mcauser » Mon Dec 07, 2020 12:11 am


c45713
Posts: 51
Joined: Fri Dec 09, 2016 7:09 pm

Re: [nRF52] I2C reconstruct issues

Post by c45713 » Mon Dec 07, 2020 7:49 pm

This is definitely an interesting observation. I'll answer here and not on github as this might take some time to figure out.

I've peeked into the source a bit, and i have one hypothesis:

First, error code of nrfx_twi_init() is not checked.
https://github.com/glennrub/micropython ... i2c.c#L123

Second, there is probably an error reported from that init() call. Due to the implementation of the nrfx twi driver:
https://github.com/NordicSemiconductor/ ... twi.c#L111
https://github.com/NordicSemiconductor/ ... twi.c#L147

The underlying driver does not clear its statics on a soft reset. Meaning, it will report the error and not update with the new configuration as statics are preserved during the soft reset.

I'll verify if this is the case. If so, i do not have an immediate fix for it, other than sending a PR to nrfx or jump to a fork driver.
(Not changed in newer nrfx version).

Cheers,
Glenn

c45713
Posts: 51
Joined: Fri Dec 09, 2016 7:09 pm

Re: [nRF52] I2C reconstruct issues

Post by c45713 » Mon Dec 07, 2020 8:33 pm

Maybe not that hard after all. I was a bit quick replying without looking carefully at the nrfx driver.

I guess the problem can be solved by issuing nrfx_twi_uninit() between the init's.
As i'm running a bit out of time to test it today, i've made a proposal on what direction i'm thinking:
https://github.com/glennrub/micropython ... f66600bd7a

Post Reply