11use core:: marker;
22
3- ///This trait shows that register has `read` method
3+ /// Trait implemented by readable registers to enable the `read` method.
44///
5- ///Registers marked with `Writable` can be also `modify`'ed
5+ /// Registers marked with `Writable` can be also `modify`'ed.
66pub trait Readable { }
77
8- ///This trait shows that register has `write`, `write_with_zero` and `reset` method
8+ /// Trait implemented by writeable registers.
99///
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.
1113pub trait Writable { }
1214
13- ///Reset value of the register
15+ /// Reset value of the register.
1416///
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.
1719pub trait ResetValue {
18- ///Register size
20+ /// Raw register type (`u8`, `u16`, `u32`, ...).
1921 type Type ;
20- ///Reset value of the register
22+
23+ /// Reset value of the register.
2124 fn reset_value ( ) -> Self :: Type ;
2225}
2326
24- ///This structure provides volatile access to register
27+ /// This structure provides volatile access to registers.
2528pub struct Reg < U , REG > {
2629 register : vcell:: VolatileCell < U > ,
2730 _marker : marker:: PhantomData < REG > ,
2831}
2932
30- unsafe impl < U : Send , REG > Send for Reg < U , REG > { }
33+ unsafe impl < U : Send , REG > Send for Reg < U , REG > { }
3134
3235impl < U , REG > Reg < U , REG >
3336where
3437 Self : Readable ,
35- U : Copy
38+ U : Copy ,
3639{
37- ///Reads the contents of `Readable` register
40+ /// Reads the contents of a `Readable` register.
3841 ///
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+ /// ```
4952 #[ inline( always) ]
5053 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+ }
5258 }
5359}
5460
5561impl < U , REG > Reg < U , REG >
5662where
57- Self : ResetValue < Type = U > + Writable ,
63+ Self : ResetValue < Type = U > + Writable ,
5864 U : Copy ,
5965{
60- ///Writes the reset value to `Writable` register
66+ /// Writes the reset value to `Writable` register.
6167 ///
62- ///Resets the register to its initial state
68+ /// Resets the register to its initial state.
6369 #[ inline( always) ]
6470 pub fn reset ( & self ) {
6571 self . register . set ( Self :: reset_value ( ) )
@@ -68,47 +74,59 @@ where
6874
6975impl < U , REG > Reg < U , REG >
7076where
71- Self : ResetValue < Type = U > + Writable ,
72- U : Copy
77+ Self : ResetValue < Type = U > + Writable ,
78+ U : Copy ,
7379{
74- ///Writes bits to `Writable` register
80+ /// Writes bits to a `Writable` register.
7581 ///
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.
8995 #[ inline( always) ]
9096 pub fn write < F > ( & self , f : F )
9197 where
92- F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self >
98+ F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self > ,
9399 {
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+ ) ;
95107 }
96108}
97109
98110impl < U , REG > Reg < U , REG >
99111where
100112 Self : Writable ,
101- U : Copy + Default
113+ U : Copy + Default ,
102114{
103- ///Writes Zero to `Writable` register
115+ /// Writes 0 to a `Writable` register.
104116 ///
105- ///Similar to `write`, but unused bits will contain 0.
117+ /// Similar to `write`, but unused bits will contain 0.
106118 #[ inline( always) ]
107119 pub fn write_with_zero < F > ( & self , f : F )
108120 where
109- F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self >
121+ F : FnOnce ( & mut W < U , Self > ) -> & mut W < U , Self > ,
110122 {
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+ ) ;
112130 }
113131}
114132
@@ -117,55 +135,68 @@ where
117135 Self : Readable + Writable ,
118136 U : Copy ,
119137{
120- ///Modifies the contents of the register
138+ /// Modifies the contents of the register by reading and then writing it.
121139 ///
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`.
137155 #[ inline( always) ]
138156 pub fn modify < F > ( & self , f : F )
139157 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 > ,
141159 {
142160 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+ ) ;
144174 }
145175}
146176
147- ///Register/field reader
177+ /// Register/field reader.
148178///
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.
151181pub struct R < U , T > {
152182 pub ( crate ) bits : U ,
153183 _reg : marker:: PhantomData < T > ,
154184}
155185
156186impl < U , T > R < U , T >
157187where
158- U : Copy
188+ U : Copy ,
159189{
160- ///Create new instance of reader
190+ /// Creates a new instance of the reader.
161191 #[ inline( always) ]
162192 pub ( crate ) fn new ( bits : U ) -> Self {
163193 Self {
164194 bits,
165195 _reg : marker:: PhantomData ,
166196 }
167197 }
168- ///Read raw bits from register/field
198+
199+ /// Reads raw bits from register/field.
169200 #[ inline( always) ]
170201 pub fn bits ( & self ) -> U {
171202 self . bits
@@ -175,7 +206,7 @@ where
175206impl < U , T , FI > PartialEq < FI > for R < U , T >
176207where
177208 U : PartialEq ,
178- FI : Copy + Into < U >
209+ FI : Copy + Into < U > ,
179210{
180211 #[ inline( always) ]
181212 fn eq ( & self , other : & FI ) -> bool {
@@ -184,46 +215,46 @@ where
184215}
185216
186217impl < FI > R < bool , FI > {
187- ///Value of the field as raw bits
218+ /// Value of the field as raw bits.
188219 #[ inline( always) ]
189220 pub fn bit ( & self ) -> bool {
190221 self . bits
191222 }
192- ///Returns `true` if the bit is clear (0)
223+ /// Returns `true` if the bit is clear (0).
193224 #[ inline( always) ]
194225 pub fn bit_is_clear ( & self ) -> bool {
195226 !self . bit ( )
196227 }
197- ///Returns `true` if the bit is set (1)
228+ /// Returns `true` if the bit is set (1).
198229 #[ inline( always) ]
199230 pub fn bit_is_set ( & self ) -> bool {
200231 self . bit ( )
201232 }
202233}
203234
204- ///Register writer
235+ /// Register writer.
205236///
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.
207238pub struct W < U , REG > {
208239 ///Writable bits
209240 pub ( crate ) bits : U ,
210241 _reg : marker:: PhantomData < REG > ,
211242}
212243
213244impl < U , REG > W < U , REG > {
214- ///Writes raw bits to the register
245+ /// Writes raw bits to the register.
215246 #[ inline( always) ]
216247 pub unsafe fn bits ( & mut self , bits : U ) -> & mut Self {
217248 self . bits = bits;
218249 self
219250 }
220251}
221252
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 ) ]
224255pub enum Variant < U , T > {
225- ///Expected variant
256+ /// Expected variant.
226257 Val ( T ) ,
227- ///Raw bits
258+ /// Raw bits.
228259 Res ( U ) ,
229260}
0 commit comments