base/
io.rs

1/// A general I/O interface.
2pub trait HardwareIo {
3   /// The value type used for operations.
4   type Value: Copy + PartialEq + BitAnd<Output = Self::Value> + BitOr<Output = Self::Value> + Not<Output = Self::Value>;
5
6   /// Reads from the interface, returning the read value.
7   fn read(&self) -> Self::Value;
8
9   /// Writes value to the interface.
10   fn write(&mut self, value: Self::Value);
11
12   /// Reads the value from the I/O interface and checks if the specified flags are set.
13   ///
14   /// # Arguments
15   ///
16   /// * `flags` - The flags to check.
17   ///
18   /// # Returns
19   ///
20   /// Returns `true` if all the specified flags are set, `false` otherwise.
21   #[inline(always)]
22   fn read_flags(&self, flags: Self::Value) -> bool {
23      return ((self.read() & flags) as Self::Value) == flags;
24   }
25
26   /// Writes the value to the I/O interface based on the specified flags and value.
27   ///
28   /// # Arguments
29   ///
30   /// * `flags` - The flags to modify.
31   /// * `value` - The value indicating whether to set (`true`) or clear (`false`) the flags.
32   #[inline(always)]
33   fn write_flags(&mut self, flags: Self::Value, value: bool) {
34      let tmp: Self::Value = match value {
35         true => self.read() | flags,
36         false => self.read() & !flags,
37      };
38
39      self.write(tmp);
40   }
41}
42
43/// Wrapper around an I/O interface providing read-only access.
44pub struct ReadOnly<I>(pub I);
45
46impl<I> ReadOnly<I> {
47   /// Creates a new `ReadOnly` wrapper instance.
48   ///
49   /// # Arguments
50   ///
51   /// * `inner` - The inner I/O interface.
52   pub const fn new(inner: I) -> Self {
53      return ReadOnly(inner);
54   }
55}
56
57impl<I: HardwareIo> ReadOnly<I> {
58   /// Reads the value from the interface.
59   #[inline(always)]
60   pub fn read(&self) -> I::Value {
61      self.0.read()
62   }
63
64   pub fn read_flags(&self, flags: I::Value) -> bool {
65      self.0.read_flags(flags)
66   }
67}
68
69/// Wrapper around an I/O interface providing write-only access.
70pub struct WriteOnly<I>(pub I);
71
72impl<I> WriteOnly<I> {
73   /// Writes the value to the I/O interface.
74   ///
75   /// # Arguments
76   ///
77   /// * `value` - The value to write.
78   pub const fn new(inner: I) -> Self {
79      return WriteOnly(inner);
80   }
81}
82
83impl<I: HardwareIo> WriteOnly<I> {
84   /// Writes the value to the I/O interface.
85   ///
86   /// # Arguments
87   ///
88   /// * `value` - The value to write.
89   #[inline(always)]
90   pub fn write(&mut self, value: I::Value) {
91      self.0.write(value);
92   }
93
94   /// Writes the value to the I/O interface based on the specified flags and value.
95   ///
96   /// # Arguments
97   ///
98   /// * `flags` - The flags to modify.
99   /// * `value` - The value indicating whether to set (`true`) or clear (`false`) the flags.
100   #[inline(always)]
101   pub fn write_flags(&mut self, flags: I::Value, value: bool) {
102      self.0.write_flags(flags, value);
103   }
104}
105
106// IMPORTS //
107
108use core::{
109   cmp::PartialEq,
110   ops::{BitAnd, BitOr, Not},
111};