@@ -9,6 +9,20 @@ pub fn render() -> Result<Vec<Tokens>> {
9
9
10
10
generic_items. push ( quote ! {
11
11
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
+ }
12
26
13
27
///Converting enumerated values to bits
14
28
pub trait ToBits <N > {
@@ -18,23 +32,105 @@ pub fn render() -> Result<Vec<Tokens>> {
18
32
} ) ;
19
33
20
34
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
+ }
25
47
}
26
48
27
- impl <U , T , FI > PartialEq < FI > for FR < U , T >
49
+ impl <U , REG > Reg < U , REG >
28
50
where
29
- U : PartialEq ,
30
- FI : ToBits < U >
51
+ Self : Readable + Deref < Target = VolatileCell < U >> ,
52
+ U : Copy
31
53
{
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( ) )
34
70
}
35
71
}
72
+ } ) ;
36
73
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 >
38
134
where
39
135
U : Copy
40
136
{
@@ -46,7 +142,7 @@ pub fn render() -> Result<Vec<Tokens>> {
46
142
_reg: marker:: PhantomData ,
47
143
}
48
144
}
49
- ///Read raw bits from field
145
+ ///Read raw bits from register/ field
50
146
#[ inline( always) ]
51
147
pub fn bits( & self ) -> U {
52
148
self . bits
@@ -55,7 +151,19 @@ pub fn render() -> Result<Vec<Tokens>> {
55
151
} ) ;
56
152
57
153
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 > {
59
167
///Value of the field as raw bits
60
168
#[ inline( always) ]
61
169
pub fn bit( & self ) -> bool {
@@ -74,6 +182,27 @@ pub fn render() -> Result<Vec<Tokens>> {
74
182
}
75
183
} ) ;
76
184
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
+
77
206
generic_items. push ( quote ! {
78
207
///Used if enumerated values cover not the whole range
79
208
#[ derive( Clone , Copy , PartialEq ) ]
@@ -88,7 +217,7 @@ pub fn render() -> Result<Vec<Tokens>> {
88
217
code. push ( quote ! {
89
218
#[ allow( unused_imports) ]
90
219
use generic:: * ;
91
- /// Common register and bit access and modify traits
220
+ ///Common register and bit access and modify traits
92
221
pub mod generic {
93
222
#( #generic_items) *
94
223
}
0 commit comments