2021-05-06 01:59:16 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-04-25 20:35:51 +00:00
|
|
|
use core::marker::PhantomData;
|
|
|
|
|
2021-04-20 01:37:49 +00:00
|
|
|
use embassy::util::Unborrow;
|
|
|
|
use embassy_extras::unborrow;
|
2021-04-14 13:34:58 +00:00
|
|
|
|
2021-04-25 20:35:51 +00:00
|
|
|
use crate::gpio::{NoPin, Pin};
|
2021-05-06 01:43:46 +00:00
|
|
|
use crate::pac::usart::{regs, vals, Usart};
|
2021-04-14 13:34:58 +00: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 01:37:49 +00:00
|
|
|
inner: impl Unborrow<Target = T>,
|
|
|
|
rx: impl Unborrow<Target = impl RxPin<T>>,
|
2021-04-25 20:35:51 +00:00
|
|
|
tx: impl Unborrow<Target = impl TxPin<T>>,
|
2021-04-20 01:37:49 +00:00
|
|
|
cts: impl Unborrow<Target = impl CtsPin<T>>,
|
|
|
|
rts: impl Unborrow<Target = impl RtsPin<T>>,
|
2021-04-14 13:34:58 +00:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2021-04-25 20:35:51 +00:00
|
|
|
unborrow!(inner, rx, tx, cts, rts);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
inner,
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
2021-04-14 13:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 20:35:51 +00:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use crate::gpio::{OptionalPin, Pin};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
pub trait Instance {
|
|
|
|
fn regs(&self) -> Usart;
|
|
|
|
}
|
|
|
|
pub trait RxPin<T: Instance>: OptionalPin {
|
|
|
|
const AF_NUM: u8;
|
|
|
|
}
|
|
|
|
pub trait TxPin<T: Instance>: OptionalPin {
|
|
|
|
const AF_NUM: u8;
|
|
|
|
}
|
|
|
|
pub trait CtsPin<T: Instance>: OptionalPin {
|
|
|
|
const AF_NUM: u8;
|
|
|
|
}
|
|
|
|
pub trait RtsPin<T: Instance>: OptionalPin {
|
|
|
|
const AF_NUM: u8;
|
|
|
|
}
|
|
|
|
pub trait CkPin<T: Instance>: OptionalPin {
|
|
|
|
const AF_NUM: u8;
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 13:34:58 +00:00
|
|
|
pub trait Instance: sealed::Instance {}
|
2021-04-25 20:35:51 +00: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> {}
|
|
|
|
|
|
|
|
impl<T: Instance> sealed::RxPin<T> for NoPin {
|
|
|
|
const AF_NUM: u8 = 0;
|
|
|
|
}
|
|
|
|
impl<T: Instance> RxPin<T> for NoPin {}
|
|
|
|
impl<T: Instance> sealed::TxPin<T> for NoPin {
|
|
|
|
const AF_NUM: u8 = 0;
|
|
|
|
}
|
|
|
|
impl<T: Instance> TxPin<T> for NoPin {}
|
|
|
|
impl<T: Instance> sealed::CtsPin<T> for NoPin {
|
|
|
|
const AF_NUM: u8 = 0;
|
|
|
|
}
|
|
|
|
impl<T: Instance> CtsPin<T> for NoPin {}
|
|
|
|
impl<T: Instance> sealed::RtsPin<T> for NoPin {
|
|
|
|
const AF_NUM: u8 = 0;
|
|
|
|
}
|
|
|
|
impl<T: Instance> RtsPin<T> for NoPin {}
|
|
|
|
impl<T: Instance> sealed::CkPin<T> for NoPin {
|
|
|
|
const AF_NUM: u8 = 0;
|
|
|
|
}
|
|
|
|
impl<T: Instance> CkPin<T> for NoPin {}
|
2021-04-14 13:34:58 +00:00
|
|
|
|
2021-04-25 20:35:51 +00:00
|
|
|
macro_rules! impl_usart {
|
2021-05-06 01:43:46 +00:00
|
|
|
($inst:ident) => {
|
2021-04-25 20:35:51 +00:00
|
|
|
impl crate::usart::sealed::Instance for peripherals::$inst {
|
2021-05-06 01:43:46 +00:00
|
|
|
fn regs(&self) -> crate::pac::usart::Usart {
|
|
|
|
crate::pac::$inst
|
2021-04-14 13:34:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-25 20:35:51 +00:00
|
|
|
impl crate::usart::Instance for peripherals::$inst {}
|
2021-04-14 13:34:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-04-25 20:35:51 +00:00
|
|
|
macro_rules! impl_usart_pin {
|
|
|
|
($inst:ident, $func:ident, $pin:ident, $num:expr) => {
|
|
|
|
impl crate::usart::sealed::$func<peripherals::$inst> for peripherals::$pin {
|
|
|
|
const AF_NUM: u8 = $num;
|
|
|
|
}
|
|
|
|
impl crate::usart::$func<peripherals::$inst> for peripherals::$pin {}
|
|
|
|
};
|
|
|
|
}
|