rust: Make SPI send/recv loops nicer

Use iterators to make the loops simpler.
This commit is contained in:
Wladimir J. van der Laan 2019-08-11 15:47:28 +00:00
parent 161bd9982e
commit a5d1f5c9ae

View File

@ -179,16 +179,13 @@ impl<IF: SPI01> SPI for SPIImpl<IF> {
self.spi.dr[0].write(|w| w.bits(0xffffffff)); self.spi.dr[0].write(|w| w.bits(0xffffffff));
self.spi.ser.write(|w| w.bits(1 << chip_select)); self.spi.ser.write(|w| w.bits(1 << chip_select));
let mut i = 0; let mut fifo_len = 0;
let mut rx_len = rx.len(); for val in rx.iter_mut() {
while rx_len != 0 { while fifo_len == 0 {
let fifo_len = self.spi.rxflr.read().bits() as usize; fifo_len = self.spi.rxflr.read().bits();
let fifo_len = cmp::min(fifo_len, rx_len);
for _ in 0..fifo_len {
rx[i] = X::trunc(self.spi.dr[0].read().bits());
i += 1;
} }
rx_len -= fifo_len; *val = X::trunc(self.spi.dr[0].read().bits());
fifo_len -= 1;
} }
self.spi.ser.write(|w| w.bits(0x00)); self.spi.ser.write(|w| w.bits(0x00));
@ -202,17 +199,13 @@ impl<IF: SPI01> SPI for SPIImpl<IF> {
self.spi.ser.write(|w| w.bits(1 << chip_select)); self.spi.ser.write(|w| w.bits(1 << chip_select));
self.spi.ssienr.write(|w| w.bits(0x01)); self.spi.ssienr.write(|w| w.bits(0x01));
// TODO: write this using iterators / slices let mut fifo_len = 0;
let mut i = 0; for &val in tx {
let mut tx_len = tx.len(); while fifo_len == 0 {
while tx_len != 0 { fifo_len = 32 - self.spi.txflr.read().bits();
let fifo_len = (32 - self.spi.txflr.read().bits()) as usize;
let fifo_len = cmp::min(fifo_len, tx_len);
for _ in 0..fifo_len {
self.spi.dr[0].write(|f| f.bits(tx[i].into()));
i += 1;
} }
tx_len -= fifo_len; self.spi.dr[0].write(|f| f.bits(val.into()));
fifo_len -= 1;
} }
while (self.spi.sr.read().bits() & 0x05) != 0x04 { while (self.spi.sr.read().bits() & 0x05) != 0x04 {