|
| 1 | +//! The IO interface |
| 2 | +//! |
| 3 | +//! Stolen from [Redox Io](https://gitlab.redox-os.org/redox-os/syscall/blob/master/src/io/io.rs) |
| 4 | +
|
| 5 | +mod pio; |
| 6 | +mod mmio; |
| 7 | +pub use self::pio::Pio; |
| 8 | +pub use self::mmio::Mmio; |
| 9 | + |
| 10 | +use core::cmp::PartialEq; |
| 11 | +use core::ops::{BitAnd, BitOr, Not}; |
| 12 | +use core::fmt::{Debug, Formatter, Error}; |
| 13 | + |
| 14 | +/// The Io trait allows for accessing device IO in a generic way, abstracting |
| 15 | +/// over different IO accesses (Port IO and Memory Mapped IO). |
| 16 | +pub trait Io { |
| 17 | + /// The width of the IO access. |
| 18 | + /// Should be a primitive type like u8, u16, u32... |
| 19 | + type Value: Copy; |
| 20 | + |
| 21 | + /// Reads from this Io. |
| 22 | + fn read(&self) -> Self::Value; |
| 23 | + |
| 24 | + /// Writes `value` to this Io. |
| 25 | + fn write(&mut self, value: Self::Value); |
| 26 | + |
| 27 | + /// Read from this Io, and mask the value with `flags`. |
| 28 | + #[inline(always)] |
| 29 | + fn readf(&self, flags: Self::Value) -> bool |
| 30 | + where |
| 31 | + Self::Value: PartialEq + BitAnd<Output = Self::Value> |
| 32 | + { |
| 33 | + (self.read() & flags) as Self::Value == flags |
| 34 | + } |
| 35 | + |
| 36 | + /// Mask `value` with `flags`, and write it to this device address. Note that |
| 37 | + /// this causes a read! |
| 38 | + #[inline(always)] |
| 39 | + fn writef(&mut self, flags: Self::Value, value: bool) |
| 40 | + where |
| 41 | + Self::Value: PartialEq + BitAnd<Output = Self::Value> + BitOr<Output = Self::Value> + Not<Output = Self::Value> |
| 42 | + { |
| 43 | + let tmp: Self::Value = if value { |
| 44 | + self.read() | flags |
| 45 | + } else { |
| 46 | + self.read() & !flags |
| 47 | + }; |
| 48 | + self.write(tmp); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// A read-only wrapper around an IO device. |
| 53 | +#[derive(Debug)] |
| 54 | +#[allow(clippy::missing_docs_in_private_items)] |
| 55 | +pub struct ReadOnly<I> { |
| 56 | + inner: I |
| 57 | +} |
| 58 | + |
| 59 | +impl<I> ReadOnly<I> { |
| 60 | + /// Create a read-only wrapper around the IO device address. |
| 61 | + pub const fn new(inner: I) -> ReadOnly<I> { |
| 62 | + ReadOnly { |
| 63 | + inner: inner |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl<I: Io> ReadOnly<I> { |
| 69 | + /// Reads from this Io. |
| 70 | + #[inline(always)] |
| 71 | + pub fn read(&self) -> I::Value { |
| 72 | + self.inner.read() |
| 73 | + } |
| 74 | + |
| 75 | + /// Read from this Io, and mask the value with `flags`. |
| 76 | + #[inline(always)] |
| 77 | + pub fn readf(&self, flags: I::Value) -> bool |
| 78 | + where |
| 79 | + <I as Io>::Value: PartialEq + BitAnd<Output = <I as Io>::Value> |
| 80 | + { |
| 81 | + self.inner.readf(flags) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// An Io that we can only write to. |
| 86 | +#[allow(clippy::missing_docs_in_private_items)] |
| 87 | +pub struct WriteOnly<I> { |
| 88 | + inner: I |
| 89 | +} |
| 90 | + |
| 91 | +impl<I> WriteOnly<I> { |
| 92 | + /// Creates a WriteOnly Io. |
| 93 | + pub const fn new(inner: I) -> WriteOnly<I> { |
| 94 | + WriteOnly { |
| 95 | + inner: inner |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl<I: Io> WriteOnly<I> { |
| 101 | + /// Writes `value` to this Io. |
| 102 | + #[inline(always)] |
| 103 | + pub fn write(&mut self, value: I::Value) { |
| 104 | + self.inner.write(value) |
| 105 | + } |
| 106 | + |
| 107 | + // writef() not exposed as it requires a read. |
| 108 | +} |
| 109 | + |
| 110 | +impl<I> Debug for WriteOnly<I> { |
| 111 | + /// Debug does not access the **write only** value. |
| 112 | + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { |
| 113 | + f.debug_struct("WriteOnly") |
| 114 | + .finish() |
| 115 | + } |
| 116 | +} |
0 commit comments