Skip to content

Commit

Permalink
Adding return of remaining transfers
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Dec 7, 2020
1 parent 0bfeeca commit e70a787
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ where
pub fn next_transfer(
&mut self,
mut new_buf: BUF,
) -> Result<(BUF, CurrentBuffer), DMAError<BUF>> {
) -> Result<(BUF, CurrentBuffer, usize), DMAError<BUF>> {
if self.double_buf.is_some()
&& DIR::direction() != DmaDirection::MemoryToMemory
{
Expand Down Expand Up @@ -513,10 +513,10 @@ where
// preceding reads"
compiler_fence(Ordering::Acquire);

let old_buf = self.buf.replace(new_buf);

// We always have a buffer, so unwrap can't fail
return Ok((old_buf.unwrap(), CurrentBuffer::FirstBuffer));
let old_buf = self.buf.replace(new_buf).unwrap();

return Ok((old_buf, CurrentBuffer::FirstBuffer, 0));
} else {
unsafe {
self.stream
Expand All @@ -535,10 +535,10 @@ where
// preceding reads"
compiler_fence(Ordering::Acquire);

let old_buf = self.double_buf.replace(new_buf);

// double buffering, unwrap can never fail
return Ok((old_buf.unwrap(), CurrentBuffer::DoubleBuffer));
let old_buf = self.double_buf.replace(new_buf).unwrap();

return Ok((old_buf, CurrentBuffer::DoubleBuffer, 0));
}
}
self.stream.disable();
Expand All @@ -547,6 +547,9 @@ where
// "No re-ordering of reads and writes across this point is allowed"
compiler_fence(Ordering::SeqCst);

// Check how many data in the transfer are remaining.
let remaining_data = STREAM::get_number_of_transfers();

// NOTE(unsafe) We now own this buffer and we won't call any &mut
// methods on it until the end of the DMA transfer
let buf_len = unsafe {
Expand All @@ -556,7 +559,9 @@ where
buf_len
};
self.stream.set_number_of_transfers(buf_len as u16);
let old_buf = self.buf.replace(new_buf);

// We own the buffer now, so unwrap is always safe.
let old_buf = self.buf.replace(new_buf).unwrap();

// Ensure that all transfers to normal memory complete before
// subsequent memory transfers.
Expand All @@ -573,7 +578,7 @@ where
self.stream.enable();
}

Ok((old_buf.unwrap(), CurrentBuffer::FirstBuffer))
Ok((old_buf, CurrentBuffer::FirstBuffer, remaining_data as usize))
}

/// Stops the stream and returns the underlying resources.
Expand Down

0 comments on commit e70a787

Please sign in to comment.