rust-embedded / embedded-hal

A Hardware Abstraction Layer (HAL) for embedded systems
Apache License 2.0
2.01k stars 202 forks source link

non blocking read #509

Closed JonasFocke01 closed 1 year ago

JonasFocke01 commented 1 year ago

Hello :),

i have a i2c sensor that is build in a way, that it can reset at any time. Sadly, this stops my rust code as well, because it could be in the middle of a transaction to that sensor, and waits for data that is never coming. I dug around in the code, but i have not found a way to implement a non_blocking_read() function alongside the normal read. Do you know what it would take to do that? I'm willing to help, but im not smart enough it seems :sweat_smile:

Dirbaio commented 1 year ago

Some HALs (for example Embassy's) have support for setting an i2c timeout, so the blocking i2c trait will return with an error instead of waiting forever. This is something the HAL does, that alters the behavior of the existing i2c blocking trait (there's no "i2c with timeout" trait).

You can also use the embedded-hal-async i2c trait. This allows doing something like with_timeout(Duration::from_secs(1), i2c.read(...)).await, which will return early with an error if the timeout is reached, without HAL collaboration.

JonasFocke01 commented 1 year ago

Thanks for your response! I will try the methods you mentioned.
The question that remains in my head is now, why is the watchdog not triggering? Or in other words, what would be needed for the watchdog to trigger on this?

Dirbaio commented 1 year ago

Watchdogs typically aren't enabled by default, you need to enable it and then feed/pet it periodically.

JonasFocke01 commented 1 year ago

That worked really well, thank you :grinning: :heart: