Skip to content

Commit fbff647

Browse files
committed
Added transactional SPI interface
See: #178 for work to get here
1 parent f88ddc5 commit fbff647

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/blocking/spi.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,52 @@ pub mod write_iter {
102102
}
103103
}
104104
}
105+
106+
/// Operation for transactional SPI trait
107+
///
108+
/// This allows composition of SPI operations into a single bus transaction
109+
#[derive(Debug, PartialEq)]
110+
pub enum Operation<'a, W: 'static> {
111+
/// Write data from the provided buffer, discarding read data
112+
Write(&'a [W]),
113+
/// Write data out while reading data into the provided buffer
114+
Transfer(&'a mut [W]),
115+
}
116+
117+
/// Transactional trait allows multiple actions to be executed
118+
/// as part of a single SPI transaction
119+
pub trait Transactional<W: 'static> {
120+
/// Associated error type
121+
type Error;
122+
123+
/// Execute the provided transactions
124+
fn try_exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
125+
}
126+
127+
/// Blocking transactional impl over spi::Write and spi::Transfer
128+
pub mod transactional {
129+
use super::{Operation, Transfer, Write};
130+
131+
/// Default implementation of `blocking::spi::Transactional<W>` for implementers of
132+
/// `spi::Write<W>` and `spi::Transfer<W>`
133+
pub trait Default<W, E> {}
134+
135+
impl<W: 'static, E, S> super::Transactional<W> for S
136+
where
137+
S: self::Default<W, E> + Write<W, Error = E> + Transfer<W, Error = E>,
138+
W: Copy + Clone,
139+
{
140+
type Error = E;
141+
142+
fn try_exec<'a>(&mut self, operations: &mut [super::Operation<'a, W>]) -> Result<(), E> {
143+
for op in operations {
144+
match op {
145+
Operation::Write(w) => self.try_write(w)?,
146+
Operation::Transfer(t) => self.try_transfer(t).map(|_| ())?,
147+
}
148+
}
149+
150+
Ok(())
151+
}
152+
}
153+
}

0 commit comments

Comments
 (0)