Skip to content

Commit 6bbce44

Browse files
committed
io
1 parent 4ca768f commit 6bbce44

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Enable `sdio` for stm32f446
1414
- port LTDC implementation and example from stm32f7xx-hal [#731]
1515
- IrDA mode for USARTs
16+
- initial `embedded-io` support [#725]
1617

1718
### Changed
1819

@@ -23,6 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2324
[#706]: https://github.com/stm32-rs/stm32f4xx-hal/pull/706
2425
[#731]: https://github.com/stm32-rs/stm32f4xx-hal/pull/731
2526
[#758]: https://github.com/stm32-rs/stm32f4xx-hal/pull/758
27+
[#725]: https://github.com/stm32-rs/stm32f4xx-hal/pull/725
2628

2729
## [v0.21.0] - 2024-05-30
2830

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ version = "1.0"
8383
[dependencies.embedded-hal-nb]
8484
version = "1.0"
8585

86+
[dependencies.embedded-io]
87+
version = "0.6.1"
88+
8689
[dependencies.stm32_i2s_v12x]
8790
version = "0.5.0"
8891
optional = true

src/serial/hal_1.rs

Lines changed: 68 additions & 0 deletions
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)