4242{
4343 ///Reads the contents of `Readable` register
4444 ///
45- ///See [reading](https://rust-embedded.github.io/book/start/registers.html#reading) in book.
45+ ///You can read the contents of a register in such way:
46+ ///```ignore
47+ ///let bits = periph.reg.read().bits();
48+ ///```
49+ ///or get the content of a particular field of a register.
50+ ///```ignore
51+ ///let reader = periph.reg.read();
52+ ///let bits = reader.field1().bits();
53+ ///let flag = reader.field2().bit_is_set();
54+ ///```
4655 #[ inline( always) ]
4756 pub fn read ( & self ) -> R < U , Self > {
4857 R { bits : self . register . get ( ) , _reg : marker:: PhantomData }
5564 U : Copy ,
5665{
5766 ///Writes the reset value to `Writable` register
67+ ///
68+ ///Resets the register to its initial state
5869 #[ inline( always) ]
5970 pub fn reset ( & self ) {
6071 self . register . set ( Self :: reset_value ( ) )
6879{
6980 ///Writes bits to `Writable` register
7081 ///
71- ///See [writing](https://rust-embedded.github.io/book/start/registers.html#writing) in book.
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+ ///Other fields will have reset value.
7295 #[ inline( always) ]
7396 pub fn write < F > ( & self , f : F )
7497 where
84107 U : Copy + Default
85108{
86109 ///Writes Zero to `Writable` register
110+ ///
111+ ///Similar to `write`, but unused bits will contain 0.
87112 #[ inline( always) ]
88113 pub fn write_with_zero < F > ( & self , f : F )
89114 where
@@ -100,7 +125,21 @@ where
100125{
101126 ///Modifies the contents of the register
102127 ///
103- ///See [modifying](https://rust-embedded.github.io/book/start/registers.html#modifying) in book.
128+ ///E.g. to do a read-modify-write sequence to change parts of a register:
129+ ///```ignore
130+ ///periph.reg.modify(|r, w| unsafe { w.bits(
131+ /// r.bits() | 3
132+ ///) });
133+ ///```
134+ ///or
135+ ///```ignore
136+ ///periph.reg.modify(|_, w| w
137+ /// .field1().bits(newfield1bits)
138+ /// .field2().set_bit()
139+ /// .field3().variant(VARIANT)
140+ ///);
141+ ///```
142+ ///Other fields will have value they had before call `modify`.
104143 #[ inline( always) ]
105144 pub fn modify < F > ( & self , f : F )
106145 where
@@ -112,6 +151,9 @@ where
112151}
113152
114153///Register/field reader
154+ ///
155+ ///Result of the [`read`](Reg::read) method of a register.
156+ ///Also it can be used in the [`modify`](Reg::read) method
115157pub struct R < U , T > {
116158 pub ( crate ) bits : U ,
117159 _reg : marker:: PhantomData < T > ,
@@ -141,6 +183,7 @@ where
141183 U : PartialEq ,
142184 FI : ToBits < U >
143185{
186+ #[ inline( always) ]
144187 fn eq ( & self , other : & FI ) -> bool {
145188 self . bits . eq ( & other. _bits ( ) )
146189 }
@@ -165,16 +208,18 @@ impl<FI> R<bool, FI> {
165208}
166209
167210///Register writer
211+ ///
212+ ///Used as an argument to the closures in the [`write`](Reg::write) and [`modify`](Reg::modify) methods of the register
168213pub struct W < U , REG > {
169214 ///Writable bits
170- pub bits : U ,
215+ pub ( crate ) bits : U ,
171216 _reg : marker:: PhantomData < REG > ,
172217}
173218
174219impl < U , REG > W < U , REG > {
175220 ///Writes raw bits to the register
176221 #[ inline( always) ]
177- pub fn bits ( & mut self , bits : U ) -> & mut Self {
222+ pub unsafe fn bits ( & mut self , bits : U ) -> & mut Self {
178223 self . bits = bits;
179224 self
180225 }
0 commit comments