9
9
{
10
10
type Error = Error ;
11
11
12
- fn read ( & mut self ) -> nb:: Result < WORD , Error > {
12
+ fn read ( & mut self ) -> nb:: Result < WORD , Self :: Error > {
13
13
self . rx . read ( )
14
14
}
15
15
}
@@ -18,8 +18,7 @@ mod nb {
18
18
type Error = Error ;
19
19
20
20
fn read ( & mut self ) -> nb:: Result < u8 , Self :: Error > {
21
- // Delegate to the Read<u16> implementation, then truncate to 8 bits
22
- Rx :: < USART , u16 > :: new ( ) . read ( ) . map ( |word16| word16 as u8 )
21
+ self . read ( )
23
22
}
24
23
}
25
24
@@ -31,33 +30,8 @@ mod nb {
31
30
impl < USART : Instance > Read < u16 > for Rx < USART , u16 > {
32
31
type Error = Error ;
33
32
34
- fn read ( & mut self ) -> nb:: Result < u16 , Error > {
35
- // NOTE(unsafe) atomic read with no side effects
36
- let sr = unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) } ;
37
-
38
- // Any error requires the dr to be read to clear
39
- if sr. pe ( ) . bit_is_set ( )
40
- || sr. fe ( ) . bit_is_set ( )
41
- || sr. nf ( ) . bit_is_set ( )
42
- || sr. ore ( ) . bit_is_set ( )
43
- {
44
- unsafe { ( * USART :: ptr ( ) ) . dr . read ( ) } ;
45
- }
46
-
47
- Err ( if sr. pe ( ) . bit_is_set ( ) {
48
- Error :: Parity . into ( )
49
- } else if sr. fe ( ) . bit_is_set ( ) {
50
- Error :: Framing . into ( )
51
- } else if sr. nf ( ) . bit_is_set ( ) {
52
- Error :: Noise . into ( )
53
- } else if sr. ore ( ) . bit_is_set ( ) {
54
- Error :: Overrun . into ( )
55
- } else if sr. rxne ( ) . bit_is_set ( ) {
56
- // NOTE(unsafe) atomic read from stateless register
57
- return Ok ( unsafe { & * USART :: ptr ( ) } . dr . read ( ) . dr ( ) . bits ( ) ) ;
58
- } else {
59
- nb:: Error :: WouldBlock
60
- } )
33
+ fn read ( & mut self ) -> nb:: Result < u16 , Self :: Error > {
34
+ self . read ( )
61
35
}
62
36
}
63
37
@@ -81,13 +55,11 @@ mod nb {
81
55
type Error = Error ;
82
56
83
57
fn write ( & mut self , word : u8 ) -> nb:: Result < ( ) , Self :: Error > {
84
- // Delegate to u16 version
85
- Tx :: < USART , u16 > :: new ( ) . write ( u16:: from ( word) )
58
+ self . write ( word)
86
59
}
87
60
88
61
fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
89
- // Delegate to u16 version
90
- Tx :: < USART , u16 > :: new ( ) . flush ( )
62
+ self . flush ( )
91
63
}
92
64
}
93
65
@@ -100,59 +72,28 @@ mod nb {
100
72
type Error = Error ;
101
73
102
74
fn write ( & mut self , word : u16 ) -> nb:: Result < ( ) , Self :: Error > {
103
- // NOTE(unsafe) atomic read with no side effects
104
- let sr = unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) } ;
105
-
106
- if sr. txe ( ) . bit_is_set ( ) {
107
- // NOTE(unsafe) atomic write to stateless register
108
- unsafe { & * USART :: ptr ( ) } . dr . write ( |w| w. dr ( ) . bits ( word) ) ;
109
- Ok ( ( ) )
110
- } else {
111
- Err ( nb:: Error :: WouldBlock )
112
- }
75
+ self . write ( word)
113
76
}
114
77
115
78
fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
116
- // NOTE(unsafe) atomic read with no side effects
117
- let sr = unsafe { ( * USART :: ptr ( ) ) . sr . read ( ) } ;
118
-
119
- if sr. tc ( ) . bit_is_set ( ) {
120
- Ok ( ( ) )
121
- } else {
122
- Err ( nb:: Error :: WouldBlock )
123
- }
79
+ self . flush ( )
124
80
}
125
81
}
126
82
}
127
83
128
84
mod blocking {
129
85
use super :: super :: { Error , Instance , Serial , Tx } ;
130
- use embedded_hal:: { blocking:: serial:: Write , serial } ;
86
+ use embedded_hal:: blocking:: serial:: Write ;
131
87
132
88
impl < USART : Instance > Write < u8 > for Tx < USART , u8 > {
133
89
type Error = Error ;
134
90
135
91
fn bwrite_all ( & mut self , bytes : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
136
- for & b in bytes {
137
- loop {
138
- match <Self as serial:: Write < u8 > >:: write ( self , b) {
139
- Err ( nb:: Error :: WouldBlock ) => continue ,
140
- Err ( nb:: Error :: Other ( err) ) => return Err ( err) ,
141
- Ok ( ( ) ) => break ,
142
- }
143
- }
144
- }
145
- Ok ( ( ) )
92
+ self . bwrite_all ( bytes)
146
93
}
147
94
148
95
fn bflush ( & mut self ) -> Result < ( ) , Self :: Error > {
149
- loop {
150
- match <Self as serial:: Write < u8 > >:: flush ( self ) {
151
- Ok ( ( ) ) => return Ok ( ( ) ) ,
152
- Err ( nb:: Error :: WouldBlock ) => continue ,
153
- Err ( nb:: Error :: Other ( err) ) => return Err ( err) ,
154
- }
155
- }
96
+ self . bflush ( )
156
97
}
157
98
}
158
99
@@ -171,35 +112,20 @@ mod blocking {
171
112
impl < USART : Instance > Write < u16 > for Tx < USART , u16 > {
172
113
type Error = Error ;
173
114
174
- fn bwrite_all ( & mut self , buffer : & [ u16 ] ) -> Result < ( ) , Self :: Error > {
175
- for & b in buffer {
176
- loop {
177
- match <Self as serial:: Write < u16 > >:: write ( self , b) {
178
- Err ( nb:: Error :: WouldBlock ) => continue ,
179
- Err ( nb:: Error :: Other ( err) ) => return Err ( err) ,
180
- Ok ( ( ) ) => break ,
181
- }
182
- }
183
- }
184
- Ok ( ( ) )
115
+ fn bwrite_all ( & mut self , slice : & [ u16 ] ) -> Result < ( ) , Self :: Error > {
116
+ self . bwrite_all ( slice)
185
117
}
186
118
187
119
fn bflush ( & mut self ) -> Result < ( ) , Self :: Error > {
188
- loop {
189
- match <Self as serial:: Write < u16 > >:: flush ( self ) {
190
- Ok ( ( ) ) => return Ok ( ( ) ) ,
191
- Err ( nb:: Error :: WouldBlock ) => continue ,
192
- Err ( nb:: Error :: Other ( err) ) => return Err ( err) ,
193
- }
194
- }
120
+ self . bflush ( )
195
121
}
196
122
}
197
123
198
124
impl < USART : Instance , PINS > Write < u16 > for Serial < USART , PINS , u16 > {
199
125
type Error = Error ;
200
126
201
- fn bwrite_all ( & mut self , bytes : & [ u16 ] ) -> Result < ( ) , Self :: Error > {
202
- self . tx . bwrite_all ( bytes )
127
+ fn bwrite_all ( & mut self , slice : & [ u16 ] ) -> Result < ( ) , Self :: Error > {
128
+ self . tx . bwrite_all ( slice )
203
129
}
204
130
205
131
fn bflush ( & mut self ) -> Result < ( ) , Self :: Error > {
0 commit comments