@@ -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,101 @@ 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- bits: U ,
24- _reg: marker:: PhantomData <T >,
35+ ///Wrapper for registers
36+ pub struct Reg <REG >( pub ( crate ) REG ) ;
37+
38+ impl <U , REG > Reg <REG >
39+ where
40+ Self : Readable ,
41+ REG : Deref <Target =VolatileCell <U >>,
42+ U : Copy
43+ {
44+ ///Reads the contents of the register
45+ #[ inline( always) ]
46+ pub fn read( & self ) -> R <U , REG > {
47+ R :: new( ( * self . 0 ) . get( ) )
48+ }
2549 }
2650
27- impl <U , T , FI > PartialEq < FI > for FR < U , T >
51+ impl <U , REG > Reg < REG >
2852 where
29- U : PartialEq ,
30- FI : ToBits <U >
53+ Self : ResetValue <U > + Writable ,
54+ REG : Deref <Target =VolatileCell <U >>,
55+ U : Copy ,
3156 {
32- fn eq( & self , other: & FI ) -> bool {
33- self . bits. eq( & other. _bits( ) )
57+ ///Writes the reset value to the register
58+ #[ inline( always) ]
59+ pub fn reset( & self ) {
60+ ( * self . 0 ) . set( Self :: reset_value( ) )
3461 }
3562 }
63+ } ) ;
3664
37- impl <U , T > FR <U , T >
65+ generic_items. push ( quote ! {
66+ impl <U , REG > Reg <REG >
67+ where
68+ Self : ResetValue <U > + Writable ,
69+ REG : Deref <Target =VolatileCell <U >>,
70+ U : Copy
71+ {
72+ ///Writes to the register
73+ #[ inline( always) ]
74+ pub fn write<F >( & self , f: F )
75+ where
76+ F : FnOnce ( & mut W <U , REG >) -> & mut W <U , REG >
77+ {
78+
79+ ( * self . 0 ) . set( f( & mut W :: new( Self :: reset_value( ) ) ) . bits) ;
80+ }
81+ }
82+ } ) ;
83+
84+ generic_items. push ( quote ! {
85+ impl <U , REG > Reg <REG >
86+ where
87+ Self : Writable ,
88+ REG : Deref <Target =VolatileCell <U >>,
89+ U : Copy + Default
90+ {
91+ ///Writes Zero to the register
92+ #[ inline( always) ]
93+ pub fn write_with_zero<F >( & self , f: F )
94+ where
95+ F : FnOnce ( & mut W <U , REG >) -> & mut W <U , REG >
96+ {
97+
98+ ( * self . 0 ) . set( f( & mut W :: new( U :: default ( ) ) ) . bits) ;
99+ }
100+ }
101+ } ) ;
102+
103+ generic_items. push ( quote ! {
104+ impl <U , REG > Reg <REG >
105+ where
106+ Self : Readable + Writable ,
107+ REG : Deref <Target = VolatileCell <U >>,
108+ U : Copy ,
109+ {
110+ ///Modifies the contents of the register
111+ #[ inline( always) ]
112+ pub fn modify<F >( & self , f: F )
113+ where
114+ for <' w> F : FnOnce ( & R <U , REG >, & ' w mut W <U , REG >) -> & ' w mut W <U , REG >
115+ {
116+ let bits = ( * self . 0 ) . get( ) ;
117+ ( * self . 0 ) . set( f( & R :: new( bits) , & mut W :: new( bits) ) . bits) ;
118+ }
119+ }
120+ } ) ;
121+
122+ generic_items. push ( quote ! {
123+ ///Register/field reader
124+ pub struct R <U , T > {
125+ bits: U ,
126+ _reg: marker:: PhantomData <T >,
127+ }
128+
129+ impl <U , T > R <U , T >
38130 where
39131 U : Copy
40132 {
@@ -46,7 +138,7 @@ pub fn render() -> Result<Vec<Tokens>> {
46138 _reg: marker:: PhantomData ,
47139 }
48140 }
49- ///Read raw bits from field
141+ ///Read raw bits from register/ field
50142 #[ inline( always) ]
51143 pub fn bits( & self ) -> U {
52144 self . bits
@@ -55,7 +147,19 @@ pub fn render() -> Result<Vec<Tokens>> {
55147 } ) ;
56148
57149 generic_items. push ( quote ! {
58- impl <FI > FR <bool , FI > {
150+ impl <U , T , FI > PartialEq <FI > for R <U , T >
151+ where
152+ U : PartialEq ,
153+ FI : ToBits <U >
154+ {
155+ fn eq( & self , other: & FI ) -> bool {
156+ self . bits. eq( & other. _bits( ) )
157+ }
158+ }
159+ } ) ;
160+
161+ generic_items. push ( quote ! {
162+ impl <FI > R <bool , FI > {
59163 ///Value of the field as raw bits
60164 #[ inline( always) ]
61165 pub fn bit( & self ) -> bool {
@@ -74,10 +178,41 @@ pub fn render() -> Result<Vec<Tokens>> {
74178 }
75179 } ) ;
76180
181+ generic_items. push ( quote ! {
182+ ///Register writer
183+ pub struct W <U , REG > {
184+ ///Writable bits
185+ pub bits: U ,
186+ _reg: marker:: PhantomData <REG >,
187+ }
188+
189+ impl <U , REG > W <U , REG > {
190+ ///Create new instance of reader
191+ #[ inline( always) ]
192+ pub ( crate ) fn new( bits: U ) -> Self {
193+ Self {
194+ bits,
195+ _reg: marker:: PhantomData ,
196+ }
197+ }
198+ }
199+ } ) ;
200+
201+ generic_items. push ( quote ! {
202+ impl <U , REG > W <U , REG > {
203+ ///Writes raw bits to the register
204+ #[ inline( always) ]
205+ pub fn bits( & mut self , bits: U ) -> & mut Self {
206+ self . bits = bits;
207+ self
208+ }
209+ }
210+ } ) ;
211+
77212 code. push ( quote ! {
78213 #[ allow( unused_imports) ]
79214 use generic:: * ;
80- /// Common register and bit access and modify traits
215+ ///Common register and bit access and modify traits
81216 pub mod generic {
82217 #( #generic_items) *
83218 }
0 commit comments