Skip to content

Commit a7a3064

Browse files
committed
Reset tx-only status after write
1 parent 2d11bac commit a7a3064

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

src/spi.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ macro_rules! spi {
204204
}
205205

206206
impl<PINS> Spi<$SPIX, PINS> {
207+
#[inline]
207208
fn nb_read<W: FrameSize>(&mut self) -> nb::Result<W, Error> {
208209
let sr = self.spi.sr.read();
209210
Err(if sr.ovr().bit_is_set() {
@@ -222,6 +223,7 @@ macro_rules! spi {
222223
nb::Error::WouldBlock
223224
})
224225
}
226+
#[inline]
225227
fn nb_write<W: FrameSize>(&mut self, word: W) -> nb::Result<(), Error> {
226228
let sr = self.spi.sr.read();
227229
Err(if sr.ovr().bit_is_set() {
@@ -239,12 +241,14 @@ macro_rules! spi {
239241
nb::Error::WouldBlock
240242
})
241243
}
242-
fn set_tx_only(&mut self) {
244+
#[inline]
245+
pub fn set_tx_only(&mut self) {
243246
self.spi
244247
.cr1
245248
.modify(|_, w| w.bidimode().set_bit().bidioe().set_bit());
246249
}
247-
fn set_bidi(&mut self) {
250+
#[inline]
251+
pub fn set_bidi(&mut self) {
248252
self.spi
249253
.cr1
250254
.modify(|_, w| w.bidimode().clear_bit().bidioe().clear_bit());
@@ -285,10 +289,14 @@ macro_rules! spi {
285289
}
286290

287291
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
292+
let catch = |spi: &mut Self| Ok(for w in words {
293+
nb::block!(spi.nb_write(*w))?
294+
});
295+
288296
self.set_tx_only();
289-
Ok(for w in words {
290-
nb::block!(self.nb_write(*w))?
291-
})
297+
let res = catch(self);
298+
self.set_bidi();
299+
res
292300
}
293301

294302
fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> {
@@ -333,16 +341,23 @@ macro_rules! spi {
333341
Ok(())
334342
}
335343
fn flush(&mut self) -> Result<(), Self::Error> {
344+
let catch = |spi: &mut Self| {
345+
// drain rx fifo
346+
while match spi.nb_read::<u8>() {
347+
Ok(_) => true,
348+
Err(nb::Error::WouldBlock) => false,
349+
Err(nb::Error::Other(e)) => { return Err(e) }
350+
} { core::hint::spin_loop() };
351+
// wait for tx fifo to be drained by the peripheral
352+
while spi.spi.sr.read().ftlvl() != 0 { core::hint::spin_loop() };
353+
Ok(())
354+
};
355+
336356
// stop receiving data
337357
self.set_tx_only();
338-
// wait for tx fifo to be drained by the peripheral
339-
while self.spi.sr.read().ftlvl() != 0 { core::hint::spin_loop() };
340-
// drain rx fifo
341-
Ok(while match self.nb_read::<u8>() {
342-
Ok(_) => true,
343-
Err(nb::Error::WouldBlock) => false,
344-
Err(nb::Error::Other(e)) => return Err(e)
345-
} { core::hint::spin_loop() })
358+
let res = catch(self);
359+
self.set_bidi();
360+
res
346361
}
347362
}
348363

0 commit comments

Comments
 (0)