Skip to content

remove NoPin, use Option #813

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- remove `NoPin`, use `Option` instead

- Fix pac `defmt` feature

## [v0.22.0] - 2024-10-04
Expand Down
8 changes: 4 additions & 4 deletions examples/analog-stopwatch-with-spi-ssd1306.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ fn main() -> ! {
//cs - pe4
//dc - pe3

let sck = gpioe.pe2;
let miso = gpioe.pe5;
let mosi = gpioe.pe6;
let sck = gpioe.pe2.into();
let miso = gpioe.pe5.into();
let mosi = gpioe.pe6.into();

let spi = Spi::new(
dp.SPI4,
(sck, miso, mosi),
(Some(sck), Some(miso), Some(mosi)),
Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
Expand Down
3 changes: 1 addition & 2 deletions examples/i2s-audio-out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use cortex_m_rt::entry;

use rtt_target::{rprintln, rtt_init_print};

use stm32f4xx_hal::gpio::NoPin;
use stm32f4xx_hal::i2s::stm32_i2s_v12x::transfer::*;
use stm32f4xx_hal::i2s::I2s;
use stm32f4xx_hal::nb::block;
Expand Down Expand Up @@ -104,7 +103,7 @@ fn main() -> ! {
.i2s_clk(61440.kHz())
.freeze();

let i2s_pins = (gpioa.pa4, gpioc.pc10, NoPin::new(), gpioc.pc12);
let i2s_pins = (gpioa.pa4, gpioc.pc10, None, gpioc.pc12);
let i2s = I2s::new(dp.SPI3, i2s_pins, &clocks);
let i2s_config = I2sTransferConfig::new_master()
.transmit()
Expand Down
8 changes: 5 additions & 3 deletions examples/ist7920-bidi-normal-spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ fn main() -> ! {
let mut led = gpioa.pa5.into_push_pull_output();
led.set_low();

let sck = gpiob.pb3.into_alternate();
let mosi = gpiob.pb5;
let sck = gpiob.pb3.into_alternate().into();
let mosi = gpiob.pb5.into();

let dc = gpiob.pb4.into_push_pull_output();
let mut res = gpiob.pb10.into_push_pull_output();
Expand All @@ -45,7 +45,9 @@ fn main() -> ! {
// Change spi transfer mode to Bidi for more efficient operations.
// let spi = Spi::new(dp.SPI1, (sck, miso, mosi), mode, 8.MHz(), &clocks).to_bidi_transfer_mode();
// or
let spi = dp.SPI1.spi_bidi((sck, mosi), mode, 8.MHz(), &clocks);
let spi = dp
.SPI1
.spi_bidi((Some(sck), Some(mosi)), mode, 8.MHz(), &clocks);

let iface = SPIInterface::new(spi, dc, cs);

Expand Down
10 changes: 5 additions & 5 deletions examples/rtic-dual-i2s-audio-in-out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ mod app {

// I2S pins: (WS, CK, MCLK, SD) for I2S2
let i2s2_pins = (
gpiob.pb12, //WS
gpiob.pb13, //CK
gpioc.pc6, //MCK
gpiob.pb15, //SD
gpiob.pb14, //ExtSD
gpiob.pb12, //WS
gpiob.pb13, //CK
Some(gpioc.pc6.into()), //MCK
gpiob.pb15, //SD
gpiob.pb14, //ExtSD
);
let i2s2 = DualI2s::new(device.SPI2, device.I2S2EXT, i2s2_pins, &clocks);
let i2s2_config = DualI2sDriverConfig::new_master()
Expand Down
12 changes: 6 additions & 6 deletions examples/rtic-i2s-audio-in-out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod app {

use super::hal;

use hal::gpio::{Edge, NoPin};
use hal::gpio::Edge;
use hal::i2s::stm32_i2s_v12x::driver::*;
use hal::i2s::I2s;
use hal::pac::Interrupt;
Expand Down Expand Up @@ -169,10 +169,10 @@ mod app {

// I2S pins: (WS, CK, MCLK, SD) for I2S2
let i2s2_pins = (
gpiob.pb12, //WS
gpiob.pb13, //CK
gpioc.pc6, //MCK
gpiob.pb15, //SD
gpiob.pb12, //WS
gpiob.pb13, //CK
Some(gpioc.pc6.into()), //MCK
gpiob.pb15, //SD
);
let i2s2 = I2s::new(device.SPI2, i2s2_pins, &clocks);
let i2s2_config = I2sDriverConfig::new_master()
Expand All @@ -187,7 +187,7 @@ mod app {
i2s2_driver.set_error_interrupt(true);

// I2S3 pins: (WS, CK, NoPin, SD) for I2S3
let i2s3_pins = (gpioa.pa4, gpioc.pc10, NoPin::new(), gpioc.pc12);
let i2s3_pins = (gpioa.pa4, gpioc.pc10, None, gpioc.pc12);
let i2s3 = I2s::new(device.SPI3, i2s3_pins, &clocks);
let i2s3_config = i2s2_config.to_slave().transmit();
let mut i2s3_driver = I2sDriver::new(i2s3, i2s3_config);
Expand Down
8 changes: 4 additions & 4 deletions examples/rtic-spi-slave-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ mod app {

let gpiob = gpiob.split();

let sck = gpiob.pb3.into_alternate();
let miso = gpiob.pb4.into_alternate();
let mosi = gpiob.pb5;
let sck = gpiob.pb3.into_alternate().into();
let miso = gpiob.pb4.into_alternate().into();
let mosi = gpiob.pb5.into();

let mode = Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
};

let mut spi3 = SpiSlave::new(spi, (sck, miso, mosi, None), mode);
let mut spi3 = SpiSlave::new(spi, (Some(sck), Some(miso), Some(mosi), None), mode);
spi3.set_internal_nss(false);

let (tx, rx) = spi3.use_dma().txrx();
Expand Down
2 changes: 1 addition & 1 deletion examples/sai-duplex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() -> ! {
num_slots: 2,
};
let tx = saia.master_tx(
(gpioe.pe2, gpioe.pe4, gpioe.pe5, gpioe.pe6),
(Some(gpioe.pe2.into()), gpioe.pe4, gpioe.pe5, gpioe.pe6),
protocol,
48.kHz(),
&clocks,
Expand Down
13 changes: 10 additions & 3 deletions examples/spi-dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,22 @@ fn main() -> ! {
.pb15
.into_alternate()
.speed(Speed::VeryHigh)
.internal_pull_up(true);
let pb13 = gpiob.pb13.into_alternate().speed(Speed::VeryHigh);
.internal_pull_up(true)
.into();
let pb13 = gpiob.pb13.into_alternate().speed(Speed::VeryHigh).into();

let mode = Mode {
polarity: Polarity::IdleLow,
phase: Phase::CaptureOnFirstTransition,
};

let spi2 = Spi::new(dp.SPI2, (pb13, NoMiso::new(), pb15), mode, 3.MHz(), &clocks);
let spi2 = Spi::new(
dp.SPI2,
(Some(pb13), None, Some(pb15)),
mode,
3.MHz(),
&clocks,
);

let buffer = cortex_m::singleton!(: [u8; ARRAY_SIZE] = [1; ARRAY_SIZE]).unwrap();

Expand Down
10 changes: 6 additions & 4 deletions examples/spi_slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ fn main() -> ! {

let gpioa = p.GPIOA.split();

let sck = gpioa.pa5.internal_resistor(Pull::Up);
let miso = gpioa.pa6.internal_resistor(Pull::Down);
let mosi = gpioa.pa7.internal_resistor(Pull::Down);
let sck = gpioa.pa5.internal_resistor(Pull::Up).into();
let miso = gpioa.pa6.internal_resistor(Pull::Down).into();
let mosi = gpioa.pa7.internal_resistor(Pull::Down).into();

// clock speed is determined by the master
let nss = gpioa.pa4.internal_resistor(Pull::Up).into();
let mut spi = p.SPI1.spi_slave((sck, miso, mosi, Some(nss)), MODE);
let mut spi = p
.SPI1
.spi_slave((Some(sck), Some(miso), Some(mosi), Some(nss)), MODE);
// alternativelly you could use software `chip select`
// let mut spi = SpiSlave::new(p.SPI1, (sck, miso, mosi, None), MODE);
// spi.set_internal_nss(false);
Expand Down
4 changes: 2 additions & 2 deletions examples/ws2812-spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use panic_halt as _;
use stm32f4xx_hal as hal;

use cortex_m_rt::entry;
use hal::{gpio::NoPin, pac, prelude::*};
use hal::{pac, prelude::*};
use smart_leds::{brightness, hsv::RGB8, SmartLedsWrite};
use ws2812_spi as ws2812;

Expand All @@ -22,7 +22,7 @@ fn main() -> ! {
let gpioa = dp.GPIOA.split();

let spi = dp.SPI1.spi(
(gpioa.pa5, NoPin::new(), gpioa.pa7),
(Some(gpioa.pa5.into()), None, Some(gpioa.pa7.into())),
ws2812::MODE,
3000.kHz(),
&clocks,
Expand Down
45 changes: 14 additions & 31 deletions src/can.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! # Controller Area Network (CAN) Interface
//!

use crate::gpio::{self, NoPin};
use crate::gpio;
use crate::pac;
use crate::rcc;

Expand Down Expand Up @@ -39,74 +39,57 @@ mod can3 {
pub trait CanExt: Sized + Instance {
fn can(self, pins: (impl Into<Self::Tx>, impl Into<Self::Rx>)) -> Can<Self>;

fn tx(self, tx_pin: impl Into<Self::Tx>) -> Can<Self>
where
NoPin: Into<Self::Rx>;
fn tx(self, tx_pin: impl Into<Self::Tx>) -> Can<Self>;

fn rx(self, rx_pin: impl Into<Self::Rx>) -> Can<Self>
where
NoPin: Into<Self::Tx>;
fn rx(self, rx_pin: impl Into<Self::Rx>) -> Can<Self>;
}

impl<CAN: Instance> CanExt for CAN {
fn can(self, pins: (impl Into<Self::Tx>, impl Into<Self::Rx>)) -> Can<Self> {
Can::new(self, pins)
}

fn tx(self, tx_pin: impl Into<Self::Tx>) -> Can<Self>
where
NoPin: Into<Self::Rx>,
{
fn tx(self, tx_pin: impl Into<Self::Tx>) -> Can<Self> {
Can::tx(self, tx_pin)
}

fn rx(self, rx_pin: impl Into<Self::Rx>) -> Can<Self>
where
NoPin: Into<Self::Tx>,
{
fn rx(self, rx_pin: impl Into<Self::Rx>) -> Can<Self> {
Can::rx(self, rx_pin)
}
}

/// Interface to the CAN peripheral.
pub struct Can<CAN: Instance> {
can: CAN,
pins: (CAN::Tx, CAN::Rx),
pins: (Option<CAN::Tx>, Option<CAN::Rx>),
}

impl<CAN: Instance> Can<CAN> {
/// Creates a CAN interface.
pub fn new(can: CAN, pins: (impl Into<CAN::Tx>, impl Into<CAN::Rx>)) -> Self {
Self::_new(can, (Some(pins.0.into()), Some(pins.1.into())))
}
fn _new(can: CAN, pins: (Option<CAN::Tx>, Option<CAN::Rx>)) -> Self {
unsafe {
CAN::enable_unchecked();
CAN::reset_unchecked();
}

let pins = (pins.0.into(), pins.1.into());

Can { can, pins }
}

pub fn release(self) -> (CAN, (CAN::Tx, CAN::Rx)) {
pub fn release(self) -> (CAN, (Option<CAN::Tx>, Option<CAN::Rx>)) {
(self.can, self.pins)
}
}

impl<CAN: Instance> Can<CAN> {
pub fn tx(usart: CAN, tx_pin: impl Into<CAN::Tx>) -> Self
where
NoPin: Into<CAN::Rx>,
{
Self::new(usart, (tx_pin, NoPin::new()))
pub fn tx(usart: CAN, tx_pin: impl Into<CAN::Tx>) -> Self {
Self::_new(usart, (Some(tx_pin.into()), None))
}
}

impl<CAN: Instance> Can<CAN> {
pub fn rx(usart: CAN, rx_pin: impl Into<CAN::Rx>) -> Self
where
NoPin: Into<CAN::Tx>,
{
Self::new(usart, (NoPin::new(), rx_pin))
pub fn rx(usart: CAN, rx_pin: impl Into<CAN::Rx>) -> Self {
Self::_new(usart, (None, Some(rx_pin.into())))
}
}

Expand Down
10 changes: 0 additions & 10 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,6 @@ pub use embedded_hal_02::digital::v2::PinState;

use core::fmt;

/// A filler pin type
#[derive(Debug, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct NoPin<Otype = PushPull>(PhantomData<Otype>);
impl<Otype> NoPin<Otype> {
pub fn new() -> Self {
Self(PhantomData)
}
}

/// Extension trait to split a GPIO peripheral in independent pins and registers
pub trait GpioExt {
/// The parts to split the GPIO into
Expand Down
28 changes: 2 additions & 26 deletions src/gpio/alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,13 @@ macro_rules! extipin {
use extipin;

macro_rules! pin {
( $($(#[$docs:meta])* <$name:ident, $Otype:ident> for $(no: $NoPin:ident,)? [$(
( $($(#[$docs:meta])* <$name:ident, $Otype:ident> for [$(
$(#[$attr:meta])* $PX:ident<$A:literal $(, Speed::$Speed:ident)?>,
)*],)*) => {
$(
#[derive(Debug)]
$(#[$docs])*
pub enum $name {
$(
None($NoPin<$Otype>),
)?

$(
$(#[$attr])*
$PX(gpio::$PX<$crate::gpio::Alternate<$A, $Otype>>),
Expand Down Expand Up @@ -128,14 +124,6 @@ macro_rules! pin {
extipin! { $( $(#[$attr])* $PX, )* }
}

$(
impl From<$NoPin<$Otype>> for $name {
fn from(p: $NoPin<$Otype>) -> Self {
Self::None(p)
}
}
)?

$(
$(#[$attr])*
impl<MODE> From<gpio::$PX<MODE>> for $name
Expand Down Expand Up @@ -175,17 +163,13 @@ macro_rules! pin {
)*
};

( $($(#[$docs:meta])* <$name:ident> default:$DefaultOtype:ident for $(no: $NoPin:ident,)? [$(
( $($(#[$docs:meta])* <$name:ident> default:$DefaultOtype:ident for [$(
$(#[$attr:meta])* $PX:ident<$A:literal>,
)*],)*) => {
$(
#[derive(Debug)]
$(#[$docs])*
pub enum $name<Otype = $DefaultOtype> {
$(
None($NoPin<Otype>),
)?

$(
$(#[$attr])*
$PX(gpio::$PX<$crate::gpio::Alternate<$A, Otype>>),
Expand Down Expand Up @@ -238,14 +222,6 @@ macro_rules! pin {
extipin! { $( $(#[$attr])* $PX, )* }
}

$(
impl<Otype> From<$NoPin<Otype>> for $name<Otype> {
fn from(p: $NoPin<Otype>) -> Self {
Self::None(p)
}
}
)?

$(
$(#[$attr])*
impl<MODE, Otype> From<gpio::$PX<MODE>> for $name<Otype>
Expand Down
Loading