Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fmt::Write for serial #278

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
source for the systemclock. Also `Clocks::pllclk()` was introduced to be able
to check, whether PLL is used.
- Add unsafe peripheral function to access underlying peripheral ([#277])
- Implement `fmt::Write` for `Serial` ([#278])
- This allowes using `writeln!` in combination with `Serial`.

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

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

[#278]: https://github.com/stm32-rs/stm32f3xx-hal/pull/278
[#277]: https://github.com/stm32-rs/stm32f3xx-hal/pull/277
[#273]: https://github.com/stm32-rs/stm32f3xx-hal/pull/273
[#271]: https://github.com/stm32-rs/stm32f3xx-hal/pull/271
Expand Down
26 changes: 24 additions & 2 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use core::{

use crate::{
gpio::{gpioa, gpiob, gpioc, AF7},
hal::{blocking, serial},
hal::{blocking, serial, serial::Write},
pac::{
self,
rcc::cfgr3::USART1SW_A,
Expand Down Expand Up @@ -926,7 +926,7 @@ where
}
}

impl<Usart, Tx, Rx> serial::Write<u8> for Serial<Usart, (Tx, Rx)>
impl<Usart, Pins> serial::Write<u8> for Serial<Usart, Pins>
where
Usart: Instance,
{
Expand Down Expand Up @@ -954,6 +954,17 @@ where
}
}

impl<Usart, Pins> fmt::Write for Serial<Usart, Pins>
where
Serial<Usart, Pins>: serial::Write<u8>,
{
fn write_str(&mut self, s: &str) -> fmt::Result {
s.bytes()
.try_for_each(|c| nb::block!(self.write(c)))
.map_err(|_| fmt::Error)
}
}

impl<USART, TX, RX> blocking::serial::write::Default<u8> for Serial<USART, (TX, RX)> where
USART: Instance
{
Expand Down Expand Up @@ -994,6 +1005,17 @@ where
}
}

impl<Usart, Pin> fmt::Write for Tx<Usart, Pin>
where
Tx<Usart, Pin>: serial::Write<u8>,
{
fn write_str(&mut self, s: &str) -> fmt::Result {
s.bytes()
.try_for_each(|c| nb::block!(self.write(c)))
.map_err(|_| fmt::Error)
}
}

impl<Usart, Pin> Rx<Usart, Pin>
where
Usart: Instance + Dma,
Expand Down