Skip to content

Commit 76012d6

Browse files
Merge #201
201: Implement `blocking::serial::Write` for `Tx` and `Serial` types r=therealprof a=mitchmindtree This adds the missing `blocking::serial::Write` implementation as discussed in #184. Closes #184. Co-authored-by: mitchmindtree <[email protected]>
2 parents c252a7a + 74436f8 commit 76012d6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/serial.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use core::fmt;
22
use core::marker::PhantomData;
33
use core::ptr;
44

5+
use embedded_hal::blocking;
56
use embedded_hal::prelude::*;
67
use embedded_hal::serial;
78
use nb::block;
@@ -1648,6 +1649,51 @@ macro_rules! halUsartImpl {
16481649
}
16491650
}
16501651
}
1652+
1653+
impl blocking::serial::Write<u8> for Tx<$USARTX> {
1654+
type Error = Error;
1655+
1656+
fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
1657+
for &b in bytes {
1658+
loop {
1659+
match self.write(b) {
1660+
Err(nb::Error::WouldBlock) => continue,
1661+
Err(nb::Error::Other(err)) => return Err(err),
1662+
Ok(()) => break,
1663+
}
1664+
}
1665+
}
1666+
Ok(())
1667+
}
1668+
1669+
fn bflush(&mut self) -> Result<(), Self::Error> {
1670+
loop {
1671+
match self.flush() {
1672+
Ok(()) => return Ok(()),
1673+
Err(nb::Error::WouldBlock) => continue,
1674+
Err(nb::Error::Other(err)) => return Err(err),
1675+
}
1676+
}
1677+
}
1678+
}
1679+
1680+
impl<PINS> blocking::serial::Write<u8> for Serial<$USARTX, PINS> {
1681+
type Error = Error;
1682+
1683+
fn bwrite_all(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
1684+
let mut tx: Tx<$USARTX> = Tx {
1685+
_usart: PhantomData,
1686+
};
1687+
tx.bwrite_all(bytes)
1688+
}
1689+
1690+
fn bflush(&mut self) -> Result<(), Self::Error> {
1691+
let mut tx: Tx<$USARTX> = Tx {
1692+
_usart: PhantomData,
1693+
};
1694+
tx.bflush()
1695+
}
1696+
}
16511697
)+
16521698
}
16531699
}

0 commit comments

Comments
 (0)