1
1
use core:: marker;
2
2
3
- ///This trait shows that register has `read` method
3
+ /// Trait implemented by readable registers to enable the `read` method.
4
4
///
5
- ///Registers marked with `Writable` can be also `modify`'ed
5
+ /// Registers marked with `Writable` can be also `modify`'ed.
6
6
pub trait Readable { }
7
7
8
- ///This trait shows that register has `write`, `write_with_zero` and `reset` method
8
+ /// Trait implemented by writeable registers.
9
9
///
10
- ///Registers marked with `Readable` can be also `modify`'ed
10
+ /// This enables the `write`, `write_with_zero` and `reset` methods.
11
+ ///
12
+ /// Registers marked with `Readable` can be also `modify`'ed.
11
13
pub trait Writable { }
12
14
13
- ///Reset value of the register
15
+ /// Reset value of the register.
14
16
///
15
- ///This value is initial value for `write` method.
16
- ///It can be also directly writed to register by `reset` method.
17
+ /// This value is the initial value for the `write` method. It can also be directly written to the
18
+ /// register by using the `reset` method.
17
19
pub trait ResetValue {
18
- ///Register size
20
+ /// Raw register type (`u8`, `u16`, `u32`, ...).
19
21
type Type ;
20
- ///Reset value of the register
22
+
23
+ /// Reset value of the register.
21
24
fn reset_value ( ) -> Self :: Type ;
22
25
}
23
26
24
- ///This structure provides volatile access to register
27
+ /// This structure provides volatile access to registers.
25
28
pub struct Reg < U , REG > {
26
29
register : vcell:: VolatileCell < U > ,
27
30
_marker : marker:: PhantomData < REG > ,
28
31
}
29
32
30
- unsafe impl < U : Send , REG > Send for Reg < U , REG > { }
33
+ unsafe impl < U : Send , REG > Send for Reg < U , REG > { }
31
34
32
35
impl < U , REG > Reg < U , REG >
33
36
where
34
37
Self : Readable ,
35
- U : Copy
38
+ U : Copy ,
36
39
{
37
- ///Reads the contents of `Readable` register
40
+ /// Reads the contents of a `Readable` register.
38
41
///
39
- ///You can read the contents of a register in such way :
40
- ///```ignore
41
- ///let bits = periph.reg.read().bits();
42
- ///```
43
- ///or get the content of a particular field of a register.
44
- ///```ignore
45
- ///let reader = periph.reg.read();
46
- ///let bits = reader.field1().bits();
47
- ///let flag = reader.field2().bit_is_set();
48
- ///```
42
+ /// You can read the raw contents of a register by using `bits` :
43
+ /// ```ignore
44
+ /// let bits = periph.reg.read().bits();
45
+ /// ```
46
+ /// or get the content of a particular field of a register:
47
+ /// ```ignore
48
+ /// let reader = periph.reg.read();
49
+ /// let bits = reader.field1().bits();
50
+ /// let flag = reader.field2().bit_is_set();
51
+ /// ```
49
52
#[ inline( always) ]
50
53
pub fn read ( & self ) -> R < U , Self > {
51
- R { bits : self . register . get ( ) , _reg : marker:: PhantomData }
54
+ R {
55
+ bits : self . register . get ( ) ,
56
+ _reg : marker:: PhantomData ,
57
+ }
52
58
}
53
59
}
54
60
55
61
impl < U , REG > Reg < U , REG >
56
62
where
57
- Self : ResetValue < Type = U > + Writable ,
63
+ Self : ResetValue < Type = U > + Writable ,
58
64
U : Copy ,
59
65
{
60
- ///Writes the reset value to `Writable` register
66
+ /// Writes the reset value to `Writable` register.
61
67
///
62
- ///Resets the register to its initial state
68
+ /// Resets the register to its initial state.
63
69
#[ inline( always) ]
64
70
pub fn reset ( & self ) {
65
71
self . register . set ( Self :: reset_value ( ) )
@@ -68,47 +74,59 @@ where
68
74
69
75
impl < U , REG > Reg < U , REG >
70
76
where
71
- Self : ResetValue < Type = U > + Writable ,
72
- U : Copy
77
+ Self : ResetValue < Type = U > + Writable ,
78
+ U : Copy ,
73
79
{
74
- ///Writes bits to `Writable` register
80
+ /// Writes bits to a `Writable` register.
75
81
///
76
- ///You can write raw bits into a register:
77
- ///```ignore
78
- ///periph.reg.write(|w| unsafe { w.bits(rawbits) });
79
- ///```
80
- ///or write only the fields you need:
81
- ///```ignore
82
- ///periph.reg.write(|w| w
83
- /// .field1().bits(newfield1bits)
84
- /// .field2().set_bit()
85
- /// .field3().variant(VARIANT)
86
- ///);
87
- ///```
88
- ///Other fields will have reset value.
82
+ /// You can write raw bits into a register:
83
+ /// ```ignore
84
+ /// periph.reg.write(|w| unsafe { w.bits(rawbits) });
85
+ /// ```
86
+ /// or write only the fields you need:
87
+ /// ```ignore
88
+ /// periph.reg.write(|w| w
89
+ /// .field1().bits(newfield1bits)
90
+ /// .field2().set_bit()
91
+ /// .field3().variant(VARIANT)
92
+ /// );
93
+ /// ```
94
+ /// In the latter case, other fields will be set to their reset value.
89
95
#[ inline( always) ]
90
96
pub fn write < F > ( & self , f : F )
91
97
where
92
- F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self >
98
+ F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self > ,
93
99
{
94
- self . register . set ( f ( & mut W { bits : Self :: reset_value ( ) , _reg : marker:: PhantomData } ) . bits ) ;
100
+ self . register . set (
101
+ f ( & mut W {
102
+ bits : Self :: reset_value ( ) ,
103
+ _reg : marker:: PhantomData ,
104
+ } )
105
+ . bits ,
106
+ ) ;
95
107
}
96
108
}
97
109
98
110
impl < U , REG > Reg < U , REG >
99
111
where
100
112
Self : Writable ,
101
- U : Copy + Default
113
+ U : Copy + Default ,
102
114
{
103
- ///Writes Zero to `Writable` register
115
+ /// Writes 0 to a `Writable` register.
104
116
///
105
- ///Similar to `write`, but unused bits will contain 0.
117
+ /// Similar to `write`, but unused bits will contain 0.
106
118
#[ inline( always) ]
107
119
pub fn write_with_zero < F > ( & self , f : F )
108
120
where
109
- F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self >
121
+ F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self > ,
110
122
{
111
- self . register . set ( f ( & mut W { bits : U :: default ( ) , _reg : marker:: PhantomData } ) . bits ) ;
123
+ self . register . set (
124
+ f ( & mut W {
125
+ bits : U :: default ( ) ,
126
+ _reg : marker:: PhantomData ,
127
+ } )
128
+ . bits ,
129
+ ) ;
112
130
}
113
131
}
114
132
@@ -117,55 +135,68 @@ where
117
135
Self : Readable + Writable ,
118
136
U : Copy ,
119
137
{
120
- ///Modifies the contents of the register
138
+ /// Modifies the contents of the register by reading and then writing it.
121
139
///
122
- ///E.g. to do a read-modify-write sequence to change parts of a register:
123
- ///```ignore
124
- ///periph.reg.modify(|r, w| unsafe { w.bits(
125
- /// r.bits() | 3
126
- ///) });
127
- ///```
128
- ///or
129
- ///```ignore
130
- ///periph.reg.modify(|_, w| w
131
- /// .field1().bits(newfield1bits)
132
- /// .field2().set_bit()
133
- /// .field3().variant(VARIANT)
134
- ///);
135
- ///```
136
- ///Other fields will have value they had before call `modify`.
140
+ /// E.g. to do a read-modify-write sequence to change parts of a register:
141
+ /// ```ignore
142
+ /// periph.reg.modify(|r, w| unsafe { w.bits(
143
+ /// r.bits() | 3
144
+ /// ) });
145
+ /// ```
146
+ /// or
147
+ /// ```ignore
148
+ /// periph.reg.modify(|_, w| w
149
+ /// .field1().bits(newfield1bits)
150
+ /// .field2().set_bit()
151
+ /// .field3().variant(VARIANT)
152
+ /// );
153
+ /// ```
154
+ /// Other fields will have the value they had before the call to `modify`.
137
155
#[ inline( always) ]
138
156
pub fn modify < F > ( & self , f : F )
139
157
where
140
- for < ' w > F : FnOnce ( & R < U , Self > , & ' w mut W < U , Self > ) -> & ' w mut W < U , Self >
158
+ for < ' w > F : FnOnce ( & R < U , Self > , & ' w mut W < U , Self > ) -> & ' w mut W < U , Self > ,
141
159
{
142
160
let bits = self . register . get ( ) ;
143
- self . register . set ( f ( & R { bits, _reg : marker:: PhantomData } , & mut W { bits, _reg : marker:: PhantomData } ) . bits ) ;
161
+ self . register . set (
162
+ f (
163
+ & R {
164
+ bits,
165
+ _reg : marker:: PhantomData ,
166
+ } ,
167
+ & mut W {
168
+ bits,
169
+ _reg : marker:: PhantomData ,
170
+ } ,
171
+ )
172
+ . bits ,
173
+ ) ;
144
174
}
145
175
}
146
176
147
- ///Register/field reader
177
+ /// Register/field reader.
148
178
///
149
- ///Result of the [ `read`](Reg::read) method of a register.
150
- ///Also it can be used in the [`modify`](Reg::read) method
179
+ /// Result of the `read` methods of registers. Also used as a closure argument in the `modify`
180
+ /// method.
151
181
pub struct R < U , T > {
152
182
pub ( crate ) bits : U ,
153
183
_reg : marker:: PhantomData < T > ,
154
184
}
155
185
156
186
impl < U , T > R < U , T >
157
187
where
158
- U : Copy
188
+ U : Copy ,
159
189
{
160
- ///Create new instance of reader
190
+ /// Creates a new instance of the reader.
161
191
#[ inline( always) ]
162
192
pub ( crate ) fn new ( bits : U ) -> Self {
163
193
Self {
164
194
bits,
165
195
_reg : marker:: PhantomData ,
166
196
}
167
197
}
168
- ///Read raw bits from register/field
198
+
199
+ /// Reads raw bits from register/field.
169
200
#[ inline( always) ]
170
201
pub fn bits ( & self ) -> U {
171
202
self . bits
@@ -175,7 +206,7 @@ where
175
206
impl < U , T , FI > PartialEq < FI > for R < U , T >
176
207
where
177
208
U : PartialEq ,
178
- FI : Copy + Into < U >
209
+ FI : Copy + Into < U > ,
179
210
{
180
211
#[ inline( always) ]
181
212
fn eq ( & self , other : & FI ) -> bool {
@@ -184,46 +215,46 @@ where
184
215
}
185
216
186
217
impl < FI > R < bool , FI > {
187
- ///Value of the field as raw bits
218
+ /// Value of the field as raw bits.
188
219
#[ inline( always) ]
189
220
pub fn bit ( & self ) -> bool {
190
221
self . bits
191
222
}
192
- ///Returns `true` if the bit is clear (0)
223
+ /// Returns `true` if the bit is clear (0).
193
224
#[ inline( always) ]
194
225
pub fn bit_is_clear ( & self ) -> bool {
195
226
!self . bit ( )
196
227
}
197
- ///Returns `true` if the bit is set (1)
228
+ /// Returns `true` if the bit is set (1).
198
229
#[ inline( always) ]
199
230
pub fn bit_is_set ( & self ) -> bool {
200
231
self . bit ( )
201
232
}
202
233
}
203
234
204
- ///Register writer
235
+ /// Register writer.
205
236
///
206
- ///Used as an argument to the closures in the [ `write`](Reg::write) and [ `modify`](Reg::modify) methods of the register
237
+ /// Used as an argument to the closures in the `write` and `modify` methods of the register.
207
238
pub struct W < U , REG > {
208
239
///Writable bits
209
240
pub ( crate ) bits : U ,
210
241
_reg : marker:: PhantomData < REG > ,
211
242
}
212
243
213
244
impl < U , REG > W < U , REG > {
214
- ///Writes raw bits to the register
245
+ /// Writes raw bits to the register.
215
246
#[ inline( always) ]
216
247
pub unsafe fn bits ( & mut self , bits : U ) -> & mut Self {
217
248
self . bits = bits;
218
249
self
219
250
}
220
251
}
221
252
222
- ///Used if enumerated values cover not the whole range
223
- #[ derive( Clone , Copy , PartialEq ) ]
253
+ /// Used if enumerated values cover not the whole range.
254
+ #[ derive( Clone , Copy , PartialEq ) ]
224
255
pub enum Variant < U , T > {
225
- ///Expected variant
256
+ /// Expected variant.
226
257
Val ( T ) ,
227
- ///Raw bits
258
+ /// Raw bits.
228
259
Res ( U ) ,
229
260
}
0 commit comments