Skip to content

Commit c001f85

Browse files
authored
Proposal for setting the IO Slew Rate (#267)
* Proposal for setting the IO Slew Rate Fixes #266 * Added a change-log entry, changed the speed enum names * cargo fmt pass
1 parent b19fbb0 commit c001f85

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

CHANGELOG.md

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

1717
- Add 16 bit dataframe size for `SPI`
1818
- Implement `timer::Cancel` trait for `CountDownTimer`
19+
- Changing Output pin slew rates through the OutputSpeed trait
1920

2021
### Added
2122

src/gpio.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ use crate::afio;
8080
use crate::pac::EXTI;
8181
use crate::rcc::APB2;
8282

83+
/// Slew rates available for Output and relevant AlternateMode Pins
84+
///
85+
/// See Table 21 "Output MODE bits" in the reference
86+
pub enum IOPinSpeed {
87+
/// Slew at 10Mhz
88+
Mhz10 = 0b01, // (yes, this one is "less" then 2Mhz)
89+
/// Slew at 2Mhz
90+
Mhz2 = 0b10,
91+
/// Slew at 50Mhz
92+
Mhz50 = 0b11,
93+
}
94+
95+
/// Allow setting of the slew rate of an IO pin
96+
///
97+
/// Initially all pins are set to the maximum slew rate
98+
pub trait OutputSpeed<CR> {
99+
fn set_speed(&mut self, cr: &mut CR, speed: IOPinSpeed);
100+
}
101+
83102
/// Extension trait to split a GPIO peripheral in independent pins and registers
84103
pub trait GpioExt {
85104
/// The to split the GPIO into
@@ -250,6 +269,8 @@ macro_rules! gpio {
250269
PinMode,
251270
Dynamic,
252271
PinModeError,
272+
OutputSpeed,
273+
IOPinSpeed,
253274
};
254275

255276
/// GPIO parts
@@ -752,6 +773,26 @@ macro_rules! gpio {
752773
}
753774
}
754775

776+
impl<MODE> OutputSpeed<$CR> for $PXi<Output<MODE>> {
777+
fn set_speed(&mut self, cr: &mut $CR, speed: IOPinSpeed){
778+
const OFFSET: u32 = (4 * $i) % 32;
779+
780+
cr.cr().modify(|r, w| unsafe {
781+
w.bits((r.bits() & !(0b11 << OFFSET)) | ((speed as u32) << OFFSET))
782+
});
783+
}
784+
}
785+
786+
impl OutputSpeed<$CR> for $PXi<Alternate<PushPull>> {
787+
fn set_speed(&mut self, cr: &mut $CR, speed: IOPinSpeed){
788+
const OFFSET: u32 = (4 * $i) % 32;
789+
790+
cr.cr().modify(|r, w| unsafe {
791+
w.bits((r.bits() & !(0b11 << OFFSET)) | ((speed as u32) << OFFSET))
792+
});
793+
}
794+
}
795+
755796
impl<MODE> toggleable::Default for $PXi<Output<MODE>> {}
756797

757798
impl<MODE> InputPin for $PXi<Input<MODE>> {

0 commit comments

Comments
 (0)