@@ -95,3 +95,97 @@ mod nb {
95
95
}
96
96
}
97
97
}
98
+
99
+ mod io {
100
+ use core:: ops:: Deref ;
101
+
102
+ use super :: super :: { Error , Instance , RegisterBlockImpl , Rx , Serial , Tx } ;
103
+ use embedded_io:: Write ;
104
+
105
+ impl embedded_io:: Error for Error {
106
+ // TODO: fix error conversion
107
+ fn kind ( & self ) -> embedded_io:: ErrorKind {
108
+ embedded_io:: ErrorKind :: Other
109
+ }
110
+ }
111
+
112
+ impl < USART : Instance , WORD > embedded_io:: ErrorType for Serial < USART , WORD > {
113
+ type Error = Error ;
114
+ }
115
+
116
+ impl < USART : Instance , WORD > embedded_io:: ErrorType for Tx < USART , WORD > {
117
+ type Error = Error ;
118
+ }
119
+
120
+ impl < USART : Instance , WORD > embedded_io:: ErrorType for Rx < USART , WORD > {
121
+ type Error = Error ;
122
+ }
123
+
124
+ impl < USART : Instance > Write for Tx < USART , u8 >
125
+ where
126
+ <USART as Instance >:: RegisterBlock : RegisterBlockImpl ,
127
+ USART : Deref < Target = <USART as Instance >:: RegisterBlock > ,
128
+ {
129
+ /*
130
+ fn write(&mut self, bytes: &[u8]) -> Result<usize, Self::Error> {
131
+ let mut iter = bytes.iter();
132
+ let Some(first) = iter.next() else {
133
+ return Ok(0);
134
+ };
135
+ // block for first byte
136
+ self.usart.write_u8(*first)?;
137
+ let mut i = 1;
138
+
139
+ // write more bytes if it's possible
140
+ for byte in iter {
141
+ match self.usart.write_u8(*byte) {
142
+ Ok(_) => {
143
+ i += 1;
144
+ }
145
+ Err(nb::Error::WouldBlock) => {
146
+ return Ok(i);
147
+ }
148
+ Err(nb::Error::Other(e)) => {
149
+ return Err(e);
150
+ }
151
+ }
152
+ }
153
+ Ok(i)
154
+ }*/
155
+ fn write ( & mut self , bytes : & [ u8 ] ) -> Result < usize , Self :: Error > {
156
+ let mut i = 0 ;
157
+ for byte in bytes. iter ( ) {
158
+ match self . usart . write_u8 ( * byte) {
159
+ Ok ( _) => {
160
+ i += 1 ;
161
+ }
162
+ Err ( nb:: Error :: WouldBlock ) => {
163
+ return Ok ( i) ;
164
+ }
165
+ Err ( nb:: Error :: Other ( e) ) => {
166
+ return Err ( e) ;
167
+ }
168
+ }
169
+ }
170
+ Ok ( i)
171
+ }
172
+
173
+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
174
+ self . usart . bflush ( ) ?;
175
+ Ok ( ( ) )
176
+ }
177
+ }
178
+
179
+ impl < USART : Instance > Write for Serial < USART , u8 >
180
+ where
181
+ Tx < USART , u8 > : Write < Error = Error > ,
182
+ {
183
+ fn write ( & mut self , bytes : & [ u8 ] ) -> Result < usize , Self :: Error > {
184
+ self . tx . write ( bytes)
185
+ }
186
+
187
+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
188
+ self . tx . flush ( )
189
+ }
190
+ }
191
+ }
0 commit comments