[][src]Struct shared_bus::BusManager

pub struct BusManager<M: BusMutex> { /* fields omitted */ }

"Manager" for a shared bus.

The manager owns the original bus peripheral (wrapped inside a mutex) and hands out proxies which can be used by device drivers for accessing the bus. Certain bus proxies can only be created with restrictions (see the individual methods for details).

Usually the type-aliases defined in this crate should be used instead of BusManager directly. Otherwise, the mutex type needs to be specified explicitly. Here is an overview of aliases (some are only available if a certain feature is enabled):

Bus ManagerMutex TypeFeature NameNotes
BusManagerSimpleshared_bus::NullMutexalways availableFor sharing within a single execution context.
BusManagerStdstd::sync::MutexstdFor platforms where std is available.
BusManagerCortexMcortex_m::interrupt::Mutexcortex-mFor Cortex-M platforms; Uses a critcal section (i.e. turns off interrupts during bus transactions).

Constructing a BusManager

There are two ways to instanciate a bus manager. Which one to use depends on the kind of sharing that is intended.

  1. When all bus users live in the same task/thread, a BusManagerSimple can be used:

    // For example:
    // let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 90.khz(), clocks, &mut rcc.apb1);
    
    let bus = shared_bus::BusManagerSimple::new(i2c);
    
    let mut proxy1 = bus.acquire_i2c();
    let mut my_device = MyDevice::new(bus.acquire_i2c());
    
    proxy1.write(0x39, &[0xc0, 0xff, 0xee]);
    my_device.do_something_on_the_bus();
  2. When users are in different execution contexts, a proper mutex type is needed and the manager must be made static to ensure it lives long enough. For this, shared-bus provides a number of macros creating such a static instance:

    // For example:
    // let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 90.khz(), clocks, &mut rcc.apb1);
    
    // The bus is a 'static reference -> it lives forever and references can be
    // shared with other threads.
    let bus: &'static _ = shared_bus::new_std!(SomeI2cBus = i2c).unwrap();
    
    let mut proxy1 = bus.acquire_i2c();
    let mut my_device = MyDevice::new(bus.acquire_i2c());
    
    // We can easily move a proxy to another thread:
    std::thread::spawn(move || {
        my_device.do_something_on_the_bus();
    });

    For other platforms, similar macros exist (e.g. new_cortexm!()).

Implementations

impl<M: BusMutex> BusManager<M>[src]

pub fn new(bus: M::Bus) -> Self[src]

Create a new bus manager for a bus.

See the documentation for BusManager for more details.

impl<M: BusMutex> BusManager<M>[src]

pub fn acquire_i2c<'a>(&'a self) -> I2cProxy<'a, M>[src]

Acquire an I2cProxy for this bus.

The returned proxy object can then be used for accessing the bus by e.g. a driver:

let bus = shared_bus::BusManagerSimple::new(i2c);

let mut proxy1 = bus.acquire_i2c();
let mut my_device = MyDevice::new(bus.acquire_i2c());

proxy1.write(0x39, &[0xc0, 0xff, 0xee]);
my_device.do_something_on_the_bus();

impl<T> BusManager<NullMutex<T>>[src]

pub fn acquire_spi<'a>(&'a self) -> SpiProxy<'a, NullMutex<T>>[src]

Acquire an SpiProxy for this bus.

Note: SPI Proxies can only be created from BusManagerSimple (= bus managers using the NullMutex). See SpiProxy for more details why.

The returned proxy object can then be used for accessing the bus by e.g. a driver:

let bus = shared_bus::BusManagerSimple::new(spi);

let mut proxy1 = bus.acquire_spi();
let mut my_device = MyDevice::new(bus.acquire_spi());

// Chip-select needs to be managed manually
cs1.set_high();
proxy1.write(&[0xc0, 0xff, 0xee]);
cs1.set_low();

my_device.do_something_on_the_bus();

Trait Implementations

impl<M: Debug + BusMutex> Debug for BusManager<M>[src]

Auto Trait Implementations

impl<M> RefUnwindSafe for BusManager<M> where
    M: RefUnwindSafe

impl<M> Send for BusManager<M> where
    M: Send

impl<M> Sync for BusManager<M> where
    M: Sync

impl<M> Unpin for BusManager<M> where
    M: Unpin

impl<M> UnwindSafe for BusManager<M> where
    M: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.