Skip to content

Commit

Permalink
try to optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Apr 23, 2024
1 parent 460646c commit 3f26c75
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use super::{
SpiDataMode,
SpiMode,
};
use crate::efuse::WR_DIS_OCODE;
use crate::{
clock::Clocks,
dma::{DmaPeripheral, Rx, Tx},
Expand Down Expand Up @@ -2661,31 +2662,31 @@ pub trait Instance: crate::private::Sealed {
for (i, chunk) in words.chunks(FIFO_SIZE).enumerate() {
self.configure_datalen(chunk.len() as u32 * 8);

for (i, w_reg) in (0..chunk.len()).step_by(4).zip(self.register_block().w_iter()) {
let state = if chunk.len() - i < 4 {
chunk.len() % 4
} else {
0
};
let word = match state {
0 => {
(chunk[i] as u32)
| (chunk[i + 1] as u32) << 8
| (chunk[i + 2] as u32) << 16
| (chunk[i + 3] as u32) << 24
{
// TODO: replace with `array_chunks` and `from_le_bytes`
let mut c_iter = chunk.chunks_exact(4);
let mut w_iter = self.register_block().w_iter();
while let Some(c) = c_iter.next() {
if let Some(w_reg) = w_iter.next() {
let word = (c[0] as u32)
| (c[1] as u32) << 8
| (c[2] as u32) << 16
| (c[3] as u32) << 24;
w_reg.write(|w| w.buf().set(word));
}

3 => {
(chunk[i] as u32) | (chunk[i + 1] as u32) << 8 | (chunk[i + 2] as u32) << 16
}
let rem = c_iter.remainder();
if !rem.is_empty() {
if let Some(w_reg) = w_iter.next() {
let word = match rem.len() {
3 => (rem[0] as u32) | (rem[1] as u32) << 8 | (rem[2] as u32) << 16,
2 => (rem[0] as u32) | (rem[1] as u32) << 8,
1 => rem[0] as u32,
_ => unreachable!(),
};
w_reg.write(|w| w.buf().set(word));
}

2 => (chunk[i] as u32) | (chunk[i + 1] as u32) << 8,

1 => chunk[i] as u32,

_ => panic!(),
};
w_reg.write(|w| w.buf().set(word));
}
}

self.update();
Expand Down

0 comments on commit 3f26c75

Please sign in to comment.