aboutsummaryrefslogtreecommitdiff
path: root/embassy-embedded-hal/src/shared_bus/spi.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-embedded-hal/src/shared_bus/spi.rs')
-rw-r--r--embassy-embedded-hal/src/shared_bus/spi.rs110
1 files changed, 110 insertions, 0 deletions
diff --git a/embassy-embedded-hal/src/shared_bus/spi.rs b/embassy-embedded-hal/src/shared_bus/spi.rs
new file mode 100644
index 000000000..3ec064ba7
--- /dev/null
+++ b/embassy-embedded-hal/src/shared_bus/spi.rs
@@ -0,0 +1,110 @@
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//! ```
28use core::{fmt::Debug, future::Future};
29use embassy::blocking_mutex::raw::RawMutex;
30use embassy::mutex::Mutex;
31
32use embedded_hal_1::digital::blocking::OutputPin;
33use embedded_hal_1::spi::ErrorType;
34use embedded_hal_async::spi;
35
36#[derive(Copy, Clone, Eq, PartialEq, Debug)]
37pub enum SpiBusDeviceError<BUS, CS> {
38 Spi(BUS),
39 Cs(CS),
40}
41
42impl<BUS, CS> spi::Error for SpiBusDeviceError<BUS, CS>
43where
44 BUS: spi::Error + Debug,
45 CS: Debug,
46{
47 fn kind(&self) -> spi::ErrorKind {
48 match self {
49 Self::Spi(e) => e.kind(),
50 Self::Cs(_) => spi::ErrorKind::Other,
51 }
52 }
53}
54
55pub struct SpiBusDevice<'a, M: RawMutex, BUS, CS> {
56 bus: &'a Mutex<M, BUS>,
57 cs: CS,
58}
59
60impl<'a, M: RawMutex, BUS, CS> SpiBusDevice<'a, M, BUS, CS> {
61 pub fn new(bus: &'a Mutex<M, BUS>, cs: CS) -> Self {
62 Self { bus, cs }
63 }
64}
65
66impl<'a, M: RawMutex, BUS, CS> spi::ErrorType for SpiBusDevice<'a, M, BUS, CS>
67where
68 BUS: spi::ErrorType,
69 CS: OutputPin,
70{
71 type Error = SpiBusDeviceError<BUS::Error, CS::Error>;
72}
73
74impl<M, BUS, CS> spi::SpiDevice for SpiBusDevice<'_, M, BUS, CS>
75where
76 M: RawMutex + 'static,
77 BUS: spi::SpiBusFlush + 'static,
78 CS: OutputPin,
79{
80 type Bus = BUS;
81
82 type TransactionFuture<'a, R, F, Fut> = impl Future<Output = Result<R, Self::Error>> + 'a
83 where
84 Self: 'a, R: 'a, F: FnOnce(*mut Self::Bus) -> Fut + 'a,
85 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a;
86
87 fn transaction<'a, R, F, Fut>(&'a mut self, f: F) -> Self::TransactionFuture<'a, R, F, Fut>
88 where
89 R: 'a,
90 F: FnOnce(*mut Self::Bus) -> Fut + 'a,
91 Fut: Future<Output = Result<R, <Self::Bus as ErrorType>::Error>> + 'a,
92 {
93 async move {
94 let mut bus = self.bus.lock().await;
95 self.cs.set_low().map_err(SpiBusDeviceError::Cs)?;
96
97 let f_res = f(&mut *bus).await;
98
99 // On failure, it's important to still flush and deassert CS.
100 let flush_res = bus.flush().await;
101 let cs_res = self.cs.set_high();
102
103 let f_res = f_res.map_err(SpiBusDeviceError::Spi)?;
104 flush_res.map_err(SpiBusDeviceError::Spi)?;
105 cs_res.map_err(SpiBusDeviceError::Cs)?;
106
107 Ok(f_res)
108 }
109 }
110}