First shot at async dma usart for stm32.

This commit is contained in:
Bob McWhirter 2021-07-14 14:14:14 -04:00
parent 667b259d53
commit a88f0028ef
3 changed files with 58 additions and 10 deletions

View File

@ -1,4 +1,5 @@
use core::future::Future; use core::future::Future;
use embassy::util::Unborrow;
pub trait WriteDma<T> { pub trait WriteDma<T> {
type WriteDmaFuture<'a>: Future<Output = ()> + 'a type WriteDmaFuture<'a>: Future<Output = ()> + 'a
@ -19,3 +20,17 @@ pub trait ReadDma<T> {
where where
T: 'a; 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
}
}

View File

@ -6,20 +6,30 @@ use embassy_extras::unborrow;
use crate::pac::usart::{regs, vals}; use crate::pac::usart::{regs, vals};
use super::*; 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, inner: T,
phantom: PhantomData<&'d mut 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( pub fn new(
inner: impl Unborrow<Target = T>, inner: impl Unborrow<Target = T>,
rx: impl Unborrow<Target = impl RxPin<T>>, rx: impl Unborrow<Target = impl RxPin<T>>,
tx: impl Unborrow<Target = impl TxPin<T>>, tx: impl Unborrow<Target = impl TxPin<T>>,
tx_dma: impl Unborrow<Target = TxDma>,
rx_dma: impl Unborrow<Target = RxDma>,
config: Config, config: Config,
) -> Self { ) -> Self {
unborrow!(inner, rx, tx); unborrow!(inner, rx, tx, tx_dma, rx_dma);
// Uncomment once we find all of the H7's UART clocks. // Uncomment once we find all of the H7's UART clocks.
T::enable(); T::enable();
@ -54,11 +64,17 @@ impl<'d, T: Instance> Uart<'d, T> {
Self { Self {
inner, inner,
phantom: PhantomData, phantom: PhantomData,
tx_dma,
rx_dma,
} }
} }
#[cfg(any(dma, dmamux))] #[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 { unsafe {
self.inner.regs().cr3().modify(|reg| { self.inner.regs().cr3().modify(|reg| {
reg.set_dmat(true); 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; type Error = Error;
fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> { fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
unsafe { unsafe {
@ -119,3 +139,15 @@ impl<'d, T: Instance> embedded_hal::blocking::serial::Write<u8> for Uart<'d, T>
Ok(()) 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)
}
}

View File

@ -17,22 +17,23 @@ use embassy_stm32::usart::{Config, Uart};
use example_common::*; use example_common::*;
use heapless::String; use heapless::String;
use stm32l4::stm32l4x5 as pac; use stm32l4::stm32l4x5 as pac;
use embassy_stm32::dma_traits::NoDma;
use embassy_traits::uart::Write as AsyncWrite;
use embassy::io::AsyncWriteExt;
#[embassy::task] #[embassy::task]
async fn main_task() { async fn main_task() {
let mut p = embassy_stm32::init(Default::default()); let mut p = embassy_stm32::init(Default::default());
let config = Config::default(); let config = Config::default();
let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, config); let mut usart = Uart::new(p.UART4, p.PA1, p.PA0, p.DMA1_3, NoDma, config);
for n in 0u32.. { for n in 0u32.. {
let mut s: String<128> = String::new(); let mut s: String<128> = String::new();
core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap(); core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
usart usart.write( s.as_bytes() ).await;
.write_dma(&mut p.DMA1_3, s.as_bytes())
.await
.unwrap();
info!("wrote DMA"); info!("wrote DMA");
} }
} }