Skip to content

Commit a6d382d

Browse files
committed
io
1 parent 2b74dd8 commit a6d382d

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ version = "1.0"
6464
[dependencies.embedded-hal-nb]
6565
version = "1.0"
6666

67+
[dependencies.embedded-io]
68+
version = "0.6.1"
69+
6770
[dependencies.stm32_i2s_v12x]
6871
version = "0.5.0"
6972
optional = true

src/serial/hal_1.rs

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

0 commit comments

Comments
 (0)