Skip to content

Commit 81912b0

Browse files
committed
OutputPort
1 parent 69357c0 commit 81912b0

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/gpio.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ use crate::syscfg::SysCfg;
6262
mod alt;
6363
pub(crate) use alt::{Const, PinA, SetAlternate};
6464
mod convert;
65+
pub mod unproven;
66+
6567
use convert::PinMode;
6668
mod partially_erased;
6769
pub use partially_erased::{PEPin, PartiallyErasedPin};

src/gpio/unproven.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use super::*;
2+
3+
pub trait OutPort {
4+
type Target;
5+
fn outport(self) -> Self::Target;
6+
}
7+
8+
macro_rules! out_port {
9+
( $name:ident => ( $($i:literal),+ ), ( $($N:ident),+ ), ( $($d:ident),* )) => {
10+
pub struct $name<const P: char $(, const $N: u8)+> {
11+
$(pub $d: Pin<Output<PushPull>, P, $N>,)+
12+
}
13+
14+
impl<const P: char $(, const $N: u8)+> OutPort for ($(Pin<Output<PushPull>, P, $N>),+) {
15+
type Target = $name<P $(, $N)+>;
16+
fn outport(self) -> Self::Target {
17+
let ($($d),+) = self;
18+
Self::Target { $($d),+ }
19+
}
20+
}
21+
22+
impl<const P: char $(, const $N: u8)+> $name<P $(, $N)+> {
23+
pub const fn new(
24+
$($d: Pin<Output<PushPull>, P, $N>,)+
25+
) -> Self {
26+
Self { $($d),+ }
27+
}
28+
const fn value_for_write_bsrr(val: u32) -> u32 {
29+
$(let $d = ((val >> $i) & 0b1) != 0;)+
30+
let r = 0;
31+
$(let r = r | (1 << (if $d { $N } else { $N + 16 }));)+
32+
r
33+
}
34+
pub fn write_u8(&mut self, word: u8) {
35+
unsafe {
36+
(*Gpio::<P>::ptr())
37+
.bsrr
38+
.write(|w| w.bits(Self::value_for_write_bsrr(word as u32)))
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
out_port!(OutPort2 => (0, 1), (N0, N1), (d0, d1));
46+
out_port!(OutPort3 => (0, 1, 2), (N0, N1, N2), (d0, d1, d2));
47+
out_port!(OutPort4 => (0, 1, 2, 3), (N0, N1, N2, N3), (d0, d1, d2, d3));
48+
out_port!(OutPort5 => (0, 1, 2, 3, 4), (N0, N1, N2, N3, N4), (d0, d1, d2, d3, d4));
49+
out_port!(OutPort6 => (0, 1, 2, 3, 4, 5), (N0, N1, N2, N3, N4, N5), (d0, d1, d2, d3, d4, d5));
50+
out_port!(OutPort7 => (0, 1, 2, 3, 4, 5, 6), (N0, N1, N2, N3, N4, N5, N6), (d0, d1, d2, d3, d4, d5, d6));
51+
out_port!(OutPort8 => (0, 1, 2, 3, 4, 5, 6, 7), (N0, N1, N2, N3, N4, N5, N6, N7), (d0, d1, d2, d3, d4, d5, d6, d7));

0 commit comments

Comments
 (0)