diff options
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/asynch/spi.rs')
| -rw-r--r-- | embassy-embedded-hal/src/shared_bus/asynch/spi.rs | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs new file mode 100644 index 000000000..f3795bb19 --- /dev/null +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs | |||
| @@ -0,0 +1,168 @@ | |||
| 1 | //! Asynchronous shared SPI bus | ||
| 2 | //! | ||
| 3 | //! # Example (nrf52) | ||
| 4 | //! | ||
| 5 | //! ```rust | ||
| 6 | //! use embassy_embedded_hal::shared_bus::spi::SpiBusDevice; | ||
| 7 | //! use embassy::mutex::Mutex; | ||
| 8 | //! use embassy::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 9 | //! | ||
| 10 | //! static SPI_BUS: Forever<Mutex<ThreadModeRawMutex, spim::Spim<SPI3>>> = Forever::new(); | ||
| 11 | //! let mut config = spim::Config::default(); | ||
| 12 | //! config.frequency = spim::Frequency::M32; | ||
| 13 | //! let irq = interrupt::take!(SPIM3); | ||
| 14 | //! let spi = spim::Spim::new_txonly(p.SPI3, irq, p.P0_15, p.P0_18, config); | ||
| 15 | //! let spi_bus = Mutex::<ThreadModeRawMutex, _>::new(spi); | ||
| 16 | //! let spi_bus = SPI_BUS.put(spi_bus); | ||
| 17 | //! | ||
| 18 | //! // Device 1, using embedded-hal-async compatible driver for ST7735 LCD display | ||
| 19 | //! let cs_pin1 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | ||
| 20 | //! let spi_dev1 = SpiBusDevice::new(spi_bus, cs_pin1); | ||
| 21 | //! let display1 = ST7735::new(spi_dev1, dc1, rst1, Default::default(), 160, 128); | ||
| 22 | //! | ||
| 23 | //! // Device 2 | ||
| 24 | //! let cs_pin2 = Output::new(p.P0_24, Level::Low, OutputDrive::Standard); | ||
| 25 | //! let spi_dev2 = SpiBusDevice::new(spi_bus, cs_pin2); | ||
| 26 | //! let display2 = ST7735::new(spi_dev2, dc2, rst2, Default::default(), 160, 128); | ||
| 27 | //! ``` | ||
| 28 | use core::fmt::Debug; | ||
| 29 | use core::future::Future; | ||
| 30 | |||
| 31 | use embassy::blocking_mutex::raw::RawMutex; | ||
| 32 | use embassy::mutex::Mutex; | ||
| 33 | use embedded_hal_1::digital::blocking::OutputPin; | ||
| 34 | use embedded_hal_1::spi::ErrorType; | ||
| 35 | use embedded_hal_async::spi; | ||
| 36 | |||
| 37 | use crate::shared_bus::SpiBusDeviceError; | ||
| 38 | use crate::SetConfig; | ||
| 39 | |||
| 40 | pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> { | ||
| 41 | bus: &'a Mutex<M, BUS>, | ||
| 42 | cs: CS, | ||
| 43 | } | ||
| 44 | |||
| 45 | impl<'a, M: RawMutex, BUS, CS> SpiBusDevice<'a, M, BUS, CS> { | ||
| 46 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS) -> Self { | ||
| 47 | Self { bus, cs } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | impl<'a, M: RawMutex, BUS, CS> spi::ErrorType for SpiBusDevice<'a, M, BUS, CS> | ||
| 52 | where | ||
| 53 | BUS: spi::ErrorType, | ||
| 54 | CS: OutputPin, | ||
| 55 | { | ||
| 56 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 57 | } | ||
| 58 | |||
| 59 | impl<BUS, CS> spi::Error for SpiBusDeviceError<BUS, CS> | ||
| 60 | where | ||
| 61 | BUS: spi::Error + Debug, | ||
| 62 | CS: Debug, | ||
| 63 | { | ||
| 64 | fn kind(&self) -> spi::ErrorKind { | ||
| 65 | match self { | ||
| 66 | Self::Spi(e) => e.kind(), | ||
| 67 | Self::Cs(_) => spi::ErrorKind::Other, | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | impl<M, BUS, CS> spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS> | ||
| 73 | where | ||
| 74 | M: RawMutex + 'static, | ||
| 75 | BUS: spi::SpiBusFlush + 'static, | ||
| 76 | CS: OutputPin, | ||
| 77 | { | ||
| 78 | type Bus = BUS; | ||
| 79 | |||
| 80 | type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a | ||
| 81 | where | ||
| 82 | Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 83 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a; | ||
| 84 | |||
| 85 | fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut> | ||
| 86 | where | ||
| 87 | R: 'a, | ||
| 88 | F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 89 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a, | ||
| 90 | { | ||
| 91 | async move { | ||
| 92 | let mut bus = self.bus.lock().await; | ||
| 93 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 94 | |||
| 95 | let f_res = f(&mut *bus).await; | ||
| 96 | |||
| 97 | // On failure, it's important to still flush and deassert CS. | ||
| 98 | let flush_res = bus.flush().await; | ||
| 99 | let cs_res = self.cs.set_high(); | ||
| 100 | |||
| 101 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 102 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 103 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 104 | |||
| 105 | Ok(f_res) | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | pub struct SpiBusDeviceWithConfig<'a, M: RawMutex, BUS: SetConfig, CS> { | ||
| 111 | bus: &'a Mutex<M, BUS>, | ||
| 112 | cs: CS, | ||
| 113 | config: BUS::Config, | ||
| 114 | } | ||
| 115 | |||
| 116 | impl<'a, M: RawMutex, BUS: SetConfig, CS> SpiBusDeviceWithConfig<'a, M, BUS, CS> { | ||
| 117 | pub fn new(bus: &'a Mutex<M, BUS>, cs: CS, config: BUS::Config) -> Self { | ||
| 118 | Self { bus, cs, config } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | impl<'a, M, BUS, CS> spi::ErrorType for SpiBusDeviceWithConfig<'a, M, BUS, CS> | ||
| 123 | where | ||
| 124 | BUS: spi::ErrorType + SetConfig, | ||
| 125 | CS: OutputPin, | ||
| 126 | M: RawMutex, | ||
| 127 | { | ||
| 128 | type Error = SpiBusDeviceError<BUS::Error, CS::Error>; | ||
| 129 | } | ||
| 130 | |||
| 131 | impl<M, BUS, CS> spi::SpiDevice for SpiBusDeviceWithConfig<'_, M, BUS, CS> | ||
| 132 | where | ||
| 133 | M: RawMutex + 'static, | ||
| 134 | BUS: spi::SpiBusFlush + SetConfig + 'static, | ||
| 135 | CS: OutputPin, | ||
| 136 | { | ||
| 137 | type Bus = BUS; | ||
| 138 | |||
| 139 | type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a | ||
| 140 | where | ||
| 141 | Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 142 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a; | ||
| 143 | |||
| 144 | fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut> | ||
| 145 | where | ||
| 146 | R: 'a, | ||
| 147 | F: FnOnce(*mut Self::Bus) -> Fut + 'a, | ||
| 148 | Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a, | ||
| 149 | { | ||
| 150 | async move { | ||
| 151 | let mut bus = self.bus.lock().await; | ||
| 152 | bus.set_config(&self.config); | ||
| 153 | self.cs.set_low().map_err(SpiBusDeviceError::Cs)?; | ||
| 154 | |||
| 155 | let f_res = f(&mut *bus).await; | ||
| 156 | |||
| 157 | // On failure, it's important to still flush and deassert CS. | ||
| 158 | let flush_res = bus.flush().await; | ||
| 159 | let cs_res = self.cs.set_high(); | ||
| 160 | |||
| 161 | let f_res = f_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 162 | flush_res.map_err(SpiBusDeviceError::Spi)?; | ||
| 163 | cs_res.map_err(SpiBusDeviceError::Cs)?; | ||
| 164 | |||
| 165 | Ok(f_res) | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
