Skip to content

Commit

Permalink
Merge pull request #278 from Sh3Rm4n/serial-write
Browse files Browse the repository at this point in the history
Implement fmt::Write for serial
  • Loading branch information
Sh3Rm4n committed Aug 16, 2021
2 parents 879bcb9 + 6ee536b commit af0779c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
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

0 comments on commit af0779c

Please sign in to comment.