diff --git a/src/lib.rs b/src/lib.rs index 1f12937f..95c9d484 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,8 @@ use enumflags2::{BitFlag, BitFlags}; +pub mod pacext; + pub use embedded_hal as hal; pub use embedded_hal_02 as hal_02; diff --git a/src/pacext.rs b/src/pacext.rs new file mode 100644 index 00000000..33b39106 --- /dev/null +++ b/src/pacext.rs @@ -0,0 +1,78 @@ +use stm32f4::{Readable, Reg, RegisterSpec, Resettable, Writable, R, W}; + +pub mod uart; + +macro_rules! wrap_r { + (pub trait $TrR:ident { + $(fn $f:ident(&self $(, $n:ident: u8)?) -> $fr:path;)* + }) => { + pub trait $TrR { + $(fn $f(&self $(, $n: u8)?) -> $fr;)* + } + impl $TrR for R { + $( + #[inline(always)] + fn $f(&self $(, $n: u8)?) -> $fr { + REG::$f(self $(, $n)?) + } + )* + } + }; +} +pub(crate) use wrap_r; + +macro_rules! wrap_w { + (pub trait $TrR:ident { + $(fn $f:ident(&mut self $(, $n:ident: u8)?) -> $fr:path;)* + }) => { + pub trait $TrR { + $(fn $f(&mut self $(, $n: u8)?) -> $fr;)* + } + + impl $TrR for W { + $( + #[inline(always)] + fn $f(&mut self $(, $n: u8)?) -> $fr { + REG::$f(self $(, $n)?) + } + )* + } + }; +} +pub(crate) use wrap_w; + +macro_rules! impl_reg { + ($($r:ident $(: $n:ident)? -> &$rty:path;)*) => { + $( + #[inline(always)] + fn $r(&self $(, $n: usize)?) -> &$rty { + self.$r($($n)?) + } + )* + }; +} +pub(crate) use impl_reg; + +macro_rules! impl_read { + ($($f:ident $(: $n:ident)? -> $fty:path;)*) => { + $( + #[inline(always)] + fn $f(r: &R $(, $n: u8)?) -> $fty { + r.$f($($n)?) + } + )* + }; +} +pub(crate) use impl_read; + +macro_rules! impl_write { + ($($f:ident $(: $n:ident)? -> $fty:path;)*) => { + $( + #[inline(always)] + fn $f(w: &mut W $(, $n: u8)?) -> $fty { + w.$f($($n)?) + } + )* + }; +} +pub(crate) use impl_write; diff --git a/src/serial/ext.rs b/src/pacext/uart.rs similarity index 82% rename from src/serial/ext.rs rename to src/pacext/uart.rs index 848c1a68..d231e22f 100644 --- a/src/serial/ext.rs +++ b/src/pacext/uart.rs @@ -2,12 +2,12 @@ use crate::{sealed, Sealed}; +use super::*; #[cfg(feature = "uart4")] use crate::pac::uart4; use crate::pac::usart1; -use stm32f4::{Readable, Reg, RegisterSpec, Resettable, Writable, R, W}; -pub trait UartExt: Sealed { +pub trait UartRB: Sealed { fn cr1(&self) -> &usart1::CR1; fn dr(&self) -> &usart1::DR; fn brr(&self) -> &usart1::BRR; @@ -21,43 +21,6 @@ pub trait UartExt: Sealed { fn gtpr(&self) -> &Reg; } -macro_rules! wrap_r { - (pub trait $TrR:ident { - $(fn $f:ident(&self $(, $n:ident: u8)?) -> $fr:path;)* - }) => { - pub trait $TrR { - $(fn $f(&self $(, $n: u8)?) -> $fr;)* - } - impl $TrR for R { - $( - #[inline(always)] - fn $f(&self $(, $n: u8)?) -> $fr { - REG::$f(self $(, $n)?) - } - )* - } - }; -} - -macro_rules! wrap_w { - (pub trait $TrR:ident { - $(fn $f:ident(&mut self $(, $n:ident: u8)?) -> $fr:path;)* - }) => { - pub trait $TrR { - $(fn $f(&mut self $(, $n: u8)?) -> $fr;)* - } - - impl $TrR for W { - $( - #[inline(always)] - fn $f(&mut self $(, $n: u8)?) -> $fr { - REG::$f(self $(, $n)?) - } - )* - } - }; -} - wrap_r! { pub trait SrR { fn pe(&self) -> usart1::sr::PE_R; @@ -190,42 +153,10 @@ mod reg { } } -macro_rules! impl_reg { - ($($r:ident -> &$rty:path;)*) => { - $( - #[inline(always)] - fn $r(&self) -> &$rty { - self.$r() - } - )* - }; -} - -macro_rules! impl_read { - ($($f:ident $(: $n:ident)? -> $fty:path;)*) => { - $( - #[inline(always)] - fn $f(r: &R $(, $n: u8)?) -> $fty { - r.$f($($n)?) - } - )* - }; -} -macro_rules! impl_write { - ($($f:ident $(: $n:ident)? -> $fty:path;)*) => { - $( - #[inline(always)] - fn $f(w: &mut W $(, $n: u8)?) -> $fty { - w.$f($($n)?) - } - )* - }; -} - macro_rules! impl_ext { ($(#[$attr:meta])* $uart:ident) => { impl Sealed for $uart::RegisterBlock {} - impl UartExt for $uart::RegisterBlock { + impl UartRB for $uart::RegisterBlock { type SRrs = $uart::sr::SRrs; type CR2rs = $uart::cr2::CR2rs; type CR3rs = $uart::cr3::CR3rs; diff --git a/src/serial.rs b/src/serial.rs index d3b3f9a5..06256733 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -18,8 +18,7 @@ use core::fmt; use core::marker::PhantomData; use enumflags2::BitFlags; -pub mod ext; -use ext::UartExt; +use crate::pacext::uart::UartRB; mod hal_02; mod hal_1; diff --git a/src/serial/uart_impls.rs b/src/serial/uart_impls.rs index 9e7cc23e..34109cf9 100644 --- a/src/serial/uart_impls.rs +++ b/src/serial/uart_impls.rs @@ -1,7 +1,9 @@ -use super::{config, config::IrdaMode, ext::*, CFlag, Error, Event, Flag}; +use crate::pacext::uart::{Cr3W, SrR, UartRB}; + +use super::{config, config::IrdaMode, CFlag, Error, Event, Flag}; use enumflags2::BitFlags; -pub trait RegisterBlockImpl: UartExt { +pub trait RegisterBlockImpl: UartRB { const IRDA: bool; fn configure_irda(&self, irda: IrdaMode, pclk_freq: u32); fn set_stopbits(&self, bits: config::StopBits);