Skip to content

Commit 34ec683

Browse files
authored
Merge pull request #835 from stm32-rs/pacext
move and rename UartExt trait
2 parents 088c931 + c612945 commit 34ec683

File tree

5 files changed

+88
-76
lines changed

5 files changed

+88
-76
lines changed

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use enumflags2::{BitFlag, BitFlags};
99

10+
pub mod pacext;
11+
1012
pub use embedded_hal as hal;
1113
pub use embedded_hal_02 as hal_02;
1214

src/pacext.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use stm32f4::{Readable, Reg, RegisterSpec, Resettable, Writable, R, W};
2+
3+
pub mod uart;
4+
5+
macro_rules! wrap_r {
6+
(pub trait $TrR:ident {
7+
$(fn $f:ident(&self $(, $n:ident: u8)?) -> $fr:path;)*
8+
}) => {
9+
pub trait $TrR {
10+
$(fn $f(&self $(, $n: u8)?) -> $fr;)*
11+
}
12+
impl<REG: reg::$TrR> $TrR for R<REG> {
13+
$(
14+
#[inline(always)]
15+
fn $f(&self $(, $n: u8)?) -> $fr {
16+
REG::$f(self $(, $n)?)
17+
}
18+
)*
19+
}
20+
};
21+
}
22+
pub(crate) use wrap_r;
23+
24+
macro_rules! wrap_w {
25+
(pub trait $TrR:ident {
26+
$(fn $f:ident(&mut self $(, $n:ident: u8)?) -> $fr:path;)*
27+
}) => {
28+
pub trait $TrR<REG: reg::$TrR> {
29+
$(fn $f(&mut self $(, $n: u8)?) -> $fr;)*
30+
}
31+
32+
impl<REG: reg::$TrR> $TrR<REG> for W<REG> {
33+
$(
34+
#[inline(always)]
35+
fn $f(&mut self $(, $n: u8)?) -> $fr {
36+
REG::$f(self $(, $n)?)
37+
}
38+
)*
39+
}
40+
};
41+
}
42+
pub(crate) use wrap_w;
43+
44+
macro_rules! impl_reg {
45+
($($r:ident $(: $n:ident)? -> &$rty:path;)*) => {
46+
$(
47+
#[inline(always)]
48+
fn $r(&self $(, $n: usize)?) -> &$rty {
49+
self.$r($($n)?)
50+
}
51+
)*
52+
};
53+
}
54+
pub(crate) use impl_reg;
55+
56+
macro_rules! impl_read {
57+
($($f:ident $(: $n:ident)? -> $fty:path;)*) => {
58+
$(
59+
#[inline(always)]
60+
fn $f(r: &R<Self> $(, $n: u8)?) -> $fty {
61+
r.$f($($n)?)
62+
}
63+
)*
64+
};
65+
}
66+
pub(crate) use impl_read;
67+
68+
macro_rules! impl_write {
69+
($($f:ident $(: $n:ident)? -> $fty:path;)*) => {
70+
$(
71+
#[inline(always)]
72+
fn $f(w: &mut W<Self> $(, $n: u8)?) -> $fty {
73+
w.$f($($n)?)
74+
}
75+
)*
76+
};
77+
}
78+
pub(crate) use impl_write;

src/serial/ext.rs renamed to src/pacext/uart.rs

+3-72
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
use crate::{sealed, Sealed};
44

5+
use super::*;
56
#[cfg(feature = "uart4")]
67
use crate::pac::uart4;
78
use crate::pac::usart1;
8-
use stm32f4::{Readable, Reg, RegisterSpec, Resettable, Writable, R, W};
99

10-
pub trait UartExt: Sealed {
10+
pub trait UartRB: Sealed {
1111
fn cr1(&self) -> &usart1::CR1;
1212
fn dr(&self) -> &usart1::DR;
1313
fn brr(&self) -> &usart1::BRR;
@@ -21,43 +21,6 @@ pub trait UartExt: Sealed {
2121
fn gtpr(&self) -> &Reg<Self::GTPRrs>;
2222
}
2323

24-
macro_rules! wrap_r {
25-
(pub trait $TrR:ident {
26-
$(fn $f:ident(&self $(, $n:ident: u8)?) -> $fr:path;)*
27-
}) => {
28-
pub trait $TrR {
29-
$(fn $f(&self $(, $n: u8)?) -> $fr;)*
30-
}
31-
impl<REG: reg::$TrR> $TrR for R<REG> {
32-
$(
33-
#[inline(always)]
34-
fn $f(&self $(, $n: u8)?) -> $fr {
35-
REG::$f(self $(, $n)?)
36-
}
37-
)*
38-
}
39-
};
40-
}
41-
42-
macro_rules! wrap_w {
43-
(pub trait $TrR:ident {
44-
$(fn $f:ident(&mut self $(, $n:ident: u8)?) -> $fr:path;)*
45-
}) => {
46-
pub trait $TrR<REG: reg::$TrR> {
47-
$(fn $f(&mut self $(, $n: u8)?) -> $fr;)*
48-
}
49-
50-
impl<REG: reg::$TrR> $TrR<REG> for W<REG> {
51-
$(
52-
#[inline(always)]
53-
fn $f(&mut self $(, $n: u8)?) -> $fr {
54-
REG::$f(self $(, $n)?)
55-
}
56-
)*
57-
}
58-
};
59-
}
60-
6124
wrap_r! {
6225
pub trait SrR {
6326
fn pe(&self) -> usart1::sr::PE_R;
@@ -190,42 +153,10 @@ mod reg {
190153
}
191154
}
192155

193-
macro_rules! impl_reg {
194-
($($r:ident -> &$rty:path;)*) => {
195-
$(
196-
#[inline(always)]
197-
fn $r(&self) -> &$rty {
198-
self.$r()
199-
}
200-
)*
201-
};
202-
}
203-
204-
macro_rules! impl_read {
205-
($($f:ident $(: $n:ident)? -> $fty:path;)*) => {
206-
$(
207-
#[inline(always)]
208-
fn $f(r: &R<Self> $(, $n: u8)?) -> $fty {
209-
r.$f($($n)?)
210-
}
211-
)*
212-
};
213-
}
214-
macro_rules! impl_write {
215-
($($f:ident $(: $n:ident)? -> $fty:path;)*) => {
216-
$(
217-
#[inline(always)]
218-
fn $f(w: &mut W<Self> $(, $n: u8)?) -> $fty {
219-
w.$f($($n)?)
220-
}
221-
)*
222-
};
223-
}
224-
225156
macro_rules! impl_ext {
226157
($(#[$attr:meta])* $uart:ident) => {
227158
impl Sealed for $uart::RegisterBlock {}
228-
impl UartExt for $uart::RegisterBlock {
159+
impl UartRB for $uart::RegisterBlock {
229160
type SRrs = $uart::sr::SRrs;
230161
type CR2rs = $uart::cr2::CR2rs;
231162
type CR3rs = $uart::cr3::CR3rs;

src/serial.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ use core::fmt;
1818
use core::marker::PhantomData;
1919
use enumflags2::BitFlags;
2020

21-
pub mod ext;
22-
use ext::UartExt;
21+
use crate::pacext::uart::UartRB;
2322
mod hal_02;
2423
mod hal_1;
2524

src/serial/uart_impls.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use super::{config, config::IrdaMode, ext::*, CFlag, Error, Event, Flag};
1+
use crate::pacext::uart::{Cr3W, SrR, UartRB};
2+
3+
use super::{config, config::IrdaMode, CFlag, Error, Event, Flag};
24
use enumflags2::BitFlags;
35

4-
pub trait RegisterBlockImpl: UartExt {
6+
pub trait RegisterBlockImpl: UartRB {
57
const IRDA: bool;
68
fn configure_irda(&self, irda: IrdaMode, pclk_freq: u32);
79
fn set_stopbits(&self, bits: config::StopBits);

0 commit comments

Comments
 (0)