@@ -33,6 +33,9 @@ macro_rules! raw_reg {
3333 const fn $mask<const WI : u8 >( ) -> $U {
3434 <$U>:: MAX >> ( $size - WI )
3535 }
36+ impl FieldSpec for $U {
37+ type Ux = $U;
38+ }
3639 } ;
3740}
3841
@@ -47,6 +50,12 @@ pub trait RegisterSpec {
4750 type Ux : RawReg ;
4851}
4952
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+
5059/// Trait implemented by readable registers to enable the `read` method.
5160///
5261/// Registers marked with `Writable` can be also be `modify`'ed.
@@ -301,19 +310,19 @@ impl<REG: RegisterSpec> W<REG> {
301310}
302311
303312#[ 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 ,
306318 _reg : marker:: PhantomData < FI > ,
307319}
308320
309- impl < U , FI > FieldReaderRaw < U , FI >
310- where
311- U : Copy ,
312- {
321+ impl < FI : FieldSpec > FieldReaderRaw < FI > {
313322 /// Creates a new instance of the reader.
314323 #[ allow( unused) ]
315324 #[ inline( always) ]
316- pub ( crate ) fn new ( bits : U ) -> Self {
325+ pub ( crate ) fn new ( bits : FI :: Ux ) -> Self {
317326 Self {
318327 bits,
319328 _reg : marker:: PhantomData ,
@@ -322,7 +331,7 @@ where
322331}
323332
324333#[ doc( hidden) ]
325- pub struct BitReaderRaw < FI > {
334+ pub struct BitReaderRaw < FI = bool > {
326335 pub ( crate ) bits : bool ,
327336 _reg : marker:: PhantomData < FI > ,
328337}
@@ -342,30 +351,26 @@ impl<FI> BitReaderRaw<FI> {
342351/// Field reader.
343352///
344353/// 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 > ;
346355
347356/// Bit-wise field reader
348357pub type BitReader < FI = bool > = BitReaderRaw < FI > ;
349358
350- impl < N , FI > FieldReader < N , FI >
351- where
352- N : Copy ,
353- {
359+ impl < FI : FieldSpec > FieldReader < FI > {
354360 /// Reads raw bits from field.
355361 #[ inline( always) ]
356- pub fn bits ( & self ) -> N {
362+ pub fn bits ( & self ) -> FI :: Ux {
357363 self . bits
358364 }
359365}
360366
361- impl < N , FI > PartialEq < FI > for FieldReader < N , FI >
367+ impl < FI > PartialEq < FI > for FieldReader < FI >
362368where
363- FI : Copy ,
364- N : PartialEq + From < FI > ,
369+ FI : FieldSpec + Copy ,
365370{
366371 #[ inline( always) ]
367372 fn eq ( & self , other : & FI ) -> bool {
368- self . bits . eq ( & N :: from ( * other) )
373+ self . bits . eq ( & FI :: Ux :: from ( * other) )
369374 }
370375}
371376
@@ -404,20 +409,20 @@ pub struct Safe;
404409pub struct Unsafe ;
405410
406411#[ 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 >
408413where
409414 REG : Writable + RegisterSpec ,
410- N : From < FI > ,
415+ FI : FieldSpec ,
411416{
412417 pub ( crate ) w : & ' a mut REG :: Writer ,
413- _field : marker:: PhantomData < ( N , FI , Safety ) > ,
418+ _field : marker:: PhantomData < ( FI , Safety ) > ,
414419}
415420
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 >
418423where
419424 REG : Writable + RegisterSpec ,
420- N : From < FI > ,
425+ FI : FieldSpec ,
421426{
422427 /// Creates a new instance of the writer
423428 #[ allow( unused) ]
@@ -431,7 +436,7 @@ where
431436}
432437
433438#[ 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 >
435440where
436441 REG : Writable + RegisterSpec ,
437442 bool : From < FI > ,
@@ -457,25 +462,25 @@ where
457462}
458463
459464/// 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 > ;
462467/// 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 > ;
465470
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 >
467472where
468473 REG : Writable + RegisterSpec ,
469- N : From < FI > ,
474+ FI : FieldSpec ,
470475{
471476 /// Field width
472477 pub const WIDTH : u8 = WI ;
473478}
474479
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 >
476481where
477482 REG : Writable + RegisterSpec ,
478- N : From < FI > ,
483+ FI : FieldSpec ,
479484{
480485 /// Field width
481486 pub const WIDTH : u8 = WI ;
@@ -531,46 +536,46 @@ bit_proxy!(BitWriter0S, Bit0S);
531536bit_proxy ! ( BitWriter1T , Bit1T ) ;
532537bit_proxy ! ( BitWriter0T , Bit0T ) ;
533538
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 >
535540where
536541 REG : Writable + RegisterSpec ,
537- REG :: Ux : From < N > ,
538- N : From < FI > ,
542+ FI : FieldSpec ,
543+ REG :: Ux : From < FI :: Ux > ,
539544{
540545 /// Writes raw bits to the field
541546 ///
542547 /// # Safety
543548 ///
544549 /// Passing incorrect value can cause undefined behaviour. See reference manual
545550 #[ 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 {
547552 self . w . bits &= !( REG :: Ux :: mask :: < WI > ( ) << OF ) ;
548553 self . w . bits |= ( REG :: Ux :: from ( value) & REG :: Ux :: mask :: < WI > ( ) ) << OF ;
549554 self . w
550555 }
551556 /// Writes `variant` to the field
552557 #[ inline( always) ]
553558 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) ) }
555560 }
556561}
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 >
558563where
559564 REG : Writable + RegisterSpec ,
560- REG :: Ux : From < N > ,
561- N : From < FI > ,
565+ FI : FieldSpec ,
566+ REG :: Ux : From < FI :: Ux > ,
562567{
563568 /// Writes raw bits to the field
564569 #[ 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 {
566571 self . w . bits &= !( REG :: Ux :: mask :: < WI > ( ) << OF ) ;
567572 self . w . bits |= ( REG :: Ux :: from ( value) & REG :: Ux :: mask :: < WI > ( ) ) << OF ;
568573 self . w
569574 }
570575 /// Writes `variant` to the field
571576 #[ inline( always) ]
572577 pub fn variant ( self , variant : FI ) -> & ' a mut REG :: Writer {
573- self . bits ( N :: from ( variant) )
578+ self . bits ( FI :: Ux :: from ( variant) )
574579 }
575580}
576581
0 commit comments