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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Memory-mapped register fields module.
//!
//! See [the top-level module documentation](self) for details.

use crate::{
    bitfield::{Bitfield, Bits},
    reg::{
        tag::{Crt, RegAtomic, RegTag, Srt, Urt},
        RReg, Reg, WReg, WoReg,
    },
    token::Token,
};
use core::ptr::{read_volatile, write_volatile};

/// The base trait for a field token of a memory-mapped register.
pub trait RegField<T: RegTag>: Token + Sync {
    /// Parent register token.
    type Reg: Reg<T>;

    /// Corresponding unsynchronized register field token.
    type URegField: RegField<Urt>;

    /// Corresponding synchronized register field token.
    type SRegField: RegField<Srt>;

    /// Corresponding copyable register field token.
    type CRegField: RegField<Crt>;

    /// The offset of the field inside the parent register.
    const OFFSET: usize;

    /// The bit-width of the field.
    const WIDTH: usize;

    /// Converts into unsynchronized register field token.
    #[inline]
    fn into_unsync(self) -> Self
    where
        Self: RegField<Urt>,
    {
        self
    }

    /// Converts into synchronized register field token.
    #[inline]
    fn into_sync(self) -> Self
    where
        Self: RegField<Srt>,
    {
        self
    }

    /// Converts into copyable register field token.
    #[inline]
    fn into_copy(self) -> Self::CRegField
    where
        T: RegAtomic,
    {
        unsafe { Self::CRegField::take() }
    }

    /// Returns a reference to the synchronized field token.
    #[inline]
    fn as_sync(&self) -> &Self::SRegField
    where
        T: RegAtomic,
    {
        unsafe { &*(self as *const Self as *const Self::SRegField) }
    }
}

/// Single-bit register field.
pub trait RegFieldBit<T: RegTag>: RegField<T> {}

/// Multiple-bits register field.
pub trait RegFieldBits<T: RegTag>: RegField<T> {}

/// Readable field of readable register.
pub trait RRRegField<T: RegTag>
where
    Self: RegField<T>,
    Self::Reg: RReg<T>,
{
    /// Reads the value from the register memory to the opaque value type.
    #[inline]
    fn load_val(&self) -> <Self::Reg as Reg<T>>::Val {
        unsafe {
            Self::Reg::val_from(read_volatile(
                Self::Reg::ADDRESS as *const <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits,
            ))
        }
    }
}

/// Writable field of writable register.
pub trait WWRegField<T: RegTag>
where
    Self: RegField<T>,
    Self::Reg: WReg<T>,
{
}

/// Read-only field of readable register.
pub trait RoRRegField<T: RegTag>
where
    Self: RRRegField<T>,
    Self::Reg: RReg<T>,
{
}

/// Write-only field of writable register.
pub trait WoWRegField<T: RegTag>
where
    Self: WWRegField<T>,
    Self::Reg: WReg<T>,
{
}

/// Write-only field of write-only register.
pub trait WoWoRegField<T: RegTag>
where
    Self: WoWRegField<T>,
    Self::Reg: WoReg<T>,
{
    /// Creates a new opaque register value, and initializes it with the reset
    /// value.
    fn default_val(&self) -> <Self::Reg as Reg<T>>::Val;

    /// Writes an opaque value `val` into the register memory.
    ///
    /// See also [`store`](WoWoRegField::store).
    fn store_val(&self, val: <Self::Reg as Reg<T>>::Val);

    /// Passes the opaque reset value to the closure `f`, then writes the result
    /// of the closure into the register memory.
    ///
    /// See also [`store_val`](WoWoRegField::store_val).
    fn store<F>(&self, f: F)
    where
        F: Fn(&mut <Self::Reg as Reg<T>>::Val);
}

/// Readable single-bit field of readable register.
pub trait RRRegFieldBit<T: RegTag>
where
    Self: RegFieldBit<T> + RRRegField<T>,
    Self::Reg: RReg<T>,
{
    /// Returns `true` if the bit is set in `val`.
    fn read(&self, val: &<Self::Reg as Reg<T>>::Val) -> bool;

    /// Reads the value from the register memory and returns `true` if the bit
    /// is set.
    fn read_bit(&self) -> bool;
}

/// Writable single-bit field of writable register.
pub trait WWRegFieldBit<T: RegTag>
where
    Self: RegFieldBit<T> + WWRegField<T>,
    Self::Reg: WReg<T>,
{
    /// Sets the bit in `val`.
    fn set(&self, val: &mut <Self::Reg as Reg<T>>::Val);

    /// Clears the bit in `val`.
    fn clear(&self, val: &mut <Self::Reg as Reg<T>>::Val);

    /// Toggles the bit in `val`.
    fn toggle(&self, val: &mut <Self::Reg as Reg<T>>::Val);
}

/// Write-only single-bit field of write-only register.
pub trait WoWoRegFieldBit<T: RegTag>
where
    Self: RegFieldBit<T> + WoWRegField<T>,
    Self::Reg: WoReg<T>,
{
    /// Writes the reset value with the bit set into the register memory.
    fn set_bit(&self);

    /// Writes the reset value with the bit cleared into the register memory.
    fn clear_bit(&self);

    /// Writes the reset value with the bit toggled into the register memory.
    fn toggle_bit(&self);
}

/// Readable multiple-bit field of readable register.
pub trait RRRegFieldBits<T: RegTag>
where
    Self: RegFieldBits<T> + RRRegField<T>,
    Self::Reg: RReg<T>,
{
    /// Extracts the field bits from `val`.
    fn read(
        &self,
        val: &<Self::Reg as Reg<T>>::Val,
    ) -> <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits;

    /// Reads the value from the register memory and extracts the field bits.
    fn read_bits(&self) -> <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits;
}

/// Writable multiple-bit field of writable register.
pub trait WWRegFieldBits<T: RegTag>
where
    Self: RegFieldBits<T> + WWRegField<T>,
    Self::Reg: WReg<T>,
{
    /// Replaces the field bits in `val` by `bits`.
    fn write(
        &self,
        val: &mut <Self::Reg as Reg<T>>::Val,
        bits: <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits,
    );
}

/// Write-only multiple-bit field of write-only register.
pub trait WoWoRegFieldBits<T: RegTag>
where
    Self: RegFieldBits<T> + WoWRegField<T>,
    Self::Reg: WoReg<T>,
{
    /// Writes the reset value with the field bits replaced by `bits` into the
    /// register memory.
    fn write_bits(&self, bits: <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits);
}

impl<T, R> WoWoRegField<T> for R
where
    T: RegTag,
    R: WoWRegField<T>,
    R::Reg: WoReg<T>,
{
    #[inline]
    fn default_val(&self) -> <Self::Reg as Reg<T>>::Val {
        unsafe { Self::Reg::val_from(<Self::Reg as Reg<T>>::RESET) }
    }

    #[inline]
    fn store_val(&self, val: <Self::Reg as Reg<T>>::Val) {
        unsafe {
            write_volatile(
                Self::Reg::ADDRESS as *mut <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits,
                val.bits(),
            );
        }
    }

    #[inline]
    fn store<F>(&self, f: F)
    where
        F: Fn(&mut <Self::Reg as Reg<T>>::Val),
    {
        let mut val = self.default_val();
        f(&mut val);
        self.store_val(val);
    }
}

impl<T, R> RRRegFieldBit<T> for R
where
    T: RegTag,
    R: RegFieldBit<T> + RRRegField<T>,
    R::Reg: RReg<T>,
{
    #[inline]
    fn read(&self, val: &<Self::Reg as Reg<T>>::Val) -> bool {
        unsafe {
            val.read_bit(<<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(
                Self::OFFSET,
            ))
        }
    }

    #[inline]
    fn read_bit(&self) -> bool {
        self.read(&self.load_val())
    }
}

impl<T, R> WWRegFieldBit<T> for R
where
    T: RegTag,
    R: RegFieldBit<T> + WWRegField<T>,
    R::Reg: WReg<T>,
{
    #[inline]
    fn set(&self, val: &mut <Self::Reg as Reg<T>>::Val) {
        unsafe {
            val.set_bit(<<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(
                Self::OFFSET,
            ));
        }
    }

    #[inline]
    fn clear(&self, val: &mut <Self::Reg as Reg<T>>::Val) {
        unsafe {
            val.clear_bit(<<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(
                Self::OFFSET,
            ));
        }
    }

    #[inline]
    fn toggle(&self, val: &mut <Self::Reg as Reg<T>>::Val) {
        unsafe {
            val.toggle_bit(<<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(
                Self::OFFSET,
            ));
        }
    }
}

impl<T, R> WoWoRegFieldBit<T> for R
where
    T: RegTag,
    R: RegFieldBit<T> + WoWRegField<T>,
    R::Reg: WoReg<T>,
{
    #[inline]
    fn set_bit(&self) {
        self.store(|val| {
            self.set(val);
        });
    }

    #[inline]
    fn clear_bit(&self) {
        self.store(|val| {
            self.clear(val);
        });
    }

    #[inline]
    fn toggle_bit(&self) {
        self.store(|val| {
            self.toggle(val);
        });
    }
}

impl<T, R> RRRegFieldBits<T> for R
where
    T: RegTag,
    R: RegFieldBits<T> + RRRegField<T>,
    R::Reg: RReg<T>,
{
    #[inline]
    fn read(
        &self,
        val: &<Self::Reg as Reg<T>>::Val,
    ) -> <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits {
        unsafe {
            val.read_bits(
                <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(Self::OFFSET),
                <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(Self::WIDTH),
            )
        }
    }

    #[inline]
    fn read_bits(&self) -> <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits {
        self.read(&self.load_val())
    }
}

impl<T, R> WWRegFieldBits<T> for R
where
    T: RegTag,
    R: RegFieldBits<T> + WWRegField<T>,
    R::Reg: WReg<T>,
{
    #[inline]
    fn write(
        &self,
        val: &mut <Self::Reg as Reg<T>>::Val,
        bits: <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits,
    ) {
        unsafe {
            val.write_bits(
                <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(Self::OFFSET),
                <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits::from_usize(Self::WIDTH),
                bits,
            );
        }
    }
}

impl<T, R> WoWoRegFieldBits<T> for R
where
    T: RegTag,
    R: RegFieldBits<T> + WoWRegField<T>,
    R::Reg: WoReg<T>,
{
    #[inline]
    fn write_bits(&self, bits: <<Self::Reg as Reg<T>>::Val as Bitfield>::Bits) {
        self.store(|val| {
            self.write(val, bits);
        });
    }
}