mmio.rs 675 Bytes
Newer Older
1
use core::ptr::{read_volatile, write_volatile};
Jeremy Soller's avatar
Jeremy Soller committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use core::mem::uninitialized;
use core::ops::{BitAnd, BitOr, Not};

use super::io::Io;

#[repr(packed)]
pub struct Mmio<T> {
    value: T,
}

impl<T> Mmio<T> {
    /// Create a new Mmio without initializing
    pub fn new() -> Self {
        Mmio {
            value: unsafe { uninitialized() }
        }
    }
}

impl<T> Io for Mmio<T> where T: Copy + PartialEq + BitAnd<Output = T> + BitOr<Output = T> + Not<Output = T> {
    type Value = T;

    fn read(&self) -> T {
25
        unsafe { read_volatile(&self.value) }
Jeremy Soller's avatar
Jeremy Soller committed
26
27
28
    }

    fn write(&mut self, value: T) {
29
        unsafe { write_volatile(&mut self.value, value) };
Jeremy Soller's avatar
Jeremy Soller committed
30
31
    }
}