Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generic IntoAf #308

Merged
merged 5 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ rustc-args = ["--cfg", "docsrs"]

[dependencies]
cfg-if = "1.0.0"
cortex-m = "0.7.2"
cortex-m = "0.7.4"
cortex-m-rt = "0.7"
defmt = { version = ">=0.2.3, <0.4.0", optional = true }
embedded-dma = "0.1.2"
Expand All @@ -55,18 +55,17 @@ enumset = { version = "1.0.6", optional = true}
bare-metal = "0.2.5"

[dev-dependencies]
cortex-m = "0.7.2"
cortex-m-semihosting = "0.3.7"
defmt-rtt = "0.3.0"
defmt-test = "0.3.0"
panic-probe = "0.3.0"
panic-semihosting = "0.5.6"
usbd-serial = "0.1.1"
usb-device = "0.2.8"
cortex-m-rtic = "=0.6.0-rc.4, <0.6.0-rc.5"
systick-monotonic = "0.1.0-rc.2"
cortex-m-rtic = "1.0"
systick-monotonic = "1.0"
panic-rtt-target = { version = "0.1", features = ["cortex-m"] }
rtt-target = { version = "0.3.0", features = ["cortex-m"] }
rtt-target = { version = "0.3.1", features = ["cortex-m"] }

[build-dependencies]
slice-group-by = "0.2.6"
Expand Down
13 changes: 6 additions & 7 deletions examples/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,20 @@ fn main() -> ! {
// Configure CAN RX and TX pins (AF9)
let rx = gpioa
.pa11
.into_af9_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
let tx = gpioa
.pa12
.into_af9_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);

// Initialize the CAN peripheral
let mut can = Can::new(dp.CAN, tx, rx, &mut rcc.apb1);

// Use loopback mode: No pins need to be assigned to peripheral.
// APB1 (PCLK1): 64MHz, Bit rate: 500kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
can.modify_config()
let mut can = bxcan::Can::builder(Can::new(dp.CAN, tx, rx, &mut rcc.apb1))
.set_bit_timing(0x001c_0003)
.set_loopback(false)
.set_silent(false);
.set_silent(false)
.leave_disabled();

let mut filters = can.modify_filters();

Expand All @@ -67,7 +66,7 @@ fn main() -> ! {
drop(filters);

// Sync to the bus and start normal operation.
block!(can.enable()).ok();
block!(can.enable_non_blocking()).ok();

let mut led0 = gpiob
.pb15
Expand Down
4 changes: 2 additions & 2 deletions examples/i2c_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ fn main() -> ! {
let mut scl =
gpiob
.pb6
.into_af4_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_af_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut sda =
gpiob
.pb7
.into_af4_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_af_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
scl.internal_pull_up(&mut gpiob.pupdr, true);
sda.internal_pull_up(&mut gpiob.pupdr, true);
let mut i2c = hal::i2c::I2c::new(
Expand Down
20 changes: 10 additions & 10 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,38 @@ fn main() -> ! {
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
let pa4 = gpioa
.pa4
.into_af2_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
let pa6 = gpioa
.pa6
.into_af2_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
let pa7 = gpioa
.pa7
.into_af2_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);

let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let pb0 = gpiob
.pb0
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb1 = gpiob
.pb1
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb4 = gpiob
.pb4
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb5 = gpiob
.pb5
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb8 = gpiob
.pb8
.into_af1_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
let pb10 = gpiob
.pb10
.into_af1_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);

let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
let pc10 = gpioc
.pc10
.into_af4_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);

// TIM3
//
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ fn main() -> ! {
let pins = (
gpioa
.pa9
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
gpioa
.pa10
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
);
let serial = Serial::new(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
let (tx, rx) = serial.split();
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_echo_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ mod app {
// configure this pin to make use of its `USART1_TX` alternative function
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
gpioa
// Rx pin
.pa10
// configure this pin to make use of its `USART1_RX` alternative function
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
);
pins.1.internal_pull_up(&mut gpioa.pupdr, true);
let mut serial: SerialType =
Expand Down
6 changes: 3 additions & 3 deletions examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ fn main() -> ! {
// Configure pins for SPI
let sck = gpioc
.pc10
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let miso = gpioc
.pc11
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let mosi = gpioc
.pc12
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);

let mut spi = Spi::new(dp.SPI3, (sck, miso, mosi), 3.MHz(), clocks, &mut rcc.apb1);

Expand Down
9 changes: 4 additions & 5 deletions examples/usb_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,10 @@ fn main() -> ! {
usb_dp.set_low().ok();
delay(clocks.sysclk().0 / 100);

let usb_dm =
gpioa
.pa11
.into_af14_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
let usb_dp = usb_dp.into_af14_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
let usb_dm = gpioa
.pa11
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
let usb_dp = usb_dp.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);

let usb = Peripheral {
usb: dp.USB,
Expand Down
4 changes: 2 additions & 2 deletions src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ where
Rx: RxPin,
{
/// Create a new `bxcan::CAN` instance.
pub fn new(can: pac::CAN, tx: Tx, rx: Rx, apb1: &mut APB1) -> bxcan::Can<Self> {
pub fn new(can: pac::CAN, tx: Tx, rx: Rx, apb1: &mut APB1) -> Self {
pac::CAN::enable(apb1);
pac::CAN::reset(apb1);

bxcan::Can::builder(Can { can, tx, rx }).enable()
Can { can, tx, rx }
}

/// Releases the CAN peripheral and associated pins
Expand Down
82 changes: 52 additions & 30 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,21 +159,11 @@ pub mod marker {
/// Marker trait for active pin modes
pub trait Active {}

macro_rules! af_marker_trait {
([$($i:literal),+ $(,)?]) => {
paste::paste! {
$(
#[doc = "Marker trait for pins with alternate function " $i " mapping"]
pub trait [<IntoAf $i>] {
/// Associated AFR register
type AFR: super::Afr;
}
)+
}
};
/// Marker trait for pins with alternate function `A` mapping
pub trait IntoAf<const A: u8> {
/// Associated AFR register
type AFR: super::Afr;
}

af_marker_trait!([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
}

/// Runtime defined GPIO port (type state)
Expand Down Expand Up @@ -673,16 +663,54 @@ where
}
}

impl<Gpio, Index, Mode> Pin<Gpio, Index, Mode>
where
Gpio: marker::GpioStatic,
Index: marker::Index,
{
/// Configures the pin to operate as an alternate function push-pull output pin
pub fn into_af_push_pull<const A: u8>(
self,
moder: &mut Gpio::MODER,
otyper: &mut Gpio::OTYPER,
afr: &mut <Self as marker::IntoAf<A>>::AFR,
) -> Pin<Gpio, Index, Alternate<PushPull, A>>
where
Self: marker::IntoAf<A>,
{
moder.alternate(self.index.index());
otyper.push_pull(self.index.index());
afr.afx(self.index.index(), A);
self.into_mode()
}

/// Configures the pin to operate as an alternate function open-drain output pin
pub fn into_af_open_drain<const A: u8>(
self,
moder: &mut Gpio::MODER,
otyper: &mut Gpio::OTYPER,
afr: &mut <Self as marker::IntoAf<A>>::AFR,
) -> Pin<Gpio, Index, Alternate<OpenDrain, A>>
where
Self: marker::IntoAf<A>,
{
moder.alternate(self.index.index());
otyper.open_drain(self.index.index());
afr.afx(self.index.index(), A);
self.into_mode()
}
}

macro_rules! af {
($i:literal, $AFi:ident, $IntoAfi:ident, $into_afi_push_pull:ident, $into_afi_open_drain:ident) => {
($i:literal, $AFi:ident, $into_afi_push_pull:ident, $into_afi_open_drain:ident) => {
paste::paste! {
#[doc = "Alternate function " $i " (type state)"]
pub type $AFi<Otype> = Alternate<Otype, $i>;
}

impl<Gpio, Index, Mode> Pin<Gpio, Index, Mode>
where
Self: marker::$IntoAfi,
Self: marker::IntoAf<$i>,
Gpio: marker::GpioStatic,
Index: marker::Index,
{
Expand All @@ -691,33 +719,27 @@ macro_rules! af {
self,
moder: &mut Gpio::MODER,
otyper: &mut Gpio::OTYPER,
afr: &mut <Self as marker::$IntoAfi>::AFR,
afr: &mut <Self as marker::IntoAf<$i>>::AFR,
) -> Pin<Gpio, Index, $AFi<PushPull>> {
moder.alternate(self.index.index());
otyper.push_pull(self.index.index());
afr.afx(self.index.index(), $i);
self.into_mode()
self.into_af_push_pull::<$i>(moder, otyper, afr)
}

/// Configures the pin to operate as an alternate function open-drain output pin
pub fn $into_afi_open_drain(
burrbull marked this conversation as resolved.
Show resolved Hide resolved
self,
moder: &mut Gpio::MODER,
otyper: &mut Gpio::OTYPER,
afr: &mut <Self as marker::$IntoAfi>::AFR,
afr: &mut <Self as marker::IntoAf<$i>>::AFR,
) -> Pin<Gpio, Index, $AFi<OpenDrain>> {
moder.alternate(self.index.index());
otyper.open_drain(self.index.index());
afr.afx(self.index.index(), $i);
self.into_mode()
self.into_af_open_drain::<$i>(moder, otyper, afr)
}
}
};

([$($i:literal),+ $(,)?]) => {
paste::paste! {
$(
af!($i, [<AF $i>], [<IntoAf $i>], [<into_af $i _push_pull>], [<into_af $i _open_drain>]);
af!($i, [<AF $i>], [<into_af $i _push_pull>], [<into_af $i _open_drain>]);
)+
}
};
Expand Down Expand Up @@ -787,7 +809,7 @@ macro_rules! gpio {
partially_erased_pin: $PXx:ident,
pins: [$(
$i:literal => (
$PXi:ident, $pxi:ident, $MODE:ty, $AFR:ident, [$($IntoAfi:ident),*],
$PXi:ident, $pxi:ident, $MODE:ty, $AFR:ident, [$($af:literal),*],
),
)+],
}) => {
Expand Down Expand Up @@ -825,7 +847,7 @@ macro_rules! gpio {
pub type $PXi<Mode> = Pin<$Gpiox, U<$i>, Mode>;

$(
impl<Mode> marker::$IntoAfi for $PXi<Mode> {
impl<Mode> marker::IntoAf<$af> for $PXi<Mode> {
type AFR = $gpiox::$AFR;
}
)*
Expand Down Expand Up @@ -1003,7 +1025,7 @@ macro_rules! gpio {
partially_erased_pin: [<P $X x>],
pins: [$(
$i => (
[<P $X $i>], [<p $x $i>], $MODE, [<AFR $LH>], [$([<IntoAf $af>]),*],
[<P $X $i>], [<p $x $i>], $MODE, [<AFR $LH>], [$($af),*],
),
)+],
});
Expand Down
4 changes: 2 additions & 2 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl MonoTimer {
/// Returns an `Instant` corresponding to "now"
pub fn now(&self) -> Instant {
Instant {
now: DWT::get_cycle_count(),
now: DWT::cycle_count(),
}
}
}
Expand All @@ -76,7 +76,7 @@ pub struct Instant {
impl Instant {
/// Ticks elapsed since the `Instant` was created
pub fn elapsed(self) -> u32 {
DWT::get_cycle_count().wrapping_sub(self.now)
DWT::cycle_count().wrapping_sub(self.now)
}
}

Expand Down