@@ -248,6 +248,11 @@ pub fn render_register_mod(
248
248
let open = Punct :: new ( '{' , Spacing :: Alone ) ;
249
249
let close = Punct :: new ( '}' , Spacing :: Alone ) ;
250
250
251
+ let debug_feature = config
252
+ . impl_debug_feature
253
+ . as_ref ( )
254
+ . map ( |feature| quote ! ( #[ cfg( feature=#feature) ] ) ) ;
255
+
251
256
if let Some ( cur_fields) = register. fields . as_ref ( ) {
252
257
// filter out all reserved fields, as we should not generate code for
253
258
// them
@@ -284,12 +289,8 @@ pub fn render_register_mod(
284
289
) ?;
285
290
}
286
291
} else if !access. can_read ( ) || register. read_action . is_some ( ) {
287
- if let Some ( feature) = & config. impl_debug_feature {
288
- r_debug_impl. extend ( quote ! {
289
- #[ cfg( feature=#feature) ]
290
- } ) ;
291
- }
292
292
r_debug_impl. extend ( quote ! {
293
+ #debug_feature
293
294
impl core:: fmt:: Debug for crate :: generic:: Reg <#regspec_ident> {
294
295
fn fmt( & self , f: & mut core:: fmt:: Formatter <' _>) -> core:: fmt:: Result {
295
296
write!( f, "(not readable)" )
@@ -298,27 +299,17 @@ pub fn render_register_mod(
298
299
} ) ;
299
300
} else {
300
301
// no register fields are defined so implement Debug to get entire register value
301
- if let Some ( feature) = & config. impl_debug_feature {
302
- r_debug_impl. extend ( quote ! {
303
- #[ cfg( feature=#feature) ]
304
- } ) ;
305
- }
306
302
r_debug_impl. extend ( quote ! {
303
+ #debug_feature
307
304
impl core:: fmt:: Debug for R {
308
305
fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result {
309
306
write!( f, "{}" , self . bits( ) )
310
307
}
311
308
}
312
- } ) ;
313
- if let Some ( feature) = & config. impl_debug_feature {
314
- r_debug_impl. extend ( quote ! {
315
- #[ cfg( feature=#feature) ]
316
- } ) ;
317
- }
318
- r_debug_impl. extend ( quote ! {
309
+ #debug_feature
319
310
impl core:: fmt:: Debug for crate :: generic:: Reg <#regspec_ident> {
320
311
fn fmt( & self , f: & mut core:: fmt:: Formatter <' _>) -> core:: fmt:: Result {
321
- self . read( ) . fmt ( f)
312
+ core :: fmt :: Debug :: fmt ( & self . read( ) , f)
322
313
}
323
314
}
324
315
} ) ;
@@ -450,15 +441,15 @@ fn render_register_mod_debug(
450
441
let open = Punct :: new ( '{' , Spacing :: Alone ) ;
451
442
let close = Punct :: new ( '}' , Spacing :: Alone ) ;
452
443
let mut r_debug_impl = TokenStream :: new ( ) ;
444
+ let debug_feature = config
445
+ . impl_debug_feature
446
+ . as_ref ( )
447
+ . map ( |feature| quote ! ( #[ cfg( feature=#feature) ] ) ) ;
453
448
454
449
// implement Debug for register readable fields that have no read side effects
455
450
if access. can_read ( ) && register. read_action . is_none ( ) {
456
- if let Some ( feature) = & config. impl_debug_feature {
457
- r_debug_impl. extend ( quote ! {
458
- #[ cfg( feature=#feature) ]
459
- } ) ;
460
- }
461
451
r_debug_impl. extend ( quote ! {
452
+ #debug_feature
462
453
impl core:: fmt:: Debug for R #open
463
454
fn fmt( & self , f: & mut core:: fmt:: Formatter ) -> core:: fmt:: Result #open
464
455
f. debug_struct( #name)
@@ -496,25 +487,17 @@ fn render_register_mod_debug(
496
487
#close
497
488
#close
498
489
} ) ;
499
- if let Some ( feature) = & config. impl_debug_feature {
500
- r_debug_impl. extend ( quote ! {
501
- #[ cfg( feature=#feature) ]
502
- } ) ;
503
- }
504
490
r_debug_impl. extend ( quote ! {
491
+ #debug_feature
505
492
impl core:: fmt:: Debug for crate :: generic:: Reg <#regspec_ident> {
506
493
fn fmt( & self , f: & mut core:: fmt:: Formatter <' _>) -> core:: fmt:: Result {
507
- self . read( ) . fmt ( f)
494
+ core :: fmt :: Debug :: fmt ( & self . read( ) , f)
508
495
}
509
496
}
510
497
} ) ;
511
498
} else if !access. can_read ( ) || register. read_action . is_some ( ) {
512
- if let Some ( feature) = & config. impl_debug_feature {
513
- r_debug_impl. extend ( quote ! {
514
- #[ cfg( feature=#feature) ]
515
- } ) ;
516
- }
517
499
r_debug_impl. extend ( quote ! {
500
+ #debug_feature
518
501
impl core:: fmt:: Debug for crate :: generic:: Reg <#regspec_ident> {
519
502
fn fmt( & self , f: & mut core:: fmt:: Formatter <' _>) -> core:: fmt:: Result {
520
503
write!( f, "(not readable)" )
@@ -755,7 +738,7 @@ pub fn fields(
755
738
// else, generate enumeratedValues into a Rust enum with functions for each variant.
756
739
if variants. is_empty ( ) {
757
740
// generate struct VALUE_READ_TY_A(fty) and From<fty> for VALUE_READ_TY_A.
758
- add_with_no_variants ( mod_items, & value_read_ty, & fty, & description, rv) ;
741
+ add_with_no_variants ( mod_items, & value_read_ty, & fty, & description, rv, config ) ;
759
742
} else {
760
743
// do we have finite definition of this enumeration in svd? If not, the later code would
761
744
// return an Option when the value read from field does not match any defined values.
@@ -770,6 +753,7 @@ pub fn fields(
770
753
& fty,
771
754
& description,
772
755
rv,
756
+ config,
773
757
) ;
774
758
has_reserved_variant = false ;
775
759
} else {
@@ -780,6 +764,7 @@ pub fn fields(
780
764
& fty,
781
765
& description,
782
766
rv,
767
+ config,
783
768
) ;
784
769
has_reserved_variant = evs. values . len ( ) != ( 1 << width) ;
785
770
}
@@ -1047,7 +1032,14 @@ pub fn fields(
1047
1032
// generate write value structure and From conversation if we can't reuse read value structure.
1048
1033
if writer_reader_different_enum {
1049
1034
if variants. is_empty ( ) {
1050
- add_with_no_variants ( mod_items, & value_write_ty, & fty, & description, rv) ;
1035
+ add_with_no_variants (
1036
+ mod_items,
1037
+ & value_write_ty,
1038
+ & fty,
1039
+ & description,
1040
+ rv,
1041
+ config,
1042
+ ) ;
1051
1043
} else {
1052
1044
add_from_variants (
1053
1045
mod_items,
@@ -1056,6 +1048,7 @@ pub fn fields(
1056
1048
& fty,
1057
1049
& description,
1058
1050
rv,
1051
+ config,
1059
1052
) ;
1060
1053
}
1061
1054
}
@@ -1324,7 +1317,13 @@ fn add_with_no_variants(
1324
1317
fty : & Ident ,
1325
1318
desc : & str ,
1326
1319
reset_value : Option < u64 > ,
1320
+ config : & Config ,
1327
1321
) {
1322
+ let defmt = config
1323
+ . impl_defmt
1324
+ . as_ref ( )
1325
+ . map ( |feature| quote ! ( #[ cfg_attr( feature = #feature, derive( defmt:: Format ) ) ] ) ) ;
1326
+
1328
1327
let cast = if fty == "bool" {
1329
1328
quote ! { val. 0 as u8 != 0 }
1330
1329
} else {
@@ -1339,6 +1338,7 @@ fn add_with_no_variants(
1339
1338
1340
1339
mod_items. extend ( quote ! {
1341
1340
#[ doc = #desc]
1341
+ #defmt
1342
1342
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
1343
1343
pub struct #pc( #fty) ;
1344
1344
impl From <#pc> for #fty {
@@ -1364,7 +1364,13 @@ fn add_from_variants<'a>(
1364
1364
fty : & Ident ,
1365
1365
desc : & str ,
1366
1366
reset_value : Option < u64 > ,
1367
+ config : & Config ,
1367
1368
) {
1369
+ let defmt = config
1370
+ . impl_defmt
1371
+ . as_ref ( )
1372
+ . map ( |feature| quote ! ( #[ cfg_attr( feature = #feature, derive( defmt:: Format ) ) ] ) ) ;
1373
+
1368
1374
let ( repr, cast) = if fty == "bool" {
1369
1375
( quote ! { } , quote ! { variant as u8 != 0 } )
1370
1376
} else {
@@ -1392,6 +1398,7 @@ fn add_from_variants<'a>(
1392
1398
1393
1399
mod_items. extend ( quote ! {
1394
1400
#[ doc = #desc]
1401
+ #defmt
1395
1402
#[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
1396
1403
#repr
1397
1404
pub enum #pc {
0 commit comments