Module embedded_hal_mock::eh1::spi

source ·
Available on crate feature eh1 only.
Expand description

SPI mock implementations.

This mock supports the specification and checking of expectations to allow automated testing of SPI based drivers. Mismatches between expected and real SPI transactions will cause runtime assertions to assist with locating faults.

Usage

use embedded_hal::spi::SpiBus;
use embedded_hal_mock::eh1::spi::{Mock as SpiMock, Transaction as SpiTransaction};
use embedded_hal_nb::spi::FullDuplex;

// Configure expectations
let expectations = [
    SpiTransaction::write(0x09),
    SpiTransaction::read(0x0A),
    SpiTransaction::write(0xFE),
    SpiTransaction::read(0xFF),
    SpiTransaction::write_vec(vec![1, 2]),
    SpiTransaction::transfer_in_place(vec![3, 4], vec![5, 6]),
];

let mut spi = SpiMock::new(&expectations);
// FullDuplex transfers
FullDuplex::write(&mut spi, 0x09).unwrap();
assert_eq!(FullDuplex::read(&mut spi).unwrap(), 0x0A);
FullDuplex::write(&mut spi, 0xFE).unwrap();
assert_eq!(FullDuplex::read(&mut spi).unwrap(), 0xFF);

// Writing
SpiBus::write(&mut spi, &vec![1, 2]).unwrap();

// Transferring
let mut buf = vec![3, 4];
spi.transfer_in_place(&mut buf).unwrap();
assert_eq!(buf, vec![5, 6]);

// Finalise expectations
spi.done();

Structs

Enums

  • SPI Transaction mode

Type Aliases

  • Mock SPI implementation