Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
78 changes: 78 additions & 0 deletions src/pacext.rs
Original file line number Diff line number Diff line change
@@ -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<REG: reg::$TrR> $TrR for R<REG> {
$(
#[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<REG: reg::$TrR> {
$(fn $f(&mut self $(, $n: u8)?) -> $fr;)*
}

impl<REG: reg::$TrR> $TrR<REG> for W<REG> {
$(
#[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<Self> $(, $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<Self> $(, $n: u8)?) -> $fty {
w.$f($($n)?)
}
)*
};
}
pub(crate) use impl_write;
75 changes: 3 additions & 72 deletions src/serial/ext.rs → src/pacext/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,43 +21,6 @@ pub trait UartExt: Sealed {
fn gtpr(&self) -> &Reg<Self::GTPRrs>;
}

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<REG: reg::$TrR> $TrR for R<REG> {
$(
#[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<REG: reg::$TrR> {
$(fn $f(&mut self $(, $n: u8)?) -> $fr;)*
}

impl<REG: reg::$TrR> $TrR<REG> for W<REG> {
$(
#[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;
Expand Down Expand Up @@ -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<Self> $(, $n: u8)?) -> $fty {
r.$f($($n)?)
}
)*
};
}
macro_rules! impl_write {
($($f:ident $(: $n:ident)? -> $fty:path;)*) => {
$(
#[inline(always)]
fn $f(w: &mut W<Self> $(, $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;
Expand Down
3 changes: 1 addition & 2 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions src/serial/uart_impls.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
Loading