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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
/// BME280 bus.
#[derive(Debug)]
pub struct Bme280Bus<SPI, CS> {
bus: SPI,
cs: CS,
}
/// SPI mode for the BME280.
///
/// The BME280 also supports mode 3.
pub const MODE: embedded_hal::spi::Mode = embedded_hal::spi::MODE_0;
/// Maximum SPI bus frequency in hertz.
pub const MAX_FREQ: u32 = 10_000_000;
/// BME280 error type.
#[derive(Debug)]
pub enum Error<SpiError, PinError> {
/// SPI bus error wrapper.
Spi(SpiError),
/// GPIO pin error wrapper.
Pin(PinError),
}
impl<SpiError, PinError> From<PinError> for Error<SpiError, PinError> {
#[inline]
fn from(e: PinError) -> Self {
Error::Pin(e)
}
}
impl<SPI, CS, SpiError, PinError> Bme280Bus<SPI, CS>
where
SPI: embedded_hal::blocking::spi::Transfer<u8, Error = SpiError>
+ embedded_hal::blocking::spi::Write<u8, Error = SpiError>,
CS: embedded_hal::digital::v2::OutputPin<Error = PinError>,
{
/// Creates a new `Bme280Bus` from a SPI peripheral and a chip select
/// digital I/O pin.
///
/// # Safety
///
/// The chip select pin must be high before being passed to this function.
///
/// # Example
///
/// ```
/// # use embedded_hal_mock as hal;
/// # let spi = hal::spi::Mock::new(&[]);
/// # let mut pin = hal::pin::Mock::new(&[
/// # hal::pin::Transaction::set(hal::pin::State::High),
/// # ]);
/// use bme280_multibus::spi::Bme280Bus;
/// use embedded_hal::digital::v2::OutputPin;
///
/// pin.set_high()?;
/// let mut bme: Bme280Bus<_, _> = Bme280Bus::new(spi, pin);
/// # Ok::<(), hal::MockError>(())
/// ```
#[inline]
pub fn new(bus: SPI, cs: CS) -> Self {
Bme280Bus { bus, cs }
}
/// Free the SPI bus and CS pin from the W5500.
///
/// # Example
///
/// ```
/// # use embedded_hal_mock as hal;
/// # let spi = hal::spi::Mock::new(&[]);
/// # let mut pin = hal::pin::Mock::new(&[
/// # hal::pin::Transaction::set(hal::pin::State::High),
/// # ]);
/// use bme280_multibus::spi::Bme280Bus;
/// use embedded_hal::digital::v2::OutputPin;
///
/// pin.set_high()?;
/// let mut bme: Bme280Bus<_, _> = Bme280Bus::new(spi, pin);
/// let (spi, pin) = bme.free();
/// # Ok::<(), hal::MockError>(())
/// ```
#[inline]
pub fn free(self) -> (SPI, CS) {
(self.bus, self.cs)
}
#[inline]
fn with_chip_enable<T, E, F>(&mut self, mut f: F) -> Result<T, E>
where
F: FnMut(&mut SPI) -> Result<T, E>,
E: core::convert::From<Error<SpiError, PinError>>,
{
self.cs.set_low().map_err(Error::Pin)?;
let result: Result<T, E> = f(&mut self.bus);
self.cs.set_high().map_err(Error::Pin)?;
result
}
}
impl<SPI, CS, SpiError, PinError> crate::Bme280Bus for Bme280Bus<SPI, CS>
where
SPI: embedded_hal::blocking::spi::Transfer<u8, Error = SpiError>
+ embedded_hal::blocking::spi::Write<u8, Error = SpiError>,
CS: embedded_hal::digital::v2::OutputPin<Error = PinError>,
{
type Error = Error<SpiError, PinError>;
fn read_regs(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Self::Error> {
self.with_chip_enable(|spi| {
spi.write(&[reg | (1 << 7)]).map_err(Error::Spi)?;
spi.transfer(buf).map_err(Error::Spi)?;
Ok(())
})
}
fn write_reg(&mut self, reg: u8, data: u8) -> Result<(), Self::Error> {
let mut buf: [u8; 2] = [reg & !(1 << 7), data];
self.with_chip_enable(|spi| {
spi.transfer(&mut buf).map_err(Error::Spi)?;
Ok(())
})
}
}
/// BME280 SPI bus implementation with an infallible GPIO
pub mod infallible_gpio {
/// BME280 bus.
#[derive(Debug)]
pub struct Bme280Bus<SPI, CS> {
bus: SPI,
cs: CS,
}
impl<SPI, CS, E> Bme280Bus<SPI, CS>
where
SPI: embedded_hal::blocking::spi::Transfer<u8, Error = E>
+ embedded_hal::blocking::spi::Write<u8, Error = E>,
CS: embedded_hal::digital::v2::OutputPin<Error = core::convert::Infallible>,
{
/// Creates a new `Bme280Bus` from a SPI peripheral and a chip select
/// digital I/O pin.
///
/// # Safety
///
/// The chip select pin must be high before being passed to this function.
///
/// # Example
///
/// ```
/// # use embedded_hal_mock as hal;
/// # let spi = hal::spi::Mock::new(&[]);
/// # struct Pin {};
/// # impl embedded_hal::digital::v2::OutputPin for Pin {
/// # type Error = core::convert::Infallible;
/// # fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) }
/// # fn set_high(&mut self) -> Result<(), Self::Error> { Ok(()) }
/// # }
/// # let mut pin = Pin {};
/// use bme280_multibus::spi::infallible_gpio::Bme280Bus;
/// use embedded_hal::digital::v2::OutputPin;
///
/// pin.set_high().unwrap();
/// let mut bme: Bme280Bus<_, _> = Bme280Bus::new(spi, pin);
/// # Ok::<(), hal::MockError>(())
/// ```
#[inline]
pub fn new(bus: SPI, cs: CS) -> Self {
Bme280Bus { bus, cs }
}
/// Free the SPI bus and CS pin from the W5500.
///
/// # Example
///
/// ```
/// # use embedded_hal_mock as hal;
/// # let spi = hal::spi::Mock::new(&[]);
/// # struct Pin {};
/// # impl embedded_hal::digital::v2::OutputPin for Pin {
/// # type Error = core::convert::Infallible;
/// # fn set_low(&mut self) -> Result<(), Self::Error> { Ok(()) }
/// # fn set_high(&mut self) -> Result<(), Self::Error> { Ok(()) }
/// # }
/// # let mut pin = Pin {};
/// use bme280_multibus::spi::infallible_gpio::Bme280Bus;
/// use embedded_hal::digital::v2::OutputPin;
///
/// pin.set_high().unwrap();
/// let mut bme: Bme280Bus<_, _> = Bme280Bus::new(spi, pin);
/// let (spi, pin) = bme.free();
/// # Ok::<(), hal::MockError>(())
/// ```
#[inline]
pub fn free(self) -> (SPI, CS) {
(self.bus, self.cs)
}
#[inline]
fn with_chip_enable<T, F>(&mut self, mut f: F) -> Result<T, E>
where
F: FnMut(&mut SPI) -> Result<T, E>,
{
self.cs.set_low().unwrap();
let result: Result<T, E> = f(&mut self.bus);
self.cs.set_high().unwrap();
result
}
}
impl<SPI, CS, E> crate::Bme280Bus for Bme280Bus<SPI, CS>
where
SPI: embedded_hal::blocking::spi::Transfer<u8, Error = E>
+ embedded_hal::blocking::spi::Write<u8, Error = E>,
CS: embedded_hal::digital::v2::OutputPin<Error = core::convert::Infallible>,
{
type Error = E;
fn read_regs(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Self::Error> {
self.with_chip_enable(|spi| {
spi.write(&[reg | (1 << 7)])?;
spi.transfer(buf)?;
Ok(())
})
}
fn write_reg(&mut self, reg: u8, data: u8) -> Result<(), Self::Error> {
let mut buf: [u8; 2] = [reg & !(1 << 7), data];
self.with_chip_enable(|spi| {
spi.transfer(&mut buf)?;
Ok(())
})
}
}
}
