Skip to content

Commit

Permalink
spi: fix apollo3 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alevy committed Sep 14, 2024
1 parent cd411b4 commit 836768f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
31 changes: 14 additions & 17 deletions boards/apollo3/lora_things_plus/src/tests/spi_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ use core::cell::Cell;
use kernel::hil::spi::{self, ClockPhase, ClockPolarity};
use kernel::hil::spi::{SpiMaster, SpiMasterClient};
use kernel::static_init;
use kernel::utilities::cells::TakeCell;
use kernel::utilities::cells::MapCell;
use kernel::utilities::leasable_buffer::SubSliceMut;
use kernel::{debug, ErrorCode};

struct SpiHostCallback {
transfer_done: Cell<bool>,
tx_len: Cell<usize>,
tx_data: TakeCell<'static, [u8]>,
rx_data: TakeCell<'static, [u8]>,
tx_data: MapCell<SubSliceMut<'static, u8>>,
rx_data: MapCell<SubSliceMut<'static, u8>>,
}

impl<'a> SpiHostCallback {
fn new(tx_data: &'static mut [u8], rx_data: &'static mut [u8]) -> Self {
SpiHostCallback {
transfer_done: Cell::new(false),
tx_len: Cell::new(0),
tx_data: TakeCell::new(tx_data),
rx_data: TakeCell::new(rx_data),
tx_data: MapCell::new(tx_data.into()),
rx_data: MapCell::new(rx_data.into()),
}
}

Expand All @@ -38,14 +39,12 @@ impl<'a> SpiHostCallback {
impl<'a> SpiMasterClient for SpiHostCallback {
fn read_write_done(
&self,
tx_data: &'static mut [u8],
rx_done: Option<&'static mut [u8]>,
tx_len: usize,
rc: Result<(), ErrorCode>,
tx_data: SubSliceMut<'static, u8>,
rx_done: Option<SubSliceMut<'static, u8>>,
rc: Result<usize, ErrorCode>,
) {
// Transfer Complete
assert_eq!(rc, Ok(()));
assert_eq!(tx_len, self.tx_len.get());
assert_eq!(rc, Ok(self.tx_len.get()));

// Capture Buffers
match rx_done {
Expand All @@ -59,9 +58,7 @@ impl<'a> SpiMasterClient for SpiHostCallback {

self.tx_data.replace(tx_data);

if self.tx_len.get() == tx_len {
self.transfer_done.set(true);
}
self.transfer_done.set(true);
}
}

Expand Down Expand Up @@ -159,7 +156,7 @@ fn spi_host_transfer_partial() {
spi_host.set_phase(ClockPhase::SampleLeading).ok();

assert_eq!(
spi_host.read_write_bytes(tx, Some(rx), cb.tx_len.get()),
spi_host.read_write_bytes(tx.into(), Some(rx.into())),
Ok(())
);
run_kernel_op(5000);
Expand Down Expand Up @@ -197,7 +194,7 @@ fn spi_host_transfer_single() {
spi_host.set_phase(ClockPhase::SampleLeading).ok();

assert_eq!(
spi_host.read_write_bytes(tx, Some(rx), cb.tx_len.get()),
spi_host.read_write_bytes(tx.into(), Some(rx.into())),
Ok(())
);
run_kernel_op(5000);
Expand All @@ -218,7 +215,7 @@ fn spi_host_transfer_single() {
cb.tx_len.set(tx2.len());

assert_eq!(
spi_host.read_write_bytes(tx2, Some(rx2), cb.tx_len.get()),
spi_host.read_write_bytes(tx2.into(), Some(rx2.into())),
Ok(())
);
run_kernel_op(5000);
Expand Down
40 changes: 14 additions & 26 deletions boards/apollo3/redboard_artemis_nano/src/tests/spi_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@ use core::cell::Cell;
use kernel::hil::spi::{self, ClockPhase, ClockPolarity};
use kernel::hil::spi::{SpiMaster, SpiMasterClient};
use kernel::static_init;
use kernel::utilities::cells::TakeCell;
use kernel::utilities::cells::MapCell;
use kernel::utilities::leasable_buffer::SubSliceMut;
use kernel::{debug, ErrorCode};

struct SpiHostCallback {
transfer_done: Cell<bool>,
tx_len: Cell<usize>,
tx_data: TakeCell<'static, [u8]>,
rx_data: TakeCell<'static, [u8]>,
tx_data: MapCell<SubSliceMut<'static, u8>>,
rx_data: MapCell<SubSliceMut<'static, u8>>,
}

impl<'a> SpiHostCallback {
fn new(tx_data: &'static mut [u8], rx_data: &'static mut [u8]) -> Self {
SpiHostCallback {
transfer_done: Cell::new(false),
tx_len: Cell::new(0),
tx_data: TakeCell::new(tx_data),
rx_data: TakeCell::new(rx_data),
tx_data: MapCell::new(tx_data.into()),
rx_data: MapCell::new(rx_data.into()),
}
}

Expand All @@ -38,14 +39,12 @@ impl<'a> SpiHostCallback {
impl<'a> SpiMasterClient for SpiHostCallback {
fn read_write_done(
&self,
tx_data: &'static mut [u8],
rx_done: Option<&'static mut [u8]>,
tx_len: usize,
rc: Result<(), ErrorCode>,
tx_data: SubSliceMut<'static, u8>,
rx_done: Option<SubSliceMut<'static, u8>>,
rc: Result<usize, ErrorCode>,
) {
// Transfer Complete
assert_eq!(rc, Ok(()));
assert_eq!(tx_len, self.tx_len.get());
assert_eq!(rc, Ok(self.tx_len.get()));

// Capture Buffers
match rx_done {
Expand All @@ -59,9 +58,7 @@ impl<'a> SpiMasterClient for SpiHostCallback {

self.tx_data.replace(tx_data);

if self.tx_len.get() == tx_len {
self.transfer_done.set(true);
}
self.transfer_done.set(true);
}
}

Expand Down Expand Up @@ -158,10 +155,7 @@ fn spi_host_transfer_partial() {
spi_host.set_polarity(ClockPolarity::IdleLow).ok();
spi_host.set_phase(ClockPhase::SampleLeading).ok();

assert_eq!(
spi_host.read_write_bytes(tx, Some(rx), cb.tx_len.get()),
Ok(())
);
assert_eq!(spi_host.read_write_bytes(tx, Some(rx)), Ok(()));
run_kernel_op(5000);

assert_eq!(cb.transfer_done.get(), true);
Expand Down Expand Up @@ -196,10 +190,7 @@ fn spi_host_transfer_single() {
spi_host.set_polarity(ClockPolarity::IdleLow).ok();
spi_host.set_phase(ClockPhase::SampleLeading).ok();

assert_eq!(
spi_host.read_write_bytes(tx, Some(rx), cb.tx_len.get()),
Ok(())
);
assert_eq!(spi_host.read_write_bytes(tx, Some(rx)), Ok(()));
run_kernel_op(5000);

assert_eq!(cb.transfer_done.get(), true);
Expand All @@ -217,10 +208,7 @@ fn spi_host_transfer_single() {
let rx2 = cb.rx_data.take().unwrap();
cb.tx_len.set(tx2.len());

assert_eq!(
spi_host.read_write_bytes(tx2, Some(rx2), cb.tx_len.get()),
Ok(())
);
assert_eq!(spi_host.read_write_bytes(tx2, Some(rx2)), Ok(()));
run_kernel_op(5000);

assert_eq!(cb.transfer_done.get(), true);
Expand Down

0 comments on commit 836768f

Please sign in to comment.