Skip to content

i2c timeouts not working by default (due to disabled DWT cycle counter) #300

Open
@inodentry

Description

@inodentry

I am using some I2C sensors that are not 100% reliable and sometimes the connection breaks.

I2C timeouts were not working, regardless of the values I provided to the builder functions when initializing the peripheral, causing my program to occasionally hang indefinitely when trying to use I2C, with no way out (other than reset or interrupt). I spent many hours in frustration, trying to figure out why this was happening.

After digging through the I2C code in this crate, I discovered that the blocking I2C implementation internally relies on the cycle counter from the DWT to implement timeouts. However, that cycle counter starts out disabled by default and must be explicitly enabled!

This was not documented anywhere!

Why does this crate not automatically take care of enabling the DWT cycle counter (that it relies on) when using blocking I2C? This should be fixed. All the APIs should automatically configure the hardware state they need.

If not, at the very least, it should be documented.

Just adding this one line to my program fixed everything and now my code works without issue:

core.DWT.enable_cycle_counter()

However, I had no way of knowing that I needed this! I spent many hours trying to figure it out!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions