Skip to content

Commit 9062ce0

Browse files
committed
per_to_tokens
1 parent 9e2b681 commit 9062ce0

File tree

1 file changed

+98
-148
lines changed

1 file changed

+98
-148
lines changed

src/generate/peripheral.rs

+98-148
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::fmt;
55
use svd_parser::expand::{
66
derive_cluster, derive_peripheral, derive_register, BlockPath, Index, RegisterPath,
77
};
8+
use syn::LitInt;
89

910
use crate::config::Config;
1011
use crate::svd::{
@@ -77,6 +78,89 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
7778
/// no stolen instances are passed to such software.
7879
};
7980

81+
let per_to_tokens = |feature_attribute: &TokenStream,
82+
description: &str,
83+
p_ty: &Ident,
84+
doc_alias: Option<TokenStream>,
85+
address: LitInt|
86+
-> TokenStream {
87+
let mut tokens = if config.raw_access {
88+
quote! {
89+
#[doc = #description]
90+
#doc_alias
91+
#feature_attribute
92+
pub struct #p_ty { rb: #base::RegisterBlock }
93+
94+
#feature_attribute
95+
unsafe impl Send for #p_ty {}
96+
97+
#feature_attribute
98+
impl #p_ty {
99+
#steal_docs
100+
pub unsafe fn steal() -> Self {
101+
Self { rb: #base::RegisterBlock::new(#address as *mut u8) }
102+
}
103+
}
104+
105+
#feature_attribute
106+
impl Deref for #p_ty {
107+
type Target = #base::RegisterBlock;
108+
109+
#[inline(always)]
110+
fn deref(&self) -> &Self::Target {
111+
&self.rb
112+
}
113+
}
114+
}
115+
} else {
116+
quote! {
117+
#[doc = #description]
118+
#doc_alias
119+
#feature_attribute
120+
pub struct #p_ty { _marker: PhantomData<*const ()> }
121+
122+
#feature_attribute
123+
unsafe impl Send for #p_ty {}
124+
125+
#feature_attribute
126+
impl #p_ty {
127+
///Pointer to the register block
128+
pub const PTR: *const #base::RegisterBlock = #address as *const _;
129+
130+
///Return the pointer to the register block
131+
#[inline(always)]
132+
pub const fn ptr() -> *const #base::RegisterBlock {
133+
Self::PTR
134+
}
135+
136+
#steal_docs
137+
pub unsafe fn steal() -> Self {
138+
Self { _marker: PhantomData }
139+
}
140+
}
141+
142+
#feature_attribute
143+
impl Deref for #p_ty {
144+
type Target = #base::RegisterBlock;
145+
146+
#[inline(always)]
147+
fn deref(&self) -> &Self::Target {
148+
unsafe { &*Self::PTR }
149+
}
150+
}
151+
}
152+
};
153+
tokens.extend(quote! {
154+
#feature_attribute
155+
impl core::fmt::Debug for #p_ty {
156+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
157+
f.debug_struct(#name_str).finish()
158+
}
159+
}
160+
});
161+
tokens
162+
};
163+
80164
match &p {
81165
Peripheral::Array(p, dim) => {
82166
let mut feature_names = Vec::with_capacity(dim.dim as _);
@@ -94,81 +178,13 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
94178
feature_attribute_n.extend(quote! { #[cfg(feature = #p_feature)] })
95179
};
96180
// Insert the peripherals structure
97-
out.extend(if config.raw_access {
98-
quote! {
99-
#[doc = #description]
100-
#doc_alias
101-
#feature_attribute_n
102-
pub struct #p_ty { rb: #base::RegisterBlock }
103-
104-
#feature_attribute_n
105-
unsafe impl Send for #p_ty {}
106-
107-
#feature_attribute_n
108-
impl #p_ty {
109-
#steal_docs
110-
pub unsafe fn steal() -> Self {
111-
Self { rb: #base::RegisterBlock::new(#address as *mut u8) }
112-
}
113-
}
114-
115-
#feature_attribute_n
116-
impl Deref for #p_ty {
117-
type Target = #base::RegisterBlock;
118-
119-
#[inline(always)]
120-
fn deref(&self) -> &Self::Target {
121-
&self.rb
122-
}
123-
}
124-
}
125-
} else {
126-
quote! {
127-
#[doc = #description]
128-
#doc_alias
129-
#feature_attribute_n
130-
pub struct #p_ty { _marker: PhantomData<*const ()> }
131-
132-
#feature_attribute_n
133-
unsafe impl Send for #p_ty {}
134-
135-
#feature_attribute_n
136-
impl #p_ty {
137-
///Pointer to the register block
138-
pub const PTR: *const #base::RegisterBlock = #address as *const _;
139-
140-
///Return the pointer to the register block
141-
#[inline(always)]
142-
pub const fn ptr() -> *const #base::RegisterBlock {
143-
Self::PTR
144-
}
145-
146-
#steal_docs
147-
pub unsafe fn steal() -> Self {
148-
Self { _marker: PhantomData }
149-
}
150-
}
151-
152-
#feature_attribute_n
153-
impl Deref for #p_ty {
154-
type Target = #base::RegisterBlock;
155-
156-
#[inline(always)]
157-
fn deref(&self) -> &Self::Target {
158-
unsafe { &*Self::PTR }
159-
}
160-
}
161-
}
162-
});
163-
164-
out.extend(quote! {
165-
#feature_attribute_n
166-
impl core::fmt::Debug for #p_ty {
167-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
168-
f.debug_struct(#name_str).finish()
169-
}
170-
}
171-
});
181+
out.extend(per_to_tokens(
182+
&feature_attribute_n,
183+
description,
184+
&p_ty,
185+
doc_alias,
186+
address,
187+
));
172188
}
173189

174190
let feature_any_attribute = quote! {#[cfg(any(#(feature = #feature_names),*))]};
@@ -191,79 +207,13 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
191207
feature_attribute.extend(quote! { #[cfg(feature = #p_feature)] })
192208
};
193209
// Insert the peripheral structure
194-
out.extend(if config.raw_access {
195-
quote! {
196-
#[doc = #description]
197-
#feature_attribute
198-
#[repr(transparent)]
199-
pub struct #p_ty { rb: #base::RegisterBlock }
200-
201-
#feature_attribute
202-
unsafe impl Send for #p_ty {}
203-
204-
#feature_attribute
205-
impl #p_ty {
206-
#steal_docs
207-
pub unsafe fn steal() -> Self {
208-
Self { rb: #base::RegisterBlock::new(#address as *mut u8) }
209-
}
210-
}
211-
212-
#feature_attribute
213-
impl Deref for #p_ty {
214-
type Target = #base::RegisterBlock;
215-
216-
#[inline(always)]
217-
fn deref(&self) -> &Self::Target {
218-
&self.rb
219-
}
220-
}
221-
}
222-
} else {
223-
quote! {
224-
#[doc = #description]
225-
#feature_attribute
226-
pub struct #p_ty { _marker: PhantomData<*const ()> }
227-
228-
#feature_attribute
229-
unsafe impl Send for #p_ty {}
230-
231-
#feature_attribute
232-
impl #p_ty {
233-
///Pointer to the register block
234-
pub const PTR: *const #base::RegisterBlock = #address as *const _;
235-
236-
///Return the pointer to the register block
237-
#[inline(always)]
238-
pub const fn ptr() -> *const #base::RegisterBlock {
239-
Self::PTR
240-
}
241-
242-
#steal_docs
243-
pub unsafe fn steal() -> Self {
244-
Self { _marker: PhantomData }
245-
}
246-
}
247-
248-
#feature_attribute
249-
impl Deref for #p_ty {
250-
type Target = #base::RegisterBlock;
251-
252-
#[inline(always)]
253-
fn deref(&self) -> &Self::Target {
254-
unsafe { &*Self::PTR }
255-
}
256-
}
257-
}
258-
});
259-
out.extend(quote! {
260-
#feature_attribute
261-
impl core::fmt::Debug for #p_ty {
262-
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
263-
f.debug_struct(#name_str).finish()
264-
}
265-
}
266-
});
210+
out.extend(per_to_tokens(
211+
&feature_attribute,
212+
&description,
213+
&p_ty,
214+
None,
215+
address,
216+
));
267217

268218
// Derived peripherals may not require re-implementation, and will instead
269219
// use a single definition of the non-derived version.

0 commit comments

Comments
 (0)