Skip to content

I2C is easy to misuse #13

@multiplemonomials

Description

@multiplemonomials

The Mbed I2C implementation has two restrictions that are easy to violate:

  • Only one I2C object may be created for each I2C peripheral on the chip
  • The frequency must be set at the bus level, not at the per-device level, because I2C has to run at the lowest-common-demoninator speed of the slowest device on the bus.

Unfortunately, these are not made clear in the docs, and even some of the built-in drivers (I2CEEBlockDevice) violate them e.g. by having a constructor that takes pins instead of an I2C object. To make matters worse, violating the restrictions will appear to work fine in some scenarios, only to cause undefined behavior later on.

To fix this, we should:

  • Implement asserts (or at least warnings) that detect when multiple I2C instances are created on the same hardware, and when the frequency of a bus is set multiple times.
  • Refactor all first-party code to only take an I2C instance, not pins, and not set the frequency after construction
  • Add information to the docs about this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    UsabilityThis makes Mbed harder to use, and needs to be cleaned up.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions