I was asked to report the issue from users on telegram, I think that this is a bgu in atomvm_lib, but maybe its atomvm.
i2c_bus:start/1 crashes the AtomVM VM on ESP32-C6 when called. The root cause is that i2c:open/1 internally calls open_port({spawn, "i2c"},
...), which causes a fatal VM crash (Load access fault / Guru Meditation Error) on ESP32-C6. My fix is/was to use i2c:open_nif/1 directly.
Environment
Steps to Reproduce
- Create an AtomVM app that calls i2c_bus:start(#{sda => 6, scl => 7, freq_hz => 100000})
- Flash and boot on ESP32-C6
- VM crashes immediately with a Load access fault / Guru Meditation Error before any I2C operation is attempted ( see here)
Root Cause
In src/i2c_bus.erl, init/1 calls:
I2C = i2c:open([
{clock_speed_hz, maps:get(freq_hz, Options)}
| maps:to_list(Options)
]),
i2c:open/1 in AtomVM resolves to open_port({spawn, "i2c"}, ...). On ESP32-C6, this crashes the VM. The NIF path (i2c:open_nif/1) works correctly.
Fix
Replace the i2c:open/1 call in init/1 with i2c:open_nif/1. Also, as a heads up, i tripped up passing clock_speed_hz in my args, which i think overwrote the freq_hz in the map (oops!)
init(Options) ->
I2C = i2c:open_nif([
{sda, maps:get(sda, Options)},
{scl, maps:get(scl, Options)},
{clock_speed_hz, maps:get(freq_hz, Options, 400000)}
]),
Notes
- This patch has been verified to fix i2c_bus + bme280 on ESP32-C6 with AtomVM v0.8.0-dev, producing correct sensor readings.
- I only assume that this is the correct behavior, i'm not sure if the fix breaks any other platform as i dont have i2c to test on other platforms.
You can do the commit yourself, or if you want, I can make the PR if thats easier.
This is my first look at i2c, so i could be doing things entirely wrong. Thank you.
Proposed patch:
https://github.com/wmealing/atomvmlib_test/blob/main/patch.diff
I was asked to report the issue from users on telegram, I think that this is a bgu in atomvm_lib, but maybe its atomvm.
i2c_bus:start/1 crashes the AtomVM VM on ESP32-C6 when called. The root cause is that i2c:open/1 internally calls open_port({spawn, "i2c"},
...), which causes a fatal VM crash (Load access fault / Guru Meditation Error) on ESP32-C6. My fix is/was to use i2c:open_nif/1 directly.
Environment
Steps to Reproduce
Root Cause
In src/i2c_bus.erl, init/1 calls:
i2c:open/1 in AtomVM resolves to open_port({spawn, "i2c"}, ...). On ESP32-C6, this crashes the VM. The NIF path (i2c:open_nif/1) works correctly.
Fix
Replace the i2c:open/1 call in init/1 with i2c:open_nif/1. Also, as a heads up, i tripped up passing clock_speed_hz in my args, which i think overwrote the freq_hz in the map (oops!)
Notes
You can do the commit yourself, or if you want, I can make the PR if thats easier.
This is my first look at i2c, so i could be doing things entirely wrong. Thank you.
Proposed patch:
https://github.com/wmealing/atomvmlib_test/blob/main/patch.diff