Skip to content

Commit d723ddc

Browse files
committed
io
1 parent e48b49d commit d723ddc

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- initial `embedded-io` support [#725]
13+
1014
### Changed
1115

1216
- Use `stm32f4-staging` until `stm32f4` is released [#706]
1317

1418
[#706]: https://github.com/stm32-rs/stm32f4xx-hal/pull/706
19+
[#725]: https://github.com/stm32-rs/stm32f4xx-hal/pull/725
1520

1621
## [v0.21.0] - 2024-05-30
1722

Diff for: Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ version = "1.0"
8181
[dependencies.embedded-hal-nb]
8282
version = "1.0"
8383

84+
[dependencies.embedded-io]
85+
version = "0.6.1"
86+
8487
[dependencies.stm32_i2s_v12x]
8588
version = "0.5.0"
8689
optional = true

Diff for: src/serial/hal_1.rs

+68
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)