@@ -33,6 +33,9 @@ macro_rules! raw_reg {
33
33
const fn $mask<const WI : u8 >( ) -> $U {
34
34
<$U>:: MAX >> ( $size - WI )
35
35
}
36
+ impl FieldSpec for $U {
37
+ type Ux = $U;
38
+ }
36
39
} ;
37
40
}
38
41
@@ -47,6 +50,12 @@ pub trait RegisterSpec {
47
50
type Ux : RawReg ;
48
51
}
49
52
53
+ /// Raw field type
54
+ pub trait FieldSpec : Sized {
55
+ /// Raw field type (`u8`, `u16`, `u32`, ...).
56
+ type Ux : Copy + PartialEq + From < Self > ;
57
+ }
58
+
50
59
/// Trait implemented by readable registers to enable the `read` method.
51
60
///
52
61
/// Registers marked with `Writable` can be also be `modify`'ed.
@@ -301,19 +310,19 @@ impl<REG: RegisterSpec> W<REG> {
301
310
}
302
311
303
312
#[ doc( hidden) ]
304
- pub struct FieldReaderRaw < U , FI > {
305
- pub ( crate ) bits : U ,
313
+ pub struct FieldReaderRaw < FI = u8 >
314
+ where
315
+ FI : FieldSpec
316
+ {
317
+ pub ( crate ) bits : FI :: Ux ,
306
318
_reg : marker:: PhantomData < FI > ,
307
319
}
308
320
309
- impl < U , FI > FieldReaderRaw < U , FI >
310
- where
311
- U : Copy ,
312
- {
321
+ impl < FI : FieldSpec > FieldReaderRaw < FI > {
313
322
/// Creates a new instance of the reader.
314
323
#[ allow( unused) ]
315
324
#[ inline( always) ]
316
- pub ( crate ) fn new ( bits : U ) -> Self {
325
+ pub ( crate ) fn new ( bits : FI :: Ux ) -> Self {
317
326
Self {
318
327
bits,
319
328
_reg : marker:: PhantomData ,
@@ -322,7 +331,7 @@ where
322
331
}
323
332
324
333
#[ doc( hidden) ]
325
- pub struct BitReaderRaw < FI > {
334
+ pub struct BitReaderRaw < FI = bool > {
326
335
pub ( crate ) bits : bool ,
327
336
_reg : marker:: PhantomData < FI > ,
328
337
}
@@ -342,30 +351,26 @@ impl<FI> BitReaderRaw<FI> {
342
351
/// Field reader.
343
352
///
344
353
/// Result of the `read` methods of fields.
345
- pub type FieldReader < N = u8 , FI = u8 > = FieldReaderRaw < N , FI > ;
354
+ pub type FieldReader < FI = u8 > = FieldReaderRaw < FI > ;
346
355
347
356
/// Bit-wise field reader
348
357
pub type BitReader < FI = bool > = BitReaderRaw < FI > ;
349
358
350
- impl < N , FI > FieldReader < N , FI >
351
- where
352
- N : Copy ,
353
- {
359
+ impl < FI : FieldSpec > FieldReader < FI > {
354
360
/// Reads raw bits from field.
355
361
#[ inline( always) ]
356
- pub fn bits ( & self ) -> N {
362
+ pub fn bits ( & self ) -> FI :: Ux {
357
363
self . bits
358
364
}
359
365
}
360
366
361
- impl < N , FI > PartialEq < FI > for FieldReader < N , FI >
367
+ impl < FI > PartialEq < FI > for FieldReader < FI >
362
368
where
363
- FI : Copy ,
364
- N : PartialEq + From < FI > ,
369
+ FI : FieldSpec + Copy ,
365
370
{
366
371
#[ inline( always) ]
367
372
fn eq ( & self , other : & FI ) -> bool {
368
- self . bits . eq ( & N :: from ( * other) )
373
+ self . bits . eq ( & FI :: Ux :: from ( * other) )
369
374
}
370
375
}
371
376
@@ -404,20 +409,20 @@ pub struct Safe;
404
409
pub struct Unsafe ;
405
410
406
411
#[ doc( hidden) ]
407
- pub struct FieldWriterRaw < ' a , REG , const WI : u8 , const O : u8 , N , FI , Safety >
412
+ pub struct FieldWriterRaw < ' a , REG , const WI : u8 , const O : u8 , FI = u8 , Safety = Unsafe >
408
413
where
409
414
REG : Writable + RegisterSpec ,
410
- N : From < FI > ,
415
+ FI : FieldSpec ,
411
416
{
412
417
pub ( crate ) w : & ' a mut REG :: Writer ,
413
- _field : marker:: PhantomData < ( N , FI , Safety ) > ,
418
+ _field : marker:: PhantomData < ( FI , Safety ) > ,
414
419
}
415
420
416
- impl < ' a , REG , const WI : u8 , const O : u8 , N , FI , Safety >
417
- FieldWriterRaw < ' a , REG , WI , O , N , FI , Safety >
421
+ impl < ' a , REG , const WI : u8 , const O : u8 , FI , Safety >
422
+ FieldWriterRaw < ' a , REG , WI , O , FI , Safety >
418
423
where
419
424
REG : Writable + RegisterSpec ,
420
- N : From < FI > ,
425
+ FI : FieldSpec ,
421
426
{
422
427
/// Creates a new instance of the writer
423
428
#[ allow( unused) ]
@@ -431,7 +436,7 @@ where
431
436
}
432
437
433
438
#[ doc( hidden) ]
434
- pub struct BitWriterRaw < ' a , REG , const O : u8 , FI , M >
439
+ pub struct BitWriterRaw < ' a , REG , const O : u8 , FI = bool , M = BitM >
435
440
where
436
441
REG : Writable + RegisterSpec ,
437
442
bool : From < FI > ,
@@ -457,25 +462,25 @@ where
457
462
}
458
463
459
464
/// Write field Proxy with unsafe `bits`
460
- pub type FieldWriter < ' a , REG , const WI : u8 , const O : u8 , N = u8 , FI = u8 > =
461
- FieldWriterRaw < ' a , REG , WI , O , N , FI , Unsafe > ;
465
+ pub type FieldWriter < ' a , REG , const WI : u8 , const O : u8 , FI = u8 > =
466
+ FieldWriterRaw < ' a , REG , WI , O , FI , Unsafe > ;
462
467
/// Write field Proxy with safe `bits`
463
- pub type FieldWriterSafe < ' a , REG , const WI : u8 , const O : u8 , N = u8 , FI = u8 > =
464
- FieldWriterRaw < ' a , REG , WI , O , N , FI , Safe > ;
468
+ pub type FieldWriterSafe < ' a , REG , const WI : u8 , const O : u8 , FI = u8 > =
469
+ FieldWriterRaw < ' a , REG , WI , O , FI , Safe > ;
465
470
466
- impl < ' a , REG , const WI : u8 , const OF : u8 , N , FI > FieldWriter < ' a , REG , WI , OF , N , FI >
471
+ impl < ' a , REG , const WI : u8 , const OF : u8 , FI > FieldWriter < ' a , REG , WI , OF , FI >
467
472
where
468
473
REG : Writable + RegisterSpec ,
469
- N : From < FI > ,
474
+ FI : FieldSpec ,
470
475
{
471
476
/// Field width
472
477
pub const WIDTH : u8 = WI ;
473
478
}
474
479
475
- impl < ' a , REG , const WI : u8 , const OF : u8 , N , FI > FieldWriterSafe < ' a , REG , WI , OF , N , FI >
480
+ impl < ' a , REG , const WI : u8 , const OF : u8 , FI > FieldWriterSafe < ' a , REG , WI , OF , FI >
476
481
where
477
482
REG : Writable + RegisterSpec ,
478
- N : From < FI > ,
483
+ FI : FieldSpec ,
479
484
{
480
485
/// Field width
481
486
pub const WIDTH : u8 = WI ;
@@ -531,46 +536,46 @@ bit_proxy!(BitWriter0S, Bit0S);
531
536
bit_proxy ! ( BitWriter1T , Bit1T ) ;
532
537
bit_proxy ! ( BitWriter0T , Bit0T ) ;
533
538
534
- impl < ' a , REG , const WI : u8 , const OF : u8 , N , FI > FieldWriter < ' a , REG , WI , OF , N , FI >
539
+ impl < ' a , REG , const WI : u8 , const OF : u8 , FI > FieldWriter < ' a , REG , WI , OF , FI >
535
540
where
536
541
REG : Writable + RegisterSpec ,
537
- REG :: Ux : From < N > ,
538
- N : From < FI > ,
542
+ FI : FieldSpec ,
543
+ REG :: Ux : From < FI :: Ux > ,
539
544
{
540
545
/// Writes raw bits to the field
541
546
///
542
547
/// # Safety
543
548
///
544
549
/// Passing incorrect value can cause undefined behaviour. See reference manual
545
550
#[ inline( always) ]
546
- pub unsafe fn bits ( self , value : N ) -> & ' a mut REG :: Writer {
551
+ pub unsafe fn bits ( self , value : FI :: Ux ) -> & ' a mut REG :: Writer {
547
552
self . w . bits &= !( REG :: Ux :: mask :: < WI > ( ) << OF ) ;
548
553
self . w . bits |= ( REG :: Ux :: from ( value) & REG :: Ux :: mask :: < WI > ( ) ) << OF ;
549
554
self . w
550
555
}
551
556
/// Writes `variant` to the field
552
557
#[ inline( always) ]
553
558
pub fn variant ( self , variant : FI ) -> & ' a mut REG :: Writer {
554
- unsafe { self . bits ( N :: from ( variant) ) }
559
+ unsafe { self . bits ( FI :: Ux :: from ( variant) ) }
555
560
}
556
561
}
557
- impl < ' a , REG , const WI : u8 , const OF : u8 , N , FI > FieldWriterSafe < ' a , REG , WI , OF , N , FI >
562
+ impl < ' a , REG , const WI : u8 , const OF : u8 , FI > FieldWriterSafe < ' a , REG , WI , OF , FI >
558
563
where
559
564
REG : Writable + RegisterSpec ,
560
- REG :: Ux : From < N > ,
561
- N : From < FI > ,
565
+ FI : FieldSpec ,
566
+ REG :: Ux : From < FI :: Ux > ,
562
567
{
563
568
/// Writes raw bits to the field
564
569
#[ inline( always) ]
565
- pub fn bits ( self , value : N ) -> & ' a mut REG :: Writer {
570
+ pub fn bits ( self , value : FI :: Ux ) -> & ' a mut REG :: Writer {
566
571
self . w . bits &= !( REG :: Ux :: mask :: < WI > ( ) << OF ) ;
567
572
self . w . bits |= ( REG :: Ux :: from ( value) & REG :: Ux :: mask :: < WI > ( ) ) << OF ;
568
573
self . w
569
574
}
570
575
/// Writes `variant` to the field
571
576
#[ inline( always) ]
572
577
pub fn variant ( self , variant : FI ) -> & ' a mut REG :: Writer {
573
- self . bits ( N :: from ( variant) )
578
+ self . bits ( FI :: Ux :: from ( variant) )
574
579
}
575
580
}
576
581
0 commit comments