Skip to content

Commit 2b74dd8

Browse files
authored
Merge pull request #726 from stm32-rs/nb2
nb::serial
2 parents c568bb9 + 40c6d91 commit 2b74dd8

File tree

4 files changed

+109
-2
lines changed

4 files changed

+109
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1212
- shorten gpio ptr access
1313
- bump embedded-hal to `1.0` (no more RC!)
1414
- make `embedded-hal` `1.0` main implementation
15+
- add `embedded-hal-nb::serial`
1516

1617
## [v0.19.0] - 2023-12-11
1718

src/prelude.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
pub use embedded_hal::delay::DelayNs as _;
4040
pub use embedded_hal_02::adc::OneShot as _embedded_hal_adc_OneShot;
4141
pub use embedded_hal_02::blocking::serial::Write as _embedded_hal_blocking_serial_Write;
42-
pub use embedded_hal_02::serial::Read as _embedded_hal_serial_Read;
43-
pub use embedded_hal_02::serial::Write as _embedded_hal_serial_Write;
4442
pub use embedded_hal_02::Capture as _embedded_hal_Capture;
4543
pub use embedded_hal_02::Pwm as _embedded_hal_Pwm;
4644
pub use embedded_hal_02::Qei as _embedded_hal_Qei;
45+
pub use embedded_hal_nb::serial::Read as _embedded_hal_serial_nb_Read;
46+
pub use embedded_hal_nb::serial::Write as _embedded_hal_serial_nb_Write;
4747
pub use fugit::ExtU32 as _fugit_ExtU32;
4848
pub use fugit::RateExtU32 as _fugit_RateExtU32;
4949

src/serial.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use core::marker::PhantomData;
1818

1919
mod hal_02;
20+
mod hal_1;
2021

2122
pub(crate) mod uart_impls;
2223
pub use uart_impls::Instance;

src/serial/hal_1.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
mod nb {
2+
use core::ops::Deref;
3+
4+
use super::super::{Error, Instance, RegisterBlockImpl, Rx, Serial, Tx};
5+
use embedded_hal_nb::serial::{ErrorKind, Read, Write};
6+
7+
impl embedded_hal_nb::serial::Error for Error {
8+
fn kind(&self) -> ErrorKind {
9+
match self {
10+
Error::Overrun => ErrorKind::Overrun,
11+
Error::FrameFormat => ErrorKind::FrameFormat,
12+
Error::Parity => ErrorKind::Parity,
13+
Error::Noise => ErrorKind::Noise,
14+
Error::Other => ErrorKind::Other,
15+
}
16+
}
17+
}
18+
19+
impl<USART: Instance, WORD> embedded_hal_nb::serial::ErrorType for Serial<USART, WORD> {
20+
type Error = Error;
21+
}
22+
impl<USART: Instance, WORD> embedded_hal_nb::serial::ErrorType for Rx<USART, WORD> {
23+
type Error = Error;
24+
}
25+
impl<USART: Instance, WORD> embedded_hal_nb::serial::ErrorType for Tx<USART, WORD> {
26+
type Error = Error;
27+
}
28+
29+
impl<USART: Instance, WORD: Copy> Read<WORD> for Serial<USART, WORD>
30+
where
31+
Rx<USART, WORD>: Read<WORD, Error = Error>,
32+
{
33+
fn read(&mut self) -> nb::Result<WORD, Self::Error> {
34+
self.rx.read()
35+
}
36+
}
37+
38+
impl<USART: Instance> Read<u8> for Rx<USART, u8>
39+
where
40+
<USART as Instance>::RegisterBlock: RegisterBlockImpl,
41+
{
42+
fn read(&mut self) -> nb::Result<u8, Self::Error> {
43+
unsafe { (*USART::ptr()).read_u8() }
44+
}
45+
}
46+
47+
/// Reads 9-bit words from the UART/USART
48+
///
49+
/// If the UART/USART was configured with `WordLength::DataBits9`, the returned value will contain
50+
/// 9 received data bits and all other bits set to zero. Otherwise, the returned value will contain
51+
/// 8 received data bits and all other bits set to zero.
52+
impl<USART: Instance> Read<u16> for Rx<USART, u16>
53+
where
54+
<USART as Instance>::RegisterBlock: RegisterBlockImpl,
55+
{
56+
fn read(&mut self) -> nb::Result<u16, Self::Error> {
57+
unsafe { (*USART::ptr()).read_u16() }
58+
}
59+
}
60+
61+
impl<USART: Instance, WORD: Copy> Write<WORD> for Serial<USART, WORD>
62+
where
63+
Tx<USART, WORD>: Write<WORD, Error = Error>,
64+
{
65+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
66+
self.tx.flush()
67+
}
68+
69+
fn write(&mut self, byte: WORD) -> nb::Result<(), Self::Error> {
70+
self.tx.write(byte)
71+
}
72+
}
73+
74+
impl<USART: Instance> Write<u8> for Tx<USART, u8>
75+
where
76+
<USART as Instance>::RegisterBlock: RegisterBlockImpl,
77+
USART: Deref<Target = <USART as Instance>::RegisterBlock>,
78+
{
79+
fn write(&mut self, word: u8) -> nb::Result<(), Self::Error> {
80+
self.usart.write_u8(word)
81+
}
82+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
83+
self.usart.flush()
84+
}
85+
}
86+
87+
/// Writes 9-bit words to the UART/USART
88+
///
89+
/// If the UART/USART was configured with `WordLength::DataBits9`, the 9 least significant bits will
90+
/// be transmitted and the other 7 bits will be ignored. Otherwise, the 8 least significant bits
91+
/// will be transmitted and the other 8 bits will be ignored.
92+
impl<USART: Instance> Write<u16> for Tx<USART, u16>
93+
where
94+
<USART as Instance>::RegisterBlock: RegisterBlockImpl,
95+
USART: Deref<Target = <USART as Instance>::RegisterBlock>,
96+
{
97+
fn write(&mut self, word: u16) -> nb::Result<(), Self::Error> {
98+
self.usart.write_u16(word)
99+
}
100+
101+
fn flush(&mut self) -> nb::Result<(), Self::Error> {
102+
self.usart.flush()
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)