shared-bus crates.io page Build Status docs.rs

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::, _>::new(i2c);

// 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()); ```

Mutex Implementation

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.

```rust extern crate sharedbus; extern crate cortexm;

// You need a newtype because you can't implement foreign traits on // foreign types. struct MyMutex(cortex_m::interrupt::Mutex);

impl sharedbus::BusMutex for MyMutex { fn create(v: T) -> MyMutex { MyMutex(cortexm::interrupt::Mutex::new(v)) }

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 = shared_bus::BusManager, P>; ```

I am welcoming patches containing mutex implementations for other platforms!

License

shared-bus is licensed under either of

at your option.