guild icon
Toit
#State bug with I2C library
Thread channel in help
z3ugma
z3ugma 01/01/2025 11:27 PM
I'm reading from an I2C sensor chip.
when I use Toit, I am not getting the results I expect per the datasheet.
When I use Arduino's Wire library, I get exactly what I expect. Here's the code comparison between the two of them.

See the attachment for the box highlighted in Red - that's the Toit script causing the chip to misbehave.
With toit, I always get exactly whatever the first value I read from the chip. With Arduino I can read different registers without issue.

I am starting to suspect it may come down to the default state of GPIO pins, one system having it high and the other having it low, or something.

I have a logic analyzer, an ESP32 dev kit WROOM-32D
z3ugma
z3ugma 01/01/2025 11:27 PM
Arduino code
z3ugma
z3ugma 01/01/2025 11:27 PM
Toit code:
import gpio import i2c bus := i2c.Bus --sda=gpio.Pin 16 --scl=gpio.Pin 26 --frequency=80_000 device := bus.device 0x57 a0 := gpio.Pin 14 --output motion := gpio.Pin 27 --input // clock := gpio.Pin 26 --output // iodev := gpio.Pin 16 --output shutdown := gpio.Pin 17 --output nrst := gpio.Pin 22 --output vddio := gpio.Pin 21 --output //For TWI: // 1. Apply power. Refer timing diagram on power up // sequence below. // 2. Set SHTDWN and IO_SELECT pin low. // 3. Set A0 and A1 according to the desired TWI slave // address (from TWI slave address table in datasheet). // 4. Drive NRST pin low then high. TWI slave address will // only be valid and according to the address set in step // 3 after a NRST toggle is applied. // 5. Read Product ID (PID) to ensure sensor is powered up // and communicating properly with host. // 6. Write 0xE4 to address 0x60 // 7. Write 0xC9 to address 0x61 main: shutdown.set 0 a0.set 1 vddio.set 1 // Drive NRST low, then high nrst.set 0 // Do this last to turn on the chip? sleep --ms=80 nrst.set 1 sleep --ms=10 // 6. Write 0xE4 to address 0x60 // 7. Write 0xC9 to address 0x61 // Read Product ID (PID) to ensure sensor is powered up // and communicating properly with host. device.write-reg 0x60 #[0xE4] device.write-reg 0x61 #[0xC9] value2 := device.read-reg 0 1 print value2 sleep --ms=50 value := device.read-reg 0x3f 1 print value sleep --ms=10 sleep --ms=2000 print "Done" device.close
floitsch
floitsch 01/01/2025 11:29 PM
Do you have the datasheet of the sensor?
z3ugma
z3ugma 01/01/2025 11:31 PM
see pg 7 "Notes on power up sequence"
z3ugma
z3ugma 01/01/2025 11:31 PM
The I2c is the "TWI" Two Wire Interface on the datasheet
floitsch
floitsch 01/01/2025 11:32 PM
I will have a look tomorrow. We did do an esp-idf upgrade recently, but if I2C works normally for my sensors it will be hard to debug. I might write a bit-banging library then, but that will take more time.
z3ugma
z3ugma 01/01/2025 11:34 PM
great! happy new year, enjoy your sleep :🙂:
floitsch
floitsch 01/01/2025 11:34 PM
Program your microcontrollers in a fast and robust high-level language. - toitlang/toit
z3ugmaOPz3ugma
great! happy new year, enjoy your sleep :🙂:
floitsch
floitsch 01/01/2025 11:35 PM
Happy new year to you too!
floitsch
floitsch 01/02/2025 03:06 PM
I have started looking into this.
I started with one of my i2c devices and that one still works. At least it's not a general I2C issue. (Although that would have made it easier to debug...)
z3ugma
z3ugma 01/02/2025 04:21 PM
ok, thank you for confirming
z3ugma
z3ugma 01/02/2025 04:21 PM
further testing has be thinking this may be an issue with the way I am powering the sensor or the way I am pulling up/pulling down other control lines
floitsch
floitsch 01/02/2025 04:22 PM
Your logic analyzer seems to indicate that Arduino and Toit have the same "messages".
floitsch
floitsch 01/02/2025 04:22 PM
the start, stop, acks, ... all look the same.
z3ugma
z3ugma 01/02/2025 04:22 PM
I have been able to make the error state occur on both Arduino and Toit. And yes, I can also see that both have the same message
floitsch
floitsch 01/02/2025 04:23 PM
One thing I noticed: the sensor wants 100ms of "reset wait time after stable supply voltage".
floitsch
floitsch 01/02/2025 04:23 PM
That sleep seems to be missing.
z3ugma
z3ugma 01/02/2025 04:23 PM
I'll try adding it in. The datasheet is not super clear on whether they mean the VDDIO IO supply voltage or the VDDA analog supply voltage
floitsch
floitsch 01/02/2025 04:24 PM
The power-up diagram has t_vrt_nrst after vddio went up.
floitsch
floitsch 01/02/2025 04:25 PM
Either way: it can't hurt.
floitsch
floitsch 01/02/2025 04:27 PM
off-topic (out of curiosity): how do you communicate with the chip? 1.8v or 2.8v?
how do you level-shift?
z3ugma
z3ugma 01/02/2025 04:28 PM
there's some sense here - I think the default state of GPIO output pins is different between arduino and toit. is that right? I feel like I remember one being GPIO default == off.

I'm using one of the bidirectional logic shift breakout boards https://www.amazon.com/dp/B07LG646VS
KeeYees 10pcs 4 Channels IIC I2C Logic Level Converter Bi-Directional Module 3.3V to 5V Shifter for Arduino (Pack of 10)
👍1
floitsch
floitsch 01/02/2025 04:29 PM
In Toit: by default output pins are set to 0.
z3ugma
z3ugma 01/02/2025 04:29 PM
uses a MOSFET and pull up resistors so that either side can pull logic low
👍1
floitsch
floitsch 01/02/2025 04:29 PM
If you just allocate them (without --output) they are floating.
z3ugma
z3ugma 01/02/2025 04:29 PM
oh and it's at 1.8V for simplicity.
z3ugma
z3ugma 01/02/2025 04:30 PM
i think in my case Hi-Z implies it will be pulled up then
floitsch
floitsch 01/02/2025 04:30 PM
One thing you could try: When allocating the GPIO pins you can use --value=... to set the start value.
floitsch
floitsch 01/02/2025 04:31 PM
a0 is probably the most likely candidate.
floitsch
floitsch 01/02/2025 04:31 PM
It must be 1 at start.
floitsch
floitsch 01/02/2025 04:32 PM
I don't see any timing constraints on a0, but can't hurt to be set it directly to 1.
floitsch
floitsch 01/02/2025 04:32 PM
(alternatively, put a sleep before the vddio.set 1)(edited)
floitsch
floitsch 01/02/2025 04:33 PM
Actually: same applies for the nrst GPIO.
floitsch
floitsch 01/02/2025 04:33 PM
Probably a good idea to set it to 1 at allocation.
37 messages in total