mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-26 00:32:33 +00:00
Merge pull request #290 from bobmcwhirter/async-usart
First shot at async dma usart for stm32.
This commit is contained in:
commit
e4325cb8fe
@ -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,13 @@ pub trait ReadDma<T> {
|
||||
where
|
||||
T: 'a;
|
||||
}
|
||||
|
||||
pub struct NoDma;
|
||||
|
||||
unsafe impl Unborrow for NoDma {
|
||||
type Target = NoDma;
|
||||
|
||||
unsafe fn unborrow(self) -> Self::Target {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
@ -6,20 +6,29 @@ 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;
|
||||
|
||||
#[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 +63,16 @@ 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> {
|
||||
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 +113,9 @@ 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, RxDma> embedded_hal::blocking::serial::Write<u8>
|
||||
for Uart<'d, T, NoDma, RxDma>
|
||||
{
|
||||
type Error = Error;
|
||||
fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
|
||||
unsafe {
|
||||
@ -119,3 +135,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)
|
||||
}
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ use embassy::executor::Executor;
|
||||
use embassy::time::Clock;
|
||||
use embassy::util::Forever;
|
||||
use embassy_stm32::usart::{Config, Uart};
|
||||
use embassy_stm32::dma_traits::NoDma;
|
||||
use example_common::*;
|
||||
|
||||
use stm32h7xx_hal as hal;
|
||||
@ -27,7 +28,7 @@ async fn main_task() {
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let config = Config::default();
|
||||
let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, config);
|
||||
let mut usart = Uart::new(p.UART7, p.PF6, p.PF7, NoDma, NoDma, config);
|
||||
|
||||
usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap();
|
||||
info!("wrote Hello, starting echo");
|
||||
|
@ -18,13 +18,14 @@ use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use stm32l4::stm32l4x5 as pac;
|
||||
use embassy_stm32::dma_traits::NoDma;
|
||||
|
||||
#[embassy::task]
|
||||
async fn main_task() {
|
||||
let p = embassy_stm32::init(Default::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, NoDma, NoDma, config);
|
||||
|
||||
usart.bwrite_all(b"Hello Embassy World!\r\n").unwrap();
|
||||
info!("wrote Hello, starting echo");
|
||||
|
@ -17,22 +17,22 @@ use embassy_stm32::usart::{Config, Uart};
|
||||
use example_common::*;
|
||||
use heapless::String;
|
||||
use stm32l4::stm32l4x5 as pac;
|
||||
use embassy_stm32::dma_traits::NoDma;
|
||||
use embassy_traits::uart::Write as AsyncWrite;
|
||||
|
||||
#[embassy::task]
|
||||
async fn main_task() {
|
||||
let mut p = embassy_stm32::init(Default::default());
|
||||
let p = embassy_stm32::init(Default::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.. {
|
||||
let mut s: String<128> = String::new();
|
||||
core::write!(&mut s, "Hello DMA World {}!\r\n", n).unwrap();
|
||||
|
||||
usart
|
||||
.write_dma(&mut p.DMA1_3, s.as_bytes())
|
||||
.await
|
||||
.unwrap();
|
||||
usart.write( s.as_bytes() ).await.ok();
|
||||
|
||||
info!("wrote DMA");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user