Skip to content

Commit 326948f

Browse files
Jonathan Pallant (42 Technology)Yatekii
authored andcommitted
Fix UART wasn't blocking until TX complete.
1 parent bbf28eb commit 326948f

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

nrf52-hal-common/src/uarte.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,19 @@ impl<T> Uarte<T> where T: Instance {
123123
return Err(Error::TxBufferTooLong);
124124
}
125125

126+
if !crate::slice_in_ram(tx_buffer){
127+
return Err(Error::BufferNotInRAM);
128+
}
129+
126130
// Conservative compiler fence to prevent optimizations that do not
127131
// take in to account actions by DMA. The fence has been placed here,
128132
// before any DMA action has started
129133
compiler_fence(SeqCst);
130134

135+
// Reset the events.
136+
self.0.events_endtx.write(|w| w);
137+
self.0.events_txstopped.write(|w| w);
138+
131139
// Set up the DMA write
132140
self.0.txd.ptr.write(|w|
133141
// We're giving the register a pointer to the stack. Since we're
@@ -153,17 +161,22 @@ impl<T> Uarte<T> where T: Instance {
153161
unsafe { w.bits(1) });
154162

155163
// Wait for transmission to end
156-
while self.0.events_endtx.read().bits() == 0 {}
157-
158-
// Reset the event, otherwise it will always read `1` from now on.
159-
self.0.events_endtx.write(|w| w);
164+
let mut endtx;
165+
let mut txstopped;
166+
loop {
167+
endtx = self.0.events_endtx.read().bits() != 0;
168+
txstopped = self.0.events_txstopped.read().bits() != 0;
169+
if endtx || txstopped {
170+
break;
171+
}
172+
}
160173

161174
// Conservative compiler fence to prevent optimizations that do not
162175
// take in to account actions by DMA. The fence has been placed here,
163176
// after all possible DMA actions have completed
164177
compiler_fence(SeqCst);
165178

166-
if self.0.txd.amount.read().bits() != tx_buffer.len() as u32 {
179+
if txstopped {
167180
return Err(Error::Transmit);
168181
}
169182

@@ -364,6 +377,7 @@ pub enum Error {
364377
Transmit,
365378
Receive,
366379
Timeout(usize),
380+
BufferNotInRAM,
367381
}
368382

369383

0 commit comments

Comments
 (0)