Skip to content

Commit 3d9f178

Browse files
committed
spi: add convenience methods to SpiDevice to do transfers.
1 parent 01e6b68 commit 3d9f178

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/spi/blocking.rs

+48
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,54 @@ pub trait SpiDevice: ErrorType {
172172
&mut self,
173173
f: impl FnOnce(&mut Self::Bus) -> Result<R, <Self::Bus as ErrorType>::Error>,
174174
) -> Result<R, Self::Error>;
175+
176+
/// Do a write within a transaction.
177+
///
178+
/// This is a convenience method equivalent to `device.transaction(|bus| bus.write(buf))`.
179+
///
180+
/// See also: [`SpiDevice::transaction`], [`SpiBusWrite::write`]
181+
fn write(&mut self, buf: &[u8]) -> Result<(), Self::Error>
182+
where
183+
Self::Bus: SpiBusWrite,
184+
{
185+
self.transaction(|bus| bus.write(buf))
186+
}
187+
188+
/// Do a read within a transaction.
189+
///
190+
/// This is a convenience method equivalent to `device.transaction(|bus| bus.read(buf))`.
191+
///
192+
/// See also: [`SpiDevice::transaction`], [`SpiBusRead::read`]
193+
fn read(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>
194+
where
195+
Self::Bus: SpiBusRead,
196+
{
197+
self.transaction(|bus| bus.read(buf))
198+
}
199+
200+
/// Do a transfer within a transaction.
201+
///
202+
/// This is a convenience method equivalent to `device.transaction(|bus| bus.transfer(read, write))`.
203+
///
204+
/// See also: [`SpiDevice::transaction`], [`SpiBus::transfer`]
205+
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error>
206+
where
207+
Self::Bus: SpiBus,
208+
{
209+
self.transaction(|bus| bus.transfer(read, write))
210+
}
211+
212+
/// Do an in-place transfer within a transaction.
213+
///
214+
/// This is a convenience method equivalent to `device.transaction(|bus| bus.transfer_in_place(buf))`.
215+
///
216+
/// See also: [`SpiDevice::transaction`], [`SpiBus::transfer_in_place`]
217+
fn transfer_in_place(&mut self, buf: &mut [u8]) -> Result<(), Self::Error>
218+
where
219+
Self::Bus: SpiBus,
220+
{
221+
self.transaction(|bus| bus.transfer_in_place(buf))
222+
}
175223
}
176224

177225
impl<T: SpiDevice> SpiDevice for &mut T {

0 commit comments

Comments
 (0)