intel_spi/
io.rs

1// SPDX-License-Identifier: MIT
2
3#![allow(dead_code)]
4
5use core::cmp::PartialEq;
6use core::ops::{BitAnd, BitOr, Not};
7
8pub trait Io {
9    type Value: Copy + PartialEq + BitAnd<Output = Self::Value> + BitOr<Output = Self::Value> + Not<Output = Self::Value>;
10
11    fn read(&self) -> Self::Value;
12    fn write(&mut self, value: Self::Value);
13
14    #[inline(always)]
15    fn readf(&self, flags: Self::Value) -> bool  {
16        (self.read() & flags) as Self::Value == flags
17    }
18
19    #[inline(always)]
20    fn writef(&mut self, flags: Self::Value, value: bool) {
21        let tmp: Self::Value = match value {
22            true => self.read() | flags,
23            false => self.read() & !flags,
24        };
25        self.write(tmp);
26    }
27}
28
29pub struct ReadOnly<I: Io> {
30    inner: I
31}
32
33impl<I: Io> ReadOnly<I> {
34    pub /* const */ fn new(inner: I) -> Self {
35        Self { inner }
36    }
37
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
49pub struct WriteOnly<I: Io> {
50    inner: I
51}
52
53impl<I: Io> WriteOnly<I> {
54    pub /* const */ fn new(inner: I) -> Self {
55        Self { inner }
56    }
57
58    #[inline(always)]
59    pub fn write(&mut self, value: I::Value) {
60        self.inner.write(value)
61    }
62
63    #[inline(always)]
64    pub fn writef(&mut self, flags: I::Value, value: bool) {
65        self.inner.writef(flags, value)
66    }
67}