stm32/usart: expose all functionality as inherent methods.

This commit is contained in:
Dario Nieuwenhuis
2022-01-14 22:43:40 +01:00
parent 98f24bf819
commit c949519714
13 changed files with 49 additions and 40 deletions

View File

@ -129,7 +129,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
}
}
async fn write_dma(&mut self, buffer: &[u8]) -> Result<(), Error>
pub async fn write(&mut self, buffer: &[u8]) -> Result<(), Error>
where
TxDma: crate::usart::TxDma<T>,
{
@ -146,7 +146,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
Ok(())
}
async fn read_dma(&mut self, buffer: &mut [u8]) -> Result<(), Error>
pub async fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error>
where
RxDma: crate::usart::RxDma<T>,
{
@ -163,7 +163,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
Ok(())
}
pub fn read_blocking(&mut self, buffer: &mut [u8]) -> Result<(), Error> {
pub fn blocking_read(&mut self, buffer: &mut [u8]) -> Result<(), Error> {
unsafe {
let r = self.inner.regs();
for b in buffer {
@ -190,6 +190,25 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
}
Ok(())
}
pub fn blocking_write(&mut self, buffer: &[u8]) -> Result<(), Error> {
unsafe {
let r = self.inner.regs();
for &b in buffer {
while !sr(r).read().txe() {}
tdr(r).write_volatile(b);
}
}
Ok(())
}
pub fn blocking_flush(&mut self) -> Result<(), Error> {
unsafe {
let r = self.inner.regs();
while !sr(r).read().tc() {}
}
Ok(())
}
}
impl<'d, T: Instance, TxDma, RxDma> embedded_hal::serial::Read<u8> for Uart<'d, T, TxDma, RxDma> {
@ -224,21 +243,10 @@ impl<'d, T: Instance, TxDma, RxDma> embedded_hal::blocking::serial::Write<u8>
{
type Error = Error;
fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
unsafe {
let r = self.inner.regs();
for &b in buffer {
while !sr(r).read().txe() {}
tdr(r).write_volatile(b);
}
}
Ok(())
self.blocking_write(buffer)
}
fn bflush(&mut self) -> Result<(), Self::Error> {
unsafe {
let r = self.inner.regs();
while !sr(r).read().tc() {}
}
Ok(())
self.blocking_flush()
}
}
@ -252,7 +260,7 @@ where
= impl Future<Output = Result<(), embassy_traits::uart::Error>> + 'a;
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
self.write_dma(buf)
self.write(buf)
.map_err(|_| embassy_traits::uart::Error::Other)
}
}
@ -267,7 +275,7 @@ where
= impl Future<Output = Result<(), embassy_traits::uart::Error>> + 'a;
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReadFuture<'a> {
self.read_dma(buf)
self.read(buf)
.map_err(|_| embassy_traits::uart::Error::Other)
}
}