@@ -40,11 +40,10 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
4040 ( false , name_snake_case. clone ( ) , BlockPath :: new ( & p. name ) )
4141 } ;
4242
43- let feature_attribute = if config. feature_group && p. group_name . is_some ( ) {
43+ let mut feature_attribute = TokenStream :: new ( ) ;
44+ if config. feature_group && p. group_name . is_some ( ) {
4445 let feature_name = p. group_name . as_ref ( ) . unwrap ( ) . to_sanitized_snake_case ( ) ;
45- quote ! ( #[ cfg( feature = #feature_name) ] )
46- } else {
47- quote ! { }
46+ feature_attribute. extend ( quote ! { #[ cfg( feature = #feature_name) ] } ) ;
4847 } ;
4948
5049 match & p {
@@ -54,18 +53,28 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
5453 let names_constant_case = names_str. clone ( ) . map ( |n| Ident :: new ( & n, span) ) ;
5554 let addresses =
5655 ( 0 ..=dim. dim ) . map ( |i| util:: hex ( p. base_address + ( i * dim. dim_increment ) as u64 ) ) ;
57-
56+ let snake_names = names
57+ . iter ( )
58+ . map ( |p_name| p_name. to_sanitized_snake_case ( ) )
59+ . collect :: < Vec < _ > > ( ) ;
60+ let feature_attribute_n = snake_names. iter ( ) . map ( |p_snake| {
61+ let mut feature_attribute = feature_attribute. clone ( ) ;
62+ if config. feature_peripheral {
63+ feature_attribute. extend ( quote ! { #[ cfg( feature = #p_snake) ] } )
64+ } ;
65+ feature_attribute
66+ } ) ;
5867 // Insert the peripherals structure
5968 out. extend ( quote ! {
6069 #(
6170 #[ doc = #description]
62- #feature_attribute
71+ #feature_attribute_n
6372 pub struct #names_constant_case { _marker: PhantomData <* const ( ) > }
6473
65- #feature_attribute
74+ #feature_attribute_n
6675 unsafe impl Send for #names_constant_case { }
6776
68- #feature_attribute
77+ #feature_attribute_n
6978 impl #names_constant_case {
7079 ///Pointer to the register block
7180 pub const PTR : * const #base:: RegisterBlock = #addresses as * const _;
@@ -77,7 +86,7 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
7786 }
7887 }
7988
80- #feature_attribute
89+ #feature_attribute_n
8190 impl Deref for #names_constant_case {
8291 type Target = #base:: RegisterBlock ;
8392
@@ -87,16 +96,34 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
8796 }
8897 }
8998
90- #feature_attribute
99+ #feature_attribute_n
91100 impl core:: fmt:: Debug for #names_constant_case {
92101 fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
93102 f. debug_struct( #names_str) . finish( )
94103 }
95104 }
96105 ) *
97106 } ) ;
107+
108+ let feature_any_attribute = quote ! { #[ cfg( any( #( feature = #snake_names) , * ) ) ] } ;
109+
110+ // Derived peripherals may not require re-implementation, and will instead
111+ // use a single definition of the non-derived version.
112+ if derive_regs {
113+ // re-export the base module to allow deriveFrom this one
114+ out. extend ( quote ! {
115+ #[ doc = #description]
116+ #feature_any_attribute
117+ pub use #base as #name_snake_case;
118+ } ) ;
119+ return Ok ( out) ;
120+ }
98121 }
99- _ => {
122+ Peripheral :: Single ( _) => {
123+ let p_snake = name. to_sanitized_snake_case ( ) ;
124+ if config. feature_peripheral {
125+ feature_attribute. extend ( quote ! { #[ cfg( feature = #p_snake) ] } )
126+ } ;
100127 // Insert the peripheral structure
101128 out. extend ( quote ! {
102129 #[ doc = #description]
@@ -135,19 +162,19 @@ pub fn render(p_original: &Peripheral, index: &Index, config: &Config) -> Result
135162 }
136163 }
137164 } ) ;
138- }
139- }
140165
141- // Derived peripherals may not require re-implementation, and will instead
142- // use a single definition of the non-derived version.
143- if derive_regs {
144- // re-export the base module to allow deriveFrom this one
145- out. extend ( quote ! {
146- #[ doc = #description]
147- #feature_attribute
148- pub use #base as #name_snake_case;
149- } ) ;
150- return Ok ( out) ;
166+ // Derived peripherals may not require re-implementation, and will instead
167+ // use a single definition of the non-derived version.
168+ if derive_regs {
169+ // re-export the base module to allow deriveFrom this one
170+ out. extend ( quote ! {
171+ #[ doc = #description]
172+ #feature_attribute
173+ pub use #base as #name_snake_case;
174+ } ) ;
175+ return Ok ( out) ;
176+ }
177+ }
151178 }
152179
153180 let description = util:: escape_brackets (
0 commit comments