Use async spi transaction helper macro.

This commit is contained in:
Dario Nieuwenhuis 2022-09-20 22:01:24 +02:00
parent c385bbf07d
commit 0d84533bcb

View File

@ -23,7 +23,7 @@ use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::channel::Channel; use embassy_sync::channel::Channel;
use embassy_time::{block_for, Duration, Timer}; use embassy_time::{block_for, Duration, Timer};
use embedded_hal_1::digital::OutputPin; use embedded_hal_1::digital::OutputPin;
use embedded_hal_async::spi::{SpiBusRead, SpiBusWrite, SpiDevice}; use embedded_hal_async::spi::{transaction, SpiBusRead, SpiBusWrite, SpiDevice};
use self::structs::*; use self::structs::*;
use crate::events::Event; use crate::events::Event;
@ -753,17 +753,13 @@ where
let cmd = cmd_word(READ, INC_ADDR, FUNC_WLAN, 0, len); let cmd = cmd_word(READ, INC_ADDR, FUNC_WLAN, 0, len);
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[cmd]).await?;
let bus = unsafe { &mut *bus }; bus.read(&mut buf[..(len as usize + 3) / 4]).await?;
async { Ok(())
bus.write(&[cmd]).await?; })
bus.read(&mut buf[..(len as usize + 3) / 4]).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
trace!("rx {:02x}", &slice8_mut(&mut buf)[..(len as usize).min(48)]); trace!("rx {:02x}", &slice8_mut(&mut buf)[..(len as usize).min(48)]);
@ -817,17 +813,13 @@ where
trace!(" {:02x}", &buf8[..total_len.min(48)]); trace!(" {:02x}", &buf8[..total_len.min(48)]);
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_WLAN, 0, total_len as _); let cmd = cmd_word(WRITE, INC_ADDR, FUNC_WLAN, 0, total_len as _);
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[cmd]).await?;
let bus = unsafe { &mut *bus }; bus.write(&buf[..(total_len / 4)]).await?;
async { Ok(())
bus.write(&[cmd]).await?; })
bus.write(&buf[..(total_len / 4)]).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
} }
fn rx(&mut self, packet: &[u8]) { fn rx(&mut self, packet: &[u8]) {
@ -1012,17 +1004,13 @@ where
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_WLAN, 0, total_len as _); let cmd = cmd_word(WRITE, INC_ADDR, FUNC_WLAN, 0, total_len as _);
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[cmd]).await?;
let bus = unsafe { &mut *bus }; bus.write(&buf[..total_len / 4]).await?;
async { Ok(())
bus.write(&[cmd]).await?; })
bus.write(&buf[..total_len / 4]).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
} }
async fn core_disable(&mut self, core: Core) { async fn core_disable(&mut self, core: Core) {
@ -1101,23 +1089,19 @@ where
let cmd = cmd_word(READ, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32); let cmd = cmd_word(READ, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32);
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[cmd]).await?;
let bus = unsafe { &mut *bus };
async {
bus.write(&[cmd]).await?;
// 4-byte response delay. // 4-byte response delay.
let mut junk = [0; 1]; let mut junk = [0; 1];
bus.read(&mut junk).await?; bus.read(&mut junk).await?;
// Read data // Read data
bus.read(&mut data[..len / 4]).await?; bus.read(&mut data[..len / 4]).await?;
Ok(()) Ok(())
} })
}) .await
.await .unwrap();
.unwrap();
// Advance ptr. // Advance ptr.
addr += len as u32; addr += len as u32;
@ -1146,17 +1130,13 @@ where
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32); let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32);
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[cmd]).await?;
let bus = unsafe { &mut *bus }; bus.write(&buf[..(len + 3) / 4]).await?;
async { Ok(())
bus.write(&[cmd]).await?; })
bus.write(&buf[..(len + 3) / 4]).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
// Advance ptr. // Advance ptr.
addr += len as u32; addr += len as u32;
@ -1270,21 +1250,17 @@ where
let cmd = cmd_word(READ, INC_ADDR, func, addr, len); let cmd = cmd_word(READ, INC_ADDR, func, addr, len);
let mut buf = [0; 1]; let mut buf = [0; 1];
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[cmd]).await?;
let bus = unsafe { &mut *bus }; if func == FUNC_BACKPLANE {
async { // 4-byte response delay.
bus.write(&[cmd]).await?; bus.read(&mut buf).await?;
if func == FUNC_BACKPLANE { }
// 4-byte response delay. bus.read(&mut buf).await?;
bus.read(&mut buf).await?; Ok(())
} })
bus.read(&mut buf).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
buf[0] buf[0]
} }
@ -1292,33 +1268,25 @@ where
async fn writen(&mut self, func: u32, addr: u32, val: u32, len: u32) { async fn writen(&mut self, func: u32, addr: u32, val: u32, len: u32) {
let cmd = cmd_word(WRITE, INC_ADDR, func, addr, len); let cmd = cmd_word(WRITE, INC_ADDR, func, addr, len);
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[cmd, val]).await?;
let bus = unsafe { &mut *bus }; Ok(())
async { })
bus.write(&[cmd, val]).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
} }
async fn read32_swapped(&mut self, addr: u32) -> u32 { async fn read32_swapped(&mut self, addr: u32) -> u32 {
let cmd = cmd_word(READ, INC_ADDR, FUNC_BUS, addr, 4); let cmd = cmd_word(READ, INC_ADDR, FUNC_BUS, addr, 4);
let mut buf = [0; 1]; let mut buf = [0; 1];
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[swap16(cmd)]).await?;
let bus = unsafe { &mut *bus }; bus.read(&mut buf).await?;
async { Ok(())
bus.write(&[swap16(cmd)]).await?; })
bus.read(&mut buf).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
swap16(buf[0]) swap16(buf[0])
} }
@ -1326,16 +1294,12 @@ where
async fn write32_swapped(&mut self, addr: u32, val: u32) { async fn write32_swapped(&mut self, addr: u32, val: u32) {
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BUS, addr, 4); let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BUS, addr, 4);
self.spi transaction!(&mut self.spi, |bus| async {
.transaction(|bus| { bus.write(&[swap16(cmd), swap16(val)]).await?;
let bus = unsafe { &mut *bus }; Ok(())
async { })
bus.write(&[swap16(cmd), swap16(val)]).await?; .await
Ok(()) .unwrap();
}
})
.await
.unwrap();
} }
} }