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

ReadPin, PinSpeed & PinPull traits #623

Merged
merged 1 commit into from
May 3, 2023
Merged
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,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Add `ReadPin`, `PinSpeed` & `PinPull` traits [#623]
- Add autoimplementations of `DMASet` [#614]
- Simplify `gpio::Outport` [#611]
- rcc `enable_unchecked`, timer features [#618]
Expand Down Expand Up @@ -38,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
[#614]: https://github.com/stm32-rs/stm32f4xx-hal/pull/614
[#617]: https://github.com/stm32-rs/stm32f4xx-hal/pull/617
[#618]: https://github.com/stm32-rs/stm32f4xx-hal/pull/618
[#623]: https://github.com/stm32-rs/stm32f4xx-hal/pull/623

## [v0.15.0] - 2023-03-13

Expand Down
1 change: 1 addition & 0 deletions examples/rtic-i2s-audio-in-out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod app {
use hal::pac::Interrupt;
use hal::pac::{EXTI, SPI2, SPI3};
use hal::prelude::*;
use stm32f4xx_hal::gpio::ReadPin;

use heapless::spsc::*;

Expand Down
48 changes: 48 additions & 0 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,26 @@ impl<const P: char, const N: u8, MODE> PinExt for Pin<P, N, MODE> {
}
}

pub trait PinSpeed {
/// Set pin speed
fn set_speed(&mut self, speed: Speed);
}

pub trait PinPull {
/// Set the internal pull-up and pull-down resistor
fn set_internal_resistor(&mut self, resistor: Pull);
}

impl<const P: char, const N: u8, MODE> PinSpeed for Pin<P, N, MODE>
where
MODE: marker::OutputSpeed,
{
#[inline(always)]
fn set_speed(&mut self, speed: Speed) {
self.set_speed(speed)
}
}

impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
where
MODE: marker::OutputSpeed,
Expand All @@ -302,6 +322,16 @@ where
}
}

impl<const P: char, const N: u8, MODE> PinPull for Pin<P, N, MODE>
where
MODE: marker::Active,
{
#[inline(always)]
fn set_internal_resistor(&mut self, resistor: Pull) {
self.set_internal_resistor(resistor)
}
}

impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
where
MODE: marker::Active,
Expand Down Expand Up @@ -467,6 +497,24 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, Output<MODE>> {
}
}

pub trait ReadPin {
#[inline(always)]
fn is_high(&self) -> bool {
!self.is_low()
}
fn is_low(&self) -> bool;
}

impl<const P: char, const N: u8, MODE> ReadPin for Pin<P, N, MODE>
where
MODE: marker::Readable,
{
#[inline(always)]
fn is_low(&self) -> bool {
self.is_low()
}
}

impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
where
MODE: marker::Readable,
Expand Down
108 changes: 78 additions & 30 deletions src/gpio/alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use f4::*;

macro_rules! extipin {
($( $(#[$attr:meta])* $PX:ident,)*) => {
fn make_interrupt_source(&mut self, _syscfg: &mut SysCfg) {
fn make_interrupt_source(&mut self, _syscfg: &mut $crate::syscfg::SysCfg) {
match self {
$(
$(#[$attr])*
Expand All @@ -14,7 +14,7 @@ macro_rules! extipin {

}

fn trigger_on_edge(&mut self, _exti: &mut EXTI, _level: Edge) {
fn trigger_on_edge(&mut self, _exti: &mut $crate::pac::EXTI, _level: $crate::gpio::Edge) {
match self {
$(
$(#[$attr])*
Expand All @@ -24,7 +24,7 @@ macro_rules! extipin {
}
}

fn enable_interrupt(&mut self, _exti: &mut EXTI) {
fn enable_interrupt(&mut self, _exti: &mut $crate::pac::EXTI) {
match self {
$(
$(#[$attr])*
Expand All @@ -33,7 +33,7 @@ macro_rules! extipin {
_ => {},
}
}
fn disable_interrupt(&mut self, _exti: &mut EXTI) {
fn disable_interrupt(&mut self, _exti: &mut $crate::pac::EXTI) {
match self {
$(
$(#[$attr])*
Expand Down Expand Up @@ -78,18 +78,15 @@ macro_rules! pin {

$(
$(#[$attr])*
$PX(gpio::$PX<Alternate<$A, $Otype>>),
$PX(gpio::$PX<$crate::gpio::Alternate<$A, $Otype>>),
)*
}

impl crate::Sealed for $name { }

#[allow(unreachable_patterns)]
impl $name {
pub fn is_high(&self) -> bool {
!self.is_low()
}
pub fn is_low(&self) -> bool {
impl $crate::gpio::ReadPin for $name {
fn is_low(&self) -> bool {
match self {
$(
$(#[$attr])*
Expand All @@ -99,8 +96,35 @@ macro_rules! pin {
}
}
}

#[allow(unreachable_patterns)]
impl $crate::gpio::PinSpeed for $name {
fn set_speed(&mut self, _speed: $crate::gpio::Speed) {
match self {
$(
$(#[$attr])*
Self::$PX(p) => p.set_speed(_speed),
)*
_ => {}
}
}
}

#[allow(unreachable_patterns)]
impl $crate::gpio::PinPull for $name {
fn set_internal_resistor(&mut self, _pull: $crate::gpio::Pull) {
match self {
$(
$(#[$attr])*
Self::$PX(p) => p.set_internal_resistor(_pull),
)*
_ => {}
}
}
}

#[allow(unreachable_patterns)]
impl ExtiPin for $name {
impl $crate::gpio::ExtiPin for $name {
extipin! { $( $(#[$attr])* $PX, )* }
}

Expand All @@ -116,16 +140,16 @@ macro_rules! pin {
$(#[$attr])*
impl<MODE> From<gpio::$PX<MODE>> for $name
where
MODE: marker::NotAlt + PinMode
MODE: $crate::gpio::marker::NotAlt + $crate::gpio::PinMode
{
fn from(p: gpio::$PX<MODE>) -> Self {
Self::$PX(p.into_mode())
}
}

$(#[$attr])*
impl From<gpio::$PX<Alternate<$A, $Otype>>> for $name {
fn from(p: gpio::$PX<Alternate<$A, $Otype>>) -> Self {
impl From<gpio::$PX<$crate::gpio::Alternate<$A, $Otype>>> for $name {
fn from(p: gpio::$PX<$crate::gpio::Alternate<$A, $Otype>>) -> Self {
Self::$PX(p)
}
}
Expand All @@ -134,8 +158,8 @@ macro_rules! pin {
#[allow(irrefutable_let_patterns)]
impl<MODE> TryFrom<$name> for gpio::$PX<MODE>
where
MODE: PinMode,
Alternate<$A, $Otype>: PinMode,
MODE: $crate::gpio::PinMode,
$crate::gpio::Alternate<$A, $Otype>: $crate::gpio::PinMode,
{
type Error = ();

Expand Down Expand Up @@ -164,18 +188,15 @@ macro_rules! pin {

$(
$(#[$attr])*
$PX(gpio::$PX<Alternate<$A, Otype>>),
$PX(gpio::$PX<$crate::gpio::Alternate<$A, Otype>>),
)*
}

impl<Otype> crate::Sealed for $name<Otype> { }

#[allow(unreachable_patterns)]
impl<Otype> $name<Otype> {
pub fn is_high(&self) -> bool {
!self.is_low()
}
pub fn is_low(&self) -> bool {
impl<Otype> $crate::gpio::ReadPin for $name<Otype> {
fn is_low(&self) -> bool {
match self {
$(
$(#[$attr])*
Expand All @@ -185,8 +206,35 @@ macro_rules! pin {
}
}
}

#[allow(unreachable_patterns)]
impl<Otype> $crate::gpio::PinSpeed for $name<Otype> {
fn set_speed(&mut self, _speed: $crate::gpio::Speed) {
match self {
$(
$(#[$attr])*
Self::$PX(p) => p.set_speed(_speed),
)*
_ => {}
}
}
}

#[allow(unreachable_patterns)]
impl<Otype> $crate::gpio::PinPull for $name<Otype> {
fn set_internal_resistor(&mut self, _pull: $crate::gpio::Pull) {
match self {
$(
$(#[$attr])*
Self::$PX(p) => p.set_internal_resistor(_pull),
)*
_ => {}
}
}
}

#[allow(unreachable_patterns)]
impl<Otype> ExtiPin for $name<Otype> {
impl<Otype> $crate::gpio::ExtiPin for $name<Otype> {
extipin! { $( $(#[$attr])* $PX, )* }
}

Expand All @@ -202,17 +250,17 @@ macro_rules! pin {
$(#[$attr])*
impl<MODE, Otype> From<gpio::$PX<MODE>> for $name<Otype>
where
MODE: marker::NotAlt + PinMode,
Alternate<$A, Otype>: PinMode,
MODE: $crate::gpio::marker::NotAlt + $crate::gpio::PinMode,
$crate::gpio::Alternate<$A, Otype>: $crate::gpio::PinMode,
{
fn from(p: gpio::$PX<MODE>) -> Self {
Self::$PX(p.into_mode())
}
}

$(#[$attr])*
impl<Otype> From<gpio::$PX<Alternate<$A, Otype>>> for $name<Otype> {
fn from(p: gpio::$PX<Alternate<$A, Otype>>) -> Self {
impl<Otype> From<gpio::$PX<$crate::gpio::Alternate<$A, Otype>>> for $name<Otype> {
fn from(p: gpio::$PX<$crate::gpio::Alternate<$A, Otype>>) -> Self {
Self::$PX(p)
}
}
Expand All @@ -221,8 +269,8 @@ macro_rules! pin {
#[allow(irrefutable_let_patterns)]
impl<MODE, Otype> TryFrom<$name<Otype>> for gpio::$PX<MODE>
where
MODE: PinMode,
Alternate<$A, Otype>: PinMode,
MODE: $crate::gpio::PinMode,
$crate::gpio::Alternate<$A, Otype>: $crate::gpio::PinMode,
{
type Error = ();

Expand Down Expand Up @@ -302,7 +350,7 @@ pub trait I2cCommon {
pub trait I2sCommon {
type Ck;
type Sd;
type Ws;
type Ws: crate::gpio::ReadPin + crate::gpio::ExtiPin;
}
pub trait I2sMaster {
type Mck;
Expand Down
5 changes: 1 addition & 4 deletions src/gpio/alt/f4.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::*;
use crate::gpio::{self, Edge, ExtiPin};
use crate::gpio::{marker, Alternate, NoPin, OpenDrain, PinMode, PushPull};
use crate::pac::EXTI;
use crate::syscfg::SysCfg;
use crate::gpio::{self, NoPin, OpenDrain, PushPull};

#[cfg(feature = "can1")]
pub mod can1 {
Expand Down
2 changes: 2 additions & 0 deletions src/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ macro_rules! i2s {
self.input_clock.raw()
}
fn ws_is_high(&self) -> bool {
use crate::gpio::ReadPin;
self.ws_pin().is_high()
}
fn ws_is_low(&self) -> bool {
use crate::gpio::ReadPin;
self.ws_pin().is_low()
}
}
Expand Down