2021-07-15 05:42:06 +02:00
|
|
|
use core::future::Future;
|
2021-07-15 06:33:34 +02:00
|
|
|
use core::marker::PhantomData;
|
2021-07-04 15:40:33 -03:00
|
|
|
use embassy::util::Unborrow;
|
2021-07-29 13:44:51 +02:00
|
|
|
use embassy_hal_common::unborrow;
|
2021-07-15 06:33:34 +02:00
|
|
|
use futures::TryFutureExt;
|
2021-07-04 15:40:33 -03:00
|
|
|
|
|
|
|
use super::*;
|
2021-07-15 05:42:06 +02:00
|
|
|
use crate::dma::NoDma;
|
2021-07-15 06:33:34 +02:00
|
|
|
use crate::pac::usart::{regs, vals};
|
2021-07-15 00:48:41 +02:00
|
|
|
|
|
|
|
pub struct Uart<'d, T: Instance, TxDma = NoDma, RxDma = NoDma> {
|
2021-07-04 15:40:33 -03:00
|
|
|
inner: T,
|
|
|
|
phantom: PhantomData<&'d mut T>,
|
2021-07-15 00:48:41 +02:00
|
|
|
tx_dma: TxDma,
|
2021-07-15 05:42:06 +02:00
|
|
|
#[allow(dead_code)]
|
2021-07-15 00:48:41 +02:00
|
|
|
rx_dma: RxDma,
|
2021-07-04 15:40:33 -03:00
|
|
|
}
|
|
|
|
|
2021-07-15 00:48:41 +02:00
|
|
|
impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
|
2021-07-04 15:40:33 -03:00
|
|
|
pub fn new(
|
|
|
|
inner: impl Unborrow<Target = T>,
|
|
|
|
rx: impl Unborrow<Target = impl RxPin<T>>,
|
|
|
|
tx: impl Unborrow<Target = impl TxPin<T>>,
|
2021-07-15 00:48:41 +02:00
|
|
|
tx_dma: impl Unborrow<Target = TxDma>,
|
|
|
|
rx_dma: impl Unborrow<Target = RxDma>,
|
2021-07-04 15:40:33 -03:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2021-07-15 00:48:41 +02:00
|
|
|
unborrow!(inner, rx, tx, tx_dma, rx_dma);
|
2021-07-04 15:40:33 -03:00
|
|
|
|
|
|
|
T::enable();
|
|
|
|
let pclk_freq = T::frequency();
|
|
|
|
|
|
|
|
// TODO: better calculation, including error checking and OVER8 if possible.
|
2021-07-15 00:48:41 +02:00
|
|
|
let div = (pclk_freq.0 + (config.baudrate / 2)) / config.baudrate;
|
2021-07-04 15:40:33 -03:00
|
|
|
|
|
|
|
let r = inner.regs();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
rx.set_as_af(rx.af_num());
|
|
|
|
tx.set_as_af(tx.af_num());
|
|
|
|
|
2021-07-04 18:34:37 -03:00
|
|
|
r.cr2().write(|_w| {});
|
|
|
|
r.cr3().write(|_w| {});
|
|
|
|
|
|
|
|
r.brr().write(|w| w.set_brr(div as u16));
|
2021-07-04 15:40:33 -03:00
|
|
|
r.cr1().write(|w| {
|
|
|
|
w.set_ue(true);
|
|
|
|
w.set_te(true);
|
|
|
|
w.set_re(true);
|
|
|
|
w.set_m0(vals::M0::BIT8);
|
|
|
|
w.set_m1(vals::M1::M0);
|
|
|
|
w.set_pce(config.parity != Parity::ParityNone);
|
|
|
|
w.set_ps(match config.parity {
|
|
|
|
Parity::ParityOdd => vals::Ps::ODD,
|
|
|
|
Parity::ParityEven => vals::Ps::EVEN,
|
|
|
|
_ => vals::Ps::EVEN,
|
|
|
|
});
|
|
|
|
});
|
2021-07-15 00:48:41 +02:00
|
|
|
r.cr2().write(|_w| {});
|
|
|
|
r.cr3().write(|_w| {});
|
2021-07-04 15:40:33 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
inner,
|
|
|
|
phantom: PhantomData,
|
2021-07-15 00:48:41 +02:00
|
|
|
tx_dma,
|
|
|
|
rx_dma,
|
2021-07-04 15:40:33 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 00:48:41 +02:00
|
|
|
async fn write_dma(&mut self, buffer: &[u8]) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
TxDma: crate::usart::TxDma<T>,
|
|
|
|
{
|
|
|
|
let ch = &mut self.tx_dma;
|
2021-07-04 15:40:33 -03:00
|
|
|
unsafe {
|
|
|
|
self.inner.regs().cr3().modify(|reg| {
|
|
|
|
reg.set_dmat(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let r = self.inner.regs();
|
|
|
|
let dst = r.tdr().ptr() as *mut u8;
|
2021-07-15 05:42:06 +02:00
|
|
|
ch.write(ch.request(), buffer, dst).await;
|
2021-07-04 15:40:33 -03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(&mut self, buffer: &mut [u8]) -> Result<(), Error> {
|
|
|
|
unsafe {
|
|
|
|
let r = self.inner.regs();
|
|
|
|
for b in buffer {
|
|
|
|
loop {
|
|
|
|
let sr = r.isr().read();
|
|
|
|
if sr.pe() {
|
|
|
|
r.rdr().read();
|
|
|
|
return Err(Error::Parity);
|
|
|
|
} else if sr.fe() {
|
|
|
|
r.rdr().read();
|
|
|
|
return Err(Error::Framing);
|
2021-07-15 00:48:41 +02:00
|
|
|
} else if sr.nf() {
|
|
|
|
r.rdr().read();
|
|
|
|
return Err(Error::Noise);
|
2021-07-04 15:40:33 -03:00
|
|
|
} else if sr.ore() {
|
|
|
|
r.rdr().read();
|
|
|
|
return Err(Error::Overrun);
|
|
|
|
} else if sr.rxne() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*b = r.rdr().read().0 as u8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 00:48:41 +02:00
|
|
|
impl<'d, T: Instance, RxDma> embedded_hal::blocking::serial::Write<u8>
|
|
|
|
for Uart<'d, T, NoDma, RxDma>
|
|
|
|
{
|
2021-07-04 15:40:33 -03:00
|
|
|
type Error = Error;
|
|
|
|
fn bwrite_all(&mut self, buffer: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
unsafe {
|
|
|
|
let r = self.inner.regs();
|
|
|
|
for &b in buffer {
|
|
|
|
while !r.isr().read().txe() {}
|
2021-07-15 00:48:41 +02:00
|
|
|
r.tdr().write_value(regs::Dr(b as u32))
|
2021-07-04 15:40:33 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn bflush(&mut self) -> Result<(), Self::Error> {
|
|
|
|
unsafe {
|
|
|
|
let r = self.inner.regs();
|
|
|
|
while !r.isr().read().tc() {}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 00:48:41 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|