embassy/embassy-rp/src/gpio.rs

1382 lines
38 KiB
Rust
Raw Normal View History

//! GPIO driver.
2022-07-03 21:54:01 +00:00
#![macro_use]
use core::convert::Infallible;
use core::future::Future;
use core::pin::Pin as FuturePin;
use core::task::{Context, Poll};
2021-03-29 02:11:32 +00:00
use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef};
use embassy_sync::waitqueue::AtomicWaker;
2022-06-12 20:15:44 +00:00
use crate::interrupt::InterruptExt;
2021-06-25 01:38:03 +00:00
use crate::pac::common::{Reg, RW};
2021-03-29 02:11:32 +00:00
use crate::pac::SIO;
use crate::{interrupt, pac, peripherals, Peripheral, RegExt};
const NEW_AW: AtomicWaker = AtomicWaker::new();
#[cfg(any(feature = "rp2040", feature = "rp235xa"))]
2024-09-16 00:48:54 +00:00
pub(crate) const BANK0_PIN_COUNT: usize = 30;
#[cfg(feature = "rp235xb")]
2024-09-16 00:48:54 +00:00
pub(crate) const BANK0_PIN_COUNT: usize = 48;
static BANK0_WAKERS: [AtomicWaker; BANK0_PIN_COUNT] = [NEW_AW; BANK0_PIN_COUNT];
#[cfg(feature = "qspi-as-gpio")]
const QSPI_PIN_COUNT: usize = 6;
#[cfg(feature = "qspi-as-gpio")]
static QSPI_WAKERS: [AtomicWaker; QSPI_PIN_COUNT] = [NEW_AW; QSPI_PIN_COUNT];
2021-03-29 02:11:32 +00:00
/// Represents a digital input or output level.
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
2021-03-29 02:11:32 +00:00
pub enum Level {
/// Logical low.
2021-03-29 02:11:32 +00:00
Low,
/// Logical high.
2021-03-29 02:11:32 +00:00
High,
}
2022-07-12 23:22:46 +00:00
impl From<bool> for Level {
fn from(val: bool) -> Self {
match val {
true => Self::High,
false => Self::Low,
}
}
}
impl From<Level> for bool {
fn from(level: Level) -> bool {
match level {
2022-07-12 23:22:46 +00:00
Level::Low => false,
Level::High => true,
}
}
}
2021-03-29 02:11:32 +00:00
/// Represents a pull setting for an input.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
2021-03-29 02:11:32 +00:00
pub enum Pull {
/// No pull.
2021-03-29 02:11:32 +00:00
None,
/// Internal pull-up resistor.
2021-03-29 02:11:32 +00:00
Up,
/// Internal pull-down resistor.
2021-03-29 02:11:32 +00:00
Down,
}
2022-07-27 20:45:46 +00:00
/// Drive strength of an output
#[derive(Debug, Eq, PartialEq)]
pub enum Drive {
/// 2 mA drive.
2022-07-27 20:45:46 +00:00
_2mA,
/// 4 mA drive.
2022-07-27 20:45:46 +00:00
_4mA,
/// 8 mA drive.
2022-07-27 20:45:46 +00:00
_8mA,
/// 1 2mA drive.
2022-07-27 20:45:46 +00:00
_12mA,
}
/// Slew rate of an output
#[derive(Debug, Eq, PartialEq)]
pub enum SlewRate {
/// Fast slew rate.
2022-07-27 20:45:46 +00:00
Fast,
/// Slow slew rate.
2022-07-27 20:45:46 +00:00
Slow,
}
2021-03-29 02:11:32 +00:00
/// A GPIO bank with up to 32 pins.
#[derive(Debug, Eq, PartialEq)]
pub enum Bank {
/// Bank 0.
2021-03-29 02:11:32 +00:00
Bank0 = 0,
/// QSPI.
#[cfg(feature = "qspi-as-gpio")]
2021-03-29 02:11:32 +00:00
Qspi = 1,
}
/// Dormant mode config.
#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct DormantWakeConfig {
/// Wake on edge high.
pub edge_high: bool,
/// Wake on edge low.
pub edge_low: bool,
/// Wake on level high.
pub level_high: bool,
/// Wake on level low.
pub level_low: bool,
}
/// GPIO input driver.
2024-01-22 20:30:29 +00:00
pub struct Input<'d> {
pin: Flex<'d>,
2021-03-29 02:11:32 +00:00
}
2024-01-22 20:30:29 +00:00
impl<'d> Input<'d> {
/// Create GPIO input driver for a [Pin] with the provided [Pull] configuration.
2022-07-12 23:22:46 +00:00
#[inline]
2024-01-22 20:30:29 +00:00
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, pull: Pull) -> Self {
2022-07-09 00:12:17 +00:00
let mut pin = Flex::new(pin);
pin.set_as_input();
pin.set_pull(pull);
Self { pin }
2021-03-29 02:11:32 +00:00
}
/// Set the pin's Schmitt trigger.
#[inline]
pub fn set_schmitt(&mut self, enable: bool) {
self.pin.set_schmitt(enable)
}
/// Get whether the pin input level is high.
2022-07-12 23:22:46 +00:00
#[inline]
pub fn is_high(&self) -> bool {
2022-07-09 00:12:17 +00:00
self.pin.is_high()
}
/// Get whether the pin input level is low.
2022-07-12 23:22:46 +00:00
#[inline]
pub fn is_low(&self) -> bool {
2022-07-09 00:12:17 +00:00
self.pin.is_low()
2021-03-29 02:11:32 +00:00
}
2022-07-12 23:22:46 +00:00
/// Returns current pin level
#[inline]
pub fn get_level(&self) -> Level {
2022-07-12 23:45:37 +00:00
self.pin.get_level()
2022-07-12 23:22:46 +00:00
}
/// Wait until the pin is high. If it is already high, return immediately.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_high(&mut self) {
self.pin.wait_for_high().await;
}
/// Wait until the pin is low. If it is already low, return immediately.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_low(&mut self) {
self.pin.wait_for_low().await;
}
/// Wait for the pin to undergo a transition from low to high.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_rising_edge(&mut self) {
self.pin.wait_for_rising_edge().await;
}
/// Wait for the pin to undergo a transition from high to low.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_falling_edge(&mut self) {
self.pin.wait_for_falling_edge().await;
}
/// Wait for the pin to undergo any transition, i.e low to high OR high to low.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_any_edge(&mut self) {
self.pin.wait_for_any_edge().await;
}
/// Configure dormant wake.
#[inline]
2024-01-22 20:30:29 +00:00
pub fn dormant_wake(&mut self, cfg: DormantWakeConfig) -> DormantWake<'_> {
self.pin.dormant_wake(cfg)
}
/// Set the pin's pad isolation
#[cfg(feature = "_rp235x")]
#[inline]
pub fn set_pad_isolation(&mut self, isolate: bool) {
self.pin.set_pad_isolation(isolate)
}
}
/// Interrupt trigger levels.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum InterruptTrigger {
/// Trigger on pin low.
LevelLow,
/// Trigger on pin high.
LevelHigh,
/// Trigger on high to low transition.
EdgeLow,
/// Trigger on low to high transition.
EdgeHigh,
/// Trigger on any transition.
AnyEdge,
}
pub(crate) unsafe fn init() {
interrupt::IO_IRQ_BANK0.disable();
interrupt::IO_IRQ_BANK0.set_priority(interrupt::Priority::P3);
interrupt::IO_IRQ_BANK0.enable();
#[cfg(feature = "qspi-as-gpio")]
{
interrupt::IO_IRQ_QSPI.disable();
interrupt::IO_IRQ_QSPI.set_priority(interrupt::Priority::P3);
interrupt::IO_IRQ_QSPI.enable();
}
}
#[cfg(feature = "rt")]
fn irq_handler<const N: usize>(bank: pac::io::Io, wakers: &[AtomicWaker; N]) {
let cpu = SIO.cpuid().read() as usize;
// There are two sets of interrupt registers, one for cpu0 and one for cpu1
// and here we are selecting the set that belongs to the currently executing
// cpu.
let proc_intx: pac::io::Int = bank.int_proc(cpu);
for pin in 0..N {
// There are 4 raw interrupt status registers, PROCx_INTS0, PROCx_INTS1,
// PROCx_INTS2, and PROCx_INTS3, and we are selecting the one that the
// current pin belongs to.
let intsx = proc_intx.ints(pin / 8);
// The status register is divided into groups of four, one group for
// each pin. Each group consists of four trigger levels LEVEL_LOW,
// LEVEL_HIGH, EDGE_LOW, and EDGE_HIGH for each pin.
2024-03-18 22:28:58 +00:00
let pin_group = pin % 8;
let event = (intsx.read().0 >> (pin_group * 4)) & 0xf;
// no more than one event can be awaited per pin at any given time, so
// we can just clear all interrupt enables for that pin without having
// to check which event was signalled.
if event != 0 {
proc_intx.inte(pin / 8).write_clear(|w| {
w.set_edge_high(pin_group, true);
w.set_edge_low(pin_group, true);
w.set_level_high(pin_group, true);
w.set_level_low(pin_group, true);
});
2024-03-18 22:28:58 +00:00
wakers[pin].wake();
}
}
}
#[cfg(feature = "rt")]
#[interrupt]
fn IO_IRQ_BANK0() {
irq_handler(pac::IO_BANK0, &BANK0_WAKERS);
}
#[cfg(all(feature = "rt", feature = "qspi-as-gpio"))]
#[interrupt]
fn IO_IRQ_QSPI() {
irq_handler(pac::IO_QSPI, &QSPI_WAKERS);
}
2023-02-24 19:01:41 +00:00
#[must_use = "futures do nothing unless you `.await` or poll them"]
2024-01-22 20:30:29 +00:00
struct InputFuture<'d> {
pin: PeripheralRef<'d, AnyPin>,
}
2024-01-22 20:30:29 +00:00
impl<'d> InputFuture<'d> {
fn new(pin: PeripheralRef<'d, AnyPin>, level: InterruptTrigger) -> Self {
2023-06-15 23:32:18 +00:00
let pin_group = (pin.pin() % 8) as usize;
// first, clear the INTR register bits. without this INTR will still
// contain reports of previous edges, causing the IRQ to fire early
// on stale state. clearing these means that we can only detect edges
// that occur *after* the clear happened, but since both this and the
// alternative are fundamentally racy it's probably fine.
// (the alternative being checking the current level and waiting for
// its inverse, but that requires reading the current level and thus
// missing anything that happened before the level was read.)
pin.io().intr(pin.pin() as usize / 8).write(|w| {
2023-06-15 23:32:18 +00:00
w.set_edge_high(pin_group, true);
w.set_edge_low(pin_group, true);
});
// Each INTR register is divided into 8 groups, one group for each
// pin, and each group consists of LEVEL_LOW, LEVEL_HIGH, EDGE_LOW,
// and EGDE_HIGH.
pin.int_proc()
.inte((pin.pin() / 8) as usize)
.write_set(|w| match level {
InterruptTrigger::LevelHigh => {
w.set_level_high(pin_group, true);
}
InterruptTrigger::LevelLow => {
w.set_level_low(pin_group, true);
}
InterruptTrigger::EdgeHigh => {
w.set_edge_high(pin_group, true);
}
InterruptTrigger::EdgeLow => {
w.set_edge_low(pin_group, true);
}
InterruptTrigger::AnyEdge => {
w.set_edge_high(pin_group, true);
w.set_edge_low(pin_group, true);
}
});
Self { pin }
}
}
2024-01-22 20:30:29 +00:00
impl<'d> Future for InputFuture<'d> {
type Output = ();
fn poll(self: FuturePin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// We need to register/re-register the waker for each poll because any
// calls to wake will deregister the waker.
let waker = match self.pin.bank() {
Bank::Bank0 => &BANK0_WAKERS[self.pin.pin() as usize],
#[cfg(feature = "qspi-as-gpio")]
Bank::Qspi => &QSPI_WAKERS[self.pin.pin() as usize],
};
waker.register(cx.waker());
// self.int_proc() will get the register offset for the current cpu,
// then we want to access the interrupt enable register for our
// pin (there are 4 of these PROC0_INTE0, PROC0_INTE1, PROC0_INTE2, and
// PROC0_INTE3 per cpu).
2023-06-15 23:32:18 +00:00
let inte: pac::io::regs::Int = self.pin.int_proc().inte((self.pin.pin() / 8) as usize).read();
// The register is divided into groups of four, one group for
// each pin. Each group consists of four trigger levels LEVEL_LOW,
// LEVEL_HIGH, EDGE_LOW, and EDGE_HIGH for each pin.
let pin_group = (self.pin.pin() % 8) as usize;
// since the interrupt handler clears all INTE flags we'll check that
// all have been cleared and unconditionally return Ready(()) if so.
// we don't need further handshaking since only a single event wait
// is possible for any given pin at any given time.
if !inte.edge_high(pin_group)
&& !inte.edge_low(pin_group)
&& !inte.level_high(pin_group)
&& !inte.level_low(pin_group)
{
return Poll::Ready(());
}
Poll::Pending
}
2021-03-29 02:11:32 +00:00
}
/// GPIO output driver.
2024-01-22 20:30:29 +00:00
pub struct Output<'d> {
pin: Flex<'d>,
2021-03-29 02:11:32 +00:00
}
2024-01-22 20:30:29 +00:00
impl<'d> Output<'d> {
/// Create GPIO output driver for a [Pin] with the provided [Level].
2022-07-09 00:12:17 +00:00
#[inline]
2024-01-22 20:30:29 +00:00
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, initial_output: Level) -> Self {
2022-07-09 00:12:17 +00:00
let mut pin = Flex::new(pin);
match initial_output {
Level::High => pin.set_high(),
Level::Low => pin.set_low(),
2021-03-29 02:11:32 +00:00
}
2022-07-09 00:12:17 +00:00
pin.set_as_output();
Self { pin }
2021-03-29 02:11:32 +00:00
}
/// Set the pin's drive strength.
#[inline]
pub fn set_drive_strength(&mut self, strength: Drive) {
self.pin.set_drive_strength(strength)
}
/// Set the pin's slew rate.
#[inline]
pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
self.pin.set_slew_rate(slew_rate)
}
/// Set the output as high.
2022-07-09 00:12:17 +00:00
#[inline]
pub fn set_high(&mut self) {
2022-07-09 00:12:17 +00:00
self.pin.set_high()
}
/// Set the output as low.
2022-07-09 00:12:17 +00:00
#[inline]
pub fn set_low(&mut self) {
2022-07-09 00:12:17 +00:00
self.pin.set_low()
}
2022-07-12 23:22:46 +00:00
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
2022-07-12 23:45:37 +00:00
self.pin.set_level(level)
2022-07-12 23:22:46 +00:00
}
/// Is the output pin set as high?
2022-07-09 00:12:17 +00:00
#[inline]
pub fn is_set_high(&self) -> bool {
2022-07-09 00:12:17 +00:00
self.pin.is_set_high()
}
/// Is the output pin set as low?
2022-07-09 00:12:17 +00:00
#[inline]
pub fn is_set_low(&self) -> bool {
2022-07-09 00:12:17 +00:00
self.pin.is_set_low()
}
2022-07-12 23:22:46 +00:00
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
2022-07-12 23:45:37 +00:00
self.pin.get_output_level()
2022-07-12 23:22:46 +00:00
}
/// Toggle pin output
#[inline]
pub fn toggle(&mut self) {
2022-07-09 00:12:17 +00:00
self.pin.toggle()
}
/// Set the pin's pad isolation
#[cfg(feature = "_rp235x")]
#[inline]
pub fn set_pad_isolation(&mut self, isolate: bool) {
self.pin.set_pad_isolation(isolate)
}
2021-03-29 02:11:32 +00:00
}
2022-07-09 00:12:17 +00:00
/// GPIO output open-drain.
2024-01-22 20:30:29 +00:00
pub struct OutputOpenDrain<'d> {
pin: Flex<'d>,
2022-07-09 00:12:17 +00:00
}
2024-01-22 20:30:29 +00:00
impl<'d> OutputOpenDrain<'d> {
/// Create GPIO output driver for a [Pin] in open drain mode with the provided [Level].
2022-07-09 00:12:17 +00:00
#[inline]
2024-01-22 20:30:29 +00:00
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, initial_output: Level) -> Self {
2022-07-09 00:12:17 +00:00
let mut pin = Flex::new(pin);
pin.set_low();
match initial_output {
Level::High => pin.set_as_input(),
Level::Low => pin.set_as_output(),
}
Self { pin }
}
/// Set the pin's drive strength.
#[inline]
pub fn set_drive_strength(&mut self, strength: Drive) {
self.pin.set_drive_strength(strength)
}
/// Set the pin's slew rate.
#[inline]
pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
self.pin.set_slew_rate(slew_rate)
}
2022-07-09 00:12:17 +00:00
/// Set the output as high.
#[inline]
pub fn set_high(&mut self) {
// For Open Drain High, disable the output pin.
self.pin.set_as_input()
}
/// Set the output as low.
#[inline]
pub fn set_low(&mut self) {
// For Open Drain Low, enable the output pin.
self.pin.set_as_output()
}
2022-07-12 23:22:46 +00:00
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
match level {
Level::Low => self.set_low(),
Level::High => self.set_high(),
}
}
2022-07-09 00:12:17 +00:00
/// Is the output level high?
#[inline]
pub fn is_set_high(&self) -> bool {
2022-07-09 00:12:17 +00:00
!self.is_set_low()
}
/// Is the output level low?
#[inline]
pub fn is_set_low(&self) -> bool {
2022-07-09 00:12:17 +00:00
self.pin.is_set_as_output()
}
2022-07-12 23:22:46 +00:00
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
2022-07-12 23:22:46 +00:00
self.is_set_high().into()
}
2022-07-09 00:12:17 +00:00
/// Toggle pin output
#[inline]
pub fn toggle(&mut self) {
self.pin.toggle_set_as_output()
2021-03-29 02:11:32 +00:00
}
/// Get whether the pin input level is high.
#[inline]
pub fn is_high(&self) -> bool {
self.pin.is_high()
}
/// Get whether the pin input level is low.
#[inline]
pub fn is_low(&self) -> bool {
self.pin.is_low()
}
/// Returns current pin level
#[inline]
pub fn get_level(&self) -> Level {
self.is_high().into()
}
/// Wait until the pin is high. If it is already high, return immediately.
#[inline]
pub async fn wait_for_high(&mut self) {
self.pin.wait_for_high().await;
}
/// Wait until the pin is low. If it is already low, return immediately.
#[inline]
pub async fn wait_for_low(&mut self) {
self.pin.wait_for_low().await;
}
/// Wait for the pin to undergo a transition from low to high.
#[inline]
pub async fn wait_for_rising_edge(&mut self) {
self.pin.wait_for_rising_edge().await;
}
/// Wait for the pin to undergo a transition from high to low.
#[inline]
pub async fn wait_for_falling_edge(&mut self) {
self.pin.wait_for_falling_edge().await;
}
/// Wait for the pin to undergo any transition, i.e low to high OR high to low.
#[inline]
pub async fn wait_for_any_edge(&mut self) {
self.pin.wait_for_any_edge().await;
}
/// Set the pin's pad isolation
#[cfg(feature = "_rp235x")]
#[inline]
pub fn set_pad_isolation(&mut self, isolate: bool) {
self.pin.set_pad_isolation(isolate)
}
2021-03-29 02:11:32 +00:00
}
2022-07-09 00:12:17 +00:00
/// GPIO flexible pin.
///
/// This pin can be either an input or output pin. The output level register bit will remain
/// set while not in output mode, so the pin's level will be 'remembered' when it is not in output
/// mode.
2024-01-22 20:30:29 +00:00
pub struct Flex<'d> {
pin: PeripheralRef<'d, AnyPin>,
}
2024-01-22 20:30:29 +00:00
impl<'d> Flex<'d> {
/// Wrap the pin in a `Flex`.
///
/// The pin remains disconnected. The initial output level is unspecified, but can be changed
/// before the pin is put into output mode.
#[inline]
2024-01-22 20:30:29 +00:00
pub fn new(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
into_ref!(pin);
2023-06-15 23:32:18 +00:00
pin.pad_ctrl().write(|w| {
#[cfg(feature = "_rp235x")]
w.set_iso(false);
2023-06-15 23:32:18 +00:00
w.set_ie(true);
});
2022-07-09 00:12:17 +00:00
pin.gpio().ctrl().write(|w| {
#[cfg(feature = "rp2040")]
2023-06-29 00:08:55 +00:00
w.set_funcsel(pac::io::vals::Gpio0ctrlFuncsel::SIO_0 as _);
#[cfg(feature = "_rp235x")]
w.set_funcsel(pac::io::vals::Gpio0ctrlFuncsel::SIOB_PROC_0 as _);
});
2024-01-22 20:30:29 +00:00
Self { pin: pin.map_into() }
}
#[inline]
2022-07-09 00:12:17 +00:00
fn bit(&self) -> u32 {
2024-09-23 17:38:43 +00:00
1 << (self.pin.pin() % 32)
2022-07-09 00:12:17 +00:00
}
/// Set the pin's pull.
#[inline]
pub fn set_pull(&mut self, pull: Pull) {
2023-06-15 23:32:18 +00:00
self.pin.pad_ctrl().modify(|w| {
w.set_ie(true);
let (pu, pd) = match pull {
Pull::Up => (true, false),
Pull::Down => (false, true),
Pull::None => (false, false),
};
w.set_pue(pu);
w.set_pde(pd);
});
}
2022-07-27 20:45:46 +00:00
/// Set the pin's drive strength.
#[inline]
pub fn set_drive_strength(&mut self, strength: Drive) {
2023-06-15 23:32:18 +00:00
self.pin.pad_ctrl().modify(|w| {
w.set_drive(match strength {
Drive::_2mA => pac::pads::vals::Drive::_2MA,
Drive::_4mA => pac::pads::vals::Drive::_4MA,
Drive::_8mA => pac::pads::vals::Drive::_8MA,
Drive::_12mA => pac::pads::vals::Drive::_12MA,
2022-07-27 20:45:46 +00:00
});
2023-06-15 23:32:18 +00:00
});
2022-07-27 20:45:46 +00:00
}
/// Set the pin's slew rate.
2022-07-27 20:45:46 +00:00
#[inline]
pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
2023-06-15 23:32:18 +00:00
self.pin.pad_ctrl().modify(|w| {
w.set_slewfast(slew_rate == SlewRate::Fast);
});
2022-07-27 20:45:46 +00:00
}
/// Set the pin's Schmitt trigger.
#[inline]
pub fn set_schmitt(&mut self, enable: bool) {
self.pin.pad_ctrl().modify(|w| {
w.set_schmitt(enable);
});
}
2022-07-09 00:12:17 +00:00
/// Put the pin into input mode.
///
/// The pull setting is left unchanged.
#[inline]
pub fn set_as_input(&mut self) {
2023-06-15 23:32:18 +00:00
self.pin.sio_oe().value_clr().write_value(self.bit())
2022-07-09 00:12:17 +00:00
}
/// Put the pin into output mode.
///
/// The pin level will be whatever was set before (or low by default). If you want it to begin
/// at a specific level, call `set_high`/`set_low` on the pin first.
#[inline]
pub fn set_as_output(&mut self) {
2023-06-15 23:32:18 +00:00
self.pin.sio_oe().value_set().write_value(self.bit())
2022-07-09 00:12:17 +00:00
}
/// Set as output pin.
2022-07-09 00:12:17 +00:00
#[inline]
fn is_set_as_output(&self) -> bool {
2023-06-15 23:32:18 +00:00
(self.pin.sio_oe().value().read() & self.bit()) != 0
2022-07-09 00:12:17 +00:00
}
/// Toggle output pin.
2022-07-09 00:12:17 +00:00
#[inline]
pub fn toggle_set_as_output(&mut self) {
2023-06-15 23:32:18 +00:00
self.pin.sio_oe().value_xor().write_value(self.bit())
2022-07-09 00:12:17 +00:00
}
/// Get whether the pin input level is high.
2022-07-09 00:12:17 +00:00
#[inline]
pub fn is_high(&self) -> bool {
2022-07-09 00:12:17 +00:00
!self.is_low()
}
/// Get whether the pin input level is low.
2022-07-09 00:12:17 +00:00
#[inline]
pub fn is_low(&self) -> bool {
2023-06-15 23:32:18 +00:00
self.pin.sio_in().read() & self.bit() == 0
2022-07-09 00:12:17 +00:00
}
2022-07-12 23:22:46 +00:00
/// Returns current pin level
#[inline]
pub fn get_level(&self) -> Level {
2022-07-12 23:22:46 +00:00
self.is_high().into()
}
2022-07-09 00:12:17 +00:00
/// Set the output as high.
#[inline]
pub fn set_high(&mut self) {
2023-06-15 23:32:18 +00:00
self.pin.sio_out().value_set().write_value(self.bit())
2022-07-09 00:12:17 +00:00
}
/// Set the output as low.
#[inline]
pub fn set_low(&mut self) {
2023-06-15 23:32:18 +00:00
self.pin.sio_out().value_clr().write_value(self.bit())
}
2022-07-12 23:22:46 +00:00
/// Set the output level.
#[inline]
pub fn set_level(&mut self, level: Level) {
match level {
Level::Low => self.set_low(),
Level::High => self.set_high(),
}
}
/// Is the output level high?
#[inline]
pub fn is_set_high(&self) -> bool {
!self.is_set_low()
}
/// Is the output level low?
#[inline]
pub fn is_set_low(&self) -> bool {
(self.pin.sio_out().value().read() & self.bit()) == 0
}
2022-07-12 23:22:46 +00:00
/// What level output is set to
#[inline]
pub fn get_output_level(&self) -> Level {
2022-07-12 23:22:46 +00:00
self.is_set_high().into()
}
/// Toggle pin output
#[inline]
pub fn toggle(&mut self) {
2023-06-15 23:32:18 +00:00
self.pin.sio_out().value_xor().write_value(self.bit())
2022-07-09 00:12:17 +00:00
}
/// Wait until the pin is high. If it is already high, return immediately.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_high(&mut self) {
2024-01-22 20:30:29 +00:00
InputFuture::new(self.pin.reborrow(), InterruptTrigger::LevelHigh).await;
}
/// Wait until the pin is low. If it is already low, return immediately.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_low(&mut self) {
2024-01-22 20:30:29 +00:00
InputFuture::new(self.pin.reborrow(), InterruptTrigger::LevelLow).await;
}
/// Wait for the pin to undergo a transition from low to high.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_rising_edge(&mut self) {
2024-01-22 20:30:29 +00:00
InputFuture::new(self.pin.reborrow(), InterruptTrigger::EdgeHigh).await;
}
/// Wait for the pin to undergo a transition from high to low.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_falling_edge(&mut self) {
2024-01-22 20:30:29 +00:00
InputFuture::new(self.pin.reborrow(), InterruptTrigger::EdgeLow).await;
}
/// Wait for the pin to undergo any transition, i.e low to high OR high to low.
#[inline]
2022-08-07 21:25:50 +00:00
pub async fn wait_for_any_edge(&mut self) {
2024-01-22 20:30:29 +00:00
InputFuture::new(self.pin.reborrow(), InterruptTrigger::AnyEdge).await;
}
/// Configure dormant wake.
#[inline]
2024-01-22 20:30:29 +00:00
pub fn dormant_wake(&mut self, cfg: DormantWakeConfig) -> DormantWake<'_> {
let idx = self.pin._pin() as usize;
self.pin.io().intr(idx / 8).write(|w| {
w.set_edge_high(idx % 8, cfg.edge_high);
w.set_edge_low(idx % 8, cfg.edge_low);
});
self.pin.io().int_dormant_wake().inte(idx / 8).write_set(|w| {
w.set_edge_high(idx % 8, cfg.edge_high);
w.set_edge_low(idx % 8, cfg.edge_low);
w.set_level_high(idx % 8, cfg.level_high);
w.set_level_low(idx % 8, cfg.level_low);
});
DormantWake {
pin: self.pin.reborrow(),
cfg,
}
}
/// Set the pin's pad isolation
#[cfg(feature = "_rp235x")]
#[inline]
pub fn set_pad_isolation(&mut self, isolate: bool) {
self.pin.pad_ctrl().modify(|w| {
w.set_iso(isolate);
});
}
2022-07-09 00:12:17 +00:00
}
2024-01-22 20:30:29 +00:00
impl<'d> Drop for Flex<'d> {
2022-07-09 00:12:17 +00:00
#[inline]
fn drop(&mut self) {
let idx = self.pin._pin() as usize;
2023-06-15 23:32:18 +00:00
self.pin.pad_ctrl().write(|_| {});
self.pin.gpio().ctrl().write(|w| {
2023-06-29 00:08:55 +00:00
w.set_funcsel(pac::io::vals::Gpio0ctrlFuncsel::NULL as _);
2023-06-15 23:32:18 +00:00
});
self.pin.io().int_dormant_wake().inte(idx / 8).write_clear(|w| {
w.set_edge_high(idx % 8, true);
w.set_edge_low(idx % 8, true);
w.set_level_high(idx % 8, true);
w.set_level_low(idx % 8, true);
});
}
}
/// Dormant wake driver.
2024-01-22 20:30:29 +00:00
pub struct DormantWake<'w> {
pin: PeripheralRef<'w, AnyPin>,
cfg: DormantWakeConfig,
}
2024-01-22 20:30:29 +00:00
impl<'w> Drop for DormantWake<'w> {
fn drop(&mut self) {
let idx = self.pin._pin() as usize;
self.pin.io().intr(idx / 8).write(|w| {
w.set_edge_high(idx % 8, self.cfg.edge_high);
w.set_edge_low(idx % 8, self.cfg.edge_low);
});
self.pin.io().int_dormant_wake().inte(idx / 8).write_clear(|w| {
w.set_edge_high(idx % 8, true);
w.set_edge_low(idx % 8, true);
w.set_level_high(idx % 8, true);
w.set_level_low(idx % 8, true);
});
}
}
2024-04-04 22:35:25 +00:00
pub(crate) trait SealedPin: Sized {
fn pin_bank(&self) -> u8;
2021-03-29 02:11:32 +00:00
2024-04-04 22:35:25 +00:00
#[inline]
fn _pin(&self) -> u8 {
self.pin_bank() & 0x7f
2024-04-04 22:35:25 +00:00
}
2021-03-29 02:11:32 +00:00
2024-04-04 22:35:25 +00:00
#[inline]
fn _bank(&self) -> Bank {
match self.pin_bank() >> 7 {
2024-04-04 22:35:25 +00:00
#[cfg(feature = "qspi-as-gpio")]
1 => Bank::Qspi,
_ => Bank::Bank0,
2021-03-29 02:11:32 +00:00
}
2024-04-04 22:35:25 +00:00
}
2021-03-29 02:11:32 +00:00
2024-04-04 22:35:25 +00:00
fn io(&self) -> pac::io::Io {
match self._bank() {
Bank::Bank0 => crate::pac::IO_BANK0,
#[cfg(feature = "qspi-as-gpio")]
Bank::Qspi => crate::pac::IO_QSPI,
}
2024-04-04 22:35:25 +00:00
}
2024-04-04 22:35:25 +00:00
fn gpio(&self) -> pac::io::Gpio {
self.io().gpio(self._pin() as _)
}
2021-03-29 02:11:32 +00:00
2024-04-04 22:35:25 +00:00
fn pad_ctrl(&self) -> Reg<pac::pads::regs::GpioCtrl, RW> {
let block = match self._bank() {
Bank::Bank0 => crate::pac::PADS_BANK0,
#[cfg(feature = "qspi-as-gpio")]
Bank::Qspi => crate::pac::PADS_QSPI,
};
block.gpio(self._pin() as _)
}
2024-04-04 22:35:25 +00:00
fn sio_out(&self) -> pac::sio::Gpio {
if cfg!(feature = "rp2040") {
SIO.gpio_out(self._bank() as _)
} else {
SIO.gpio_out((self._pin() / 32) as _)
}
2024-04-04 22:35:25 +00:00
}
2024-04-04 22:35:25 +00:00
fn sio_oe(&self) -> pac::sio::Gpio {
if cfg!(feature = "rp2040") {
SIO.gpio_oe(self._bank() as _)
} else {
SIO.gpio_oe((self._pin() / 32) as _)
}
2024-04-04 22:35:25 +00:00
}
2024-04-04 22:35:25 +00:00
fn sio_in(&self) -> Reg<u32, RW> {
if cfg!(feature = "rp2040") {
SIO.gpio_in(self._bank() as _)
} else {
SIO.gpio_in((self._pin() / 32) as _)
}
2024-04-04 22:35:25 +00:00
}
2024-04-04 22:35:25 +00:00
fn int_proc(&self) -> pac::io::Int {
let proc = SIO.cpuid().read();
self.io().int_proc(proc as _)
2021-03-29 02:11:32 +00:00
}
}
/// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin].
2024-04-04 22:35:25 +00:00
#[allow(private_bounds)]
pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + SealedPin + Sized + 'static {
2021-03-29 02:11:32 +00:00
/// Degrade to a generic pin struct
fn degrade(self) -> AnyPin {
AnyPin {
pin_bank: self.pin_bank(),
}
}
/// Returns the pin number within a bank
#[inline]
fn pin(&self) -> u8 {
self._pin()
}
/// Returns the bank of this pin
#[inline]
fn bank(&self) -> Bank {
self._bank()
}
2021-03-29 02:11:32 +00:00
}
/// Type-erased GPIO pin
2021-03-29 02:11:32 +00:00
pub struct AnyPin {
pin_bank: u8,
}
2022-07-03 21:54:01 +00:00
2024-02-15 23:10:20 +00:00
impl AnyPin {
/// Unsafely create a new type-erased pin.
///
/// # Safety
///
/// You must ensure that youre only using one instance of this type at a time.
pub unsafe fn steal(pin_bank: u8) -> Self {
Self { pin_bank }
}
}
impl_peripheral!(AnyPin);
2022-07-03 21:54:01 +00:00
2021-03-29 02:11:32 +00:00
impl Pin for AnyPin {}
2024-04-04 22:35:25 +00:00
impl SealedPin for AnyPin {
2021-03-29 02:11:32 +00:00
fn pin_bank(&self) -> u8 {
self.pin_bank
}
}
2021-06-25 16:17:59 +00:00
// ==========================
2021-03-29 02:11:32 +00:00
macro_rules! impl_pin {
($name:ident, $bank:expr, $pin_num:expr) => {
impl Pin for peripherals::$name {}
2024-04-04 22:35:25 +00:00
impl SealedPin for peripherals::$name {
#[inline]
2021-03-29 02:11:32 +00:00
fn pin_bank(&self) -> u8 {
($bank as u8) * 128 + $pin_num
2021-03-29 02:11:32 +00:00
}
}
2022-07-23 12:27:45 +00:00
impl From<peripherals::$name> for crate::gpio::AnyPin {
fn from(val: peripherals::$name) -> Self {
crate::gpio::Pin::degrade(val)
}
}
2021-03-29 02:11:32 +00:00
};
}
impl_pin!(PIN_0, Bank::Bank0, 0);
impl_pin!(PIN_1, Bank::Bank0, 1);
impl_pin!(PIN_2, Bank::Bank0, 2);
impl_pin!(PIN_3, Bank::Bank0, 3);
impl_pin!(PIN_4, Bank::Bank0, 4);
impl_pin!(PIN_5, Bank::Bank0, 5);
impl_pin!(PIN_6, Bank::Bank0, 6);
impl_pin!(PIN_7, Bank::Bank0, 7);
impl_pin!(PIN_8, Bank::Bank0, 8);
impl_pin!(PIN_9, Bank::Bank0, 9);
impl_pin!(PIN_10, Bank::Bank0, 10);
impl_pin!(PIN_11, Bank::Bank0, 11);
impl_pin!(PIN_12, Bank::Bank0, 12);
impl_pin!(PIN_13, Bank::Bank0, 13);
impl_pin!(PIN_14, Bank::Bank0, 14);
impl_pin!(PIN_15, Bank::Bank0, 15);
impl_pin!(PIN_16, Bank::Bank0, 16);
impl_pin!(PIN_17, Bank::Bank0, 17);
impl_pin!(PIN_18, Bank::Bank0, 18);
impl_pin!(PIN_19, Bank::Bank0, 19);
impl_pin!(PIN_20, Bank::Bank0, 20);
impl_pin!(PIN_21, Bank::Bank0, 21);
impl_pin!(PIN_22, Bank::Bank0, 22);
impl_pin!(PIN_23, Bank::Bank0, 23);
impl_pin!(PIN_24, Bank::Bank0, 24);
impl_pin!(PIN_25, Bank::Bank0, 25);
impl_pin!(PIN_26, Bank::Bank0, 26);
impl_pin!(PIN_27, Bank::Bank0, 27);
impl_pin!(PIN_28, Bank::Bank0, 28);
impl_pin!(PIN_29, Bank::Bank0, 29);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_30, Bank::Bank0, 30);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_31, Bank::Bank0, 31);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_32, Bank::Bank0, 32);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_33, Bank::Bank0, 33);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_34, Bank::Bank0, 34);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_35, Bank::Bank0, 35);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_36, Bank::Bank0, 36);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_37, Bank::Bank0, 37);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_38, Bank::Bank0, 38);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_39, Bank::Bank0, 39);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_40, Bank::Bank0, 40);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_41, Bank::Bank0, 41);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_42, Bank::Bank0, 42);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_43, Bank::Bank0, 43);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_44, Bank::Bank0, 44);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_45, Bank::Bank0, 45);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_46, Bank::Bank0, 46);
#[cfg(feature = "rp235xb")]
impl_pin!(PIN_47, Bank::Bank0, 47);
// TODO rp235x bank1 as gpio support
#[cfg(feature = "qspi-as-gpio")]
2021-03-29 02:11:32 +00:00
impl_pin!(PIN_QSPI_SCLK, Bank::Qspi, 0);
#[cfg(feature = "qspi-as-gpio")]
2021-03-29 02:11:32 +00:00
impl_pin!(PIN_QSPI_SS, Bank::Qspi, 1);
#[cfg(feature = "qspi-as-gpio")]
2021-03-29 02:11:32 +00:00
impl_pin!(PIN_QSPI_SD0, Bank::Qspi, 2);
#[cfg(feature = "qspi-as-gpio")]
2021-03-29 02:11:32 +00:00
impl_pin!(PIN_QSPI_SD1, Bank::Qspi, 3);
#[cfg(feature = "qspi-as-gpio")]
2021-03-29 02:11:32 +00:00
impl_pin!(PIN_QSPI_SD2, Bank::Qspi, 4);
#[cfg(feature = "qspi-as-gpio")]
2021-03-29 02:11:32 +00:00
impl_pin!(PIN_QSPI_SD3, Bank::Qspi, 5);
2022-02-15 16:28:48 +00:00
// ====================
mod eh02 {
use super::*;
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::InputPin for Input<'d> {
2022-02-15 16:28:48 +00:00
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
2022-02-15 16:28:48 +00:00
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
2022-02-15 16:28:48 +00:00
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::OutputPin for Output<'d> {
2022-02-15 16:28:48 +00:00
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Output<'d> {
2022-02-15 16:28:48 +00:00
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
2022-02-15 16:28:48 +00:00
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
2022-02-15 16:28:48 +00:00
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Output<'d> {
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
Ok(self.toggle())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::InputPin for OutputOpenDrain<'d> {
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::OutputPin for OutputOpenDrain<'d> {
type Error = Infallible;
#[inline]
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
#[inline]
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for OutputOpenDrain<'d> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for OutputOpenDrain<'d> {
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
Ok(self.toggle())
}
}
2022-07-09 00:12:17 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::InputPin for Flex<'d> {
2022-07-09 00:12:17 +00:00
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
2022-07-09 00:12:17 +00:00
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
2022-07-09 00:12:17 +00:00
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::OutputPin for Flex<'d> {
2022-07-09 00:12:17 +00:00
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Flex<'d> {
2022-07-09 00:12:17 +00:00
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
2022-07-09 00:12:17 +00:00
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
2022-07-09 00:12:17 +00:00
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Flex<'d> {
2022-07-09 00:12:17 +00:00
type Error = Infallible;
#[inline]
fn toggle(&mut self) -> Result<(), Self::Error> {
Ok(self.toggle())
}
}
2022-02-15 16:28:48 +00:00
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::ErrorType for Input<'d> {
type Error = Infallible;
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::InputPin for Input<'d> {
2023-12-14 15:01:51 +00:00
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
2022-02-15 16:28:48 +00:00
2023-12-14 15:01:51 +00:00
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
2022-02-15 16:28:48 +00:00
}
}
2022-02-15 16:28:48 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::ErrorType for Output<'d> {
type Error = Infallible;
}
2022-02-15 16:28:48 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::OutputPin for Output<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
2022-02-15 16:28:48 +00:00
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
2022-02-15 16:28:48 +00:00
}
}
2022-02-15 16:28:48 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::StatefulOutputPin for Output<'d> {
2023-12-14 15:01:51 +00:00
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
2022-02-15 16:28:48 +00:00
}
2023-12-14 15:01:51 +00:00
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
}
}
2022-02-15 16:28:48 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::ErrorType for OutputOpenDrain<'d> {
type Error = Infallible;
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::OutputPin for OutputOpenDrain<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
2022-07-09 00:12:17 +00:00
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
2022-07-09 00:12:17 +00:00
}
}
2022-07-09 00:12:17 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::StatefulOutputPin for OutputOpenDrain<'d> {
2023-12-14 15:01:51 +00:00
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
2022-07-09 00:12:17 +00:00
2023-12-14 15:01:51 +00:00
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
2022-07-09 00:12:17 +00:00
}
}
2022-07-09 00:12:17 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::InputPin for OutputOpenDrain<'d> {
2023-12-14 15:01:51 +00:00
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
2022-07-09 00:12:17 +00:00
}
2023-12-14 15:01:51 +00:00
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
2022-07-09 00:12:17 +00:00
}
}
2022-07-09 00:12:17 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::ErrorType for Flex<'d> {
type Error = Infallible;
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::InputPin for Flex<'d> {
2023-12-14 15:01:51 +00:00
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_high())
}
2023-12-14 15:01:51 +00:00
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_low())
2022-07-09 00:12:17 +00:00
}
}
2022-07-09 00:12:17 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::OutputPin for Flex<'d> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Ok(self.set_high())
2022-07-09 00:12:17 +00:00
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Ok(self.set_low())
2022-07-09 00:12:17 +00:00
}
}
2022-07-09 00:12:17 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_1::digital::StatefulOutputPin for Flex<'d> {
2023-12-14 15:01:51 +00:00
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_high())
}
2022-07-09 00:12:17 +00:00
2023-12-14 15:01:51 +00:00
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok((*self).is_set_low())
2022-07-09 00:12:17 +00:00
}
}
2022-07-09 00:12:17 +00:00
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_async::digital::Wait for Flex<'d> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_async::digital::Wait for Input<'d> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}
2024-01-22 20:30:29 +00:00
impl<'d> embedded_hal_async::digital::Wait for OutputOpenDrain<'d> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
2022-02-15 16:28:48 +00:00
}