Skip to content

Commit

Permalink
remove PULL generic from Input
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Oct 10, 2024
1 parent 51321ac commit ef51481
Show file tree
Hide file tree
Showing 19 changed files with 298 additions and 335 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Use independent `Spi` and `SpiSlave` structures instead of `OP` generic [#462]
- Take `&Clocks` instead of `Clocks` [#498]
- Temporary replace `stm32f1` with `stm32f1-staging` [#503]
- Remove `PULL` generic from `Input` mode,
temporary disable `as_(mode)` [#512]

### Changed

Expand Down Expand Up @@ -58,6 +60,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#506]: https://github.com/stm32-rs/stm32f1xx-hal/pull/506
[#509]: https://github.com/stm32-rs/stm32f1xx-hal/pull/509
[#511]: https://github.com/stm32-rs/stm32f1xx-hal/pull/511
[#512]: https://github.com/stm32-rs/stm32f1xx-hal/pull/512

## [v0.10.0] - 2022-12-12

Expand Down
4 changes: 2 additions & 2 deletions examples/can-loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use panic_halt as _;

use cortex_m_rt::entry;
use nb::block;
use stm32f1xx_hal::{can::Can, gpio::Floating, pac, prelude::*};
use stm32f1xx_hal::{can::Can, pac, prelude::*};

#[entry]
fn main() -> ! {
Expand All @@ -25,7 +25,7 @@ fn main() -> ! {
// resonator must be used.
rcc.cfgr.use_hse(8.MHz()).freeze(&mut flash.acr);

let can = Can::<_, Floating>::new_loopback(
let can = Can::new_loopback(
dp.CAN1,
#[cfg(not(feature = "connectivity"))]
dp.USB,
Expand Down
6 changes: 3 additions & 3 deletions examples/can-rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ mod app {
use super::{enqueue_frame, PriorityFrame};
use bxcan::{filter::Mask32, ExtendedId, Fifo, Frame, Interrupts, Rx0, StandardId, Tx};
use heapless::binary_heap::{BinaryHeap, Max};
use stm32f1xx_hal::{can::Can, gpio::Floating, pac::CAN1, prelude::*};
use stm32f1xx_hal::{can::Can, pac::CAN1, prelude::*};

#[local]
struct Local {
Expand Down Expand Up @@ -89,14 +89,14 @@ mod app {
let mut afio = cx.device.AFIO.constrain();

#[cfg(not(feature = "connectivity"))]
let can = Can::<_, Floating>::new(
let can = Can::new(
cx.device.CAN1,
cx.device.USB,
(can_tx_pin, can_rx_pin, &mut afio.mapr),
);

#[cfg(feature = "connectivity")]
let can = Can::<_, Floating>::new(cx.device.CAN1, (can_tx_pin, can_rx_pin, &mut afio.mapr));
let can = Can::new(cx.device.CAN1, (can_tx_pin, can_rx_pin, &mut afio.mapr));

// APB1 (PCLK1): 16MHz, Bit rate: 1000kBit/s, Sample Point 87.5%
// Value was calculated with http://www.bittiming.can-wiki.info/
Expand Down
6 changes: 2 additions & 4 deletions examples/exti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ use stm32f1xx_hal::{pac, prelude::*};
// where the interrupt is not yet enabled (i.e. no concurrent accesses can occur).
// After enabling the interrupt, main() may not have any references to these objects any more.
// For the sake of minimalism, we do not use RTIC here, which would be the better way.
static mut LED: MaybeUninit<stm32f1xx_hal::gpio::gpioc::PC13<Output<PushPull>>> =
MaybeUninit::uninit();
static mut INT_PIN: MaybeUninit<stm32f1xx_hal::gpio::gpioa::PA7<Input<Floating>>> =
MaybeUninit::uninit();
static mut LED: MaybeUninit<stm32f1xx_hal::gpio::gpioc::PC13<Output>> = MaybeUninit::uninit();
static mut INT_PIN: MaybeUninit<stm32f1xx_hal::gpio::gpioa::PA7<Input>> = MaybeUninit::uninit();

#[interrupt]
fn EXTI9_5() {
Expand Down
6 changes: 3 additions & 3 deletions examples/exti_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use panic_halt as _;
#[rtic::app(device = stm32f1xx_hal::pac)]
mod app {
use stm32f1xx_hal::{
gpio::{gpioa::PA0, gpioc::PC13, Edge, ExtiPin, Input, Output, PullDown, PushPull},
gpio::{gpioa::PA0, gpioc::PC13, Edge, ExtiPin, Input, Output},
prelude::*,
};

Expand All @@ -17,8 +17,8 @@ mod app {

#[local]
struct Local {
button: PA0<Input<PullDown>>,
led: PC13<Output<PushPull>>,
button: PA0<Input>,
led: PC13<Output>,
}

#[init]
Expand Down
File renamed without changes.
8 changes: 2 additions & 6 deletions src/afio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::pac::{self, afio, AFIO, RCC};
use crate::rcc::{Enable, Reset};

use crate::gpio::{
Debugger, Floating, Input, PA15, {PB3, PB4},
Debugger, Input, PA15, {PB3, PB4},
};
use crate::sealed::Sealed;

Expand Down Expand Up @@ -94,11 +94,7 @@ impl MAPR {
pa15: PA15<Debugger>,
pb3: PB3<Debugger>,
pb4: PB4<Debugger>,
) -> (
PA15<Input<Floating>>,
PB3<Input<Floating>>,
PB4<Input<Floating>>,
) {
) -> (PA15<Input>, PB3<Input>, PB4<Input>) {
self.jtag_enabled = false;
// Avoid duplicating swj_cfg write code
self.modify_mapr(|_, w| w);
Expand Down
78 changes: 30 additions & 48 deletions src/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@
//! | RX | PB5 | PB12 |
use crate::afio::Remap;
use crate::gpio::{self, Alternate, Cr, Floating, Input, NoPin, PinMode, PullUp, PushPull};
use crate::gpio::{self, Alternate, Cr, Input, NoPin, PushPull};
use crate::pac::{self, RCC};

pub trait InMode {}
impl InMode for Floating {}
impl InMode for PullUp {}

pub struct Pins<TX, RX> {
pub tx: TX,
pub rx: RX,
Expand Down Expand Up @@ -72,50 +68,42 @@ macro_rules! remap {
)+
None(NoPin<PushPull>),
}
pub enum Rx<PULL> {
pub enum Rx {
$(
$RX(gpio::$RX<Input<PULL>>),
$RX(gpio::$RX<Input>),
)+
None(NoPin<PULL>),
None(NoPin<PushPull>),
}

$(
impl<PULL: InMode> From<(gpio::$TX<Alternate>, gpio::$RX<Input<PULL>>, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<PULL>> {
fn from(p: (gpio::$TX<Alternate>, gpio::$RX<Input<PULL>>, &mut <$PER as Remap>::Mapr)) -> Self {
impl From<(gpio::$TX<Alternate>, gpio::$RX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx> {
fn from(p: (gpio::$TX<Alternate>, gpio::$RX, &mut <$PER as Remap>::Mapr)) -> Self {
<$PER>::remap(p.2, $remap);
Self { tx: Tx::$TX(p.0), rx: Rx::$RX(p.1) }
}
}

impl<PULL> From<(gpio::$TX, gpio::$RX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<PULL>>
where
Input<PULL>: PinMode,
PULL: InMode,
{
impl From<(gpio::$TX, gpio::$RX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx> {
fn from(p: (gpio::$TX, gpio::$RX, &mut <$PER as Remap>::Mapr)) -> Self {
let mut cr = Cr;
let tx = p.0.into_mode(&mut cr);
let rx = p.1.into_mode(&mut cr);
let rx = p.1;
<$PER>::remap(p.2, $remap);
Self { tx: Tx::$TX(tx), rx: Rx::$RX(rx) }
}
}

impl From<(gpio::$TX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<Floating>> {
impl From<(gpio::$TX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx> {
fn from(p: (gpio::$TX, &mut <$PER as Remap>::Mapr)) -> Self {
let tx = p.0.into_mode(&mut Cr);
<$PER>::remap(p.1, $remap);
Self { tx: Tx::$TX(tx), rx: Rx::None(NoPin::new()) }
}
}

impl<PULL> From<(gpio::$RX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx<PULL>>
where
Input<PULL>: PinMode,
PULL: InMode,
{
impl From<(gpio::$RX, &mut <$PER as Remap>::Mapr)> for Pins<Tx, Rx> {
fn from(p: (gpio::$RX, &mut <$PER as Remap>::Mapr)) -> Self {
let rx = p.0.into_mode(&mut Cr);
let rx = p.0;
<$PER>::remap(p.1, $remap);
Self { tx: Tx::None(NoPin::new()), rx: Rx::$RX(rx) }
}
Expand All @@ -129,31 +117,25 @@ pub trait CanExt: Sized + Instance {
fn can(
self,
#[cfg(not(feature = "connectivity"))] usb: pac::USB,
pins: impl Into<Pins<Self::Tx, Self::Rx<Floating>>>,
) -> Can<Self, Floating>;
fn can_loopback(
self,
#[cfg(not(feature = "connectivity"))] usb: pac::USB,
) -> Can<Self, Floating>;
pins: impl Into<Pins<Self::Tx, Self::Rx>>,
) -> Can<Self>;
fn can_loopback(self, #[cfg(not(feature = "connectivity"))] usb: pac::USB) -> Can<Self>;
}

impl<CAN: Instance> CanExt for CAN {
fn can(
self,
#[cfg(not(feature = "connectivity"))] usb: pac::USB,
pins: impl Into<Pins<Self::Tx, Self::Rx<Floating>>>,
) -> Can<Self, Floating> {
pins: impl Into<Pins<Self::Tx, Self::Rx>>,
) -> Can<Self> {
Can::new(
self,
#[cfg(not(feature = "connectivity"))]
usb,
pins,
)
}
fn can_loopback(
self,
#[cfg(not(feature = "connectivity"))] usb: pac::USB,
) -> Can<Self, Floating> {
fn can_loopback(self, #[cfg(not(feature = "connectivity"))] usb: pac::USB) -> Can<Self> {
Can::new_loopback(
self,
#[cfg(not(feature = "connectivity"))]
Expand All @@ -164,35 +146,35 @@ impl<CAN: Instance> CanExt for CAN {

pub trait Instance: crate::rcc::Enable {
type Tx;
type Rx<PULL>;
type Rx;
}
impl Instance for pac::CAN1 {
type Tx = can1::Tx;
type Rx<PULL> = can1::Rx<PULL>;
type Rx = can1::Rx;
}
#[cfg(feature = "connectivity")]
impl Instance for pac::CAN2 {
type Tx = can2::Tx;
type Rx<PULL> = can2::Rx<PULL>;
type Rx = can2::Rx;
}

/// Interface to the CAN peripheral.
#[allow(unused)]
pub struct Can<CAN: Instance, PULL = Floating> {
pub struct Can<CAN: Instance> {
can: CAN,
pins: Option<Pins<CAN::Tx, CAN::Rx<PULL>>>,
pins: Option<Pins<CAN::Tx, CAN::Rx>>,
}

impl<CAN: Instance, PULL> Can<CAN, PULL> {
impl<CAN: Instance> Can<CAN> {
/// Creates a CAN interface.
///
/// CAN shares SRAM with the USB peripheral. Take ownership of USB to
/// prevent accidental shared usage.
pub fn new(
can: CAN,
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
pins: impl Into<Pins<CAN::Tx, CAN::Rx<PULL>>>,
) -> Can<CAN, PULL> {
pins: impl Into<Pins<CAN::Tx, CAN::Rx>>,
) -> Can<CAN> {
let rcc = unsafe { &(*RCC::ptr()) };
CAN::enable(rcc);

Expand All @@ -204,26 +186,26 @@ impl<CAN: Instance, PULL> Can<CAN, PULL> {
pub fn new_loopback(
can: CAN,
#[cfg(not(feature = "connectivity"))] _usb: pac::USB,
) -> Can<CAN, PULL> {
) -> Can<CAN> {
let rcc = unsafe { &(*RCC::ptr()) };
CAN::enable(rcc);

Can { can, pins: None }
}
}

unsafe impl<PULL> bxcan::Instance for Can<pac::CAN1, PULL> {
unsafe impl bxcan::Instance for Can<pac::CAN1> {
const REGISTERS: *mut bxcan::RegisterBlock = pac::CAN1::ptr() as *mut _;
}

#[cfg(feature = "connectivity")]
unsafe impl<PULL> bxcan::Instance for Can<pac::CAN2, PULL> {
unsafe impl bxcan::Instance for Can<pac::CAN2> {
const REGISTERS: *mut bxcan::RegisterBlock = pac::CAN2::ptr() as *mut _;
}

unsafe impl<PULL> bxcan::FilterOwner for Can<pac::CAN1, PULL> {
unsafe impl bxcan::FilterOwner for Can<pac::CAN1> {
const NUM_FILTER_BANKS: u8 = 28;
}

#[cfg(feature = "connectivity")]
unsafe impl<PULL> bxcan::MasterInstance for Can<pac::CAN1, PULL> {}
unsafe impl bxcan::MasterInstance for Can<pac::CAN1> {}
Loading

0 comments on commit ef51481

Please sign in to comment.