@@ -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) ? {
0 commit comments