rust-embedded / embedded-hal

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

embedded-hal-bus does not impliment embedded-hal-async traits, despite claiming to #572

Open Sycrosity opened 6 months ago

Sycrosity commented 6 months ago

Despite it being stated that the async feature "enables embedded-hal-async support", there is no code implementation of this. Async shared busses would be very useful, especially for crates which only support the embedded-hal-async I2C traits, to be able to safely share i2c busses over.

Dirbaio commented 6 months ago

There's one for ExclusiveDevice.

The problem with async shared buses is there's no "standard" async mutex, so it's left for crates from the ecosystem for now. For example there's embassy-embedded-hal containing impls using embassy-sync's async Mutex.

Sycrosity commented 6 months ago

There's one for ExclusiveDevice.

That slipped past me, there indeed is - however there isn't any for I2C.

The problem with async shared buses is there's no "standard" async mutex, so it's left for crates from the ecosystem for now. For example there's embassy-embedded-hal containing impls using embassy-sync's async Mutex.

That seemed to be the issue when and around searching this issue - do you know of an example of a project using embedded asynchronous shared mutex's? Thank you for the links, I will investigate further into these.

liarokapisv commented 3 months ago

Does the above have implications for the async implementation of the RefCellDevice ?

Dirbaio commented 3 months ago

RefCell is not async, so RefCellDevice can't implement the async traits. if it did you'd get BorrowMutErrors if you tried to use it from several tasks at the same time.

if you want async shared SpiDevice, use embassy-embedded-hal, which implements it using embassy-sync's async Mutex, which allows sharing between async tasks properly.

liarokapisv commented 3 months ago

RefCellDevice is useful even if only used by a single task. Eg you could implement a fair round-robin scheduler for the different devices in a single task. We still have the advantages of async like being composable with other tasks and consuming less processor time by taking advantage of dma without needing large sync primitives. It also helps reusing async drivers.