@@ -9,6 +9,20 @@ pub fn render() -> Result<Vec<Tokens>> {
99
1010 generic_items. push ( quote ! {
1111 use core:: marker;
12+ use core:: ops:: Deref ;
13+ use vcell:: VolatileCell ;
14+
15+ ///Marker trait for readable register/field
16+ pub trait Readable { }
17+
18+ ///Marker trait for writable register/field
19+ pub trait Writable { }
20+
21+ ///Reset value of the register
22+ pub trait ResetValue <U > {
23+ ///Reset value of the register
24+ fn reset_value( ) -> U ;
25+ }
1226
1327 ///Converting enumerated values to bits
1428 pub trait ToBits <N > {
@@ -18,23 +32,105 @@ pub fn render() -> Result<Vec<Tokens>> {
1832 } ) ;
1933
2034 generic_items. push ( quote ! {
21- ///Value read from the register
22- pub struct FR <U , T > {
23- pub ( crate ) bits: U ,
24- _reg: marker:: PhantomData <T >,
35+ ///Wrapper for registers
36+ pub struct Reg <U , REG > {
37+ register: vcell:: VolatileCell <U >,
38+ _marker: marker:: PhantomData <REG >,
39+ }
40+
41+ impl <U , REG > core:: ops:: Deref for Reg <U , REG > {
42+ type Target = vcell:: VolatileCell <U >;
43+ #[ inline( always) ]
44+ fn deref( & self ) -> & Self :: Target {
45+ & self . register
46+ }
2547 }
2648
27- impl <U , T , FI > PartialEq < FI > for FR < U , T >
49+ impl <U , REG > Reg < U , REG >
2850 where
29- U : PartialEq ,
30- FI : ToBits < U >
51+ Self : Readable + Deref < Target = VolatileCell < U >> ,
52+ U : Copy
3153 {
32- fn eq( & self , other: & FI ) -> bool {
33- self . bits. eq( & other. _bits( ) )
54+ ///Reads the contents of the register
55+ #[ inline( always) ]
56+ pub fn read( & self ) -> R <U , Self > {
57+ R { bits: ( * self ) . get( ) , _reg: marker:: PhantomData }
58+ }
59+ }
60+
61+ impl <U , REG > Reg <U , REG >
62+ where
63+ Self : ResetValue <U > + Writable + Deref <Target =VolatileCell <U >>,
64+ U : Copy ,
65+ {
66+ ///Writes the reset value to the register
67+ #[ inline( always) ]
68+ pub fn reset( & self ) {
69+ ( * self ) . set( Self :: reset_value( ) )
3470 }
3571 }
72+ } ) ;
3673
37- impl <U , T > FR <U , T >
74+ generic_items. push ( quote ! {
75+ impl <U , REG > Reg <U , REG >
76+ where
77+ Self : ResetValue <U > + Writable + Deref <Target =VolatileCell <U >>,
78+ U : Copy
79+ {
80+ ///Writes to the register
81+ #[ inline( always) ]
82+ pub fn write<F >( & self , f: F )
83+ where
84+ F : FnOnce ( & mut W <U , Self >) -> & mut W <U , Self >
85+ {
86+ ( * self ) . set( f( & mut W { bits: Self :: reset_value( ) , _reg: marker:: PhantomData } ) . bits) ;
87+ }
88+ }
89+ } ) ;
90+
91+ generic_items. push ( quote ! {
92+ impl <U , REG > Reg <U , REG >
93+ where
94+ Self : Writable + Deref <Target =VolatileCell <U >>,
95+ U : Copy + Default
96+ {
97+ ///Writes Zero to the register
98+ #[ inline( always) ]
99+ pub fn write_with_zero<F >( & self , f: F )
100+ where
101+ F : FnOnce ( & mut W <U , Self >) -> & mut W <U , Self >
102+ {
103+ ( * self ) . set( f( & mut W { bits: U :: default ( ) , _reg: marker:: PhantomData } ) . bits) ;
104+ }
105+ }
106+ } ) ;
107+
108+ generic_items. push ( quote ! {
109+ impl <U , REG > Reg <U , REG >
110+ where
111+ Self : Readable + Writable + Deref <Target = VolatileCell <U >>,
112+ U : Copy ,
113+ {
114+ ///Modifies the contents of the register
115+ #[ inline( always) ]
116+ pub fn modify<F >( & self , f: F )
117+ where
118+ for <' w> F : FnOnce ( & R <U , Self >, & ' w mut W <U , Self >) -> & ' w mut W <U , Self >
119+ {
120+ let bits = ( * self ) . get( ) ;
121+ ( * self ) . set( f( & R { bits, _reg: marker:: PhantomData } , & mut W { bits, _reg: marker:: PhantomData } ) . bits) ;
122+ }
123+ }
124+ } ) ;
125+
126+ generic_items. push ( quote ! {
127+ ///Register/field reader
128+ pub struct R <U , T > {
129+ pub ( crate ) bits: U ,
130+ _reg: marker:: PhantomData <T >,
131+ }
132+
133+ impl <U , T > R <U , T >
38134 where
39135 U : Copy
40136 {
@@ -46,7 +142,7 @@ pub fn render() -> Result<Vec<Tokens>> {
46142 _reg: marker:: PhantomData ,
47143 }
48144 }
49- ///Read raw bits from field
145+ ///Read raw bits from register/ field
50146 #[ inline( always) ]
51147 pub fn bits( & self ) -> U {
52148 self . bits
@@ -55,7 +151,19 @@ pub fn render() -> Result<Vec<Tokens>> {
55151 } ) ;
56152
57153 generic_items. push ( quote ! {
58- impl <FI > FR <bool , FI > {
154+ impl <U , T , FI > PartialEq <FI > for R <U , T >
155+ where
156+ U : PartialEq ,
157+ FI : ToBits <U >
158+ {
159+ fn eq( & self , other: & FI ) -> bool {
160+ self . bits. eq( & other. _bits( ) )
161+ }
162+ }
163+ } ) ;
164+
165+ generic_items. push ( quote ! {
166+ impl <FI > R <bool , FI > {
59167 ///Value of the field as raw bits
60168 #[ inline( always) ]
61169 pub fn bit( & self ) -> bool {
@@ -74,6 +182,27 @@ pub fn render() -> Result<Vec<Tokens>> {
74182 }
75183 } ) ;
76184
185+ generic_items. push ( quote ! {
186+ ///Register writer
187+ pub struct W <U , REG > {
188+ ///Writable bits
189+ pub bits: U ,
190+ _reg: marker:: PhantomData <REG >,
191+ }
192+ } ) ;
193+
194+ generic_items. push ( quote ! {
195+ impl <U , REG > W <U , REG > {
196+ ///Writes raw bits to the register
197+ #[ inline( always) ]
198+ pub fn bits( & mut self , bits: U ) -> & mut Self {
199+ self . bits = bits;
200+ self
201+ }
202+ }
203+ } ) ;
204+
205+
77206 generic_items. push ( quote ! {
78207 ///Used if enumerated values cover not the whole range
79208 #[ derive( Clone , Copy , PartialEq ) ]
@@ -88,7 +217,7 @@ pub fn render() -> Result<Vec<Tokens>> {
88217 code. push ( quote ! {
89218 #[ allow( unused_imports) ]
90219 use generic:: * ;
91- /// Common register and bit access and modify traits
220+ ///Common register and bit access and modify traits
92221 pub mod generic {
93222 #( #generic_items) *
94223 }
0 commit comments