Skip to content

Commit 734d1d2

Browse files
committed
bit-banding stm32
1 parent c94dc77 commit 734d1d2

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct Config {
1717
pub target: Target,
1818
pub atomics: bool,
1919
pub atomics_feature: Option<String>,
20+
pub bit_banding: bool,
2021
pub generic_mod: bool,
2122
pub make_mod: bool,
2223
pub skip_crate_attributes: bool,

src/generate/device.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
143143
let generic_file = include_str!("generic.rs");
144144
let generic_reg_file = include_str!("generic_reg_vcell.rs");
145145
let generic_atomic_file = include_str!("generic_atomic.rs");
146+
let generic_bb_file = include_str!("generic_bb.rs");
146147
if config.generic_mod {
147148
let mut file = File::create(
148149
config
@@ -159,6 +160,9 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
159160
}
160161
writeln!(file, "\n{generic_atomic_file}")?;
161162
}
163+
if config.bit_banding {
164+
writeln!(file, "\n{generic_bb_file}")?;
165+
}
162166

163167
if !config.make_mod {
164168
out.extend(quote! {
@@ -177,6 +181,9 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
177181
}
178182
syn::parse_file(generic_atomic_file)?.to_tokens(&mut tokens);
179183
}
184+
if config.bit_banding {
185+
syn::parse_file(generic_bb_file)?.to_tokens(&mut tokens);
186+
}
180187

181188
out.extend(quote! {
182189
#[allow(unused_imports)]

src/generate/generic_bb.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
impl<REG: Writable> Reg<REG> {
2+
/// Writes bit with "bit-banding".
3+
#[inline(always)]
4+
pub fn bb_write<F, FI>(&self, f: F, set: bool)
5+
where
6+
F: FnOnce(&mut W<REG>) -> BitWriter<REG, FI>,
7+
bool: From<FI>,
8+
{
9+
self.bbwrite(f, set)
10+
}
11+
#[inline(always)]
12+
fn bbwrite<F, FI, B>(&self, f: F, set: bool)
13+
where
14+
F: FnOnce(&mut W<REG>) -> raw::BitWriter<REG, FI, B>,
15+
bool: From<FI>,
16+
{
17+
// Start address of the peripheral memory region capable of being addressed by bit-banding
18+
const PERI_ADDRESS_START: usize = 0x4000_0000;
19+
const PERI_BIT_BAND_BASE: usize = 0x4200_0000;
20+
21+
let addr = self.as_ptr() as usize;
22+
let bit = f(&mut W {
23+
bits: REG::Ux::default(),
24+
_reg: marker::PhantomData,
25+
})
26+
.o as usize;
27+
let bit = bit as usize;
28+
let bb_addr = (PERI_BIT_BAND_BASE + (addr - PERI_ADDRESS_START) * 32) + 4 * bit;
29+
unsafe { core::ptr::write_volatile(bb_addr as *mut u32, u32::from(set)) };
30+
}
31+
32+
/// Sets bit with "bit-banding".
33+
#[inline(always)]
34+
pub fn bb_set<F, FI, B>(&self, f: F)
35+
where
36+
F: FnOnce(&mut W<REG>) -> raw::BitWriter<REG, FI, B>,
37+
bool: From<FI>,
38+
B: BBSet,
39+
{
40+
self.bbwrite(f, true);
41+
}
42+
43+
/// Clears bit with "bit-banding".
44+
#[inline(always)]
45+
pub fn bb_clear<F, FI, B>(&self, f: F)
46+
where
47+
F: FnOnce(&mut W<REG>) -> raw::BitWriter<REG, FI, B>,
48+
bool: From<FI>,
49+
B: BBClear,
50+
{
51+
self.bbwrite(f, false);
52+
}
53+
}
54+
55+
pub trait BBSet {}
56+
pub trait BBClear {}
57+
impl BBSet for BitM {}
58+
impl BBSet for Bit1S {}
59+
impl BBSet for Bit1C {}
60+
impl BBSet for Bit1T {}
61+
impl BBClear for BitM {}
62+
impl BBClear for Bit0S {}
63+
impl BBClear for Bit0C {}
64+
impl BBClear for Bit0T {}

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ fn run() -> Result<()> {
128128
.action(ArgAction::Set)
129129
.value_name("FEATURE"),
130130
)
131+
.arg(
132+
Arg::new("bit_banding")
133+
.long("bit-banding")
134+
.alias("bit_banding")
135+
.action(ArgAction::SetTrue)
136+
.help("Generate bit-banding register modification API"),
137+
)
131138
.arg(
132139
Arg::new("ignore_groups")
133140
.long("ignore-groups")

0 commit comments

Comments
 (0)