Skip to content

Commit df8b0f9

Browse files
Merge pull request #49 from datdenkikniet/fix_input_pins
Update STM32F107 pin macro
2 parents 324e81d + 0866fa4 commit df8b0f9

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

src/setup.rs

Lines changed: 38 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -260,60 +260,53 @@ impl_pins!(
260260
mod stm32f1 {
261261
use super::*;
262262
use stm32f1xx_hal::gpio::{
263-
gpioa, gpiob, gpioc, gpiod, Alternate, Floating, IOPinSpeed, Input, OutputSpeed, PushPull,
263+
gpioa::*, gpiob::*, gpioc::*, gpiod::*, Alternate, Floating, IOPinSpeed, Input,
264+
OutputSpeed, PushPull,
264265
};
265266

266267
// STM32F1xx's require access to the CRL/CRH registers to change pin mode. As a result, we
267268
// require that pins are already in the necessary mode before constructing `EthPins` as it
268269
// would be inconvenient to pass CRL and CRH through to the `AlternateVeryHighSpeed` callsite.
269270

270-
type PA1 = gpioa::PA1<Input<Floating>>;
271-
type PA2 = gpioa::PA2<Alternate<PushPull>>;
272-
type PA7 = gpioa::PA7<Input<Floating>>;
273-
type PB11 = gpiob::PB11<Alternate<PushPull>>;
274-
type PB12 = gpiob::PB12<Alternate<PushPull>>;
275-
type PB13 = gpiob::PB13<Alternate<PushPull>>;
276-
type PC1 = gpioc::PC1<Alternate<PushPull>>;
277-
type PC4 = gpioc::PC4<Input<Floating>>;
278-
type PC5 = gpioc::PC5<Input<Floating>>;
279-
type PD8 = gpiod::PD8<Input<Floating>>;
280-
type PD9 = gpiod::PD9<Input<Floating>>;
281-
type PD10 = gpiod::PD10<Input<Floating>>;
282-
283-
unsafe impl RmiiRefClk for PA1 {}
284-
unsafe impl RmiiCrsDv for PA7 {}
285-
unsafe impl RmiiCrsDv for PD8 {}
286-
unsafe impl RmiiTxEN for PB11 {}
287-
unsafe impl RmiiTxD0 for PB12 {}
288-
unsafe impl RmiiTxD1 for PB13 {}
289-
unsafe impl RmiiRxD0 for PC4 {}
290-
unsafe impl RmiiRxD0 for PD9 {}
291-
unsafe impl RmiiRxD1 for PC5 {}
292-
unsafe impl RmiiRxD1 for PD10 {}
293-
294-
macro_rules! impl_alt_very_high_speed {
295-
($($PIN:ident),*) => {
271+
macro_rules! impl_pins {
272+
($($type:ident: [$(($PIN:ty, $is_input:literal)),+]),*) => {
296273
$(
297-
impl AlternateVeryHighSpeed for $PIN {
298-
fn into_af11_very_high_speed(self) {
299-
// Within this critical section, modifying the `CRL` register can
300-
// only be unsound if this critical section preempts other code
301-
// that is modifying the same register
302-
cortex_m::interrupt::free(|_| {
303-
// SAFETY: this is sound as long as the API of the HAL and structure of the CRL
304-
// struct does not change. In case the size of the `CRL` struct is changed, compilation
305-
// will fail as `mem::transmute` can only convert between types of the same size.
306-
//
307-
// This guards us from unsound behaviour introduced by point releases of the f1 hal
308-
let acrl: &mut _ = &mut unsafe { core::mem::transmute(()) };
309-
let mut pin = self.into_alternate_push_pull(acrl);
310-
pin.set_speed(acrl, IOPinSpeed::Mhz50);
311-
});
274+
$(
275+
unsafe impl $type for $PIN {}
276+
impl AlternateVeryHighSpeed for $PIN {
277+
fn into_af11_very_high_speed(self) {
278+
// Within this critical section, modifying the `CRL` register can
279+
// only be unsound if this critical section preempts other code
280+
// that is modifying the same register
281+
cortex_m::interrupt::free(|_| {
282+
// SAFETY: this is sound as long as the API of the HAL and structure of the CRL
283+
// struct does not change. In case the size of the `CRL` struct is changed, compilation
284+
// will fail as `mem::transmute` can only convert between types of the same size.
285+
//
286+
// This guards us from unsound behaviour introduced by point releases of the f1 hal
287+
let cr: &mut _ = &mut unsafe { core::mem::transmute(()) };
288+
// The speed can only be changed on output pins
289+
let mut pin = self.into_alternate_push_pull(cr);
290+
pin.set_speed(cr, IOPinSpeed::Mhz50);
291+
292+
if $is_input {
293+
pin.into_floating_input(cr);
294+
}
295+
});
296+
}
312297
}
313-
}
298+
)+
314299
)*
315-
}
300+
};
316301
}
317302

318-
impl_alt_very_high_speed!(PA1, PA2, PA7, PB11, PB12, PB13, PC1, PC4, PC5, PD8, PD9, PD10);
303+
impl_pins!(
304+
RmiiRefClk: [(PA1<Input<Floating>>, true)],
305+
RmiiCrsDv: [(PA7<Input<Floating>>, true), (PD8<Input<Floating>>, true)],
306+
RmiiTxEN: [(PB11<Alternate<PushPull>>, false)],
307+
RmiiTxD0: [(PB12<Alternate<PushPull>>, false)],
308+
RmiiTxD1: [(PB13<Alternate<PushPull>>, false)],
309+
RmiiRxD0: [(PC4<Input<Floating>>, true), (PD9<Input<Floating>>, true)],
310+
RmiiRxD1: [(PC5<Input<Floating>>, true), (PD10<Input<Floating>>, true)]
311+
);
319312
}

0 commit comments

Comments
 (0)