1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use hal::blocking::i2c;
use hal::blocking::spi;

use mutex;

#[cfg(feature = "std")]
use std::cell;

#[cfg(not(feature = "std"))]
use core::cell;

#[cfg(feature = "std")]
use std::marker;

#[cfg(not(feature = "std"))]
use core::marker;

/// A manager for managing a shared bus.
///
/// The manager owns the actual peripheral and hands out proxies which can be
/// used by your devices.
///
/// When creating a bus manager you need to specify which
/// mutex type should be used.
///
/// # Examples
/// ```
/// # extern crate shared_bus;
/// # use shared_bus::BusManager;
/// # struct MyDevice;
/// # impl MyDevice {
/// #     pub fn new<T>(t: T) -> MyDevice { MyDevice }
/// # }
///
/// # let i2c = ();
/// // For example:
/// // let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 90.khz(), clocks, &mut rcc.apb1);
///
/// let manager = BusManager::<std::sync::Mutex<_>, _>::new(i2c);
///
/// // You can now acquire bus handles:
/// let mut handle1 = manager.acquire();
/// let mut mydevice = MyDevice::new(manager.acquire());
/// ```
pub struct BusManager<M: mutex::BusMutex<cell::RefCell<T>>, T>(M, marker::PhantomData<T>);

impl<M: mutex::BusMutex<cell::RefCell<T>>, T> BusManager<M, T> {
    /// Create a new manager for a bus peripheral `d`.
    ///
    /// When creating the manager you need to specify which mutex type should be used:
    /// ```
    /// # extern crate shared_bus;
    /// # use shared_bus::BusManager;
    /// # let bus = ();
    /// let manager = BusManager::<std::sync::Mutex<_>, _>::new(bus);
    /// ```
    pub fn new(d: T) -> BusManager<M, T> {
        let mutex = M::create(cell::RefCell::new(d));

        BusManager(mutex, marker::PhantomData)
    }

    /// Acquire a proxy for this bus.
    pub fn acquire<'a>(&'a self) -> BusProxy<'a, M, T> {
        BusProxy(&self.0, marker::PhantomData)
    }
}

/// A proxy type that can be used instead of an actual bus peripheral.
///
/// `BusProxy` implements all bus traits and can thus be used in place of the
/// actual bus peripheral.
///
/// `BusProxies` are created by calling [`BusManager::acquire`]
pub struct BusProxy<'a, M: 'a + mutex::BusMutex<cell::RefCell<T>>, T>(
    &'a M,
    marker::PhantomData<T>,
);

impl<'a, M, I2C: i2c::Write> i2c::Write for BusProxy<'a, M, I2C>
where
    M: 'a + mutex::BusMutex<cell::RefCell<I2C>>,
{
    type Error = I2C::Error;

    fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
        self.0.lock(|lock| {
            let mut i = lock.borrow_mut();
            i.write(addr, bytes)
        })
    }
}

impl<'a, M, I2C: i2c::Read> i2c::Read for BusProxy<'a, M, I2C>
where
    M: 'a + mutex::BusMutex<cell::RefCell<I2C>>,
{
    type Error = I2C::Error;

    fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
        self.0.lock(|lock| {
            let mut i = lock.borrow_mut();
            i.read(address, buffer)
        })
    }
}

impl<'a, M, I2C: i2c::WriteRead> i2c::WriteRead for BusProxy<'a, M, I2C>
where
    M: 'a + mutex::BusMutex<cell::RefCell<I2C>>,
{
    type Error = I2C::Error;

    fn write_read(
        &mut self,
        address: u8,
        bytes: &[u8],
        buffer: &mut [u8],
    ) -> Result<(), Self::Error> {
        self.0.lock(|lock| {
            let mut i = lock.borrow_mut();
            i.write_read(address, bytes, buffer)
        })
    }
}

impl<'a, M, SPI: spi::Transfer<u8>> spi::Transfer<u8> for BusProxy<'a, M, SPI>
where
    M: 'a + mutex::BusMutex<cell::RefCell<SPI>>,
{
    type Error = SPI::Error;

    fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
        self.0.lock(move |lock| {
            let mut i = lock.borrow_mut();
            i.transfer(words)
        })
    }
}

impl<'a, M, SPI: spi::Write<u8>> spi::Write<u8> for BusProxy<'a, M, SPI>
where
    M: 'a + mutex::BusMutex<cell::RefCell<SPI>>,
{
    type Error = SPI::Error;

    fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
        self.0.lock(|lock| {
            let mut i = lock.borrow_mut();
            i.write(words)
        })
    }
}