diff --git a/CHANGELOG.md b/CHANGELOG.md index b99f3e44..7cb6125b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed + +- [breaking-change] Make `Alternate` generic over `Otype` instead of separate `Alternate` and `AlternateOD` [#372] + ### Added - `count_down` constructor for `Timer` -> `CountDownTimer` without start [#382] diff --git a/examples/i2s-audio-out-dma.rs b/examples/i2s-audio-out-dma.rs index d0713018..117138cb 100644 --- a/examples/i2s-audio-out-dma.rs +++ b/examples/i2s-audio-out-dma.rs @@ -52,10 +52,11 @@ use stm32f4xx_hal::delay::Delay; use stm32f4xx_hal::dma::config::DmaConfig; use stm32f4xx_hal::dma::MemoryToPeripheral; use stm32f4xx_hal::dma::{Stream5, StreamsTuple, Transfer}; -use stm32f4xx_hal::gpio::gpioa::PA4; -use stm32f4xx_hal::gpio::gpioc::{PC10, PC12, PC7}; -use stm32f4xx_hal::gpio::Alternate; -use stm32f4xx_hal::gpio::AF6; +use stm32f4xx_hal::gpio::{ + gpioa::PA4, + gpioc::{PC10, PC12, PC7}, + Alternate, PushPull, +}; use stm32f4xx_hal::i2c::I2c; use stm32f4xx_hal::i2s::I2s; use stm32f4xx_hal::pac::{interrupt, Interrupt}; @@ -216,10 +217,10 @@ type I2sDmaTransfer = Transfer< I2s< SPI3, ( - PA4>, - PC10>, - PC7>, - PC12>, + PA4>, + PC10>, + PC7>, + PC12>, ), >, TransmitMode, diff --git a/src/can.rs b/src/can.rs index 6b2527e4..adb1ee73 100644 --- a/src/can.rs +++ b/src/can.rs @@ -20,8 +20,8 @@ pub trait Pins: sealed::Sealed { macro_rules! pins { ($($PER:ident => ($tx:ident<$txaf:literal>, $rx:ident<$rxaf:literal>),)+) => { $( - impl crate::can::sealed::Sealed for ($tx>, $rx>) {} - impl crate::can::Pins for ($tx>, $rx>) { + impl crate::can::sealed::Sealed for ($tx>, $rx>) {} + impl crate::can::Pins for ($tx>, $rx>) { type Instance = $PER; } )+ diff --git a/src/fmpi2c.rs b/src/fmpi2c.rs index 73fdc432..d29f0089 100644 --- a/src/fmpi2c.rs +++ b/src/fmpi2c.rs @@ -1,7 +1,7 @@ use core::ops::Deref; use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; -use crate::gpio::{gpiob, gpioc, gpiod, gpiof, Const, SetAlternateOD}; +use crate::gpio::{gpiob, gpioc, gpiod, gpiof, Const, OpenDrain, SetAlternate}; use crate::i2c::{Error, PinScl, PinSda}; use crate::pac::{fmpi2c1, FMPI2C1, RCC}; use crate::rcc::{Enable, Reset}; @@ -87,8 +87,8 @@ pin!(PinScl for gpiof::PF15<4>); impl FMPI2c where - SCL: PinScl> + SetAlternateOD, - SDA: PinSda> + SetAlternateOD, + SCL: PinScl> + SetAlternate, + SDA: PinSda> + SetAlternate, { pub fn new>(i2c: FMPI2C1, mut pins: (SCL, SDA), mode: M) -> Self { unsafe { diff --git a/src/fsmc_lcd/pins.rs b/src/fsmc_lcd/pins.rs index dad26abf..d8ec5ce1 100644 --- a/src/fsmc_lcd/pins.rs +++ b/src/fsmc_lcd/pins.rs @@ -605,10 +605,10 @@ mod common_pins { }; use crate::gpio::gpiof::{PF0, PF1, PF12, PF13, PF14, PF15, PF2, PF3, PF4, PF5}; use crate::gpio::gpiog::{PG0, PG1, PG10, PG12, PG13, PG2, PG3, PG4, PG5, PG9}; - use crate::gpio::Alternate; + use crate::gpio::{Alternate, PushPull}; // All FSMC/FMC pins use AF12 - type FmcAlternate = Alternate<12>; + type FmcAlternate = Alternate; impl PinD2 for PD0 {} impl PinD3 for PD1 {} @@ -725,27 +725,27 @@ mod extra_pins { use crate::gpio::gpiob::{PB12, PB14}; use crate::gpio::gpioc::{PC11, PC12, PC2, PC3, PC4, PC5, PC6}; use crate::gpio::gpiod::PD2; - use crate::gpio::Alternate; + use crate::gpio::{Alternate, PushPull}; // Most FSMC/FMC pins use AF12, but a few use AF10 - type FmcAlternate = Alternate<12>; + type FmcAlternate = Alternate; impl PinD4 for PA2 {} impl PinD5 for PA3 {} impl PinD6 for PA4 {} impl PinD7 for PA5 {} impl PinD13 for PB12 {} - impl PinD0 for PB14> {} + impl PinD0 for PB14> {} impl PinWriteEnable for PC2 {} impl PinAddress for PC3 {} impl Sealed for PC3 {} impl PinChipSelect4 for PC4 {} impl Sealed for PC4 {} impl PinReadEnable for PC5 {} - impl PinD1 for PC6> {} - impl PinD2 for PC11> {} - impl PinD3 for PC12> {} - impl PinWriteEnable for PD2> {} + impl PinD1 for PC6> {} + impl PinD2 for PC11> {} + impl PinD3 for PC12> {} + impl PinWriteEnable for PD2> {} // Sealed trait boilerplate impl Sealed for PA2 {} @@ -753,11 +753,11 @@ mod extra_pins { impl Sealed for PA4 {} impl Sealed for PA5 {} impl Sealed for PB12 {} - impl Sealed for PB14> {} + impl Sealed for PB14> {} impl Sealed for PC2 {} impl Sealed for PC5 {} - impl Sealed for PC6> {} - impl Sealed for PC11> {} - impl Sealed for PC12> {} - impl Sealed for PD2> {} + impl Sealed for PC6> {} + impl Sealed for PC11> {} + impl Sealed for PC12> {} + impl Sealed for PD2> {} } diff --git a/src/gpio.rs b/src/gpio.rs index 6e854349..15bfb4bf 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -23,7 +23,6 @@ //! Each GPIO pin can be set to various modes: //! //! - **Alternate**: Pin mode required when the pin is driven by other peripherals -//! - **AlternateOD**: Pin mode required when the pin is driven by other peripherals and has open drain //! - **Analog**: Analog input to be used with ADC. //! - Input //! - **PullUp**: Input connected to high with a weak pull up resistor. Will be high when nothing @@ -56,7 +55,7 @@ use crate::pac::EXTI; use crate::syscfg::SysCfg; mod convert; -pub(crate) use convert::{Const, SetAlternate, SetAlternateOD}; +pub(crate) use convert::{Const, SetAlternate}; mod partially_erased; pub use partially_erased::{PEPin, PartiallyErasedPin}; mod erased; @@ -83,10 +82,7 @@ pub trait PinExt { } /// Some alternate mode (type state) -pub struct Alternate; - -/// Some alternate mode in open drain configuration (type state) -pub struct AlternateOD; +pub struct Alternate(PhantomData); // Compatibility constants pub const AF0: u8 = 0; @@ -313,7 +309,7 @@ impl Pin, P, N> { } } -impl Pin, P, N> { +impl Pin, P, N> { /// Set pin speed pub fn set_speed(self, speed: Speed) -> Self { let offset = 2 * { N }; @@ -341,9 +337,9 @@ impl Pin, P, N> { } } -impl Pin, P, N> { +impl Pin, P, N> { /// Turns pin alternate configuration pin into open drain - pub fn set_open_drain(self) -> Pin, P, N> { + pub fn set_open_drain(self) -> Pin, P, N> { let offset = { N }; unsafe { (*Gpio::

::ptr()) diff --git a/src/gpio/convert.rs b/src/gpio/convert.rs index 361840a2..f5f041c1 100644 --- a/src/gpio/convert.rs +++ b/src/gpio/convert.rs @@ -8,7 +8,7 @@ impl Assert { } impl From, P, N>> - for Pin, P, N> + for Pin, P, N> { #[inline(always)] fn from(f: Pin, P, N>) -> Self { @@ -17,7 +17,7 @@ impl From, P, N>> } impl From, P, N>> - for Pin, P, N> + for Pin, P, N> { #[inline(always)] fn from(f: Pin, P, N>) -> Self { @@ -25,24 +25,26 @@ impl From, P, N> } } -impl From> for Pin, P, N> { +impl From> + for Pin, P, N> +{ #[inline(always)] fn from(f: Pin) -> Self { f.into_alternate::() } } -impl From, P, N>> - for Pin, P, N> +impl From, P, N>> + for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_alternate::() } } impl From, P, N>> - for Pin, P, N> + for Pin, P, N> { #[inline(always)] fn from(f: Pin, P, N>) -> Self { @@ -51,7 +53,7 @@ impl From, P, N>> } impl From, P, N>> - for Pin, P, N> + for Pin, P, N> { #[inline(always)] fn from(f: Pin, P, N>) -> Self { @@ -60,7 +62,7 @@ impl From, P, N> } impl From> - for Pin, P, N> + for Pin, P, N> { #[inline(always)] fn from(f: Pin) -> Self { @@ -68,11 +70,11 @@ impl From> } } -impl From, P, N>> - for Pin, P, N> +impl From, P, N>> + for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_alternate_open_drain::() } } @@ -107,20 +109,20 @@ impl From> for Pin } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_pull_down_input() } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_pull_down_input() } } @@ -153,20 +155,20 @@ impl From> for Pin, } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_pull_up_input() } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_pull_up_input() } } @@ -201,20 +203,20 @@ impl From> for Pin } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_floating_input() } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_floating_input() } } @@ -244,20 +246,20 @@ impl From> for Pin From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_open_drain_output() } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_open_drain_output() } } @@ -287,20 +289,20 @@ impl From> for Pin From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_push_pull_output() } } -impl From, P, N>> +impl From, P, N>> for Pin, P, N> { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_push_pull_output() } } @@ -319,18 +321,20 @@ impl From, P, N>> for Pin From, P, N>> for Pin { +impl From, P, N>> + for Pin +{ #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_analog() } } -impl From, P, N>> +impl From, P, N>> for Pin { #[inline(always)] - fn from(f: Pin, P, N>) -> Self { + fn from(f: Pin, P, N>) -> Self { f.into_analog() } } @@ -360,14 +364,14 @@ impl Pin { } } /// Configures the pin to operate alternate mode - pub fn into_alternate(mut self) -> Pin, P, N> { + pub fn into_alternate(mut self) -> Pin, P, N> { self.set_alternate::(); Pin::new() } /// Configures the pin to operate in alternate open drain mode #[allow(path_statements)] - pub fn into_alternate_open_drain(self) -> Pin, P, N> { + pub fn into_alternate_open_drain(self) -> Pin, P, N> { self.into_alternate::().set_open_drain() } @@ -642,15 +646,17 @@ impl PinMode for Output { pub struct Const; -pub trait SetAlternate { +pub trait SetAlternate { fn set_alt_mode(&mut self); fn restore_mode(&mut self); } -impl SetAlternate<0> for NoPin { +impl SetAlternate for NoPin { fn set_alt_mode(&mut self) {} fn restore_mode(&mut self) {} } -impl SetAlternate for Pin { +impl SetAlternate + for Pin +{ fn set_alt_mode(&mut self) { self.set_alternate::(); } @@ -660,20 +666,9 @@ impl SetAlternate for } } -impl SetAlternate for Pin, P, N> { - fn set_alt_mode(&mut self) {} - fn restore_mode(&mut self) {} -} - -pub trait SetAlternateOD { - fn set_alt_mode(&mut self); - fn restore_mode(&mut self); -} -impl SetAlternateOD<0> for NoPin { - fn set_alt_mode(&mut self) {} - fn restore_mode(&mut self) {} -} -impl SetAlternateOD for Pin { +impl SetAlternate + for Pin +{ fn set_alt_mode(&mut self) { self.set_alternate::(); unsafe { @@ -688,7 +683,16 @@ impl SetAlternateOD f } } -impl SetAlternateOD for Pin, P, N> { +impl SetAlternate + for Pin, P, N> +{ + fn set_alt_mode(&mut self) {} + fn restore_mode(&mut self) {} +} + +impl SetAlternate + for Pin, P, N> +{ fn set_alt_mode(&mut self) {} fn restore_mode(&mut self) {} } diff --git a/src/i2c.rs b/src/i2c.rs index f1399787..28b13f75 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -4,7 +4,7 @@ use embedded_hal::blocking::i2c::{Read, Write, WriteRead}; use crate::pac::i2c1; use crate::rcc::{Enable, Reset}; -use crate::gpio::{Const, SetAlternateOD}; +use crate::gpio::{Const, OpenDrain, SetAlternate}; #[cfg(feature = "i2c3")] use crate::pac::I2C3; use crate::pac::{I2C1, I2C2, RCC}; @@ -282,8 +282,8 @@ impl Instance for I2C3 {} impl I2c where I2C: Instance, - SCL: PinScl> + SetAlternateOD, - SDA: PinSda> + SetAlternateOD, + SCL: PinScl> + SetAlternate, + SDA: PinSda> + SetAlternate, { pub fn new>(i2c: I2C, mut pins: (SCL, SDA), mode: M, clocks: Clocks) -> Self { unsafe { diff --git a/src/i2s.rs b/src/i2s.rs index 0923780b..719f7be4 100644 --- a/src/i2s.rs +++ b/src/i2s.rs @@ -2,7 +2,7 @@ //! //! This module is only available if the `i2s` feature is enabled. -use crate::gpio::{Const, NoPin, SetAlternate}; +use crate::gpio::{Const, NoPin, PushPull, SetAlternate}; use stm32_i2s_v12x::{Instance, RegisterBlock}; use crate::pac::RCC; @@ -293,10 +293,10 @@ impl where SPI: I2sFreq + rcc::Enable + rcc::Reset, - WS: PinWs> + SetAlternate, - CK: PinCk> + SetAlternate, - MCLK: PinMck> + SetAlternate, - SD: PinSd> + SetAlternate, + WS: PinWs> + SetAlternate, + CK: PinCk> + SetAlternate, + MCLK: PinMck> + SetAlternate, + SD: PinSd> + SetAlternate, { /// Creates an I2s object around an SPI peripheral and pins /// diff --git a/src/otg_fs.rs b/src/otg_fs.rs index 5dda424d..a262a805 100644 --- a/src/otg_fs.rs +++ b/src/otg_fs.rs @@ -7,7 +7,7 @@ use crate::pac; use crate::gpio::{ gpioa::{PA11, PA12}, - Alternate, + Alternate, PushPull, }; use crate::rcc::{Enable, Reset}; use crate::time::Hertz; @@ -19,8 +19,8 @@ pub struct USB { pub usb_global: pac::OTG_FS_GLOBAL, pub usb_device: pac::OTG_FS_DEVICE, pub usb_pwrclk: pac::OTG_FS_PWRCLK, - pub pin_dm: PA11>, - pub pin_dp: PA12>, + pub pin_dm: PA11>, + pub pin_dp: PA12>, pub hclk: Hertz, } diff --git a/src/otg_hs.rs b/src/otg_hs.rs index 49859e1d..62a2550b 100644 --- a/src/otg_hs.rs +++ b/src/otg_hs.rs @@ -10,7 +10,7 @@ use crate::pac; use crate::gpio::{ gpiob::{PB14, PB15}, - Alternate, + Alternate, PushPull, }; use crate::rcc::{Enable, Reset}; use crate::time::Hertz; @@ -22,8 +22,8 @@ pub struct USB { pub usb_global: pac::OTG_HS_GLOBAL, pub usb_device: pac::OTG_HS_DEVICE, pub usb_pwrclk: pac::OTG_HS_PWRCLK, - pub pin_dm: PB14>, - pub pin_dp: PB15>, + pub pin_dm: PB14>, + pub pin_dp: PB15>, pub hclk: Hertz, } diff --git a/src/sdio.rs b/src/sdio.rs index cbabd5a7..4f1faa1f 100644 --- a/src/sdio.rs +++ b/src/sdio.rs @@ -1,7 +1,7 @@ //! Sdio host #[allow(unused_imports)] -use crate::gpio::{gpioa::*, gpiob::*, gpioc::*, gpiod::*, Alternate}; +use crate::gpio::{gpioa::*, gpiob::*, gpioc::*, gpiod::*, Alternate, PushPull}; use crate::pac::{self, RCC, SDIO}; use crate::rcc::{Clocks, Enable, Reset}; pub use sdio_host::{ @@ -85,32 +85,32 @@ macro_rules! pins { feature = "stm32f479" ))] pins! { - CLK: [PC12>] - CMD: [PD2>] - D0: [PC8>] - D1: [PC9>] - D2: [PC10>] - D3: [PC11>] + CLK: [PC12>] + CMD: [PD2>] + D0: [PC8>] + D1: [PC9>] + D2: [PC10>] + D3: [PC11>] } #[cfg(any(feature = "stm32f412", feature = "stm32f413", feature = "stm32f423"))] pins! { - CLK: [PB15>] - CMD: [PA6>] - D0: [PB4>, PB6>] - D1: [PA8>] - D2: [PA9>] - D3: [PB5>] + CLK: [PB15>] + CMD: [PA6>] + D0: [PB4>, PB6>] + D1: [PA8>] + D2: [PA9>] + D3: [PB5>] } #[cfg(feature = "stm32f411")] pins! { - CLK: [PB15>] - CMD: [PA6>] - D0: [PB4>, PB7>] - D1: [PA8>] - D2: [PA9>] - D3: [PB5>] + CLK: [PB15>] + CMD: [PA6>] + D0: [PB4>, PB7>] + D1: [PA8>] + D2: [PA9>] + D3: [PB5>] } #[derive(Copy, Clone, Eq, PartialEq)] diff --git a/src/serial.rs b/src/serial.rs index 0bf662a4..dd954773 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -25,7 +25,7 @@ use embedded_hal::prelude::*; use embedded_hal::serial; use nb::block; -use crate::gpio::{Const, SetAlternate}; +use crate::gpio::{Const, PushPull, SetAlternate}; #[cfg(feature = "gpiod")] use crate::gpio::gpiod; @@ -669,8 +669,8 @@ impl AsMut> for Serial { impl Serial where - TX: PinTx> + SetAlternate, - RX: PinRx> + SetAlternate, + TX: PinTx> + SetAlternate, + RX: PinRx> + SetAlternate, USART: Instance, { /* @@ -809,7 +809,7 @@ where impl Serial where - TX: PinTx> + SetAlternate, + TX: PinTx> + SetAlternate, USART: Instance, { pub fn tx( @@ -824,7 +824,7 @@ where impl Serial where - RX: PinRx> + SetAlternate, + RX: PinRx> + SetAlternate, USART: Instance, { pub fn rx( diff --git a/src/spi.rs b/src/spi.rs index b9d50215..3d4e413c 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -1,7 +1,7 @@ use core::ops::Deref; use core::ptr; -use crate::gpio::{Const, NoPin, SetAlternate}; +use crate::gpio::{Const, NoPin, PushPull, SetAlternate}; use embedded_hal::spi; pub use embedded_hal::spi::{Mode, Phase, Polarity}; @@ -417,9 +417,9 @@ impl Spi where SPI: Instance, - SCK: PinSck> + SetAlternate, - MISO: PinMiso> + SetAlternate, - MOSI: PinMosi> + SetAlternate, + SCK: PinSck> + SetAlternate, + MISO: PinMiso> + SetAlternate, + MOSI: PinMosi> + SetAlternate, { pub fn new( spi: SPI, @@ -459,9 +459,9 @@ impl Spi where SPI: Instance, - SCK: PinSck> + SetAlternate, - MISO: PinMiso> + SetAlternate, - MOSI: PinMosi> + SetAlternate, + SCK: PinSck> + SetAlternate, + MISO: PinMiso> + SetAlternate, + MOSI: PinMosi> + SetAlternate, { pub fn new_bidi( spi: SPI, @@ -501,9 +501,9 @@ impl where SPI: Instance, - SCK: PinSck> + SetAlternate, - MISO: PinMiso> + SetAlternate, - MOSI: PinMosi> + SetAlternate, + SCK: PinSck> + SetAlternate, + MISO: PinMiso> + SetAlternate, + MOSI: PinMosi> + SetAlternate, { pub fn release(mut self) -> (SPI, (SCK, MISO, MOSI)) { self.pins.0.restore_mode(); diff --git a/src/timer.rs b/src/timer.rs index c4c18db1..3326533e 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -483,7 +483,7 @@ use crate::gpio::gpiof::*; #[allow(unused)] #[cfg(feature = "gpioi")] use crate::gpio::gpioi::*; -use crate::gpio::{gpioa::*, gpiob::*, Alternate, AlternateOD}; +use crate::gpio::{gpioa::*, gpiob::*, Alternate}; #[allow(unused)] use crate::gpio::{gpioc::*, gpioh::*}; @@ -496,8 +496,7 @@ pub trait PinC4 {} macro_rules! channel_impl { ( $( $TIM:ident, $PINC:ident, $PINX:ident, $AF:literal; )+ ) => { $( - impl $PINC for $PINX> {} - impl $PINC for $PINX> {} + impl $PINC for $PINX> {} )+ }; }