Skip to content

Commit d099991

Browse files
committed
Fix end of transaction check and account for no flushing when a simple receiver/transmitter
1 parent dd86364 commit d099991

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/spi.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,15 @@ impl<SPI: Instance, W: Word> Inner<SPI, W> {
505505
}
506506
}
507507

508+
fn is_receiver(&self) -> bool {
509+
match self.communication_mode() {
510+
CommunicationMode::FullDuplex => true,
511+
CommunicationMode::HalfDuplex => !self.is_half_duplex_transmitter(),
512+
CommunicationMode::SimplexTransmitter => false,
513+
CommunicationMode::SimplexReceiver => true,
514+
}
515+
}
516+
508517
/// Set SPI to transmit mode in half duplex operation
509518
/// Only valid in half duplex operation. This is provided for non-blocking calls to be able to
510519
/// change direction of communication. Blocking calls already handle this
@@ -615,6 +624,11 @@ impl<SPI: Instance, W: Word> Inner<SPI, W> {
615624
/// occurs.
616625
#[inline(always)]
617626
fn flush_tx_fifo(&mut self, len: usize) -> Result<usize, Error> {
627+
// If the device is not a transmitter, no bytes can be written/flushed, so just return
628+
// immediately.
629+
if !self.is_transmitter() {
630+
return Ok(len);
631+
}
618632
for i in 0..len {
619633
if !self.write_if_ready(W::default())? {
620634
return Ok(i);
@@ -641,6 +655,11 @@ impl<SPI: Instance, W: Word> Inner<SPI, W> {
641655
/// occurs.
642656
#[inline(always)]
643657
fn discard_rx_fifo(&mut self, len: usize) -> Result<usize, Error> {
658+
// If the device is not a receiver, no bytes will be received or available to read/discard,
659+
// so just return immediately.
660+
if !self.is_receiver() {
661+
return Ok(len);
662+
}
644663
for i in 0..len {
645664
let mut dummy = W::default();
646665
if !self.read_if_ready(&mut dummy)? {

src/spi/transaction.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ impl<'a, OP, W> Transaction<OP, W> {
233233
impl<OP: Op<W>, W> Transaction<OP, W> {
234234
#[inline(always)]
235235
pub(super) fn is_complete(&mut self) -> bool {
236-
self.is_read_complete() && self.is_write_complete()
236+
self.is_read_complete()
237+
&& self.rx_remainder_to_discard() == 0
238+
&& self.is_write_complete()
239+
&& self.tx_flush_remainder() == 0
237240
}
238241

239242
#[inline(always)]

0 commit comments

Comments
 (0)