Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMA: Add read_available #119

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions src/dma/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,8 @@ where
pub fn read_exact(
&mut self,
dat: &mut [<PERIPHERAL as TargetAddress<PeripheralToMemory>>::MemSize],
) -> usize {
) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the returned value is always data.len() i see no pint in returning it.

let blen = unsafe { self.transfer.buf.static_write_buffer().1 };
let pos = self.r_pos;
let read = dat.len();

assert!(
Expand All @@ -352,6 +351,29 @@ where

while self.elements_available() < read {}

self.read_unchecked(read, dat);
}

/// Read the direcly available data
pub fn read_available(
&mut self,
dat: &mut [<PERIPHERAL as TargetAddress<PeripheralToMemory>>::MemSize],
) -> &mut [<PERIPHERAL as TargetAddress<PeripheralToMemory>>::MemSize] {
let read = self.elements_available().min(dat.len());

self.read_unchecked(read, dat);

&mut dat[0..read]
}

fn read_unchecked(
&mut self,
read: usize,
dat: &mut [<PERIPHERAL as TargetAddress<PeripheralToMemory>>::MemSize],
) {
let blen = unsafe { self.transfer.buf.static_write_buffer().1 };
let pos = self.r_pos;

fence(Ordering::SeqCst);

if pos + read <= blen {
Expand All @@ -373,9 +395,6 @@ where
}

fence(Ordering::SeqCst);

// return the number of bytes read
read
}

/// Starts the transfer, the closure will be executed right after enabling
Expand Down
Loading