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_time::{block_for, Duration, Timer};
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 crate::events::Event;
@ -753,14 +753,10 @@ where
let cmd = cmd_word(READ, INC_ADDR, FUNC_WLAN, 0, len);
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[cmd]).await?;
bus.read(&mut buf[..(len as usize + 3) / 4]).await?;
Ok(())
}
})
.await
.unwrap();
@ -817,14 +813,10 @@ where
trace!(" {:02x}", &buf8[..total_len.min(48)]);
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_WLAN, 0, total_len as _);
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[cmd]).await?;
bus.write(&buf[..(total_len / 4)]).await?;
Ok(())
}
})
.await
.unwrap();
@ -1012,14 +1004,10 @@ where
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_WLAN, 0, total_len as _);
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[cmd]).await?;
bus.write(&buf[..total_len / 4]).await?;
Ok(())
}
})
.await
.unwrap();
@ -1101,10 +1089,7 @@ where
let cmd = cmd_word(READ, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32);
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[cmd]).await?;
// 4-byte response delay.
@ -1114,7 +1099,6 @@ where
// Read data
bus.read(&mut data[..len / 4]).await?;
Ok(())
}
})
.await
.unwrap();
@ -1146,14 +1130,10 @@ where
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BACKPLANE, window_offs, len as u32);
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[cmd]).await?;
bus.write(&buf[..(len + 3) / 4]).await?;
Ok(())
}
})
.await
.unwrap();
@ -1270,10 +1250,7 @@ where
let cmd = cmd_word(READ, INC_ADDR, func, addr, len);
let mut buf = [0; 1];
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[cmd]).await?;
if func == FUNC_BACKPLANE {
// 4-byte response delay.
@ -1281,7 +1258,6 @@ where
}
bus.read(&mut buf).await?;
Ok(())
}
})
.await
.unwrap();
@ -1292,13 +1268,9 @@ where
async fn writen(&mut self, func: u32, addr: u32, val: u32, len: u32) {
let cmd = cmd_word(WRITE, INC_ADDR, func, addr, len);
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[cmd, val]).await?;
Ok(())
}
})
.await
.unwrap();
@ -1308,14 +1280,10 @@ where
let cmd = cmd_word(READ, INC_ADDR, FUNC_BUS, addr, 4);
let mut buf = [0; 1];
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[swap16(cmd)]).await?;
bus.read(&mut buf).await?;
Ok(())
}
})
.await
.unwrap();
@ -1326,13 +1294,9 @@ where
async fn write32_swapped(&mut self, addr: u32, val: u32) {
let cmd = cmd_word(WRITE, INC_ADDR, FUNC_BUS, addr, 4);
self.spi
.transaction(|bus| {
let bus = unsafe { &mut *bus };
async {
transaction!(&mut self.spi, |bus| async {
bus.write(&[swap16(cmd), swap16(val)]).await?;
Ok(())
}
})
.await
.unwrap();