Skip to content

Commit

Permalink
Attempt to add read_single_word
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Dec 15, 2023
1 parent 88a5d60 commit 64ff0c9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
50 changes: 45 additions & 5 deletions src/common/lpspi/read_part.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::num::NonZeroUsize;

use super::{
ral,
transfer_actions::{ByteOrder, ReadAction},
LpspiReadPart,
};

impl<const N: u8> LpspiReadPart<'_, N> {
fn fifo_read_data_available(&self) -> bool {
fn fifo_read_data_available(&mut self) -> bool {
ral::read_reg!(
ral::lpspi,
self.data.lpspi.instance(),
Expand All @@ -14,7 +16,7 @@ impl<const N: u8> LpspiReadPart<'_, N> {
)
}

async fn wait_for_read_watermark(&self, watermark: u32) {
async fn wait_for_read_watermark(&mut self, watermark: u32) {
self.data
.lpspi
.wait_for_rx_watermark(watermark)
Expand Down Expand Up @@ -44,14 +46,52 @@ impl<const N: u8> LpspiReadPart<'_, N> {
) {
for action in actions {
if action.len.get() < 4 {
todo!();
// self.read_single_word(action.buf, byteorder, action.len)
// .await
self.read_single_word(action.buf, byteorder, action.len)
.await
} else {
todo!();
// self.read_u32_stream(action.buf, byteorder, action.len)
// .await;
}
}
}

async fn rx_fifo_read_data(&mut self, num_leftover: usize) -> u32 {
self.wait_for_read_data_available(num_leftover).await;
ral::read_reg!(ral::lpspi, self.data.lpspi.instance(), RDR)
}

pub async unsafe fn read_single_word(
&mut self,
data: *mut u8,
byteorder: ByteOrder,
len: NonZeroUsize,
) {
assert!(len.get() < 4);

let reverse_bytes = match byteorder {
ByteOrder::Normal => false,
ByteOrder::WordReversed => true,
ByteOrder::HalfWordReversed => true,
};

log::info!("Receiving ...");
let value = self.rx_fifo_read_data(1).await;
log::info!("Read: 0x{:02x}", value);
let rx_buffer = value.to_le_bytes();

let active_buffer = &rx_buffer[(4 - len.get())..];
if reverse_bytes {
active_buffer
.iter()
.rev()
.enumerate()
.for_each(|(pos, val)| data.add(pos).write(*val));
} else {
active_buffer
.iter()
.enumerate()
.for_each(|(pos, val)| data.add(pos).write(*val));
}
}
}
8 changes: 4 additions & 4 deletions src/common/lpspi/write_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
};

impl<const N: u8> LpspiWritePart<'_, N> {
fn fifo_write_space_available(&self) -> bool {
fn fifo_write_space_available(&mut self) -> bool {
ral::read_reg!(
ral::lpspi,
self.data.lpspi.instance(),
Expand All @@ -18,17 +18,17 @@ impl<const N: u8> LpspiWritePart<'_, N> {
)
}

async fn wait_for_write_watermark(&self) {
async fn wait_for_write_watermark(&mut self) {
self.data.lpspi.wait_for_tx_watermark().await.unwrap();
}

async fn wait_for_write_space_available(&self) {
async fn wait_for_write_space_available(&mut self) {
if !self.fifo_write_space_available() {
self.wait_for_write_watermark().await;
}
}

async fn tx_fifo_enqueue_data(&self, val: u32) {
async fn tx_fifo_enqueue_data(&mut self, val: u32) {
self.wait_for_write_space_available().await;
ral::write_reg!(ral::lpspi, self.data.lpspi.instance(), TDR, val);
}
Expand Down

0 comments on commit 64ff0c9

Please sign in to comment.