Skip to content

Commit 59b9543

Browse files
bors[bot]burrbull
andauthored
Merge #623
623: ReadPin, PinSpeed & PinPull traits r=therealprof a=burrbull Co-authored-by: Andrey Zgarbul <[email protected]>
2 parents b622e01 + 71f8717 commit 59b9543

File tree

6 files changed

+132
-34
lines changed

6 files changed

+132
-34
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

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

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

examples/rtic-i2s-audio-in-out.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ mod app {
8787
use hal::pac::Interrupt;
8888
use hal::pac::{EXTI, SPI2, SPI3};
8989
use hal::prelude::*;
90+
use stm32f4xx_hal::gpio::ReadPin;
9091

9192
use heapless::spsc::*;
9293

src/gpio.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,26 @@ impl<const P: char, const N: u8, MODE> PinExt for Pin<P, N, MODE> {
280280
}
281281
}
282282

283+
pub trait PinSpeed {
284+
/// Set pin speed
285+
fn set_speed(&mut self, speed: Speed);
286+
}
287+
288+
pub trait PinPull {
289+
/// Set the internal pull-up and pull-down resistor
290+
fn set_internal_resistor(&mut self, resistor: Pull);
291+
}
292+
293+
impl<const P: char, const N: u8, MODE> PinSpeed for Pin<P, N, MODE>
294+
where
295+
MODE: marker::OutputSpeed,
296+
{
297+
#[inline(always)]
298+
fn set_speed(&mut self, speed: Speed) {
299+
self.set_speed(speed)
300+
}
301+
}
302+
283303
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
284304
where
285305
MODE: marker::OutputSpeed,
@@ -302,6 +322,16 @@ where
302322
}
303323
}
304324

325+
impl<const P: char, const N: u8, MODE> PinPull for Pin<P, N, MODE>
326+
where
327+
MODE: marker::Active,
328+
{
329+
#[inline(always)]
330+
fn set_internal_resistor(&mut self, resistor: Pull) {
331+
self.set_internal_resistor(resistor)
332+
}
333+
}
334+
305335
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
306336
where
307337
MODE: marker::Active,
@@ -467,6 +497,24 @@ impl<const P: char, const N: u8, MODE> Pin<P, N, Output<MODE>> {
467497
}
468498
}
469499

500+
pub trait ReadPin {
501+
#[inline(always)]
502+
fn is_high(&self) -> bool {
503+
!self.is_low()
504+
}
505+
fn is_low(&self) -> bool;
506+
}
507+
508+
impl<const P: char, const N: u8, MODE> ReadPin for Pin<P, N, MODE>
509+
where
510+
MODE: marker::Readable,
511+
{
512+
#[inline(always)]
513+
fn is_low(&self) -> bool {
514+
self.is_low()
515+
}
516+
}
517+
470518
impl<const P: char, const N: u8, MODE> Pin<P, N, MODE>
471519
where
472520
MODE: marker::Readable,

src/gpio/alt.rs

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub use f4::*;
33

44
macro_rules! extipin {
55
($( $(#[$attr:meta])* $PX:ident,)*) => {
6-
fn make_interrupt_source(&mut self, _syscfg: &mut SysCfg) {
6+
fn make_interrupt_source(&mut self, _syscfg: &mut $crate::syscfg::SysCfg) {
77
match self {
88
$(
99
$(#[$attr])*
@@ -14,7 +14,7 @@ macro_rules! extipin {
1414

1515
}
1616

17-
fn trigger_on_edge(&mut self, _exti: &mut EXTI, _level: Edge) {
17+
fn trigger_on_edge(&mut self, _exti: &mut $crate::pac::EXTI, _level: $crate::gpio::Edge) {
1818
match self {
1919
$(
2020
$(#[$attr])*
@@ -24,7 +24,7 @@ macro_rules! extipin {
2424
}
2525
}
2626

27-
fn enable_interrupt(&mut self, _exti: &mut EXTI) {
27+
fn enable_interrupt(&mut self, _exti: &mut $crate::pac::EXTI) {
2828
match self {
2929
$(
3030
$(#[$attr])*
@@ -33,7 +33,7 @@ macro_rules! extipin {
3333
_ => {},
3434
}
3535
}
36-
fn disable_interrupt(&mut self, _exti: &mut EXTI) {
36+
fn disable_interrupt(&mut self, _exti: &mut $crate::pac::EXTI) {
3737
match self {
3838
$(
3939
$(#[$attr])*
@@ -78,18 +78,15 @@ macro_rules! pin {
7878

7979
$(
8080
$(#[$attr])*
81-
$PX(gpio::$PX<Alternate<$A, $Otype>>),
81+
$PX(gpio::$PX<$crate::gpio::Alternate<$A, $Otype>>),
8282
)*
8383
}
8484

8585
impl crate::Sealed for $name { }
8686

8787
#[allow(unreachable_patterns)]
88-
impl $name {
89-
pub fn is_high(&self) -> bool {
90-
!self.is_low()
91-
}
92-
pub fn is_low(&self) -> bool {
88+
impl $crate::gpio::ReadPin for $name {
89+
fn is_low(&self) -> bool {
9390
match self {
9491
$(
9592
$(#[$attr])*
@@ -99,8 +96,35 @@ macro_rules! pin {
9996
}
10097
}
10198
}
99+
100+
#[allow(unreachable_patterns)]
101+
impl $crate::gpio::PinSpeed for $name {
102+
fn set_speed(&mut self, _speed: $crate::gpio::Speed) {
103+
match self {
104+
$(
105+
$(#[$attr])*
106+
Self::$PX(p) => p.set_speed(_speed),
107+
)*
108+
_ => {}
109+
}
110+
}
111+
}
112+
113+
#[allow(unreachable_patterns)]
114+
impl $crate::gpio::PinPull for $name {
115+
fn set_internal_resistor(&mut self, _pull: $crate::gpio::Pull) {
116+
match self {
117+
$(
118+
$(#[$attr])*
119+
Self::$PX(p) => p.set_internal_resistor(_pull),
120+
)*
121+
_ => {}
122+
}
123+
}
124+
}
125+
102126
#[allow(unreachable_patterns)]
103-
impl ExtiPin for $name {
127+
impl $crate::gpio::ExtiPin for $name {
104128
extipin! { $( $(#[$attr])* $PX, )* }
105129
}
106130

@@ -116,16 +140,16 @@ macro_rules! pin {
116140
$(#[$attr])*
117141
impl<MODE> From<gpio::$PX<MODE>> for $name
118142
where
119-
MODE: marker::NotAlt + PinMode
143+
MODE: $crate::gpio::marker::NotAlt + $crate::gpio::PinMode
120144
{
121145
fn from(p: gpio::$PX<MODE>) -> Self {
122146
Self::$PX(p.into_mode())
123147
}
124148
}
125149

126150
$(#[$attr])*
127-
impl From<gpio::$PX<Alternate<$A, $Otype>>> for $name {
128-
fn from(p: gpio::$PX<Alternate<$A, $Otype>>) -> Self {
151+
impl From<gpio::$PX<$crate::gpio::Alternate<$A, $Otype>>> for $name {
152+
fn from(p: gpio::$PX<$crate::gpio::Alternate<$A, $Otype>>) -> Self {
129153
Self::$PX(p)
130154
}
131155
}
@@ -134,8 +158,8 @@ macro_rules! pin {
134158
#[allow(irrefutable_let_patterns)]
135159
impl<MODE> TryFrom<$name> for gpio::$PX<MODE>
136160
where
137-
MODE: PinMode,
138-
Alternate<$A, $Otype>: PinMode,
161+
MODE: $crate::gpio::PinMode,
162+
$crate::gpio::Alternate<$A, $Otype>: $crate::gpio::PinMode,
139163
{
140164
type Error = ();
141165

@@ -164,18 +188,15 @@ macro_rules! pin {
164188

165189
$(
166190
$(#[$attr])*
167-
$PX(gpio::$PX<Alternate<$A, Otype>>),
191+
$PX(gpio::$PX<$crate::gpio::Alternate<$A, Otype>>),
168192
)*
169193
}
170194

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

173197
#[allow(unreachable_patterns)]
174-
impl<Otype> $name<Otype> {
175-
pub fn is_high(&self) -> bool {
176-
!self.is_low()
177-
}
178-
pub fn is_low(&self) -> bool {
198+
impl<Otype> $crate::gpio::ReadPin for $name<Otype> {
199+
fn is_low(&self) -> bool {
179200
match self {
180201
$(
181202
$(#[$attr])*
@@ -185,8 +206,35 @@ macro_rules! pin {
185206
}
186207
}
187208
}
209+
210+
#[allow(unreachable_patterns)]
211+
impl<Otype> $crate::gpio::PinSpeed for $name<Otype> {
212+
fn set_speed(&mut self, _speed: $crate::gpio::Speed) {
213+
match self {
214+
$(
215+
$(#[$attr])*
216+
Self::$PX(p) => p.set_speed(_speed),
217+
)*
218+
_ => {}
219+
}
220+
}
221+
}
222+
223+
#[allow(unreachable_patterns)]
224+
impl<Otype> $crate::gpio::PinPull for $name<Otype> {
225+
fn set_internal_resistor(&mut self, _pull: $crate::gpio::Pull) {
226+
match self {
227+
$(
228+
$(#[$attr])*
229+
Self::$PX(p) => p.set_internal_resistor(_pull),
230+
)*
231+
_ => {}
232+
}
233+
}
234+
}
235+
188236
#[allow(unreachable_patterns)]
189-
impl<Otype> ExtiPin for $name<Otype> {
237+
impl<Otype> $crate::gpio::ExtiPin for $name<Otype> {
190238
extipin! { $( $(#[$attr])* $PX, )* }
191239
}
192240

@@ -202,17 +250,17 @@ macro_rules! pin {
202250
$(#[$attr])*
203251
impl<MODE, Otype> From<gpio::$PX<MODE>> for $name<Otype>
204252
where
205-
MODE: marker::NotAlt + PinMode,
206-
Alternate<$A, Otype>: PinMode,
253+
MODE: $crate::gpio::marker::NotAlt + $crate::gpio::PinMode,
254+
$crate::gpio::Alternate<$A, Otype>: $crate::gpio::PinMode,
207255
{
208256
fn from(p: gpio::$PX<MODE>) -> Self {
209257
Self::$PX(p.into_mode())
210258
}
211259
}
212260

213261
$(#[$attr])*
214-
impl<Otype> From<gpio::$PX<Alternate<$A, Otype>>> for $name<Otype> {
215-
fn from(p: gpio::$PX<Alternate<$A, Otype>>) -> Self {
262+
impl<Otype> From<gpio::$PX<$crate::gpio::Alternate<$A, Otype>>> for $name<Otype> {
263+
fn from(p: gpio::$PX<$crate::gpio::Alternate<$A, Otype>>) -> Self {
216264
Self::$PX(p)
217265
}
218266
}
@@ -221,8 +269,8 @@ macro_rules! pin {
221269
#[allow(irrefutable_let_patterns)]
222270
impl<MODE, Otype> TryFrom<$name<Otype>> for gpio::$PX<MODE>
223271
where
224-
MODE: PinMode,
225-
Alternate<$A, Otype>: PinMode,
272+
MODE: $crate::gpio::PinMode,
273+
$crate::gpio::Alternate<$A, Otype>: $crate::gpio::PinMode,
226274
{
227275
type Error = ();
228276

@@ -302,7 +350,7 @@ pub trait I2cCommon {
302350
pub trait I2sCommon {
303351
type Ck;
304352
type Sd;
305-
type Ws;
353+
type Ws: crate::gpio::ReadPin + crate::gpio::ExtiPin;
306354
}
307355
pub trait I2sMaster {
308356
type Mck;

src/gpio/alt/f4.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use super::*;
2-
use crate::gpio::{self, Edge, ExtiPin};
3-
use crate::gpio::{marker, Alternate, NoPin, OpenDrain, PinMode, PushPull};
4-
use crate::pac::EXTI;
5-
use crate::syscfg::SysCfg;
2+
use crate::gpio::{self, NoPin, OpenDrain, PushPull};
63

74
#[cfg(feature = "can1")]
85
pub mod can1 {

src/i2s.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,11 @@ macro_rules! i2s {
160160
self.input_clock.raw()
161161
}
162162
fn ws_is_high(&self) -> bool {
163+
use crate::gpio::ReadPin;
163164
self.ws_pin().is_high()
164165
}
165166
fn ws_is_low(&self) -> bool {
167+
use crate::gpio::ReadPin;
166168
self.ws_pin().is_low()
167169
}
168170
}

0 commit comments

Comments
 (0)