915: Implement support for LPUART r=Dirbaio a=FrozenDroid

Turns out zero commits is not quite enough to get it implemented (#800), so let's try it again ;)

Co-authored-by: Vincent Stakenburg <v.stakenburg@cosinuss.nl>
This commit is contained in:
bors[bot] 2022-08-20 00:55:45 +00:00 committed by GitHub
commit 872e0026ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 64 deletions

View File

@ -244,11 +244,11 @@ fn main() {
(("usart", "CTS"), quote!(crate::usart::CtsPin)), (("usart", "CTS"), quote!(crate::usart::CtsPin)),
(("usart", "RTS"), quote!(crate::usart::RtsPin)), (("usart", "RTS"), quote!(crate::usart::RtsPin)),
(("usart", "CK"), quote!(crate::usart::CkPin)), (("usart", "CK"), quote!(crate::usart::CkPin)),
(("usart", "TX"), quote!(crate::usart::TxPin)), (("lpuart", "TX"), quote!(crate::usart::TxPin)),
(("usart", "RX"), quote!(crate::usart::RxPin)), (("lpuart", "RX"), quote!(crate::usart::RxPin)),
(("usart", "CTS"), quote!(crate::usart::CtsPin)), (("lpuart", "CTS"), quote!(crate::usart::CtsPin)),
(("usart", "RTS"), quote!(crate::usart::RtsPin)), (("lpuart", "RTS"), quote!(crate::usart::RtsPin)),
(("usart", "CK"), quote!(crate::usart::CkPin)), (("lpuart", "CK"), quote!(crate::usart::CkPin)),
(("spi", "SCK"), quote!(crate::spi::SckPin)), (("spi", "SCK"), quote!(crate::spi::SckPin)),
(("spi", "MOSI"), quote!(crate::spi::MosiPin)), (("spi", "MOSI"), quote!(crate::spi::MosiPin)),
(("spi", "MISO"), quote!(crate::spi::MisoPin)), (("spi", "MISO"), quote!(crate::spi::MisoPin)),
@ -497,6 +497,8 @@ fn main() {
// (kind, signal) => trait // (kind, signal) => trait
(("usart", "RX"), quote!(crate::usart::RxDma)), (("usart", "RX"), quote!(crate::usart::RxDma)),
(("usart", "TX"), quote!(crate::usart::TxDma)), (("usart", "TX"), quote!(crate::usart::TxDma)),
(("lpuart", "RX"), quote!(crate::usart::RxDma)),
(("lpuart", "TX"), quote!(crate::usart::TxDma)),
(("spi", "RX"), quote!(crate::spi::RxDma)), (("spi", "RX"), quote!(crate::spi::RxDma)),
(("spi", "TX"), quote!(crate::spi::TxDma)), (("spi", "TX"), quote!(crate::spi::TxDma)),
(("i2c", "RX"), quote!(crate::i2c::RxDma)), (("i2c", "RX"), quote!(crate::i2c::RxDma)),

View File

@ -9,14 +9,14 @@ use futures::future::poll_fn;
use super::*; use super::*;
pub struct State<'d, T: Instance>(StateStorage<StateInner<'d, T>>); pub struct State<'d, T: BasicInstance>(StateStorage<StateInner<'d, T>>);
impl<'d, T: Instance> State<'d, T> { impl<'d, T: BasicInstance> State<'d, T> {
pub fn new() -> Self { pub fn new() -> Self {
Self(StateStorage::new()) Self(StateStorage::new())
} }
} }
struct StateInner<'d, T: Instance> { struct StateInner<'d, T: BasicInstance> {
phantom: PhantomData<&'d mut T>, phantom: PhantomData<&'d mut T>,
rx_waker: WakerRegistration, rx_waker: WakerRegistration,
@ -26,16 +26,16 @@ struct StateInner<'d, T: Instance> {
tx: RingBuffer<'d>, tx: RingBuffer<'d>,
} }
unsafe impl<'d, T: Instance> Send for StateInner<'d, T> {} unsafe impl<'d, T: BasicInstance> Send for StateInner<'d, T> {}
unsafe impl<'d, T: Instance> Sync for StateInner<'d, T> {} unsafe impl<'d, T: BasicInstance> Sync for StateInner<'d, T> {}
pub struct BufferedUart<'d, T: Instance> { pub struct BufferedUart<'d, T: BasicInstance> {
inner: PeripheralMutex<'d, StateInner<'d, T>>, inner: PeripheralMutex<'d, StateInner<'d, T>>,
} }
impl<'d, T: Instance> Unpin for BufferedUart<'d, T> {} impl<'d, T: BasicInstance> Unpin for BufferedUart<'d, T> {}
impl<'d, T: Instance> BufferedUart<'d, T> { impl<'d, T: BasicInstance> BufferedUart<'d, T> {
pub fn new( pub fn new(
state: &'d mut State<'d, T>, state: &'d mut State<'d, T>,
_uart: Uart<'d, T, NoDma, NoDma>, _uart: Uart<'d, T, NoDma, NoDma>,
@ -66,7 +66,7 @@ impl<'d, T: Instance> BufferedUart<'d, T> {
} }
} }
impl<'d, T: Instance> StateInner<'d, T> impl<'d, T: BasicInstance> StateInner<'d, T>
where where
Self: 'd, Self: 'd,
{ {
@ -135,7 +135,7 @@ where
} }
} }
impl<'d, T: Instance> PeripheralState for StateInner<'d, T> impl<'d, T: BasicInstance> PeripheralState for StateInner<'d, T>
where where
Self: 'd, Self: 'd,
{ {
@ -152,11 +152,11 @@ impl embedded_io::Error for Error {
} }
} }
impl<'d, T: Instance> embedded_io::Io for BufferedUart<'d, T> { impl<'d, T: BasicInstance> embedded_io::Io for BufferedUart<'d, T> {
type Error = Error; type Error = Error;
} }
impl<'d, T: Instance> embedded_io::asynch::Read for BufferedUart<'d, T> { impl<'d, T: BasicInstance> embedded_io::asynch::Read for BufferedUart<'d, T> {
type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>> type ReadFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
where where
Self: 'a; Self: 'a;
@ -194,7 +194,7 @@ impl<'d, T: Instance> embedded_io::asynch::Read for BufferedUart<'d, T> {
} }
} }
impl<'d, T: Instance> embedded_io::asynch::BufRead for BufferedUart<'d, T> { impl<'d, T: BasicInstance> embedded_io::asynch::BufRead for BufferedUart<'d, T> {
type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>> type FillBufFuture<'a> = impl Future<Output = Result<&'a [u8], Self::Error>>
where where
Self: 'a; Self: 'a;
@ -231,7 +231,7 @@ impl<'d, T: Instance> embedded_io::asynch::BufRead for BufferedUart<'d, T> {
} }
} }
impl<'d, T: Instance> embedded_io::asynch::Write for BufferedUart<'d, T> { impl<'d, T: BasicInstance> embedded_io::asynch::Write for BufferedUart<'d, T> {
type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>> type WriteFuture<'a> = impl Future<Output = Result<usize, Self::Error>>
where where
Self: 'a; Self: 'a;

View File

@ -6,9 +6,10 @@ use embassy_hal_common::{into_ref, PeripheralRef};
use crate::dma::NoDma; use crate::dma::NoDma;
use crate::gpio::sealed::AFType; use crate::gpio::sealed::AFType;
use crate::interrupt::Interrupt; #[cfg(any(lpuart_v1, lpuart_v2))]
use crate::pac::usart::{regs, vals}; use crate::pac::lpuart::{regs, vals, Lpuart as Regs};
use crate::rcc::RccPeripheral; #[cfg(not(any(lpuart_v1, lpuart_v2)))]
use crate::pac::usart::{regs, vals, Usart as Regs};
use crate::{peripherals, Peripheral}; use crate::{peripherals, Peripheral};
#[derive(Clone, Copy, PartialEq, Eq, Debug)] #[derive(Clone, Copy, PartialEq, Eq, Debug)]
@ -71,22 +72,23 @@ pub enum Error {
Parity, Parity,
} }
pub struct Uart<'d, T: Instance, TxDma = NoDma, RxDma = NoDma> { pub struct Uart<'d, T: BasicInstance, TxDma = NoDma, RxDma = NoDma> {
phantom: PhantomData<&'d mut T>,
tx: UartTx<'d, T, TxDma>, tx: UartTx<'d, T, TxDma>,
rx: UartRx<'d, T, RxDma>, rx: UartRx<'d, T, RxDma>,
} }
pub struct UartTx<'d, T: Instance, TxDma = NoDma> { pub struct UartTx<'d, T: BasicInstance, TxDma = NoDma> {
phantom: PhantomData<&'d mut T>, phantom: PhantomData<&'d mut T>,
tx_dma: PeripheralRef<'d, TxDma>, tx_dma: PeripheralRef<'d, TxDma>,
} }
pub struct UartRx<'d, T: Instance, RxDma = NoDma> { pub struct UartRx<'d, T: BasicInstance, RxDma = NoDma> {
phantom: PhantomData<&'d mut T>, phantom: PhantomData<&'d mut T>,
rx_dma: PeripheralRef<'d, RxDma>, rx_dma: PeripheralRef<'d, RxDma>,
} }
impl<'d, T: Instance, TxDma> UartTx<'d, T, TxDma> { impl<'d, T: BasicInstance, TxDma> UartTx<'d, T, TxDma> {
fn new(tx_dma: PeripheralRef<'d, TxDma>) -> Self { fn new(tx_dma: PeripheralRef<'d, TxDma>) -> Self {
Self { Self {
tx_dma, tx_dma,
@ -132,7 +134,7 @@ impl<'d, T: Instance, TxDma> UartTx<'d, T, TxDma> {
} }
} }
impl<'d, T: Instance, RxDma> UartRx<'d, T, RxDma> { impl<'d, T: BasicInstance, RxDma> UartRx<'d, T, RxDma> {
fn new(rx_dma: PeripheralRef<'d, RxDma>) -> Self { fn new(rx_dma: PeripheralRef<'d, RxDma>) -> Self {
Self { Self {
rx_dma, rx_dma,
@ -187,7 +189,7 @@ impl<'d, T: Instance, RxDma> UartRx<'d, T, RxDma> {
} }
} }
impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> { impl<'d, T: BasicInstance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
pub fn new( pub fn new(
_inner: impl Peripheral<P = T> + 'd, _inner: impl Peripheral<P = T> + 'd,
rx: impl Peripheral<P = impl RxPin<T>> + 'd, rx: impl Peripheral<P = impl RxPin<T>> + 'd,
@ -203,7 +205,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
let pclk_freq = T::frequency(); let pclk_freq = T::frequency();
// TODO: better calculation, including error checking and OVER8 if possible. // TODO: better calculation, including error checking and OVER8 if possible.
let div = (pclk_freq.0 + (config.baudrate / 2)) / config.baudrate; let div = (pclk_freq.0 + (config.baudrate / 2)) / config.baudrate * T::MULTIPLIER;
let r = T::regs(); let r = T::regs();
@ -235,6 +237,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
Self { Self {
tx: UartTx::new(tx_dma), tx: UartTx::new(tx_dma),
rx: UartRx::new(rx_dma), rx: UartRx::new(rx_dma),
phantom: PhantomData {},
} }
} }
@ -275,7 +278,7 @@ impl<'d, T: Instance, TxDma, RxDma> Uart<'d, T, TxDma, RxDma> {
mod eh02 { mod eh02 {
use super::*; use super::*;
impl<'d, T: Instance, RxDma> embedded_hal_02::serial::Read<u8> for UartRx<'d, T, RxDma> { impl<'d, T: BasicInstance, RxDma> embedded_hal_02::serial::Read<u8> for UartRx<'d, T, RxDma> {
type Error = Error; type Error = Error;
fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
let r = T::regs(); let r = T::regs();
@ -302,7 +305,7 @@ mod eh02 {
} }
} }
impl<'d, T: Instance, TxDma> embedded_hal_02::blocking::serial::Write<u8> for UartTx<'d, T, TxDma> { impl<'d, T: BasicInstance, TxDma> embedded_hal_02::blocking::serial::Write<u8> for UartTx<'d, T, TxDma> {
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> {
self.blocking_write(buffer) self.blocking_write(buffer)
@ -312,14 +315,14 @@ mod eh02 {
} }
} }
impl<'d, T: Instance, TxDma, RxDma> embedded_hal_02::serial::Read<u8> for Uart<'d, T, TxDma, RxDma> { impl<'d, T: BasicInstance, TxDma, RxDma> embedded_hal_02::serial::Read<u8> for Uart<'d, T, TxDma, RxDma> {
type Error = Error; type Error = Error;
fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> { fn read(&mut self) -> Result<u8, nb::Error<Self::Error>> {
embedded_hal_02::serial::Read::read(&mut self.rx) embedded_hal_02::serial::Read::read(&mut self.rx)
} }
} }
impl<'d, T: Instance, TxDma, RxDma> embedded_hal_02::blocking::serial::Write<u8> for Uart<'d, T, TxDma, RxDma> { impl<'d, T: BasicInstance, TxDma, RxDma> embedded_hal_02::blocking::serial::Write<u8> for Uart<'d, T, TxDma, RxDma> {
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> {
self.blocking_write(buffer) self.blocking_write(buffer)
@ -345,15 +348,15 @@ mod eh1 {
} }
} }
impl<'d, T: Instance, TxDma, RxDma> embedded_hal_1::serial::ErrorType for Uart<'d, T, TxDma, RxDma> { impl<'d, T: BasicInstance, TxDma, RxDma> embedded_hal_1::serial::ErrorType for Uart<'d, T, TxDma, RxDma> {
type Error = Error; type Error = Error;
} }
impl<'d, T: Instance, TxDma> embedded_hal_1::serial::ErrorType for UartTx<'d, T, TxDma> { impl<'d, T: BasicInstance, TxDma> embedded_hal_1::serial::ErrorType for UartTx<'d, T, TxDma> {
type Error = Error; type Error = Error;
} }
impl<'d, T: Instance, RxDma> embedded_hal_1::serial::ErrorType for UartRx<'d, T, RxDma> { impl<'d, T: BasicInstance, RxDma> embedded_hal_1::serial::ErrorType for UartRx<'d, T, RxDma> {
type Error = Error; type Error = Error;
} }
} }
@ -362,7 +365,7 @@ cfg_if::cfg_if! {
if #[cfg(all(feature = "unstable-traits", feature = "nightly", feature = "_todo_embedded_hal_serial"))] { if #[cfg(all(feature = "unstable-traits", feature = "nightly", feature = "_todo_embedded_hal_serial"))] {
use core::future::Future; use core::future::Future;
impl<'d, T: Instance, TxDma> embedded_hal_async::serial::Write for UartTx<'d, T, TxDma> impl<'d, T: BasicInstance, TxDma> embedded_hal_async::serial::Write for UartTx<'d, T, TxDma>
where where
TxDma: crate::usart::TxDma<T>, TxDma: crate::usart::TxDma<T>,
{ {
@ -379,7 +382,7 @@ cfg_if::cfg_if! {
} }
} }
impl<'d, T: Instance, RxDma> embedded_hal_async::serial::Read for UartRx<'d, T, RxDma> impl<'d, T: BasicInstance, RxDma> embedded_hal_async::serial::Read for UartRx<'d, T, RxDma>
where where
RxDma: crate::usart::RxDma<T>, RxDma: crate::usart::RxDma<T>,
{ {
@ -390,7 +393,7 @@ cfg_if::cfg_if! {
} }
} }
impl<'d, T: Instance, TxDma, RxDma> embedded_hal_async::serial::Write for Uart<'d, T, TxDma, RxDma> impl<'d, T: BasicInstance, TxDma, RxDma> embedded_hal_async::serial::Write for Uart<'d, T, TxDma, RxDma>
where where
TxDma: crate::usart::TxDma<T>, TxDma: crate::usart::TxDma<T>,
{ {
@ -407,7 +410,7 @@ cfg_if::cfg_if! {
} }
} }
impl<'d, T: Instance, TxDma, RxDma> embedded_hal_async::serial::Read for Uart<'d, T, TxDma, RxDma> impl<'d, T: BasicInstance, TxDma, RxDma> embedded_hal_async::serial::Read for Uart<'d, T, TxDma, RxDma>
where where
RxDma: crate::usart::RxDma<T>, RxDma: crate::usart::RxDma<T>,
{ {
@ -442,60 +445,90 @@ fn sr(r: crate::pac::usart::Usart) -> crate::pac::common::Reg<regs::Sr, crate::p
#[cfg(usart_v1)] #[cfg(usart_v1)]
#[allow(unused)] #[allow(unused)]
unsafe fn clear_interrupt_flags(_r: crate::pac::usart::Usart, _sr: regs::Sr) { unsafe fn clear_interrupt_flags(_r: Regs, _sr: regs::Sr) {
// On v1 the flags are cleared implicitly by reads and writes to DR. // On v1 the flags are cleared implicitly by reads and writes to DR.
} }
#[cfg(usart_v2)] #[cfg(usart_v2)]
fn tdr(r: crate::pac::usart::Usart) -> *mut u8 { fn tdr(r: Regs) -> *mut u8 {
r.tdr().ptr() as _ r.tdr().ptr() as _
} }
#[cfg(usart_v2)] #[cfg(usart_v2)]
fn rdr(r: crate::pac::usart::Usart) -> *mut u8 { fn rdr(r: Regs) -> *mut u8 {
r.rdr().ptr() as _ r.rdr().ptr() as _
} }
#[cfg(usart_v2)] #[cfg(usart_v2)]
fn sr(r: crate::pac::usart::Usart) -> crate::pac::common::Reg<regs::Ixr, crate::pac::common::R> { fn sr(r: Regs) -> crate::pac::common::Reg<regs::Isr, crate::pac::common::R> {
r.isr() r.isr()
} }
#[cfg(usart_v2)] #[cfg(usart_v2)]
#[allow(unused)] #[allow(unused)]
unsafe fn clear_interrupt_flags(r: crate::pac::usart::Usart, sr: regs::Ixr) { unsafe fn clear_interrupt_flags(r: Regs, sr: regs::Isr) {
r.icr().write(|w| *w = sr); r.icr().write(|w| *w = regs::Icr(sr.0));
} }
pub(crate) mod sealed { pub(crate) mod sealed {
pub trait Instance { use super::*;
fn regs() -> crate::pac::usart::Usart;
pub trait BasicInstance: crate::rcc::RccPeripheral {
const MULTIPLIER: u32;
type Interrupt: crate::interrupt::Interrupt;
fn regs() -> Regs;
}
pub trait FullInstance: BasicInstance {
fn regs_uart() -> crate::pac::usart::Usart;
} }
} }
pub trait Instance: sealed::Instance + RccPeripheral { pub trait BasicInstance: sealed::BasicInstance {}
type Interrupt: Interrupt;
pub trait FullInstance: sealed::FullInstance {}
pin_trait!(RxPin, BasicInstance);
pin_trait!(TxPin, BasicInstance);
pin_trait!(CtsPin, BasicInstance);
pin_trait!(RtsPin, BasicInstance);
pin_trait!(CkPin, BasicInstance);
dma_trait!(TxDma, BasicInstance);
dma_trait!(RxDma, BasicInstance);
macro_rules! impl_lpuart {
($inst:ident, $irq:ident, $mul:expr) => {
impl sealed::BasicInstance for crate::peripherals::$inst {
const MULTIPLIER: u32 = $mul;
type Interrupt = crate::interrupt::$irq;
fn regs() -> Regs {
Regs(crate::pac::$inst.0)
}
} }
pin_trait!(RxPin, Instance); impl BasicInstance for peripherals::$inst {}
pin_trait!(TxPin, Instance); };
pin_trait!(CtsPin, Instance); }
pin_trait!(RtsPin, Instance);
pin_trait!(CkPin, Instance);
dma_trait!(TxDma, Instance);
dma_trait!(RxDma, Instance);
foreach_interrupt!( foreach_interrupt!(
($inst:ident, lpuart, $block:ident, $signal_name:ident, $irq:ident) => {
impl_lpuart!($inst, $irq, 255);
};
($inst:ident, usart, $block:ident, $signal_name:ident, $irq:ident) => { ($inst:ident, usart, $block:ident, $signal_name:ident, $irq:ident) => {
impl sealed::Instance for peripherals::$inst { impl_lpuart!($inst, $irq, 1);
fn regs() -> crate::pac::usart::Usart {
impl sealed::FullInstance for peripherals::$inst {
fn regs_uart() -> crate::pac::usart::Usart {
crate::pac::$inst crate::pac::$inst
} }
} }
impl Instance for peripherals::$inst { impl FullInstance for peripherals::$inst {
type Interrupt = crate::interrupt::$irq;
} }
}; };
); );

@ -1 +1 @@
Subproject commit 758c9e74625c68bc23d66ced8bfeb5643c63cec9 Subproject commit 14a448c318192fe9da1c95a4de1beb4ec4892f1c