Skip to content

Commit

Permalink
Add DMA buffer view support to I8080 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic Fischer committed Oct 2, 2024
1 parent 30aef58 commit d7b829b
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions esp-hal/src/lcd_cam/lcd/i8080.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use core::{
fmt::Formatter,
marker::PhantomData,
mem::{size_of, ManuallyDrop},
ops::{Deref, DerefMut},
};

use fugit::HertzU32;
Expand Down Expand Up @@ -360,7 +361,7 @@ impl<'d, CH: DmaChannel, DM: Mode> I8080<'d, CH, DM> {

Ok(I8080Transfer {
i8080: ManuallyDrop::new(self),
tx_buf: ManuallyDrop::new(data),
buf_view: ManuallyDrop::new(data.into_view()),
})
}
}
Expand All @@ -373,12 +374,12 @@ impl<'d, CH: DmaChannel, DM: Mode> core::fmt::Debug for I8080<'d, CH, DM> {

/// Represents an ongoing (or potentially finished) transfer using the I8080 LCD
/// interface
pub struct I8080Transfer<'d, BUF, CH: DmaChannel, DM: Mode> {
pub struct I8080Transfer<'d, BUF: DmaTxBuffer, CH: DmaChannel, DM: Mode> {
i8080: ManuallyDrop<I8080<'d, CH, DM>>,
tx_buf: ManuallyDrop<BUF>,
buf_view: ManuallyDrop<BUF::View>,
}

impl<'d, BUF, CH: DmaChannel, DM: Mode> I8080Transfer<'d, BUF, CH, DM> {
impl<'d, BUF: DmaTxBuffer, CH: DmaChannel, DM: Mode> I8080Transfer<'d, BUF, CH, DM> {
/// Returns true when [Self::wait] will not block.
pub fn is_done(&self) -> bool {
self.i8080
Expand Down Expand Up @@ -411,9 +412,9 @@ impl<'d, BUF, CH: DmaChannel, DM: Mode> I8080Transfer<'d, BUF, CH, DM> {

// SAFETY: Since forget is called on self, we know that self.i8080 and
// self.tx_buf won't be touched again.
let (i8080, tx_buf) = unsafe {
let (i8080, view) = unsafe {
let i8080 = ManuallyDrop::take(&mut self.i8080);
let tx_buf = ManuallyDrop::take(&mut self.tx_buf);
let tx_buf = ManuallyDrop::take(&mut self.buf_view);
core::mem::forget(self);
(i8080, tx_buf)
};
Expand All @@ -424,7 +425,7 @@ impl<'d, BUF, CH: DmaChannel, DM: Mode> I8080Transfer<'d, BUF, CH, DM> {
Ok(())
};

(result, i8080, tx_buf)
(result, i8080, BUF::from_view(view))
}

fn stop_peripherals(&mut self) {
Expand All @@ -439,7 +440,21 @@ impl<'d, BUF, CH: DmaChannel, DM: Mode> I8080Transfer<'d, BUF, CH, DM> {
}
}

impl<'d, BUF, CH: DmaChannel> I8080Transfer<'d, BUF, CH, crate::Async> {
impl<'d, BUF: DmaTxBuffer, CH: DmaChannel, DM: Mode> Deref for I8080Transfer<'d, BUF, CH, DM> {
type Target = BUF::View;

fn deref(&self) -> &Self::Target {
&self.buf_view
}
}

impl<'d, BUF: DmaTxBuffer, CH: DmaChannel, DM: Mode> DerefMut for I8080Transfer<'d, BUF, CH, DM> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.buf_view
}
}

impl<'d, BUF: DmaTxBuffer, CH: DmaChannel> I8080Transfer<'d, BUF, CH, crate::Async> {
/// Waits for [Self::is_done] to return true.
pub async fn wait_for_done(&mut self) {
use core::{
Expand Down Expand Up @@ -478,15 +493,15 @@ impl<'d, BUF, CH: DmaChannel> I8080Transfer<'d, BUF, CH, crate::Async> {
}
}

impl<'d, BUF, CH: DmaChannel, DM: Mode> Drop for I8080Transfer<'d, BUF, CH, DM> {
impl<'d, BUF: DmaTxBuffer, CH: DmaChannel, DM: Mode> Drop for I8080Transfer<'d, BUF, CH, DM> {
fn drop(&mut self) {
self.stop_peripherals();

// SAFETY: This is Drop, we know that self.i8080 and self.tx_buf
// won't be touched again.
unsafe {
ManuallyDrop::drop(&mut self.i8080);
ManuallyDrop::drop(&mut self.tx_buf);
ManuallyDrop::drop(&mut self.buf_view);
}
}
}
Expand Down

0 comments on commit d7b829b

Please sign in to comment.