]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
1 | use core::cmp::PartialEq; |
2 | use core::ops::{BitAnd, BitOr, Not}; | |
3 | ||
4 | pub trait Io { | |
5 | type Value: Copy + PartialEq + BitAnd<Output = Self::Value> + BitOr<Output = Self::Value> + Not<Output = Self::Value>; | |
6 | ||
7 | fn read(&self) -> Self::Value; | |
8 | fn write(&mut self, value: Self::Value); | |
9 | ||
10 | #[inline(always)] | |
11 | fn readf(&self, flags: Self::Value) -> bool { | |
12 | (self.read() & flags) as Self::Value == flags | |
13 | } | |
14 | ||
15 | #[inline(always)] | |
16 | fn writef(&mut self, flags: Self::Value, value: bool) { | |
17 | let tmp: Self::Value = match value { | |
18 | true => self.read() | flags, | |
19 | false => self.read() & !flags, | |
20 | }; | |
21 | self.write(tmp); | |
22 | } | |
23 | } | |
24 | ||
136023e0 | 25 | pub struct ReadOnly<I> { |
ea8adc8c XL |
26 | inner: I |
27 | } | |
28 | ||
136023e0 | 29 | impl<I> ReadOnly<I> { |
ea8adc8c XL |
30 | pub const fn new(inner: I) -> ReadOnly<I> { |
31 | ReadOnly { | |
32 | inner: inner | |
33 | } | |
34 | } | |
136023e0 | 35 | } |
ea8adc8c | 36 | |
136023e0 | 37 | impl<I: Io> ReadOnly<I> { |
ea8adc8c XL |
38 | #[inline(always)] |
39 | pub fn read(&self) -> I::Value { | |
40 | self.inner.read() | |
41 | } | |
42 | ||
43 | #[inline(always)] | |
44 | pub fn readf(&self, flags: I::Value) -> bool { | |
45 | self.inner.readf(flags) | |
46 | } | |
47 | } | |
48 | ||
136023e0 | 49 | pub struct WriteOnly<I> { |
ea8adc8c XL |
50 | inner: I |
51 | } | |
52 | ||
136023e0 | 53 | impl<I> WriteOnly<I> { |
ea8adc8c XL |
54 | pub const fn new(inner: I) -> WriteOnly<I> { |
55 | WriteOnly { | |
56 | inner: inner | |
57 | } | |
58 | } | |
136023e0 | 59 | } |
ea8adc8c | 60 | |
136023e0 | 61 | impl<I: Io> WriteOnly<I> { |
ea8adc8c XL |
62 | #[inline(always)] |
63 | pub fn write(&mut self, value: I::Value) { | |
64 | self.inner.write(value) | |
65 | } | |
66 | ||
67 | #[inline(always)] | |
68 | pub fn writef(&mut self, flags: I::Value, value: bool) { | |
69 | self.inner.writef(flags, value) | |
70 | } | |
71 | } |