-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathgeneric_bb.rs
64 lines (60 loc) · 1.77 KB
/
generic_bb.rs
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
impl<REG: Writable> Reg<REG> {
/// Writes bit with "bit-banding".
#[inline(always)]
pub fn bb_write<F, FI>(&self, f: F, set: bool)
where
F: FnOnce(&mut W<REG>) -> BitWriter<REG, FI>,
bool: From<FI>,
{
self.bbwrite(f, set)
}
#[inline(always)]
fn bbwrite<F, FI, B>(&self, f: F, set: bool)
where
F: FnOnce(&mut W<REG>) -> raw::BitWriter<REG, FI, B>,
bool: From<FI>,
{
// Start address of the peripheral memory region capable of being addressed by bit-banding
const PERI_ADDRESS_START: usize = 0x4000_0000;
const PERI_BIT_BAND_BASE: usize = 0x4200_0000;
let addr = self.as_ptr() as usize;
let bit = f(&mut W {
bits: REG::Ux::default(),
_reg: marker::PhantomData,
})
.o as usize;
let bit = bit as usize;
let bb_addr = (PERI_BIT_BAND_BASE + (addr - PERI_ADDRESS_START) * 32) + 4 * bit;
unsafe { core::ptr::write_volatile(bb_addr as *mut u32, u32::from(set)) };
}
/// Sets bit with "bit-banding".
#[inline(always)]
pub fn bb_set<F, FI, B>(&self, f: F)
where
F: FnOnce(&mut W<REG>) -> raw::BitWriter<REG, FI, B>,
bool: From<FI>,
B: BBSet,
{
self.bbwrite(f, true);
}
/// Clears bit with "bit-banding".
#[inline(always)]
pub fn bb_clear<F, FI, B>(&self, f: F)
where
F: FnOnce(&mut W<REG>) -> raw::BitWriter<REG, FI, B>,
bool: From<FI>,
B: BBClear,
{
self.bbwrite(f, false);
}
}
pub trait BBSet {}
pub trait BBClear {}
impl BBSet for BitM {}
impl BBSet for Bit1S {}
impl BBSet for Bit1C {}
impl BBSet for Bit1T {}
impl BBClear for BitM {}
impl BBClear for Bit0S {}
impl BBClear for Bit0C {}
impl BBClear for Bit0T {}