guild icon
Toit
#using i2c from within the c-envelope and the toit container
Thread channel in help
AbedN
AbedN 02/26/2025 02:00 PM
Hi,
I'm running a toit container code on top of my custom c-envelope and it happens to be that both need i2c for different usages; i am getting this error when i run the container:

E (11421) i2c: i2c driver install error
**
Decoding by jag, device has version <2.0.0-alpha.174>
**
EXCEPTION error.
UNKNOWN ERROR 0xffffffff(-1)
0: i2c-init <sdk>/i2c.toit:324:3
1: Bus <sdk>/i2c.toit:58:12
2: LBI2CDevice <pkg:toit-lightbug>/devices/i2c.toit:14:10
3: RH2.I2C-DEVICE <pkg:toit-lightbug>/devices/rh2.toit:10:24
4: run-global-initializer.<block> <sdk>/core/objects.toit:291:28
5: run-global-initializer
<sdk>/core/objects.toit:257:1
6: RH2.I2C-WRITER <pkg:toit-lightbug>/devices/rh2.toit:12:31
7: run-global-initializer.<block> <sdk>/core/objects.toit:291:28
8: run-global-initializer
<sdk>/core/objects.toit:257:1
9: RH2.out <pkg:toit-lightbug>/devices/rh2.toit:16:12
10: Comms.sendSwitching
<pkg:toit-lightbug>/services/comms.toit:265:15
11: Comms.send <pkg:toit-lightbug>/services/comms.toit:246:5
12: main echo.toit:48:8
**

[jaguar] ERROR: program 3e919e89-5ded-63a7-c1df-5f8a8ed7ae4d stopped - exit code 1)

is there a way to go around this without having to move all i2c communication tasks to either side of the stack?
(edited)
floitsch
floitsch 02/26/2025 02:35 PM
There could be two reasons for this:
- usage of the old and the new I2C driver at the same time. This will be fixed once I migrate the Toit I2C code to the new driver API.
- you are trying to use the same I2C peripheral. From the Toit side we don't give you the option for which I2C peripheral you are going to use. We just assume that all of them are free. You could either try to use the I2C_NUM_1 in your C code (since we likely will use I2C_NUM_0 first). However, that assumes the old ESP-API. I think the new API doesn't let you choose the peripheral anymore.

I hope to start migrating the i2c within this or next week.
AbedN
AbedN 02/26/2025 03:02 PM
i'm using Arduino's Wire library for i2c on the c side, could this also be a reason?
floitsch
floitsch 02/26/2025 03:03 PM
Afaik Arduino is just a wrapper around the esp-idf.
floitsch
floitsch 02/26/2025 03:05 PM
In theory, you could create an i2c service in C that implements the I2C.
Typically i2c operations are not time-critical, so it wouldn't be a big issue that they go through the service-boundary.
However, while cool, it might not be trivial and debugging (like every C code) isn't always easy.
5 messages in total