embassy/embassy-stm32/src/usart.rs

103 lines
2.5 KiB
Rust
Raw Normal View History

#![macro_use]
2021-04-25 22:35:51 +02:00
use core::marker::PhantomData;
2021-04-20 03:37:49 +02:00
use embassy::util::Unborrow;
use embassy_extras::unborrow;
2021-04-14 15:34:58 +02:00
2021-04-25 22:35:51 +02:00
use crate::gpio::{NoPin, Pin};
use crate::pac::usart::{regs, vals, Usart};
2021-04-14 15:34:58 +02:00
use crate::peripherals;
#[non_exhaustive]
pub struct Config {
pub baudrate: u32,
pub data_bits: u8,
pub stop_bits: u8,
}
impl Default for Config {
fn default() -> Self {
Self {
baudrate: 115200,
data_bits: 8,
stop_bits: 1,
}
}
}
pub struct Uart<'d, T: Instance> {
inner: T,
phantom: PhantomData<&'d mut T>,
}
impl<'d, T: Instance> Uart<'d, T> {
pub fn new(
2021-04-20 03:37:49 +02:00
inner: impl Unborrow<Target = T>,
rx: impl Unborrow<Target = impl RxPin<T>>,
2021-04-25 22:35:51 +02:00
tx: impl Unborrow<Target = impl TxPin<T>>,
2021-04-20 03:37:49 +02:00
cts: impl Unborrow<Target = impl CtsPin<T>>,
rts: impl Unborrow<Target = impl RtsPin<T>>,
2021-04-14 15:34:58 +02:00
config: Config,
) -> Self {
2021-04-25 22:35:51 +02:00
unborrow!(inner, rx, tx, cts, rts);
Self {
inner,
phantom: PhantomData,
}
2021-04-14 15:34:58 +02:00
}
}
2021-04-25 22:35:51 +02:00
pub(crate) mod sealed {
use crate::gpio::{OptionalPin, Pin};
use super::*;
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-04-14 15:34:58 +02:00
pub trait Instance: sealed::Instance {}
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> {}
macro_rules! impl_usart {
($inst:ident) => {
2021-04-25 22:35:51 +02:00
impl crate::usart::sealed::Instance for peripherals::$inst {
fn regs(&self) -> crate::pac::usart::Usart {
crate::pac::$inst
2021-04-14 15:34:58 +02:00
}
}
2021-04-25 22:35:51 +02:00
impl crate::usart::Instance for peripherals::$inst {}
2021-04-14 15:34:58 +02:00
};
}
2021-04-25 22:35:51 +02:00
macro_rules! impl_usart_pin {
($inst:ident, $func:ident, $pin:ident, $af:expr) => {
2021-04-25 22:35:51 +02:00
impl crate::usart::sealed::$func<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
2021-04-25 22:35:51 +02:00
}
impl crate::usart::$func<peripherals::$inst> for peripherals::$pin {}
};
}