Skip to content

Commit

Permalink
bit-banding stm32
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Jan 19, 2025
1 parent c94dc77 commit 734d1d2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Config {
pub target: Target,
pub atomics: bool,
pub atomics_feature: Option<String>,
pub bit_banding: bool,
pub generic_mod: bool,
pub make_mod: bool,
pub skip_crate_attributes: bool,
Expand Down
7 changes: 7 additions & 0 deletions src/generate/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
let generic_file = include_str!("generic.rs");
let generic_reg_file = include_str!("generic_reg_vcell.rs");
let generic_atomic_file = include_str!("generic_atomic.rs");
let generic_bb_file = include_str!("generic_bb.rs");
if config.generic_mod {
let mut file = File::create(
config
Expand All @@ -159,6 +160,9 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result<Toke
}
writeln!(file, "\n{generic_atomic_file}")?;
}
if config.bit_banding {
writeln!(file, "\n{generic_bb_file}")?;
}

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

out.extend(quote! {
#[allow(unused_imports)]
Expand Down
64 changes: 64 additions & 0 deletions src/generate/generic_bb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 {}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ fn run() -> Result<()> {
.action(ArgAction::Set)
.value_name("FEATURE"),
)
.arg(
Arg::new("bit_banding")
.long("bit-banding")
.alias("bit_banding")
.action(ArgAction::SetTrue)
.help("Generate bit-banding register modification API"),
)
.arg(
Arg::new("ignore_groups")
.long("ignore-groups")
Expand Down

0 comments on commit 734d1d2

Please sign in to comment.