dma_api/dma/
box.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use core::alloc::Layout;

use crate::Direction;

use super::DCommon;

pub struct DBox<T> {
    inner: DCommon<T>,
}

impl<T> DBox<T> {
    const SIZE: usize = core::mem::size_of::<T>();

    pub fn zero(direction: Direction) -> Option<Self> {
        let layout = Layout::new::<T>();

        Some(Self {
            inner: DCommon::zeros(layout, direction)?,
        })
    }
    pub fn bus_addr(&self) -> u64 {
        self.inner.bus_addr
    }

    pub fn read(&self) -> T {
        unsafe {
            let ptr = self.inner.addr;

            self.inner.preper_read(ptr.cast(), Self::SIZE);

            ptr.read_volatile()
        }
    }

    pub fn write(&mut self, value: T) {
        unsafe {
            let ptr = self.inner.addr;

            ptr.write_volatile(value);

            self.inner.confirm_write(ptr.cast(), Self::SIZE);
        }
    }

    pub fn modify(&mut self, f: impl FnOnce(&mut T)) {
        unsafe {
            let mut ptr = self.inner.addr;

            self.inner.preper_read(ptr.cast(), Self::SIZE);

            f(ptr.as_mut());

            self.inner.confirm_write(ptr.cast(), Self::SIZE);
        }
    }
}