Skip to content

Commit a03f188

Browse files
lulfhargoniX
authored andcommitted
Restrict serial API to read only 1 byte at a time to avoid DMA issues.
1 parent 016fcf7 commit a03f188

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

nrf-hal-common/src/uarte.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ where
202202
///
203203
/// The buffer must have a length of at most 255 bytes.
204204
pub fn read(&mut self, rx_buffer: &mut [u8]) -> Result<(), Error> {
205-
start_read(&*self.0, rx_buffer)?;
205+
start_read(&*self.0, rx_buffer, rx_buffer.len())?;
206206

207207
// Wait for transmission to end.
208208
while self.0.events_endrx.read().bits() == 0 {}
@@ -240,7 +240,7 @@ where
240240
I: timer::Instance,
241241
{
242242
// Start the read.
243-
start_read(&self.0, rx_buffer)?;
243+
start_read(&self.0, rx_buffer, rx_buffer.len())?;
244244

245245
// Start the timeout timer.
246246
timer.start(cycles);
@@ -363,7 +363,11 @@ fn stop_write(uarte: &uarte0::RegisterBlock) {
363363

364364
/// Start a UARTE read transaction by setting the control
365365
/// values and triggering a read task.
366-
fn start_read(uarte: &uarte0::RegisterBlock, rx_buffer: &mut [u8]) -> Result<(), Error> {
366+
fn start_read(
367+
uarte: &uarte0::RegisterBlock,
368+
rx_buffer: &mut [u8],
369+
nbytes: usize,
370+
) -> Result<(), Error> {
367371
if rx_buffer.len() > EASY_DMA_SIZE {
368372
return Err(Error::RxBufferTooLong);
369373
}
@@ -392,7 +396,7 @@ fn start_read(uarte: &uarte0::RegisterBlock, rx_buffer: &mut [u8]) -> Result<(),
392396
//
393397
// The MAXCNT field is at least 8 bits wide and accepts the full
394398
// range of values.
395-
unsafe { w.maxcnt().bits(rx_buffer.len() as _) });
399+
unsafe { w.maxcnt().bits(nbytes.min(rx_buffer.len()) as _) });
396400

397401
// Start UARTE Receive transaction.
398402
uarte.tasks_startrx.write(|w|
@@ -700,17 +704,16 @@ where
700704
}
701705

702706
if in_progress {
703-
let b = self.rx_buf[0];
704707
uarte.events_rxstarted.reset();
705708

706709
finalize_read(uarte);
707710

708711
if uarte.rxd.amount.read().bits() != 1 as u32 {
709712
return Err(nb::Error::Other(Error::Receive));
710713
}
711-
Ok(b)
714+
Ok(self.rx_buf[0])
712715
} else {
713-
start_read(&uarte, self.rx_buf)?;
716+
start_read(&uarte, self.rx_buf, 1)?;
714717
Err(nb::Error::WouldBlock)
715718
}
716719
}

0 commit comments

Comments
 (0)