embassy/embassy-stm32/src/usart/mod.rs

116 lines
2.7 KiB
Rust
Raw Normal View History

#![macro_use]
#[cfg_attr(usart_v1, path = "v1.rs")]
2021-06-30 16:03:18 +02:00
#[cfg_attr(usart_v2, path = "v2.rs")]
mod _version;
2021-06-03 17:27:17 +02:00
use crate::peripherals;
pub use _version::*;
2021-04-14 15:34:58 +02:00
2021-05-15 03:52:58 +02:00
use crate::gpio::Pin;
use crate::pac::usart::Usart;
2021-06-29 16:59:22 +02:00
use crate::rcc::RccPeripheral;
2021-04-14 15:34:58 +02:00
2021-05-15 03:52:58 +02:00
/// Serial error
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
2021-04-14 15:34:58 +02:00
#[non_exhaustive]
2021-05-15 03:52:58 +02:00
pub enum Error {
/// Framing error
Framing,
/// Noise error
Noise,
/// RX buffer overrun
Overrun,
/// Parity check error
Parity,
2021-04-14 15:34:58 +02:00
}
2021-04-25 22:35:51 +02:00
pub(crate) mod sealed {
use super::*;
2021-06-29 19:00:52 +02:00
#[cfg(dma)]
2021-06-25 20:00:11 +02:00
use crate::dma::WriteDma;
2021-05-15 03:52:58 +02:00
2021-04-25 22:35:51 +02:00
pub trait Instance {
fn regs(&self) -> Usart;
}
pub trait RxPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
2021-04-25 22:35:51 +02:00
}
pub trait TxPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
2021-04-25 22:35:51 +02:00
}
pub trait CtsPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
2021-04-25 22:35:51 +02:00
}
pub trait RtsPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
2021-04-25 22:35:51 +02:00
}
pub trait CkPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
2021-04-25 22:35:51 +02:00
}
2021-06-25 20:00:11 +02:00
2021-06-29 19:00:52 +02:00
#[cfg(dma)]
2021-06-25 20:00:11 +02:00
pub trait RxDma<T: Instance> {}
2021-06-29 19:00:52 +02:00
#[cfg(dma)]
2021-06-25 20:00:11 +02:00
pub trait TxDma<T: Instance>: WriteDma<T> {}
2021-04-25 22:35:51 +02:00
}
2021-06-25 20:00:11 +02:00
2021-06-29 16:59:22 +02:00
pub trait Instance: sealed::Instance + RccPeripheral {}
2021-04-25 22:35:51 +02:00
pub trait RxPin<T: Instance>: sealed::RxPin<T> {}
pub trait TxPin<T: Instance>: sealed::TxPin<T> {}
pub trait CtsPin<T: Instance>: sealed::CtsPin<T> {}
pub trait RtsPin<T: Instance>: sealed::RtsPin<T> {}
pub trait CkPin<T: Instance>: sealed::CkPin<T> {}
2021-06-29 19:00:52 +02:00
#[cfg(dma)]
2021-06-25 20:00:11 +02:00
pub trait RxDma<T: Instance>: sealed::RxDma<T> {}
2021-06-29 19:00:52 +02:00
#[cfg(dma)]
2021-06-25 20:00:11 +02:00
pub trait TxDma<T: Instance>: sealed::TxDma<T> {}
2021-06-03 17:27:17 +02:00
crate::pac::peripherals!(
(usart, $inst:ident) => {
impl sealed::Instance for peripherals::$inst {
fn regs(&self) -> crate::pac::usart::Usart {
crate::pac::$inst
2021-04-14 15:34:58 +02:00
}
}
2021-06-03 17:27:17 +02:00
impl Instance for peripherals::$inst {}
2021-04-14 15:34:58 +02:00
};
2021-06-03 17:27:17 +02:00
);
2021-04-14 15:34:58 +02:00
2021-06-03 17:27:17 +02:00
macro_rules! impl_pin {
($inst:ident, $pin:ident, $signal:ident, $af:expr) => {
impl sealed::$signal<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
2021-04-25 22:35:51 +02:00
}
2021-06-03 17:27:17 +02:00
impl $signal<peripherals::$inst> for peripherals::$pin {}
2021-04-25 22:35:51 +02:00
};
}
2021-06-03 17:27:17 +02:00
crate::pac::peripheral_pins!(
($inst:ident, usart, USART, $pin:ident, TX, $af:expr) => {
impl_pin!($inst, $pin, TxPin, $af);
};
($inst:ident, usart, USART, $pin:ident, RX, $af:expr) => {
impl_pin!($inst, $pin, RxPin, $af);
};
($inst:ident, usart, USART, $pin:ident, CTS, $af:expr) => {
impl_pin!($inst, $pin, CtsPin, $af);
};
($inst:ident, usart, USART, $pin:ident, RTS, $af:expr) => {
impl_pin!($inst, $pin, RtsPin, $af);
};
($inst:ident, usart, USART, $pin:ident, CK, $af:expr) => {
impl_pin!($inst, $pin, CkPin, $af);
};
);