Skip to content

Commit

Permalink
spi: add Read and separate-buffers Transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Nov 3, 2021
1 parent 4f3ada1 commit 151a658
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
dynamically.
- Added `Debug` to all spi mode types.
- Add impls of all traits for references (`&T` or `&mut T` depending on the trait) when `T` implements the trait.
- SPI: Added blocking `Read` trait and `Read` transactional operation
- SPI: Added blocking `Transfer` trait with separate buffers (single-buffer `Transfer` has been renamed `TransferInplace`)

### Changed
- Swap PWM channel arguments to references
Expand Down
36 changes: 33 additions & 3 deletions src/spi/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,41 @@
//! traits. To save boilerplate when that's the case a `Default` marker trait may be provided.
//! Implementing that marker trait will opt in your type into a blanket implementation.

/// Blocking transfer
/// Blocking transfer with separate buffers
pub trait Transfer<W> {
/// Error type
type Error: crate::spi::Error;

/// Writes and reads simultaneously. `write` is written to the slave on MOSI and
/// words received on MISO are stored in `read`.
///
/// It is allowed for `read` and `write` to have different lengths, even zero length.
/// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
/// incoming words after `read` has been filled will be discarded. If `write` is shorter,
/// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
/// typically `0x00`, `0xFF`, or configurable.
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error>;
}

/// Blocking transfer with single buffer (in-place)
pub trait TransferInplace<W> {
/// Error type
type Error: core::fmt::Debug;

/// Writes and reads simultaneously. The contents of `words` are
/// written to the slave, and the received words are stored into the same
/// `words` buffer, overwriting it.
fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
}

/// Blocking read
pub trait Read<W> {
/// Error type
type Error;

/// Reads `words` to the slave. The word value sent on MOSI during
/// reading is implementation-defined, typically `0x00`, `0xFF`, or configurable.
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
}

impl<T: Transfer<W>, W> Transfer<W> for &mut T {
Expand Down Expand Up @@ -67,10 +93,14 @@ impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
/// This allows composition of SPI operations into a single bus transaction
#[derive(Debug, PartialEq)]
pub enum Operation<'a, W: 'static> {
/// Read data into the provided buffer.
Read(&'a mut [W]),
/// Write data from the provided buffer, discarding read data
Write(&'a [W]),
/// Write data out while reading data into the provided buffer
Transfer(&'a mut [W]),
Transfer(&'a mut [W], &'a [W]),
/// Write data out while reading data into the provided buffer
TransferInplace(&'a mut [W]),
}

/// Transactional trait allows multiple actions to be executed
Expand Down

0 comments on commit 151a658

Please sign in to comment.