Skip to content

Commit 63601fa

Browse files
committed
Fix loop condition
1 parent 25d9d9f commit 63601fa

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

esp-hal/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222
- RMT: `ChannelCreator::configure_tx` and `ChannelCreator::configure_rx` don't take a pin anymore, instead `Channel::with_pin` has been added. (#4302)
2323
- RMT: Configuration errors have been split out of `rmt::Error` into the new `rmt::ConfigError` enum. (#4494)
2424
- RMT: `Rmt::new()` now returns `Error::UnreachableTargetFrequency` instead of panicking when requesting 0 Hz. (#4509)
25+
- UART: changed the FIFO threshold in `read_exact_async` to reduce the likelyhood of `FifoOverflowed` errors. (#?)
2526

2627
- Internal clock configuration rework (#4501)
2728

esp-hal/src/uart/mod.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)