Provide rp2040_hal::gpio::pin::Pin::into_push_pull_output_in_state

This commit is contained in:
Jan Niehusmann 2022-09-17 13:10:28 +00:00
parent 19e2fbea1c
commit fa83510277
5 changed files with 45 additions and 20 deletions

View file

@ -4,7 +4,6 @@
use adafruit_itsy_bitsy_rp2040::entry; use adafruit_itsy_bitsy_rp2040::entry;
use core::iter::once; use core::iter::once;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::timer::CountDown; use embedded_hal::timer::CountDown;
use fugit::ExtU32; use fugit::ExtU32;
use panic_halt as _; use panic_halt as _;
@ -14,6 +13,7 @@ use ws2812_pio::Ws2812;
use adafruit_itsy_bitsy_rp2040::{ use adafruit_itsy_bitsy_rp2040::{
hal::{ hal::{
clocks::{init_clocks_and_plls, Clock}, clocks::{init_clocks_and_plls, Clock},
gpio::PinState,
pac, pac,
pio::PIOExt, pio::PIOExt,
watchdog::Watchdog, watchdog::Watchdog,
@ -51,9 +51,7 @@ fn main() -> ! {
let led = pins.neopixel_data.into_mode(); let led = pins.neopixel_data.into_mode();
pins.neopixel_power pins.neopixel_power
.into_push_pull_output() .into_push_pull_output_in_state(PinState::High);
.set_high()
.unwrap();
let timer = Timer::new(pac.TIMER, &mut pac.RESETS); let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
let mut delay = timer.count_down(); let mut delay = timer.count_down();

View file

@ -4,7 +4,6 @@
use adafruit_qt_py_rp2040::entry; use adafruit_qt_py_rp2040::entry;
use core::iter::once; use core::iter::once;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::timer::CountDown; use embedded_hal::timer::CountDown;
use fugit::ExtU32; use fugit::ExtU32;
use panic_halt as _; use panic_halt as _;
@ -14,6 +13,7 @@ use ws2812_pio::Ws2812;
use adafruit_qt_py_rp2040::{ use adafruit_qt_py_rp2040::{
hal::{ hal::{
clocks::{init_clocks_and_plls, Clock}, clocks::{init_clocks_and_plls, Clock},
gpio::PinState,
pac, pac,
pio::PIOExt, pio::PIOExt,
watchdog::Watchdog, watchdog::Watchdog,
@ -51,9 +51,7 @@ fn main() -> ! {
let led = pins.neopixel_data.into_mode(); let led = pins.neopixel_data.into_mode();
pins.neopixel_power pins.neopixel_power
.into_push_pull_output() .into_push_pull_output_in_state(PinState::High);
.set_high()
.unwrap();
let timer = Timer::new(pac.TIMER, &mut pac.RESETS); let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
let mut delay = timer.count_down(); let mut delay = timer.count_down();

View file

@ -11,6 +11,7 @@ use pimoroni_plasma_2040 as bsp;
use bsp::hal::{ use bsp::hal::{
clocks::{init_clocks_and_plls, Clock}, clocks::{init_clocks_and_plls, Clock},
gpio::PinState,
pac, pac,
sio::Sio, sio::Sio,
watchdog::Watchdog, watchdog::Watchdog,
@ -49,12 +50,11 @@ fn main() -> ! {
&mut pac.RESETS, &mut pac.RESETS,
); );
let mut led_green = pins.led_green.into_push_pull_output(); let mut led_green = pins
let mut led_red = pins.led_red.into_push_pull_output(); .led_green
let mut led_blue = pins.led_blue.into_push_pull_output(); .into_push_pull_output_in_state(PinState::High);
led_green.set_high().unwrap(); let mut led_red = pins.led_red.into_push_pull_output_in_state(PinState::High);
led_red.set_high().unwrap(); let mut led_blue = pins.led_blue.into_push_pull_output_in_state(PinState::High);
led_blue.set_high().unwrap();
loop { loop {
led_green.set_low().unwrap(); led_green.set_low().unwrap();

View file

@ -32,6 +32,8 @@ use seeeduino_xiao_rp2040::hal::pac;
// higher-level drivers. // higher-level drivers.
use seeeduino_xiao_rp2040::hal; use seeeduino_xiao_rp2040::hal;
use hal::gpio::PinState;
// The minimum PWM value (i.e. LED brightness) we want // The minimum PWM value (i.e. LED brightness) we want
const LOW: u16 = 0; const LOW: u16 = 0;
@ -98,12 +100,12 @@ fn main() -> ! {
channel.set_duty(u16::MAX); channel.set_duty(u16::MAX);
// Set the blue LED to be an output, initially off // Set the blue LED to be an output, initially off
let mut led_blue_pin = pins.led_blue.into_push_pull_output(); let mut led_blue_pin = pins.led_blue.into_push_pull_output_in_state(PinState::High);
led_blue_pin.set_high().unwrap();
// Turn off the green LED // Turn off the green LED
let mut led_green_pin = pins.led_green.into_push_pull_output(); let mut _led_green_pin = pins
led_green_pin.set_high().unwrap(); .led_green
.into_push_pull_output_in_state(PinState::High);
loop { loop {
// Blink blue LED at 1 Hz // Blink blue LED at 1 Hz

View file

@ -105,6 +105,7 @@ use core::marker::PhantomData;
use crate::gpio::dynpin::DynFunction; use crate::gpio::dynpin::DynFunction;
#[cfg(feature = "eh1_0_alpha")] #[cfg(feature = "eh1_0_alpha")]
use eh1_0_alpha::digital as eh1; use eh1_0_alpha::digital as eh1;
pub use embedded_hal::digital::v2::PinState;
use hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin}; use hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};
use core::mem::transmute; use core::mem::transmute;
@ -527,18 +528,44 @@ where
self.into_mode() self.into_mode()
} }
/// Configure the pin to operate as a push-pull output /// Configure the pin to operate as a push-pull output.
///
/// If you want to specify the initial pin state, use [`Pin::into_push_pull_output_in_state`].
#[inline] #[inline]
pub fn into_push_pull_output(self) -> Pin<I, PushPullOutput> { pub fn into_push_pull_output(self) -> Pin<I, PushPullOutput> {
self.into_mode() self.into_mode()
} }
/// Configure the pin to operate as a readable push pull output /// Configure the pin to operate as a push-pull output, specifying an initial
/// state which is applied immediately.
#[inline]
pub fn into_push_pull_output_in_state(mut self, state: PinState) -> Pin<I, PushPullOutput> {
match state {
PinState::High => self._set_high(),
PinState::Low => self._set_low(),
}
self.into_mode()
}
/// Configure the pin to operate as a readable push pull output.
///
/// If you want to specify the initial pin state, use [`Pin::into_readable_output_in_state`].
#[inline] #[inline]
pub fn into_readable_output(self) -> Pin<I, ReadableOutput> { pub fn into_readable_output(self) -> Pin<I, ReadableOutput> {
self.into_mode() self.into_mode()
} }
/// Configure the pin to operate as a readable push pull output, specifying an initial
/// state which is applied immediately.
#[inline]
pub fn into_readable_output_in_state(mut self, state: PinState) -> Pin<I, ReadableOutput> {
match state {
PinState::High => self._set_high(),
PinState::Low => self._set_low(),
}
self.into_mode()
}
/// Read the current drive strength of the pin. /// Read the current drive strength of the pin.
#[inline] #[inline]
pub fn get_drive_strength(&self) -> OutputDriveStrength { pub fn get_drive_strength(&self) -> OutputDriveStrength {