Merge pull request #255 from thalesfragoso/od-pin
stm32: Allow for open drain configuration for output pin
This commit is contained in:
commit
cdb0c72849
@ -18,6 +18,43 @@ pub enum Pull {
|
||||
Down,
|
||||
}
|
||||
|
||||
impl From<Pull> for vals::Pupdr {
|
||||
fn from(pull: Pull) -> Self {
|
||||
use Pull::*;
|
||||
|
||||
match pull {
|
||||
None => vals::Pupdr::FLOATING,
|
||||
Up => vals::Pupdr::PULLUP,
|
||||
Down => vals::Pupdr::PULLDOWN,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Speed settings
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub enum Speed {
|
||||
Low,
|
||||
Medium,
|
||||
#[cfg(not(syscfg_f0))]
|
||||
High,
|
||||
VeryHigh,
|
||||
}
|
||||
|
||||
impl From<Speed> for vals::Ospeedr {
|
||||
fn from(speed: Speed) -> Self {
|
||||
use Speed::*;
|
||||
|
||||
match speed {
|
||||
Low => vals::Ospeedr::LOWSPEED,
|
||||
Medium => vals::Ospeedr::MEDIUMSPEED,
|
||||
#[cfg(not(syscfg_f0))]
|
||||
High => vals::Ospeedr::HIGHSPEED,
|
||||
VeryHigh => vals::Ospeedr::VERYHIGHSPEED,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// GPIO input driver.
|
||||
pub struct Input<'d, T: Pin> {
|
||||
pub(crate) pin: T,
|
||||
@ -31,12 +68,7 @@ impl<'d, T: Pin> Input<'d, T> {
|
||||
cortex_m::interrupt::free(|_| unsafe {
|
||||
let r = pin.block();
|
||||
let n = pin.pin() as usize;
|
||||
let val = match pull {
|
||||
Pull::None => vals::Pupdr::FLOATING,
|
||||
Pull::Up => vals::Pupdr::PULLUP,
|
||||
Pull::Down => vals::Pupdr::PULLDOWN,
|
||||
};
|
||||
r.pupdr().modify(|w| w.set_pupdr(n, val));
|
||||
r.pupdr().modify(|w| w.set_pupdr(n, pull.into()));
|
||||
r.otyper().modify(|w| w.set_ot(n, vals::Ot::PUSHPULL));
|
||||
r.moder().modify(|w| w.set_moder(n, vals::Moder::INPUT));
|
||||
});
|
||||
@ -86,7 +118,7 @@ pub struct Output<'d, T: Pin> {
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Output<'d, T> {
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level) -> Self {
|
||||
pub fn new(pin: impl Unborrow<Target = T> + 'd, initial_output: Level, speed: Speed) -> Self {
|
||||
unborrow!(pin);
|
||||
|
||||
match initial_output {
|
||||
@ -99,6 +131,8 @@ impl<'d, T: Pin> Output<'d, T> {
|
||||
let n = pin.pin() as usize;
|
||||
r.pupdr().modify(|w| w.set_pupdr(n, vals::Pupdr::FLOATING));
|
||||
r.moder().modify(|w| w.set_moder(n, vals::Moder::OUTPUT));
|
||||
r.otyper().modify(|w| w.set_ot(n, vals::Ot::PUSHPULL));
|
||||
pin.set_speed(speed);
|
||||
});
|
||||
|
||||
Self {
|
||||
@ -150,6 +184,83 @@ impl<'d, T: Pin> StatefulOutputPin for Output<'d, T> {
|
||||
|
||||
impl<'d, T: Pin> toggleable::Default for Output<'d, T> {}
|
||||
|
||||
/// GPIO output open-drain driver.
|
||||
pub struct OutputOpenDrain<'d, T: Pin> {
|
||||
pub(crate) pin: T,
|
||||
phantom: PhantomData<&'d mut T>,
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> OutputOpenDrain<'d, T> {
|
||||
pub fn new(
|
||||
pin: impl Unborrow<Target = T> + 'd,
|
||||
initial_output: Level,
|
||||
speed: Speed,
|
||||
pull: Pull,
|
||||
) -> Self {
|
||||
unborrow!(pin);
|
||||
|
||||
match initial_output {
|
||||
Level::High => pin.set_high(),
|
||||
Level::Low => pin.set_low(),
|
||||
}
|
||||
|
||||
cortex_m::interrupt::free(|_| unsafe {
|
||||
let r = pin.block();
|
||||
let n = pin.pin() as usize;
|
||||
r.pupdr().modify(|w| w.set_pupdr(n, pull.into()));
|
||||
r.moder().modify(|w| w.set_moder(n, vals::Moder::OUTPUT));
|
||||
r.otyper().modify(|w| w.set_ot(n, vals::Ot::OPENDRAIN));
|
||||
pin.set_speed(speed);
|
||||
});
|
||||
|
||||
Self {
|
||||
pin,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> Drop for OutputOpenDrain<'d, T> {
|
||||
fn drop(&mut self) {
|
||||
cortex_m::interrupt::free(|_| unsafe {
|
||||
let r = self.pin.block();
|
||||
let n = self.pin.pin() as usize;
|
||||
r.pupdr().modify(|w| w.set_pupdr(n, vals::Pupdr::FLOATING));
|
||||
r.moder().modify(|w| w.set_moder(n, vals::Moder::INPUT));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> OutputPin for OutputOpenDrain<'d, T> {
|
||||
type Error = Infallible;
|
||||
|
||||
/// Set the output as high.
|
||||
fn set_high(&mut self) -> Result<(), Self::Error> {
|
||||
self.pin.set_high();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set the output as low.
|
||||
fn set_low(&mut self) -> Result<(), Self::Error> {
|
||||
self.pin.set_low();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'d, T: Pin> InputPin for OutputOpenDrain<'d, T> {
|
||||
type Error = Infallible;
|
||||
|
||||
fn is_high(&self) -> Result<bool, Self::Error> {
|
||||
self.is_low().map(|v| !v)
|
||||
}
|
||||
|
||||
fn is_low(&self) -> Result<bool, Self::Error> {
|
||||
// NOTE(safety) Atomic read
|
||||
let state = unsafe { self.pin.block().idr().read().idr(self.pin.pin() as usize) };
|
||||
Ok(state == vals::Idr::LOW)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) mod sealed {
|
||||
use super::*;
|
||||
|
||||
@ -206,6 +317,13 @@ pub(crate) mod sealed {
|
||||
.moder()
|
||||
.modify(|w| w.set_moder(pin, vals::Moder::ANALOG));
|
||||
}
|
||||
|
||||
unsafe fn set_speed(&self, speed: Speed) {
|
||||
let pin = self._pin() as usize;
|
||||
self.block()
|
||||
.ospeedr()
|
||||
.modify(|w| w.set_ospeedr(pin, speed.into()));
|
||||
}
|
||||
}
|
||||
|
||||
pub trait OptionalPin {}
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
@ -40,7 +40,7 @@ fn main() -> ! {
|
||||
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let mut led = Output::new(p.PB7, Level::High);
|
||||
let mut led = Output::new(p.PB7, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull};
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
||||
use embedded_hal::digital::v2::{InputPin, OutputPin};
|
||||
use example_common::*;
|
||||
|
||||
@ -41,9 +41,9 @@ fn main() -> ! {
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let button = Input::new(p.PC13, Pull::Down);
|
||||
let mut led1 = Output::new(p.PB0, Level::High);
|
||||
let _led2 = Output::new(p.PB7, Level::High);
|
||||
let mut led3 = Output::new(p.PB14, Level::High);
|
||||
let mut led1 = Output::new(p.PB0, Level::High, Speed::Low);
|
||||
let _led2 = Output::new(p.PB7, Level::High, Speed::Low);
|
||||
let mut led3 = Output::new(p.PB14, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
if button.is_high().unwrap() {
|
||||
|
@ -9,7 +9,7 @@
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
@ -58,7 +58,7 @@ fn main() -> ! {
|
||||
Config::default(),
|
||||
);
|
||||
|
||||
let mut cs = Output::new(p.PE0, Level::High);
|
||||
let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh);
|
||||
|
||||
loop {
|
||||
let mut buf = [0x0A; 4];
|
||||
|
@ -8,15 +8,15 @@
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
use stm32h7::stm32h743 as pac;
|
||||
|
||||
use stm32h7xx_hal as hal;
|
||||
use hal::prelude::*;
|
||||
use stm32h7xx_hal as hal;
|
||||
|
||||
#[entry]
|
||||
fn main() -> ! {
|
||||
@ -24,8 +24,7 @@ fn main() -> ! {
|
||||
|
||||
let pp = pac::Peripherals::take().unwrap();
|
||||
|
||||
let pwrcfg = pp.PWR.constrain()
|
||||
.freeze();
|
||||
let pwrcfg = pp.PWR.constrain().freeze();
|
||||
|
||||
let rcc = pp.RCC.constrain();
|
||||
|
||||
@ -60,7 +59,7 @@ fn main() -> ! {
|
||||
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let mut led = Output::new(p.PB14, Level::High);
|
||||
let mut led = Output::new(p.PB14, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
@ -71,5 +70,4 @@ fn main() -> ! {
|
||||
led.set_low().unwrap();
|
||||
cortex_m::asm::delay(10_000_000);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,7 +9,7 @@
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::{
|
||||
gpio::{Level, Output},
|
||||
gpio::{Level, Output, Speed},
|
||||
rcc::*,
|
||||
};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
@ -25,7 +25,7 @@ fn main() -> ! {
|
||||
|
||||
Rcc::new(p.RCC).enable_debug_wfe(&mut p.DBGMCU, true);
|
||||
|
||||
let mut led = Output::new(p.PB5, Level::High);
|
||||
let mut led = Output::new(p.PB5, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
|
@ -9,7 +9,7 @@
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::{
|
||||
gpio::{Input, Level, Output, Pull},
|
||||
gpio::{Input, Level, Output, Pull, Speed},
|
||||
rcc::*,
|
||||
};
|
||||
use embedded_hal::digital::v2::{InputPin, OutputPin};
|
||||
@ -25,8 +25,8 @@ fn main() -> ! {
|
||||
Rcc::new(p.RCC).enable_debug_wfe(&mut p.DBGMCU, true);
|
||||
|
||||
let button = Input::new(p.PB2, Pull::Up);
|
||||
let mut led1 = Output::new(p.PA5, Level::High);
|
||||
let mut led2 = Output::new(p.PB5, Level::High);
|
||||
let mut led1 = Output::new(p.PA5, Level::High, Speed::Low);
|
||||
let mut led2 = Output::new(p.PB5, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
if button.is_high().unwrap() {
|
||||
|
@ -9,7 +9,7 @@
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
@ -36,7 +36,7 @@ fn main() -> ! {
|
||||
Config::default(),
|
||||
);
|
||||
|
||||
let mut cs = Output::new(p.PA15, Level::High);
|
||||
let mut cs = Output::new(p.PA15, Level::High, Speed::VeryHigh);
|
||||
|
||||
loop {
|
||||
let mut buf = [0x0A; 4];
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
@ -39,7 +39,7 @@ fn main() -> ! {
|
||||
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let mut led = Output::new(p.PB14, Level::High);
|
||||
let mut led = Output::new(p.PB14, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull};
|
||||
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
|
||||
use embedded_hal::digital::v2::{InputPin, OutputPin};
|
||||
use example_common::*;
|
||||
|
||||
@ -40,8 +40,8 @@ fn main() -> ! {
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let button = Input::new(p.PC13, Pull::Up);
|
||||
let mut led1 = Output::new(p.PA5, Level::High);
|
||||
let mut led2 = Output::new(p.PB14, Level::High);
|
||||
let mut led1 = Output::new(p.PA5, Level::High, Speed::Low);
|
||||
let mut led2 = Output::new(p.PB14, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
if button.is_high().unwrap() {
|
||||
|
@ -9,7 +9,7 @@
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
@ -52,7 +52,7 @@ fn main() -> ! {
|
||||
Config::default(),
|
||||
);
|
||||
|
||||
let mut cs = Output::new(p.PE0, Level::High);
|
||||
let mut cs = Output::new(p.PE0, Level::High, Speed::VeryHigh);
|
||||
|
||||
loop {
|
||||
let mut buf = [0x0A; 4];
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use embassy_stm32::gpio::{Level, Output};
|
||||
use embassy_stm32::gpio::{Level, Output, Speed};
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
use example_common::*;
|
||||
|
||||
@ -28,7 +28,7 @@ fn main() -> ! {
|
||||
|
||||
let p = embassy_stm32::init(Default::default());
|
||||
|
||||
let mut led = Output::new(p.PB0, Level::High);
|
||||
let mut led = Output::new(p.PB0, Level::High, Speed::Low);
|
||||
|
||||
loop {
|
||||
info!("high");
|
||||
|
Loading…
Reference in New Issue
Block a user