wip usart

This commit is contained in:
Dario Nieuwenhuis
2021-04-14 15:34:58 +02:00
parent 170536b073
commit ef4d9d243e
4 changed files with 204 additions and 2 deletions

View File

@ -1,5 +1,6 @@
use embassy_extras::peripherals;
#[rustfmt::skip]
peripherals!(
// GPIO Port A
PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15,
@ -10,6 +11,12 @@ peripherals!(
// todo more ports
// EXTI
EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5, EXTI6, EXTI7, EXTI8, EXTI9, EXTI10, EXTI11, EXTI12,
EXTI13, EXTI14, EXTI15,
EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI5, EXTI6, EXTI7, EXTI8, EXTI9, EXTI10, EXTI11, EXTI12, EXTI13, EXTI14, EXTI15,
// USART
USART1,
USART2,
USART3,
USART6,
);

View File

@ -32,5 +32,6 @@ pub mod exti;
pub mod gpio;
//pub mod rtc;
//pub mod interrupt;
pub mod usart;
pub(crate) use stm32_metapac as pac;

View File

@ -0,0 +1,64 @@
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);