embassy/embassy-stm32/src/usart.rs
Dario Nieuwenhuis ef4d9d243e wip usart
2021-04-20 02:44:55 +02:00

65 lines
1.4 KiB
Rust

use embassy::util::PeripheralBorrow;
use crate::pac::usart_v1::{regs, vals, Usart};
use crate::peripherals;
mod sealed {
use super::*;
pub trait Instance {
fn regs(&self) -> Usart;
}
}
#[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(
inner: impl PeripheralBorrow<Target = T>,
tx: impl PeripheralBorrow<Target = impl TxPin<T>>,
rx: impl PeripheralBorrow<Target = impl RxPin<T>>,
cts: impl PeripheralBorrow<Target = impl CtsPin<T>>,
rts: impl PeripheralBorrow<Target = impl RtsPin<T>>,
config: Config,
) -> Self {
unborrow!(inner, tx, rx, cts, rts);
}
}
pub trait Instance: sealed::Instance {}
macro_rules! impl_instance {
($type:ident, $addr:expr) => {
impl sealed::Instance for peripherals::$type {
fn regs(&self) -> Usart {
Usart($addr as _)
}
}
impl Instance for peripherals::$type {}
};
}
impl_instance!(USART1, 0x40011000);
impl_instance!(USART2, 0x40004400);
impl_instance!(USART3, 0x40004800);
impl_instance!(USART6, 0x40011400);