@@ -95,3 +95,71 @@ mod nb {
9595 }
9696 }
9797}
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+ fn write(&mut self, bytes: &[u8]) -> Result<usize, Self::Error> {
130+ let mut i = 0;
131+ for byte in bytes.iter() {
132+ match self.usart.write_u8(*byte) {
133+ Ok(_) => {
134+ i += 1;
135+ }
136+ Err(nb::Error::WouldBlock) => {
137+ return Ok(i);
138+ }
139+ Err(nb::Error::Other(e)) => {
140+ return Err(e);
141+ }
142+ }
143+ }
144+ Ok(i)
145+ }
146+
147+ fn flush(&mut self) -> Result<(), Self::Error> {
148+ self.usart.bflush()?;
149+ Ok(())
150+ }
151+ }
152+
153+ impl<USART: Instance> Write for Serial<USART, u8>
154+ where
155+ Tx<USART, u8>: Write<Error = Error>,
156+ {
157+ fn write(&mut self, bytes: &[u8]) -> Result<usize, Self::Error> {
158+ self.tx.write(bytes)
159+ }
160+
161+ fn flush(&mut self) -> Result<(), Self::Error> {
162+ self.tx.flush()
163+ }
164+ }
165+ }
0 commit comments