i2c.try_lock hangs

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
cagiva
 
Posts: 56
Joined: Wed Nov 04, 2015 10:00 pm

i2c.try_lock hangs

Post by cagiva »

I'm using an ESP32-S3 TFT Feather with the Adalogger FeatherWing and if I call the try_lock code below from code.py, then it hangs. Could it be that the CircuitPython library is managing the I2C lock for me automatically by grabbing it before it does an I2C transaction?

Code: Select all

import board
i2c = board.I2C()
while not i2c.try_lock():  pass
Running this code from REPL works fine though.

Code: Select all

import board; i2c=board.I2C(); i2c.try_lock(); [hex(a) for a in i2c.scan()]; i2c.unlock()

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: i2c.try_lock hangs

Post by danhalbert »

Is your first example the whole program? If not, could you post the whole program, or at least up to the point of error?

What version of CircuitPython are you using?

This works for me on a Feather ESP32-S3 running a post 8.0.0-beta.6 build, but I would expect it to work on any build. It prints "got lock" and then loops.

Code: Select all

import board
i2c = board.I2C()
while not i2c.try_lock():
    print("failed to get lock")
    pass
print("got lock")
while True:
    pass

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: i2c.try_lock hangs

Post by blakebr »

Dan,

Could these be related?
viewtopic.php?t=198129
Bruce

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: i2c.try_lock hangs

Post by danhalbert »

@blakebr - I don't think so; this is very simple without the extra libraries, it appears. But I'd like to see more code.

User avatar
cagiva
 
Posts: 56
Joined: Wed Nov 04, 2015 10:00 pm

Re: i2c.try_lock hangs

Post by cagiva »

@danhalbert,

It turned out that it's actually hanging in the PCF8523 call.

Code: Select all

import board
import adafruit_pcf8523

i2c = board.I2C()
while not i2c.try_lock():
    print("failed to get lock")
    pass
print("got lock")

rtc = adafruit_pcf8523.PCF8523(i2c)  #<--- Hangs here

User avatar
danhalbert
 
Posts: 4652
Joined: Tue Aug 08, 2017 12:37 pm

Re: i2c.try_lock hangs

Post by danhalbert »

Don't grab the lock before you do`adafruit_pcf8523.PCF8523(i2c)`. Just take that code away. All our libraries take care of managing the locking themselves. They use `adafruit_busdevice`, which does the lock management.

User avatar
cagiva
 
Posts: 56
Joined: Wed Nov 04, 2015 10:00 pm

Re: i2c.try_lock hangs

Post by cagiva »

Copy that. Thanks @danhalbert. 👍

User avatar
blakebr
 
Posts: 957
Joined: Tue Apr 17, 2012 6:23 pm

Re: i2c.try_lock hangs

Post by blakebr »

Dan,

I put the code in the original thread.
I apologize for stepping on this thread.

Bruce

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”