shared-bus is a crate to allow sharing bus peripherals safely between multiple devices.
Typical usage of this crate might look like this: ```rust extern crate shared_bus;
// Create your bus peripheral as usual: let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 90.khz(), clocks, &mut rcc.apb1);
let manager = shared_bus::BusManager::
// You can now acquire bus handles:
let mut handle = manager.acquire();
// handle implements i2c::{Read, Write, WriteRead}
, depending on the
// implementations of the underlying peripheral
let mut mydevice = MyDevice::new(manager.acquire());
```
To do its job, shared-bus needs a mutex. Because each platform has its own
mutex type, shared-bus uses an abstraction: BusMutex
. This type
needs to be implemented for your platforms mutex type to allow using this
crate.
std
is available, activate the std
feature to enable the implementation
of BusMutex
for std::sync::Mutex
.cortex-m
, activate the cortexm
feature to enable the implementation
of BusMutex
for cortex_m::interrupt::Mutex
.```rust extern crate sharedbus; extern crate cortexm;
// You need a newtype because you can't implement foreign traits on
// foreign types.
struct MyMutex
impl
fn lock<R, F: FnOnce(&T) -> R>(&self, f: F) -> R {
cortex_m::interrupt::free(|cs| {
let v = self.0.borrow(cs);
f(v)
})
}
}
type MyBusManager
I am welcoming patches containing mutex implementations for other platforms!
shared-bus is licensed under either of
at your option.