@@ -255,6 +255,7 @@ pub fn fields(
255
255
name : & ' a str ,
256
256
offset : Tokens ,
257
257
pc_r : Ident ,
258
+ _pc_r : Ident ,
258
259
pc_w : Ident ,
259
260
sc : Ident ,
260
261
bits : Ident ,
@@ -274,6 +275,7 @@ pub fn fields(
274
275
let sc = f. name . to_sanitized_snake_case ( ) ;
275
276
let pc = f. name . to_sanitized_upper_case ( ) ;
276
277
let pc_r = Ident :: from ( & * format ! ( "{}R" , pc) ) ;
278
+ let _pc_r = Ident :: from ( & * format ! ( "{}_R" , pc) ) ;
277
279
let pc_w = Ident :: from ( & * format ! ( "{}W" , pc) ) ;
278
280
let _pc_w = Ident :: from ( & * format ! ( "_{}W" , pc) ) ;
279
281
let _sc = Ident :: from ( & * format ! ( "_{}" , sc) ) ;
@@ -296,6 +298,7 @@ pub fn fields(
296
298
_sc,
297
299
description,
298
300
pc_r,
301
+ _pc_r,
299
302
pc_w,
300
303
bits,
301
304
width,
@@ -344,22 +347,23 @@ pub fn fields(
344
347
let value = if offset != 0 {
345
348
let offset = & f. offset ;
346
349
quote ! {
347
- ( ( self . bits >> #offset) & #mask) #cast
350
+ ( ( self . bits( ) >> #offset) & #mask) #cast
348
351
}
349
352
} else {
350
353
quote ! {
351
- ( self . bits & #mask) #cast
354
+ ( self . bits( ) & #mask) #cast
352
355
}
353
356
} ;
354
357
358
+ let pc_r = & f. pc_r ;
359
+ let _pc_r = & f. _pc_r ;
360
+
355
361
if let Some ( ( evs, base) ) = lookup_filter ( & lookup_results, Usage :: Read ) {
356
- let has_reserved_variant = evs. values . len ( ) != ( 1 << f. width ) ;
357
362
let variants = Variant :: from_enumerated_values ( evs) ?;
358
363
359
- let pc_r = & f. pc_r ;
360
364
if let Some ( base) = & base {
361
365
let pc = base. field . to_sanitized_upper_case ( ) ;
362
- let base_pc_r = Ident :: from ( & * format ! ( "{}R " , pc) ) ;
366
+ let base_pc_r = Ident :: from ( & * format ! ( "{}_R " , pc) ) ;
363
367
let desc = format ! ( "Possible values of the field `{}`" , f. name, ) ;
364
368
365
369
if let ( Some ( peripheral) , Some ( register) ) = ( & base. peripheral , & base. register ) {
@@ -370,20 +374,20 @@ pub fn fields(
370
374
371
375
mod_items. push ( quote ! {
372
376
#[ doc = #desc]
373
- pub type #pc_r = crate :: #pmod_:: #rmod_:: #base_pc_r;
377
+ pub type #_pc_r = crate :: #pmod_:: #rmod_:: #base_pc_r;
374
378
} ) ;
375
379
} else if let Some ( register) = & base. register {
376
380
let mod_ = register. to_sanitized_snake_case ( ) ;
377
381
let mod_ = Ident :: from ( & * mod_) ;
378
382
379
383
mod_items. push ( quote ! {
380
384
#[ doc = #desc]
381
- pub type #pc_r = super :: #mod_:: #base_pc_r;
385
+ pub type #_pc_r = super :: #mod_:: #base_pc_r;
382
386
} ) ;
383
387
} else {
384
388
mod_items. push ( quote ! {
385
389
#[ doc = #desc]
386
- pub type #pc_r = #base_pc_r;
390
+ pub type #_pc_r = #base_pc_r;
387
391
} ) ;
388
392
}
389
393
}
@@ -393,15 +397,15 @@ pub fn fields(
393
397
r_impl_items. push ( quote ! {
394
398
#[ doc = #description]
395
399
#[ inline( always) ]
396
- pub fn #sc( & self ) -> #pc_r {
397
- #pc_r :: _from ( #value )
400
+ pub fn #sc( & self ) -> #_pc_r {
401
+ #_pc_r :: new ( #value )
398
402
}
399
403
} ) ;
400
404
401
405
if base. is_none ( ) {
402
406
let desc = format ! ( "Possible values of the field `{}`" , f. name, ) ;
403
407
404
- let mut vars = variants
408
+ let vars = variants
405
409
. iter ( )
406
410
. map ( |v| {
407
411
let desc = util:: escape_brackets ( & v. doc ) ;
@@ -412,12 +416,7 @@ pub fn fields(
412
416
}
413
417
} )
414
418
. collect :: < Vec < _ > > ( ) ;
415
- if has_reserved_variant {
416
- vars. push ( quote ! {
417
- ///Reserved
418
- _Reserved( #fty)
419
- } ) ;
420
- }
419
+
421
420
mod_items. push ( quote ! {
422
421
#[ doc = #desc]
423
422
#[ derive( Clone , Copy , Debug , PartialEq ) ]
@@ -428,78 +427,22 @@ pub fn fields(
428
427
429
428
let mut enum_items = vec ! [ ] ;
430
429
431
- let mut arms = variants
432
- . iter ( )
433
- . map ( |v| {
434
- let value = util:: hex_or_bool ( v. value as u64 , f. width ) ;
435
- let pc = & v. pc ;
436
-
437
- quote ! {
438
- #pc_r:: #pc => #value
439
- }
440
- } )
441
- . collect :: < Vec < _ > > ( ) ;
442
- if has_reserved_variant {
443
- arms. push ( quote ! {
444
- #pc_r:: _Reserved( bits) => bits
445
- } ) ;
446
- }
447
-
448
- if f. width == 1 {
449
- enum_items. push ( quote ! {
450
- ///Returns `true` if the bit is clear (0)
451
- #[ inline( always) ]
452
- pub fn bit_is_clear( & self ) -> bool {
453
- !self . #bits( )
454
- }
455
-
456
- ///Returns `true` if the bit is set (1)
457
- #[ inline( always) ]
458
- pub fn bit_is_set( & self ) -> bool {
459
- self . #bits( )
460
- }
461
- } ) ;
462
- }
430
+ let arms = variants. iter ( ) . map ( |v| {
431
+ let pc = & v. pc ;
432
+ let value = util:: unsuffixed_or_bool ( v. value , f. width ) ;
463
433
464
- enum_items. push ( quote ! {
465
- ///Value of the field as raw bits
466
- #[ inline( always) ]
467
- pub fn #bits( & self ) -> #fty {
468
- match * self {
469
- #( #arms) , *
470
- }
434
+ quote ! {
435
+ #pc_r:: #pc => #value
471
436
}
472
437
} ) ;
473
438
474
- let mut arms = variants
475
- . iter ( )
476
- . map ( |v| {
477
- let i = util:: unsuffixed_or_bool ( v. value , f. width ) ;
478
- let pc = & v. pc ;
479
-
480
- quote ! {
481
- #i => #pc_r:: #pc
482
- }
483
- } )
484
- . collect :: < Vec < _ > > ( ) ;
485
-
486
- if has_reserved_variant {
487
- arms. push ( quote ! {
488
- i => #pc_r:: _Reserved( i)
489
- } ) ;
490
- } else if 1 << f. width . to_ty_width ( ) ? != variants. len ( ) {
491
- arms. push ( quote ! {
492
- _ => unreachable!( )
493
- } ) ;
494
- }
495
-
496
- enum_items. push ( quote ! {
497
- #[ allow( missing_docs) ]
498
- #[ doc( hidden) ]
499
- #[ inline( always) ]
500
- pub fn _from( value: #fty) -> #pc_r {
501
- match value {
502
- #( #arms) , * ,
439
+ mod_items. push ( quote ! {
440
+ impl crate :: ToBits <#fty> for #pc_r {
441
+ #[ inline( always) ]
442
+ fn _bits( & self ) -> #fty {
443
+ match * self {
444
+ #( #arms) , *
445
+ }
503
446
}
504
447
}
505
448
} ) ;
@@ -525,58 +468,30 @@ pub fn fields(
525
468
}
526
469
527
470
mod_items. push ( quote ! {
528
- impl #pc_r {
471
+ ///Reader of the field
472
+ pub type #_pc_r = crate :: FR <#fty, #pc_r>;
473
+ impl #_pc_r {
529
474
#( #enum_items) *
530
475
}
531
476
} ) ;
532
477
}
478
+
533
479
} else {
534
480
let description = & util:: escape_brackets ( & f. description ) ;
535
- let pc_r = & f. pc_r ;
536
481
let sc = & f. sc ;
537
482
r_impl_items. push ( quote ! {
538
483
#[ doc = #description]
539
484
#[ inline( always) ]
540
- pub fn #sc( & self ) -> #pc_r {
541
- let bits = #value;
542
- #pc_r { bits }
485
+ pub fn #sc( & self ) -> #_pc_r {
486
+ #_pc_r:: new ( #value )
543
487
}
544
488
} ) ;
545
489
546
- let mut pc_r_impl_items = vec ! [ quote! {
547
- ///Value of the field as raw bits
548
- #[ inline( always) ]
549
- pub fn #bits( & self ) -> #fty {
550
- self . bits
551
- }
552
- } ] ;
553
-
554
- if f. width == 1 {
555
- pc_r_impl_items. push ( quote ! {
556
- ///Returns `true` if the bit is clear (0)
557
- #[ inline( always) ]
558
- pub fn bit_is_clear( & self ) -> bool {
559
- !self . #bits( )
560
- }
561
-
562
- ///Returns `true` if the bit is set (1)
563
- #[ inline( always) ]
564
- pub fn bit_is_set( & self ) -> bool {
565
- self . #bits( )
566
- }
567
- } ) ;
568
- }
569
-
570
490
mod_items. push ( quote ! {
571
- ///Value of the field
572
- pub struct #pc_r {
573
- bits: #fty,
574
- }
491
+ ///Reader of the field
492
+ pub type #_pc_r = crate :: FR <#fty, #fty>;
493
+ } )
575
494
576
- impl #pc_r {
577
- #( #pc_r_impl_items) *
578
- }
579
- } ) ;
580
495
}
581
496
}
582
497
0 commit comments