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
use crate::reg::{
    field::{RRRegFieldBit, WWRegFieldBit, WoWoRegFieldBit},
    tag::{RegTag, Urt},
    RReg, Reg, WReg, WoReg,
};
use core::ptr::{read_volatile, write_volatile};

/// The peripheral bit-band alias start.
pub const BIT_BAND_BASE: usize = 0x4200_0000;

/// The peripheral bit-band region width.
pub const BIT_BAND_WIDTH: usize = 5;

/// Register located in the peripheral bit-band region.
pub trait RegBitBand<T: RegTag>: Reg<T> {}

/// Readable single-bit field of readable register located in the peripheral
/// bit-band region.
#[allow(clippy::upper_case_acronyms)]
pub trait RRRegFieldBitBand<T: RegTag>
where
    Self: RRRegFieldBit<T>,
    Self::Reg: RegBitBand<T> + RReg<T>,
{
    /// Reads the value of this bit through the peripheral bit-band region
    /// alias.
    fn read_bit_band(&self) -> bool;

    /// Returns a raw pointer to the bit-band alias address of this field.
    ///
    /// See also [`WWRegFieldBitBand::to_bit_band_mut_ptr`].
    fn to_bit_band_ptr(&self) -> *const usize;
}

/// Writable single-bit field of writable register located in the peripheral
/// bit-band region.
#[allow(clippy::upper_case_acronyms)]
pub trait WWRegFieldBitBand<T: RegTag>
where
    Self: WWRegFieldBitBandMarker<T>,
    Self::Reg: RegBitBand<T> + WReg<T>,
{
    /// Sets this bit through the peripheral bit-band region alias.
    fn set_bit_band(&self);

    /// Clears this bit through the peripheral bit-band region alias.
    fn clear_bit_band(&self);

    /// Returns a mutable raw pointer to the bit-band alias address of this
    /// field.
    ///
    /// See also [`RRRegFieldBitBand::to_bit_band_ptr`].
    fn to_bit_band_mut_ptr(&self) -> *mut usize;
}

#[allow(clippy::upper_case_acronyms)]
#[marker]
pub trait WWRegFieldBitBandMarker<T: RegTag>
where
    Self: WWRegFieldBit<T>,
    Self::Reg: RegBitBand<T> + WReg<T>,
{
}

impl<T, R> RRRegFieldBitBand<T> for R
where
    T: RegTag,
    R: RRRegFieldBit<T>,
    R::Reg: RegBitBand<T> + RReg<T>,
{
    #[inline]
    fn read_bit_band(&self) -> bool {
        unsafe { read_volatile(self.to_bit_band_ptr()) != 0 }
    }

    #[inline]
    fn to_bit_band_ptr(&self) -> *const usize {
        bit_band_addr::<T, Self::Reg>(Self::OFFSET) as *const usize
    }
}

impl<T, R> WWRegFieldBitBand<T> for R
where
    T: RegTag,
    R: WWRegFieldBitBandMarker<T>,
    R::Reg: RegBitBand<T> + WReg<T>,
{
    #[inline]
    fn set_bit_band(&self) {
        unsafe { write_volatile(self.to_bit_band_mut_ptr(), 1) };
    }

    #[inline]
    fn clear_bit_band(&self) {
        unsafe { write_volatile(self.to_bit_band_mut_ptr(), 0) };
    }

    #[inline]
    fn to_bit_band_mut_ptr(&self) -> *mut usize {
        bit_band_addr::<T, Self::Reg>(Self::OFFSET) as *mut usize
    }
}

impl<T, R> WWRegFieldBitBandMarker<T> for R
where
    T: RegTag,
    R: WoWoRegFieldBit<T>,
    R::Reg: RegBitBand<T> + WoReg<T>,
{
}

impl<R> WWRegFieldBitBandMarker<Urt> for R
where
    R: WWRegFieldBit<Urt>,
    R::Reg: RegBitBand<Urt> + WReg<Urt>,
{
}

fn bit_band_addr<T: RegTag, R: RegBitBand<T>>(offset: usize) -> usize {
    BIT_BAND_BASE
        + (((R::ADDRESS + (offset >> 3)) & ((0b1 << (BIT_BAND_WIDTH << 2)) - 1)) << BIT_BAND_WIDTH)
        + ((offset & (8 - 1)) << 2)
}

#[cfg(test)]
mod tests {
    use super::*;
    use drone_core::reg;

    reg! {
        #[allow(dead_code)]
        R LOW => {
            address => 0x4000_0000; size => 0x20; reset => 0x0000_0000; traits => { RegBitBand };
            fields => { TEST_BIT => { offset => 0; width => 1 } };
        };
    }

    reg! {
        #[allow(dead_code)]
        R HIGH => {
            address => 0x400F_FFFC; size => 0x20; reset => 0x0000_0000; traits => { RegBitBand };
            fields => { TEST_BIT => { offset => 0; width => 1 } };
        };
    }

    #[test]
    fn reg_bit_band_addr() {
        assert_eq!(bit_band_addr::<Urt, r_low::Reg<Urt>>(0), 0x4200_0000);
        assert_eq!(bit_band_addr::<Urt, r_low::Reg<Urt>>(7), 0x4200_001C);
        assert_eq!(bit_band_addr::<Urt, r_low::Reg<Urt>>(31), 0x4200_007C);
        assert_eq!(bit_band_addr::<Urt, r_high::Reg<Urt>>(24), 0x43FF_FFE0);
        assert_eq!(bit_band_addr::<Urt, r_high::Reg<Urt>>(31), 0x43FF_FFFC);
    }
}