diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eb79d85..56bf5aef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/). ## [Unreleased] +- Generic `Periph` - Add `mtvec_align` field to `riscv_config` to configure the byte alignment of interrupt vector table. - Fix reexport path when "%s" inside "derivedFrom" - Force using rust edition 2021 in CI diff --git a/src/generate/device.rs b/src/generate/device.rs index d2b3ee9c..16e2f195 100644 --- a/src/generate/device.rs +++ b/src/generate/device.rs @@ -62,11 +62,6 @@ pub fn render(d: &Device, config: &Config, device_x: &mut String) -> Result { + _marker: marker::PhantomData, +} + +unsafe impl Send for Periph {} + +impl Periph { + ///Pointer to the register block + pub const PTR: *const RB = A as *const _; + + ///Return the pointer to the register block + #[inline(always)] + pub const fn ptr() -> *const RB { + Self::PTR + } + + /// Steal an instance of this peripheral + /// + /// # Safety + /// + /// Ensure that the new instance of the peripheral cannot be used in a way + /// that may race with any existing instances, for example by only + /// accessing read-only or write-only registers, or by consuming the + /// original peripheral and using critical sections to coordinate + /// access between multiple new instances. + /// + /// Additionally, other software such as HALs may rely on only one + /// peripheral instance existing to ensure memory safety; ensure + /// no stolen instances are passed to such software. + pub unsafe fn steal() -> Self { + Self { + _marker: marker::PhantomData, + } + } +} + +impl core::ops::Deref for Periph { + type Target = RB; + + #[inline(always)] + fn deref(&self) -> &Self::Target { + unsafe { &*Self::PTR } + } +} + /// Raw register type (`u8`, `u16`, `u32`, ...) pub trait RawReg: Copy @@ -247,7 +293,10 @@ impl W { self } } -impl W where REG: Writable { +impl W +where + REG: Writable, +{ /// Writes raw bits to the register. #[inline(always)] pub fn set(&mut self, bits: REG::Ux) -> &mut Self { @@ -335,7 +384,8 @@ pub struct RangeFrom; pub struct RangeTo; /// Write field Proxy -pub type FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe> = raw::FieldWriter<'a, REG, WI, FI, Safety>; +pub type FieldWriter<'a, REG, const WI: u8, FI = u8, Safety = Unsafe> = + raw::FieldWriter<'a, REG, WI, FI, Safety>; impl FieldWriter<'_, REG, WI, FI, Safety> where @@ -390,7 +440,8 @@ where } } -impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64> FieldWriter<'a, REG, WI, FI, Range> +impl<'a, REG, const WI: u8, FI, const MIN: u64, const MAX: u64> + FieldWriter<'a, REG, WI, FI, Range> where REG: Writable + RegisterSpec, FI: FieldSpec, @@ -478,7 +529,7 @@ macro_rules! bit_proxy { pub const fn width(&self) -> u8 { Self::WIDTH } - + /// Field offset #[inline(always)] pub const fn offset(&self) -> u8 { diff --git a/src/generate/peripheral.rs b/src/generate/peripheral.rs index 44f7594b..f5de3902 100644 --- a/src/generate/peripheral.rs +++ b/src/generate/peripheral.rs @@ -63,25 +63,6 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result feature_attribute.extend(quote! { #[cfg(feature = #feature_name)] }); }; - let steal_fn = quote! { - /// Steal an instance of this peripheral - /// - /// # Safety - /// - /// Ensure that the new instance of the peripheral cannot be used in a way - /// that may race with any existing instances, for example by only - /// accessing read-only or write-only registers, or by consuming the - /// original peripheral and using critical sections to coordinate - /// access between multiple new instances. - /// - /// Additionally, other software such as HALs may rely on only one - /// peripheral instance existing to ensure memory safety; ensure - /// no stolen instances are passed to such software. - pub unsafe fn steal() -> Self { - Self { _marker: PhantomData } - } - }; - let phtml = config.settings.html_url.as_ref().map(|url| { let doc = format!("See peripheral [structure]({url}#{})", &path.peripheral); quote!(#[doc = ""] #[doc = #doc]) @@ -98,34 +79,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result #phtml #doc_alias #feature_attribute - pub struct #p_ty { _marker: PhantomData<*const ()> } - - #feature_attribute - unsafe impl Send for #p_ty {} - - #feature_attribute - impl #p_ty { - ///Pointer to the register block - pub const PTR: *const #base::RegisterBlock = #address as *const _; - - ///Return the pointer to the register block - #[inline(always)] - pub const fn ptr() -> *const #base::RegisterBlock { - Self::PTR - } - - #steal_fn - } - - #feature_attribute - impl Deref for #p_ty { - type Target = #base::RegisterBlock; - - #[inline(always)] - fn deref(&self) -> &Self::Target { - unsafe { &*Self::PTR } - } - } + pub type #p_ty = crate::Periph<#base::RegisterBlock, #address>; #feature_attribute impl core::fmt::Debug for #p_ty {