@@ -1072,23 +1072,28 @@ impl<'d> UartRx<'d, Async> {
10721072 async fn wait_for_buffered_data (
10731073 & mut self ,
10741074 minimum : usize ,
1075- preferred : usize ,
1075+ max_threshold : usize ,
10761076 listen_for_timeout : bool ,
10771077 ) -> Result < ( ) , RxError > {
1078- while self . uart . info ( ) . rx_fifo_count ( ) < ( minimum as u16 ) . min ( Info :: RX_FIFO_MAX_THRHD ) {
1079- let amount = u16:: try_from ( preferred)
1080- . unwrap_or ( Info :: RX_FIFO_MAX_THRHD )
1081- . min ( Info :: RX_FIFO_MAX_THRHD ) ;
1078+ let current_threshold = self . uart . info ( ) . rx_fifo_full_threshold ( ) ;
10821079
1083- let current = self . uart . info ( ) . rx_fifo_full_threshold ( ) ;
1084- let _guard = if current > amount {
1080+ // User preference takes priority.
1081+ let max_threshold = max_threshold. min ( current_threshold as usize ) as u16 ;
1082+ let minimum = minimum. min ( Info :: RX_FIFO_MAX_THRHD as usize ) as u16 ;
1083+
1084+ // The effective threshold must be >= minimum. We ensure this by lowering the minimum number
1085+ // of returnable bytes.
1086+ let minimum = minimum. min ( max_threshold) ;
1087+
1088+ while self . uart . info ( ) . rx_fifo_count ( ) < minimum {
1089+ let _guard = if current_threshold > max_threshold {
10851090 // We're ignoring the user configuration here to ensure that this is not waiting
10861091 // for more data than the buffer. We'll restore the original value after the
10871092 // future resolved.
10881093 let info = self . uart . info ( ) ;
1089- unwrap ! ( info. set_rx_fifo_full_threshold( amount ) ) ;
1094+ unwrap ! ( info. set_rx_fifo_full_threshold( max_threshold ) ) ;
10901095 Some ( OnDrop :: new ( || {
1091- unwrap ! ( info. set_rx_fifo_full_threshold( current ) ) ;
1096+ unwrap ! ( info. set_rx_fifo_full_threshold( current_threshold ) ) ;
10921097 } ) )
10931098 } else {
10941099 None
0 commit comments