diff options
| author | Henrik Alsér <[email protected]> | 2022-07-06 23:02:16 +0200 |
|---|---|---|
| committer | Henrik Alsér <[email protected]> | 2022-07-06 23:02:16 +0200 |
| commit | 4b4fe7245ba62569bbee684b22c8e7155af55a53 (patch) | |
| tree | 5aaf39f31d19650df6ad92d2bddd643d9b498d56 /embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |
| parent | 6baddaf53982b75149cb7e91280c571f7fe2e7bc (diff) | |
Add EH 0.2 impls + example docs
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/blocking/spi.rs')
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/blocking/spi.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs index c08bcbf62..81cf97457 100644 --- a/embassy-embedded-hal/src/shared_bus/blocking/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/blocking/spi.rs | |||
| @@ -1,4 +1,23 @@ | |||
| 1 | //! Blocking shared SPI bus | 1 | //! Blocking shared SPI bus |
| 2 | //! | ||
| 3 | //! # Example (nrf52) | ||
| 4 | //! | ||
| 5 | //! ```rust | ||
| 6 | //! use embassy_embedded_hal::shared_bus::blocking::spi::SpiBusDevice; | ||
| 7 | //! use embassy::blocking_mutex::{NoopMutex, raw::NoopRawMutex}; | ||
| 8 | //! | ||
| 9 | //! static SPI_BUS: Forever<NoopMutex<RefCell<Spim<SPI3>>>> = Forever::new(); | ||
| 10 | //! let irq = interrupt::take!(SPIM3); | ||
| 11 | //! let spi = Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, Config::default()); | ||
| 12 | //! let spi_bus = NoopMutex::new(RefCell::new(spi)); | ||
| 13 | //! let spi_bus = SPI_BUS.put(spi_bus); | ||
| 14 | //! | ||
| 15 | //! // Device 1, using embedded-hal compatible driver for ST7735 LCD display | ||
| 16 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | ||
| 17 | //! let spi_dev1 = SpiBusDevice::new(spi_bus, cs_pin1); | ||
| 18 | //! let display1 = ST7735::new(spi_dev1, dc1, rst1, Default::default(), false, 160, 128); | ||
| 19 | //! ``` | ||
| 20 | |||
| 2 | use core::cell::RefCell; | 21 | use core::cell::RefCell; |
| 3 | 22 | ||
| 4 | use embassy::blocking_mutex::raw::RawMutex; | 23 | use embassy::blocking_mutex::raw::RawMutex; |
| @@ -55,3 +74,44 @@ where | |||
| 55 | }) | 74 | }) |
| 56 | } | 75 | } |
| 57 | } | 76 | } |
| 77 | |||
| 78 | impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Transfer<u8> for SpiBusDevice<'_, M, BUS, CS> | ||
| 79 | where | ||
| 80 | M: RawMutex, | ||
| 81 | BUS: embedded_hal_02::blocking::spi::Transfer<u8, Error = BusErr>, | ||
| 82 | CS: OutputPin<Error = CsErr>, | ||
| 83 | { | ||
| 84 | type Error = SpiBusDeviceError<BusErr, CsErr>; | ||
| 85 | fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { | ||
| 86 | self.bus.lock(|bus| { | ||
| 87 | let mut bus = bus.borrow_mut(); | ||
| 88 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 89 | let f_res = bus.transfer(words); | ||
| 90 | let cs_res = self.cs.set_high(); | ||
| 91 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 92 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 93 | Ok(f_res) | ||
| 94 | }) | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | impl<'d, M, BUS, CS, BusErr, CsErr> embedded_hal_02::blocking::spi::Write<u8> for SpiBusDevice<'_, M, BUS, CS> | ||
| 99 | where | ||
| 100 | M: RawMutex, | ||
| 101 | BUS: embedded_hal_02::blocking::spi::Write<u8, Error = BusErr>, | ||
| 102 | CS: OutputPin<Error = CsErr>, | ||
| 103 | { | ||
| 104 | type Error = SpiBusDeviceError<BusErr, CsErr>; | ||
| 105 | |||
| 106 | fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { | ||
| 107 | self.bus.lock(|bus| { | ||
| 108 | let mut bus = bus.borrow_mut(); | ||
| 109 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 110 | let f_res = bus.write(words); | ||
| 111 | let cs_res = self.cs.set_high(); | ||
| 112 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 113 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 114 | Ok(f_res) | ||
| 115 | }) | ||
| 116 | } | ||
| 117 | } | ||
