Files
drone_core
drone_cortexm
drone_ctypes
drone_nrf_map
drone_nrf_map_periph_uarte
drone_nrf_map_pieces
drone_nrf_map_pieces_1
drone_nrf_map_pieces_10
drone_nrf_map_pieces_11
drone_nrf_map_pieces_12
drone_nrf_map_pieces_2
drone_nrf_map_pieces_3
drone_nrf_map_pieces_4
drone_nrf_map_pieces_5
drone_nrf_map_pieces_6
drone_nrf_map_pieces_7
drone_nrf_map_pieces_8
drone_nrf_map_pieces_9
futures
futures_channel
futures_core
futures_io
futures_sink
futures_task
futures_util
pin_project_lite
pin_utils
proc_macro_nested
typenum
  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);
    }
}