use core::marker::PhantomData;
use crate::rcc::AHB2;
pub trait GpioExt {
type Parts;
fn split(self, ahb: &mut AHB2) -> Self::Parts;
}
pub struct Input<MODE> {
_mode: PhantomData<MODE>,
}
pub struct Floating;
pub struct PullDown;
pub struct PullUp;
pub struct Output<MODE> {
_mode: PhantomData<MODE>,
}
pub struct PushPull;
pub struct OpenDrain;
pub struct Alternate<AF, MODE>
{
_af: PhantomData<AF>,
_mode: PhantomData<MODE>,
}
pub struct AF0;
pub struct AF1;
pub struct AF2;
pub struct AF3;
pub struct AF4;
pub struct AF5;
pub struct AF6;
pub struct AF7;
pub struct AF8;
pub struct AF9;
pub struct AF10;
pub struct AF11;
pub struct AF12;
pub struct AF13;
pub struct AF14;
pub struct AF15;
macro_rules! gpio {
($GPIOX:ident, $gpiox:ident, $gpioy:ident, $iopxenr:ident, $iopxrst:ident, $PXx:ident, [
$($PXi:ident: ($pxi:ident, $i:expr, $MODE:ty, $AFR:ident),)+
]) => {
pub mod $gpiox {
use core::marker::PhantomData;
use crate::hal::digital::{OutputPin, InputPin};
use crate::stm32::{$gpioy, $GPIOX};
use crate::rcc::AHB2;
use super::{
Alternate, AF4, AF5, AF6, AF7, AF8, AF9, Floating, GpioExt, Input, OpenDrain, Output,
PullDown, PullUp, PushPull,
};
pub struct Parts {
pub afrh: AFRH,
pub afrl: AFRL,
pub moder: MODER,
pub otyper: OTYPER,
pub pupdr: PUPDR,
$(
pub $pxi: $PXi<$MODE>,
)+
}
impl GpioExt for $GPIOX {
type Parts = Parts;
fn split(self, ahb: &mut AHB2) -> Parts {
ahb.enr().modify(|_, w| w.$iopxenr().set_bit());
ahb.rstr().modify(|_, w| w.$iopxrst().set_bit());
ahb.rstr().modify(|_, w| w.$iopxrst().clear_bit());
Parts {
afrh: AFRH { _0: () },
afrl: AFRL { _0: () },
moder: MODER { _0: () },
otyper: OTYPER { _0: () },
pupdr: PUPDR { _0: () },
$(
$pxi: $PXi { _mode: PhantomData },
)+
}
}
}
pub struct AFRL {
_0: (),
}
impl AFRL {
pub(crate) fn afr(&mut self) -> &$gpioy::AFRL {
unsafe { &(*$GPIOX::ptr()).afrl }
}
}
pub struct AFRH {
_0: (),
}
impl AFRH {
#[allow(dead_code)]
pub(crate) fn afr(&mut self) -> &$gpioy::AFRH {
unsafe { &(*$GPIOX::ptr()).afrh }
}
}
pub struct MODER {
_0: (),
}
impl MODER {
pub(crate) fn moder(&mut self) -> &$gpioy::MODER {
unsafe { &(*$GPIOX::ptr()).moder }
}
}
pub struct OTYPER {
_0: (),
}
impl OTYPER {
pub(crate) fn otyper(&mut self) -> &$gpioy::OTYPER {
unsafe { &(*$GPIOX::ptr()).otyper }
}
}
pub struct PUPDR {
_0: (),
}
impl PUPDR {
pub(crate) fn pupdr(&mut self) -> &$gpioy::PUPDR {
unsafe { &(*$GPIOX::ptr()).pupdr }
}
}
pub struct $PXx<MODE> {
i: u8,
_mode: PhantomData<MODE>,
}
impl<MODE> OutputPin for $PXx<Output<MODE>> {
fn set_high(&mut self) {
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << self.i)) }
}
fn set_low(&mut self) {
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + self.i))) }
}
}
$(
pub struct $PXi<MODE> {
_mode: PhantomData<MODE>,
}
impl<MODE> $PXi<MODE> {
pub fn into_af4(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF4, MODE>> {
let offset = 2 * $i;
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
let af = 4;
let offset = 4 * ($i % 8);
afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_af5(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF5, MODE>> {
let offset = 2 * $i;
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
let af = 5;
let offset = 4 * ($i % 8);
afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_af6(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF6, MODE>> {
let offset = 2 * $i;
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
let af = 6;
let offset = 4 * ($i % 8);
afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_af7(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF7, MODE>> {
let offset = 2 * $i;
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
let af = 7;
let offset = 4 * ($i % 8);
afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_af8(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF8, MODE>> {
let offset = 2 * $i;
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
let af = 8;
let offset = 4 * ($i % 8);
afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_af9(
self,
moder: &mut MODER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF9, MODE>> {
let offset = 2 * $i;
let mode = 0b10;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
let af = 9;
let offset = 4 * ($i % 8);
afr.afr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b1111 << offset)) | (af << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_floating_input(
self,
moder: &mut MODER,
pupdr: &mut PUPDR,
) -> $PXi<Input<Floating>> {
let offset = 2 * $i;
moder
.moder()
.modify(|r, w| unsafe { w.bits(r.bits() & !(0b11 << offset)) });
pupdr
.pupdr()
.modify(|r, w| unsafe { w.bits(r.bits() & !(0b11 << offset)) });
$PXi { _mode: PhantomData }
}
pub fn into_pull_down_input(
self,
moder: &mut MODER,
pupdr: &mut PUPDR,
) -> $PXi<Input<PullDown>> {
let offset = 2 * $i;
moder
.moder()
.modify(|r, w| unsafe { w.bits(r.bits() & !(0b11 << offset)) });
pupdr.pupdr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (0b10 << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_pull_up_input(
self,
moder: &mut MODER,
pupdr: &mut PUPDR,
) -> $PXi<Input<PullUp>> {
let offset = 2 * $i;
moder
.moder()
.modify(|r, w| unsafe { w.bits(r.bits() & !(0b11 << offset)) });
pupdr.pupdr().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (0b01 << offset))
});
$PXi { _mode: PhantomData }
}
pub fn into_open_drain_output(
self,
moder: &mut MODER,
otyper: &mut OTYPER,
) -> $PXi<Output<OpenDrain>> {
let offset = 2 * $i;
let mode = 0b01;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
otyper
.otyper()
.modify(|r, w| unsafe { w.bits(r.bits() | (0b1 << $i)) });
$PXi { _mode: PhantomData }
}
pub fn into_push_pull_output(
self,
moder: &mut MODER,
otyper: &mut OTYPER,
) -> $PXi<Output<PushPull>> {
let offset = 2 * $i;
let mode = 0b01;
moder.moder().modify(|r, w| unsafe {
w.bits((r.bits() & !(0b11 << offset)) | (mode << offset))
});
otyper
.otyper()
.modify(|r, w| unsafe { w.bits(r.bits() & !(0b1 << $i)) });
$PXi { _mode: PhantomData }
}
pub fn into_touch_sample(
self,
moder: &mut MODER,
otyper: &mut OTYPER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF9, Output<OpenDrain>>> {
let od = self.into_open_drain_output(moder, otyper);
od.into_af9(moder, afr)
}
pub fn into_touch_channel(
self,
moder: &mut MODER,
otyper: &mut OTYPER,
afr: &mut $AFR,
) -> $PXi<Alternate<AF9, Output<PushPull>>> {
let od = self.into_push_pull_output(moder, otyper);
od.into_af9(moder, afr)
}
}
impl $PXi<Output<OpenDrain>> {
pub fn internal_pull_up(&mut self, pupdr: &mut PUPDR, on: bool) {
let offset = 2 * $i;
pupdr.pupdr().modify(|r, w| unsafe {
w.bits(
(r.bits() & !(0b11 << offset)) | if on {
0b01 << offset
} else {
0
},
)
});
}
}
impl<MODE> $PXi<Output<MODE>> {
pub fn downgrade(self) -> $PXx<Output<MODE>> {
$PXx {
i: $i,
_mode: self._mode,
}
}
}
impl<MODE> OutputPin for $PXi<Output<MODE>> {
fn set_high(&mut self) {
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << $i)) }
}
fn set_low(&mut self) {
unsafe { (*$GPIOX::ptr()).bsrr.write(|w| w.bits(1 << (16 + $i))) }
}
}
impl<MODE> InputPin for $PXi<Input<MODE>> {
fn is_high(&self) -> bool {
!self.is_low()
}
fn is_low(&self) -> bool {
unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 }
}
}
)+
}
}
}
gpio!(GPIOA, gpioa, gpioa, gpioaen, gpioarst, PAx, [
PA0: (pa0, 0, Input<Floating>, AFRL),
PA1: (pa1, 1, Input<Floating>, AFRL),
PA2: (pa2, 2, Input<Floating>, AFRL),
PA3: (pa3, 3, Input<Floating>, AFRL),
PA4: (pa4, 4, Input<Floating>, AFRL),
PA5: (pa5, 5, Input<Floating>, AFRL),
PA6: (pa6, 6, Input<Floating>, AFRL),
PA7: (pa7, 7, Input<Floating>, AFRL),
PA8: (pa8, 8, Input<Floating>, AFRH),
PA9: (pa9, 9, Input<Floating>, AFRH),
PA10: (pa10, 10, Input<Floating>, AFRH),
PA11: (pa11, 11, Input<Floating>, AFRH),
PA12: (pa12, 12, Input<Floating>, AFRH),
PA13: (pa13, 13, Input<Floating>, AFRH),
PA14: (pa14, 14, Input<Floating>, AFRH),
PA15: (pa15, 15, Input<Floating>, AFRH),
]);
gpio!(GPIOB, gpiob, gpiob, gpioben, gpiobrst, PBx, [
PB0: (pb0, 0, Input<Floating>, AFRL),
PB1: (pb1, 1, Input<Floating>, AFRL),
PB2: (pb2, 2, Input<Floating>, AFRL),
PB3: (pb3, 3, Input<Floating>, AFRL),
PB4: (pb4, 4, Input<Floating>, AFRL),
PB5: (pb5, 5, Input<Floating>, AFRL),
PB6: (pb6, 6, Input<Floating>, AFRL),
PB7: (pb7, 7, Input<Floating>, AFRL),
PB8: (pb8, 8, Input<Floating>, AFRH),
PB9: (pb9, 9, Input<Floating>, AFRH),
PB10: (pb10, 10, Input<Floating>, AFRH),
PB11: (pb11, 11, Input<Floating>, AFRH),
]);
gpio!(GPIOD, gpiod, gpioc, gpioden, gpiodrst, PDx, [
PD0: (pd0, 0, Input<Floating>, AFRL),
PD1: (pd1, 1, Input<Floating>, AFRL),
PD2: (pd2, 2, Input<Floating>, AFRL),
PD3: (pd3, 3, Input<Floating>, AFRL),
PD4: (pd4, 4, Input<Floating>, AFRL),
PD5: (pd5, 5, Input<Floating>, AFRL),
PD6: (pd6, 6, Input<Floating>, AFRL),
PD7: (pd7, 7, Input<Floating>, AFRL),
PD8: (pd8, 8, Input<Floating>, AFRH),
]);
gpio!(GPIOE, gpioe, gpioc, gpioeen, gpioerst, PEx, [
PE0: (pe0, 0, Input<Floating>, AFRL),
PE1: (pe1, 1, Input<Floating>, AFRL),
PE2: (pe2, 2, Input<Floating>, AFRL),
PE3: (pe3, 3, Input<Floating>, AFRL),
PE4: (pe4, 4, Input<Floating>, AFRL),
PE5: (pe5, 5, Input<Floating>, AFRL),
PE6: (pe6, 6, Input<Floating>, AFRL),
PE7: (pe7, 7, Input<Floating>, AFRL),
PE8: (pe8, 8, Input<Floating>, AFRH),
]);