Lopy4 I2C trouble [OSError: I2C bus error]
-
Hi , i have a small trouble with i2c and Lopy4
my firmware version is MicroPython v1.8.6-849-83e2f7f on 2018-03-19; LoPy4 with ESP32
I have several slaves on one I2C bus and only one on the second i2c bus
When i work with one instance of i2c with several slave all work fine, but if i initialize a second instance i2c of first instance fail to work and return OSError: I2C bus error
Sorry for my english, it's not my native language, some code will be help to understood
Any idea to deal with this bug ? I have misunderstood how i2c work on lopy
?from machine import I2C from machine import Timer import utime import PCA9534 as Slio import zonemgt as Heating import LM73 as Thermal import SSD1306 as Screen import MCP7940 as Rtc #I2C0 for SSD1306, LM73, and MCP7940 i2c=I2C(0) i2c.init(I2C.MASTER,baudrate=400000) Screen.initialize(Screen.kDisplayI2C128x64,i2c) #<-work fine Rtc.init(i2c) #<-work fine, rtc is read on set to correct time Temperature=Thermal.read_temp(i2c) #<-work temperature is read from sensor print("Rtc init done") print("Thermal="+str(Temperature)) Rtc.init(i2c) #<-still working fine i2c_slio=I2C(1) i2c_slio.init(I2C.MASTER, pins=('P23','P22'),baudrate=400000) Rtc.init(i2c) #<- any access to I2C0 slave fail with OSError, here with Rtc but LM73 or SSD1306 cannot be read/write all i2c0 function fail
-
@Eric73 Thank you. It helped me
-
@xykon Thank you very much, your explanation make sense, now my software works !
-
@eric73 I think the reason it gets stuck is because when you run
i2c_slio=I2C(1)
you assign I2C bus 1 to the default gpio pins. Now I2C bus 0 no longer has any pins assigned to it and that's why the scan is never ending.Either way I'll get a notification if there are any updates here so let me know when you have a chance to test this.
-
@xykon Thank for your feedback, unfortunatly i can't test this code before 15th may , i will post you a result at this date (i have a lot of holliday to finish before end of may and i don't come back at work before 15th may).
I think you don't need any hardware to test this behaviour, just having the programm terminating is a good think as actually it's stuck in infinite loop in latest
slave=i2c.scan()
-
@eric73 Can you try the code like this please?
from machine import I2C i2c=I2C(0, mode=I2C.MASTER,baudrate=400000) slave=i2c.scan() #<- show slave list print(str(slave)) slave=i2c.scan() #<-show slave list print(str(slave)) i2c_slio=I2C(1, mode=I2C.MASTER, pins=('P23','P22'),baudrate=400000) slave=i2c_slio.scan() #<-show slave list print(str(slave)) slave=i2c.scan() #<- board stucked in infinite loop, REPL is no more responsive to command print(str(slave))
Please let me know if this works better otherwise I'll set up some hardware to test this further...
-
@robert-hh Thank for your answer, i will specify my hardware setting, i have designed a custom board hosting my lopy4.
So on P23 there's no sd-card i only wiil used it as i2c signal. In fact, when i used it as i2c-p23, it work fine for i2c communication (i can scan and read write for my slave).
The "bug" is after i call init function for I2C(1) i cannot still use I2C(0) communication on default pins (P9-P10) (scan will run in endless loop and read or write function cause an os-error).
I seem that i can't use simultaneously the both I2C instance ?
-
@eric73 P23 is used also for the SD card. Maybe that is the reason for your trouble.
-
A simplier code that everibody can run will show similar behaviour
from machine import I2C i2c=I2C(0) i2c.init(I2C.MASTER,baudrate=400000) slave=i2c.scan() #<- show slave list print(str(slave)) slave=i2c.scan() #<-show slave list print(str(slave)) i2c_slio=I2C(1) i2c_slio.init(I2C.MASTER, pins=('P23','P22'),baudrate=400000) slave=i2c_slio.scan() #<-show slave list print(str(slave)) slave=i2c.scan() #<- board stucked in infinite loop, REPL is no more responsive to command print(str(slave))