First shot at async dma usart for stm32.
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use core::future::Future;
|
||||
use embassy::util::Unborrow;
|
||||
|
||||
pub trait WriteDma<T> {
|
||||
type WriteDmaFuture<'a>: Future<Output = ()> + 'a
|
||||
@ -19,3 +20,17 @@ pub trait ReadDma<T> {
|
||||
where
|
||||
T: 'a;
|
||||
}
|
||||
|
||||
pub trait NoDmaMarker {}
|
||||
|
||||
pub struct NoDma;
|
||||
|
||||
impl NoDmaMarker for NoDma {}
|
||||
|
||||
unsafe impl Unborrow for NoDma {
|
||||
type Target = NoDma;
|
||||
|
||||
unsafe fn unborrow(self) -> Self::Target {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@ -6,20 +6,30 @@ use embassy_extras::unborrow;
|
||||
use crate::pac::usart::{regs, vals};
|
||||
|
||||
use super::*;
|
||||
use core::future::Future;
|
||||
use futures::TryFutureExt;
|
||||
|
||||
pub struct Uart<'d, T: Instance> {
|
||||
use crate::dma_traits::NoDma;
|
||||
use crate::dma_traits::NoDmaMarker;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct Uart<'d, T: Instance, TxDma = NoDma, RxDma = NoDma> {
|
||||
inner: T,
|
||||
phantom: PhantomData<&'d mut T>,
|
||||
tx_dma: TxDma,
|
||||
rx_dma: RxDma,
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> Uart<'d, T> {
|
||||
impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
|
||||
pub fn new(
|
||||
inner: impl Unborrow<Target = T>,
|
||||
rx: impl Unborrow<Target = impl RxPin<T>>,
|
||||
tx: impl Unborrow<Target = impl TxPin<T>>,
|
||||
tx_dma: impl Unborrow<Target = TxDma>,
|
||||
rx_dma: impl Unborrow<Target = RxDma>,
|
||||
config: Config,
|
||||
) -> Self {
|
||||
unborrow!(inner, rx, tx);
|
||||
unborrow!(inner, rx, tx, tx_dma, rx_dma);
|
||||
|
||||
// Uncomment once we find all of the H7's UART clocks.
|
||||
T::enable();
|
||||
@ -54,11 +64,17 @@ impl<'d, T: Instance> Uart<'d, T> {
|
||||
Self {
|
||||
inner,
|
||||
phantom: PhantomData,
|
||||
tx_dma,
|
||||
rx_dma,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(dma, dmamux))]
|
||||
pub async fn write_dma(&mut self, ch: &mut impl TxDma<T>, buffer: &[u8]) -> Result<(), Error> {
|
||||
pub async fn write_dma(&mut self, buffer: &[u8]) -> Result<(), Error>
|
||||
where
|
||||
TxDma: crate::usart::TxDma<T>,
|
||||
{
|
||||
let ch = &mut self.tx_dma;
|
||||
unsafe {
|
||||
self.inner.regs().cr3().modify(|reg| {
|
||||
reg.set_dmat(true);
|
||||
@ -99,7 +115,11 @@ impl<'d, T: Instance> Uart<'d, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Instance> embedded_hal::blocking::serial::Write<u8> for Uart<'d, T> {
|
||||
impl<'d, T: Instance, TxDma, RxDma> embedded_hal::blocking::serial::Write<u8>
|
||||
for Uart<'d, T, TxDma, RxDma>
|
||||
where
|
||||
TxDma: NoDmaMarker,
|
||||
{
|
||||
type Error = Error;
|
||||
fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
|
||||
unsafe {
|
||||
@ -119,3 +139,15 @@ impl<'d, T: Instance> embedded_hal::blocking::serial::Write<u8> for Uart<'d, T>
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
// rustfmt::skip because intellij removes the 'where' claus on the associated type.
|
||||
#[rustfmt::skip]
|
||||
impl<'d, T: Instance, TxDma, RxDma> embassy_traits::uart::Write for Uart<'d, T, TxDma, RxDma>
|
||||
where TxDma: crate::usart::TxDma<T>
|
||||
{
|
||||
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), embassy_traits::uart::Error>>;
|
||||
|
||||
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Self::WriteFuture<'a> {
|
||||
self.write_dma(buf).map_err(|_| embassy_traits::uart::Error::Other)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user