1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use super::*;
use core::ptr::{read_volatile, write_volatile};

/// Memory-mapped register token. Types which implement this trait should be
/// zero-sized. This is a zero-cost abstraction for safely working with
/// memory-mapped registers.
pub trait Reg<T: RegTag>: Sized {
  /// Type that wraps a raw register value.
  type Val: RegVal;

  /// Memory address of the register.
  const ADDRESS: usize;
}

/// Referenceable register.
pub trait RegRef<'a, T: RegTag>: Reg<T> {
  /// Type that wraps a raw register value and a register reference.
  type Hold: RegHold<'a, T, Self>;

  /// Creates a new `Hold` for `val`.
  #[inline(always)]
  fn hold(&'a self, val: Self::Val) -> Self::Hold {
    unsafe { Self::Hold::new(self, val) }
  }

  /// Creates a new `Hold` with reset value.
  #[inline(always)]
  fn default(&'a self) -> Self::Hold {
    self.hold(self.default_val())
  }

  /// Returns a default value.
  #[inline(always)]
  fn default_val(&self) -> Self::Val {
    unsafe { Self::Val::default() }
  }
}

/// Register that can read its value.
pub trait RReg<T: RegTag>: Reg<T> {
  /// Reads and wraps a register value from its memory address.
  #[cfg_attr(feature = "clippy", allow(needless_lifetimes))]
  #[inline(always)]
  fn load<'a>(&'a self) -> <Self as RegRef<'a, T>>::Hold
  where
    Self: RegRef<'a, T>,
  {
    self.hold(self.load_val())
  }

  /// Reads a register value from its memory address.
  #[inline(always)]
  fn load_val(&self) -> Self::Val {
    unsafe { Self::Val::from_raw(read_volatile(self.to_ptr())) }
  }

  /// Returns an unsafe constant pointer to the register's memory address.
  #[inline(always)]
  fn to_ptr(&self) -> *const <Self::Val as RegVal>::Raw {
    Self::ADDRESS as *const <Self::Val as RegVal>::Raw
  }
}

/// Register that can write its value.
pub trait WReg<T: RegTag>: Reg<T> {
  /// Returns an unsafe mutable pointer to the register's memory address.
  #[inline(always)]
  fn to_mut_ptr(&self) -> *mut <Self::Val as RegVal>::Raw {
    Self::ADDRESS as *mut <Self::Val as RegVal>::Raw
  }
}

/// Read-only register.
pub trait RoReg<T: RegTag>: RReg<T> {}

/// Write-only register.
pub trait WoReg<T: RegTag>: WReg<T> {}

/// Register that can write its value in a multi-threaded context.
// FIXME https://github.com/rust-lang/rust/issues/46397
pub trait WRegShared<'a, T: RegShared>: WReg<T> + RegRef<'a, T> {
  /// Updates a new reset value with `f` and writes the result to the register's
  /// memory address.
  fn reset<F>(&'a self, f: F)
  where
    F: for<'b> FnOnce(&'b mut <Self as RegRef<'a, T>>::Hold)
      -> &'b mut <Self as RegRef<'a, T>>::Hold;

  /// Writes `val` into the register.
  fn store_val(&self, val: Self::Val);

  /// Writes the reset value to the register.
  fn store_default(&'a self);
}

/// Register that can write its value in a single-threaded context.
// FIXME https://github.com/rust-lang/rust/issues/46397
pub trait WRegUnique<'a>: WReg<Utt> + RegRef<'a, Utt> {
  /// Updates a new reset value with `f` and writes the result to the register's
  /// memory address.
  fn reset<F>(&'a mut self, f: F)
  where
    F: for<'b> FnOnce(&'b mut <Self as RegRef<'a, Utt>>::Hold)
      -> &'b mut <Self as RegRef<'a, Utt>>::Hold;

  /// Writes `val` into the register.
  fn store_val(&mut self, val: Self::Val);

  /// Writes the reset value to the register.
  fn store_default(&'a mut self);
}

/// Register that can read and write its value in a single-threaded context.
// FIXME https://github.com/rust-lang/rust/issues/46397
pub trait RwRegUnique<'a>: RReg<Utt> + WRegUnique<'a> + RegRef<'a, Utt> {
  /// Atomically updates the register's value.
  fn modify<F>(&'a mut self, f: F)
  where
    F: for<'b> FnOnce(&'b mut <Self as RegRef<'a, Utt>>::Hold)
      -> &'b mut <Self as RegRef<'a, Utt>>::Hold;
}

impl<'a, T, U> WRegShared<'a, T> for U
where
  T: RegShared,
  U: WReg<T> + RegRef<'a, T>,
  // Extra bound to make the dot operator checking `WRegUnique` first.
  U::Val: RegVal,
{
  #[inline(always)]
  fn reset<F>(&'a self, f: F)
  where
    F: for<'b> FnOnce(&'b mut <U as RegRef<'a, T>>::Hold)
      -> &'b mut <U as RegRef<'a, T>>::Hold,
  {
    self.store_val(f(&mut self.default()).val());
  }

  #[inline(always)]
  fn store_val(&self, val: U::Val) {
    unsafe { write_volatile(self.to_mut_ptr(), val.raw()) };
  }

  #[inline(always)]
  fn store_default(&'a self) {
    self.store_val(self.default_val());
  }
}

impl<'a, T> WRegUnique<'a> for T
where
  T: WReg<Utt> + RegRef<'a, Utt>,
{
  #[inline(always)]
  fn reset<F>(&'a mut self, f: F)
  where
    F: for<'b> FnOnce(&'b mut <T as RegRef<'a, Utt>>::Hold)
      -> &'b mut <T as RegRef<'a, Utt>>::Hold,
  {
    unsafe {
      write_volatile(self.to_mut_ptr(), f(&mut self.default()).val().raw());
    }
  }

  #[inline(always)]
  fn store_val(&mut self, val: T::Val) {
    unsafe { write_volatile(self.to_mut_ptr(), val.raw()) };
  }

  #[inline(always)]
  fn store_default(&'a mut self) {
    unsafe { write_volatile(self.to_mut_ptr(), self.default_val().raw()) };
  }
}

impl<'a, T> RwRegUnique<'a> for T
where
  T: RReg<Utt> + WRegUnique<'a> + RegRef<'a, Utt>,
{
  #[inline(always)]
  fn modify<F>(&'a mut self, f: F)
  where
    F: for<'b> FnOnce(&'b mut <T as RegRef<'a, Utt>>::Hold)
      -> &'b mut <T as RegRef<'a, Utt>>::Hold,
  {
    unsafe {
      write_volatile(self.to_mut_ptr(), f(&mut self.load()).val().raw());
    }
  }
}