Skip to content

Commit af0779c

Browse files
authored
Merge pull request #278 from Sh3Rm4n/serial-write
Implement fmt::Write for serial
2 parents 879bcb9 + 6ee536b commit af0779c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
6565
source for the systemclock. Also `Clocks::pllclk()` was introduced to be able
6666
to check, whether PLL is used.
6767
- Add unsafe peripheral function to access underlying peripheral ([#277])
68+
- Implement `fmt::Write` for `Serial` ([#278])
69+
- This allowes using `writeln!` in combination with `Serial`.
6870

6971
[`enumset`]: https://crates.io/crates/enumset
7072

@@ -470,6 +472,7 @@ let clocks = rcc
470472
[defmt]: https://github.com/knurling-rs/defmt
471473
[filter]: https://defmt.ferrous-systems.com/filtering.html
472474

475+
[#278]: https://github.com/stm32-rs/stm32f3xx-hal/pull/278
473476
[#277]: https://github.com/stm32-rs/stm32f3xx-hal/pull/277
474477
[#273]: https://github.com/stm32-rs/stm32f3xx-hal/pull/273
475478
[#271]: https://github.com/stm32-rs/stm32f3xx-hal/pull/271

src/serial.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use core::{
1515

1616
use crate::{
1717
gpio::{gpioa, gpiob, gpioc, AF7},
18-
hal::{blocking, serial},
18+
hal::{blocking, serial, serial::Write},
1919
pac::{
2020
self,
2121
rcc::cfgr3::USART1SW_A,
@@ -926,7 +926,7 @@ where
926926
}
927927
}
928928

929-
impl<Usart, Tx, Rx> serial::Write<u8> for Serial<Usart, (Tx, Rx)>
929+
impl<Usart, Pins> serial::Write<u8> for Serial<Usart, Pins>
930930
where
931931
Usart: Instance,
932932
{
@@ -954,6 +954,17 @@ where
954954
}
955955
}
956956

957+
impl<Usart, Pins> fmt::Write for Serial<Usart, Pins>
958+
where
959+
Serial<Usart, Pins>: serial::Write<u8>,
960+
{
961+
fn write_str(&mut self, s: &str) -> fmt::Result {
962+
s.bytes()
963+
.try_for_each(|c| nb::block!(self.write(c)))
964+
.map_err(|_| fmt::Error)
965+
}
966+
}
967+
957968
impl<USART, TX, RX> blocking::serial::write::Default<u8> for Serial<USART, (TX, RX)> where
958969
USART: Instance
959970
{
@@ -994,6 +1005,17 @@ where
9941005
}
9951006
}
9961007

1008+
impl<Usart, Pin> fmt::Write for Tx<Usart, Pin>
1009+
where
1010+
Tx<Usart, Pin>: serial::Write<u8>,
1011+
{
1012+
fn write_str(&mut self, s: &str) -> fmt::Result {
1013+
s.bytes()
1014+
.try_for_each(|c| nb::block!(self.write(c)))
1015+
.map_err(|_| fmt::Error)
1016+
}
1017+
}
1018+
9971019
impl<Usart, Pin> Rx<Usart, Pin>
9981020
where
9991021
Usart: Instance + Dma,

0 commit comments

Comments
 (0)