Skip to content

i2c_bus crashes VM on ESP32-C6: i2c:open/1 uses open_port which may not exist ? #17

@wmealing

Description

@wmealing

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

  1. Create an AtomVM app that calls i2c_bus:start(#{sda => 6, scl => 7, freq_hz => 100000})
  2. Flash and boot on ESP32-C6
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions