sifli_hal/gpio/
mod.rs

1//! GPIO driver.
2#![macro_use]
3use core::convert::Infallible;
4use core::future::Future;
5use core::pin::Pin as FuturePin;
6use core::task::{Context, Poll};
7
8use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef};
9use embassy_sync::waitqueue::AtomicWaker;
10use hpsys::HpsysPin;
11
12use crate::interrupt::InterruptExt;
13use crate::{interrupt, peripherals, Peripheral};
14use crate::utils::BitIter64;
15
16// TODO: move this const to _generated.rs
17#[cfg(any(feature = "sf32lb52x"))]
18pub(crate) const PA_PIN_COUNT: usize = 44;
19
20pub mod hpsys;
21
22static PA_WAKERS: [AtomicWaker; PA_PIN_COUNT] = [const { AtomicWaker::new() }; PA_PIN_COUNT];
23
24/// Represents a digital input or output level.
25#[derive(Debug, Eq, PartialEq, Clone, Copy)]
26pub enum Level {
27    /// Logical low.
28    Low,
29    /// Logical high.
30    High,
31}
32
33impl From<bool> for Level {
34    fn from(val: bool) -> Self {
35        match val {
36            true => Self::High,
37            false => Self::Low,
38        }
39    }
40}
41
42impl From<Level> for bool {
43    fn from(level: Level) -> bool {
44        match level {
45            Level::Low => false,
46            Level::High => true,
47        }
48    }
49}
50
51impl core::ops::Not for Level {
52    type Output = Self;
53
54    fn not(self) -> Self::Output {
55        (!bool::from(self)).into()
56    }
57}
58
59/// Represents a pull setting for an input.
60#[derive(Debug, Clone, Copy, Eq, PartialEq)]
61pub enum Pull {
62    /// No pull.
63    None,
64    /// Internal pull-up resistor.
65    Up,
66    /// Internal pull-down resistor.
67    Down,
68}
69
70/// Drive strength of an output
71#[derive(Debug, Eq, PartialEq)]
72pub enum Drive {
73    /// min drive, {ds1, ds0} = 0b00
74    Drive0,
75    /// {ds1, ds0} = 0b01
76    Drive1,
77    /// {ds1, ds0} = 0b10
78    Drive2,
79    /// {ds1, ds0} = 0b11
80    Drive3,
81}
82/// Slew rate of an output
83#[derive(Debug, Eq, PartialEq)]
84pub enum SlewRate {
85    /// Fast slew rate.
86    Fast,
87    /// Slow slew rate.
88    Slow,
89}
90
91/// GPIO input driver.
92pub struct Input<'d> {
93    pin: Flex<'d>,
94}
95
96impl<'d> Input<'d> {
97    /// Create GPIO input driver for a [Pin] with the provided [Pull] configuration.
98    #[inline]
99    pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, pull: Pull) -> Self {
100        let mut pin = Flex::new(pin);
101        pin.set_pull(pull);
102        pin.set_as_input();
103        
104        Self { pin }
105    }
106
107    /// Set the pin's Schmitt trigger.
108    #[inline]
109    pub fn set_schmitt(&mut self, enable: bool) {
110        self.pin.set_schmitt(enable)
111    }
112
113    /// Get whether the pin input level is high.
114    #[inline]
115    pub fn is_high(&self) -> bool {
116        self.pin.is_high()
117    }
118
119    /// Get whether the pin input level is low.
120    #[inline]
121    pub fn is_low(&self) -> bool {
122        self.pin.is_low()
123    }
124
125    /// Returns current pin level
126    #[inline]
127    pub fn get_level(&self) -> Level {
128        self.pin.get_level()
129    }
130
131    /// Wait until the pin is high. If it is already high, return immediately.
132    #[inline]
133    pub async fn wait_for_high(&mut self) {
134        self.pin.wait_for_high().await;
135    }
136
137    /// Wait until the pin is low. If it is already low, return immediately.
138    #[inline]
139    pub async fn wait_for_low(&mut self) {
140        self.pin.wait_for_low().await;
141    }
142
143    /// Wait for the pin to undergo a transition from low to high.
144    #[inline]
145    pub async fn wait_for_rising_edge(&mut self) {
146        self.pin.wait_for_rising_edge().await;
147    }
148
149    /// Wait for the pin to undergo a transition from high to low.
150    #[inline]
151    pub async fn wait_for_falling_edge(&mut self) {
152        self.pin.wait_for_falling_edge().await;
153    }
154
155    /// Wait for the pin to undergo any transition, i.e low to high OR high to low.
156    #[inline]
157    pub async fn wait_for_any_edge(&mut self) {
158        self.pin.wait_for_any_edge().await;
159    }
160}
161
162/// Interrupt trigger levels.
163#[derive(Debug, Eq, PartialEq, Copy, Clone)]
164#[cfg_attr(feature = "defmt", derive(defmt::Format))]
165pub enum InterruptTrigger {
166    /// Trigger on pin low.
167    LevelLow,
168    /// Trigger on pin high.
169    LevelHigh,
170    /// Trigger on high to low transition.
171    EdgeLow,
172    /// Trigger on low to high transition.
173    EdgeHigh,
174    /// Trigger on any transition.
175    AnyEdge,
176}
177
178pub(crate) unsafe fn init(gpio1_it_priority: interrupt::Priority) {
179    unsafe {
180        interrupt::GPIO1.disable();
181        interrupt::GPIO1.set_priority(gpio1_it_priority);
182        interrupt::GPIO1.enable();
183    }
184    crate::rcc::enable::<peripherals::HPSYS_GPIO>();
185
186    // We should not reset PINMUX here, otherwise the pins used for PSRAM
187    // or FLASH will be invalid.
188    // PINMUX is already turned on in the bootloader.
189}
190
191#[cfg(feature = "rt")]
192fn irq_handler<const N: usize>(wakers: &[AtomicWaker; N]) {
193    let gpio = crate::pac::HPSYS_GPIO;
194
195    // Read both interrupt status registers
196    let isr0 = gpio.isr0().read().0;
197    let isr1 = gpio.isr1().read().0;
198    // trace!("gpio irq: isr0={:x} isr1={:x}", isr0, isr1);
199    
200    // Combine into a single 64-bit status for easier iteration
201    let status = (isr1 as u64) << 32 | isr0 as u64;
202
203    if status == 0 {
204        return;
205    }
206
207    for pin_idx in BitIter64(status) {
208        let mut pin = HpsysPin::new(pin_idx);
209
210        // Disable the interrupt for this pin to prevent re-firing.
211        // The future will re-enable it on the next await.
212        pin.disable_interrupt();
213
214        // Clear the interrupt flag.
215        pin.clear_interrupt();
216        
217        // TODO: The C HAL implementation (`HAL_GPIO_EXTI_IRQHandler`) includes logic
218        // to clear the corresponding AON (Always-On) Wakeup Source Register (WSR)
219        // if a GPIO pin is also configured as a wakeup pin. This is important for
220        // low-power sleep/wakeup functionality.
221        // This implementation omits that step as it requires a mapping from GPIO
222        // pins to AON wakeup pins and access to AON registers, which is beyond
223        // the scope of a plain GPIO interrupt handler. If you use GPIO interrupts
224        // to wake the system from sleep, you may need to add this logic manually
225        // by calling `HAL_HPAON_CLEAR_WSR` or its Rust equivalent.
226
227        wakers[pin_idx as usize].wake();
228    }
229}
230
231#[cfg(feature = "rt")]
232#[interrupt]
233fn GPIO1() {
234    irq_handler(&PA_WAKERS);
235}
236
237#[must_use = "futures do nothing unless you `.await` or poll them"]
238struct InputFuture<'d> {
239    pin: PeripheralRef<'d, AnyPin>,
240}
241
242impl<'d> InputFuture<'d> {
243    fn new(pin: PeripheralRef<'d, AnyPin>, trigger: InterruptTrigger) -> Self {
244        let mut hpsys_pin = HpsysPin::new(pin.pin_bank());
245        // Configure the hardware for the desired interrupt.
246        hpsys_pin.set_interrupt_trigger(trigger);
247        // Clear any pending interrupt flags before we start waiting.
248        hpsys_pin.clear_interrupt();
249        // Enable the interrupt.
250        hpsys_pin.enable_interrupt();
251
252        //
253        hpsys_pin.set_high();
254        Self { pin }
255    }
256}
257
258impl<'d> Drop for InputFuture<'d> {
259    fn drop(&mut self) {
260        // When the future is dropped, disable the interrupt for the pin
261        // to prevent stray interrupts.
262        // let mut pin = HpsysPin::new(self.pin.pin_bank());
263        // pin.disable_interrupt();
264        // pin.clear_interrupt();
265    }
266}
267
268impl<'d> Future for InputFuture<'d> {
269    type Output = ();
270
271    fn poll(self: FuturePin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
272        let pin_idx = self.pin.pin() as usize;
273        let hpsys_pin = HpsysPin::new(self.pin.pin_bank());
274
275        // Register the waker.
276        PA_WAKERS[pin_idx].register(cx.waker());
277
278        // Check if the interrupt has already fired since we registered the waker.
279        // The ISR will clear the IER bit, so if it's clear, we know it fired.
280        if hpsys_pin.is_interrupt_disabled() {
281            Poll::Ready(())
282        } else {
283            Poll::Pending
284        }
285    }
286}
287
288/// GPIO output driver.
289pub struct Output<'d> {
290    pin: Flex<'d>,
291}
292
293impl<'d> Output<'d> {
294    /// Create GPIO output driver for a [Pin] with the provided [Level].
295    #[inline]
296    pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, initial_output: Level) -> Self {
297        let mut pin = Flex::new(pin);
298        pin.set_level(initial_output);
299
300        pin.set_as_output();
301        Self { pin }
302    }
303
304    /// Set the pin's drive strength.
305    #[inline]
306    pub fn set_drive_strength(&mut self, strength: Drive) {
307        self.pin.set_drive_strength(strength)
308    }
309
310    /// Set the pin's slew rate.
311    #[inline]
312    pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
313        self.pin.set_slew_rate(slew_rate)
314    }
315
316    /// Set the output as high.
317    #[inline]
318    pub fn set_high(&mut self) {
319        self.pin.set_high()
320    }
321
322    /// Set the output as low.
323    #[inline]
324    pub fn set_low(&mut self) {
325        self.pin.set_low()
326    }
327
328    /// Set the output level.
329    #[inline]
330    pub fn set_level(&mut self, level: Level) {
331        self.pin.set_level(level)
332    }
333
334    /// Is the output pin set as high?
335    #[inline]
336    pub fn is_set_high(&self) -> bool {
337        self.pin.is_set_high()
338    }
339
340    /// Is the output pin set as low?
341    #[inline]
342    pub fn is_set_low(&self) -> bool {
343        self.pin.is_set_low()
344    }
345
346    /// What level output is set to
347    #[inline]
348    pub fn get_output_level(&self) -> Level {
349        self.pin.get_output_level()
350    }
351
352    /// Toggle pin output
353    #[inline]
354    pub fn toggle(&mut self) {
355        self.pin.toggle()
356    }
357}
358
359/// GPIO output open-drain.
360pub struct OutputOpenDrain<'d> {
361    pin: Flex<'d>,
362}
363
364impl<'d> OutputOpenDrain<'d> {
365    /// Create GPIO output driver for a [Pin] in open drain mode with the provided [Level].
366    #[inline]
367    pub fn new(pin: impl Peripheral<P = impl Pin> + 'd, initial_output: Level) -> Self {
368        let mut pin = Flex::new(pin);
369        // SET_OPEN_DRAIN_FLAG
370        pin.set_low();
371
372        pin.set_as_output_od();
373
374        pin.set_level(initial_output);
375        Self { pin }
376    }
377
378    /// Set the pin's pull-up.
379    #[inline]
380    pub fn set_pullup(&mut self, enable: bool) {
381        if enable {
382            self.pin.set_pull(Pull::Up);
383        } else {
384            self.pin.set_pull(Pull::None);
385        }
386    }
387
388    /// Set the pin's drive strength.
389    #[inline]
390    pub fn set_drive_strength(&mut self, strength: Drive) {
391        self.pin.set_drive_strength(strength)
392    }
393
394    /// Set the pin's slew rate.
395    #[inline]
396    pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
397        self.pin.set_slew_rate(slew_rate)
398    }
399
400    /// Set the output as high.
401    #[inline]
402    pub fn set_high(&mut self) {
403        // For Open Drain High, disable the output pin.
404        self.pin.set_as_input()
405    }
406
407    /// Set the output as low.
408    #[inline]
409    pub fn set_low(&mut self) {
410        // For Open Drain Low, enable the output pin.
411        self.pin.set_as_output()
412    }
413
414    /// Set the output level.
415    #[inline]
416    pub fn set_level(&mut self, level: Level) {
417        match level {
418            Level::Low => self.set_low(),
419            Level::High => self.set_high(),
420        }
421    }
422
423    /// Is the output level high?
424    #[inline]
425    pub fn is_set_high(&self) -> bool {
426        !self.is_set_low()
427    }
428
429    /// Is the output level low?
430    #[inline]
431    pub fn is_set_low(&self) -> bool {
432        self.pin.is_set_as_output()
433    }
434
435    /// What level output is set to
436    #[inline]
437    pub fn get_output_level(&self) -> Level {
438        self.is_set_high().into()
439    }
440
441    /// Toggle pin output
442    #[inline]
443    pub fn toggle(&mut self) {
444        self.pin.toggle()
445    }
446
447    /// Get whether the pin input level is high.
448    #[inline]
449    pub fn is_high(&self) -> bool {
450        self.pin.is_high()
451    }
452
453    /// Get whether the pin input level is low.
454    #[inline]
455    pub fn is_low(&self) -> bool {
456        self.pin.is_low()
457    }
458
459    /// Returns current pin level
460    #[inline]
461    pub fn get_level(&self) -> Level {
462        self.is_high().into()
463    }
464
465    /// Wait until the pin is high. If it is already high, return immediately.
466    #[inline]
467    pub async fn wait_for_high(&mut self) {
468        self.pin.wait_for_high().await;
469    }
470
471    /// Wait until the pin is low. If it is already low, return immediately.
472    #[inline]
473    pub async fn wait_for_low(&mut self) {
474        self.pin.wait_for_low().await;
475    }
476
477    /// Wait for the pin to undergo a transition from low to high.
478    #[inline]
479    pub async fn wait_for_rising_edge(&mut self) {
480        self.pin.wait_for_rising_edge().await;
481    }
482
483    /// Wait for the pin to undergo a transition from high to low.
484    #[inline]
485    pub async fn wait_for_falling_edge(&mut self) {
486        self.pin.wait_for_falling_edge().await;
487    }
488
489    /// Wait for the pin to undergo any transition, i.e low to high OR high to low.
490    #[inline]
491    pub async fn wait_for_any_edge(&mut self) {
492        self.pin.wait_for_any_edge().await;
493    }
494}
495
496pub struct Analog<'d> {
497    _pin: Flex<'d>,
498}
499
500impl<'d> Analog<'d> {
501    /// Create GPIO analog driver for a [Pin].
502    #[inline]
503    pub fn new(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
504        let mut pin = Flex::new(pin);
505        pin.set_as_analog();
506        Self { _pin: pin}
507    }
508}
509
510
511
512/// GPIO flexible pin.
513///
514/// This pin can be either an input or output pin. The output level register bit will remain
515/// set while not in output mode, so the pin's level will be 'remembered' when it is not in output
516/// mode.
517pub struct Flex<'d> {
518    pub(crate) pin: PeripheralRef<'d, AnyPin>,
519    inner: HpsysPin,
520}
521
522impl<'d> Flex<'d> {
523    /// Create a new Flex pin
524    pub fn new(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
525        into_ref!(pin);
526        let mut flex = Self {
527            inner: HpsysPin::new(pin.pin_bank()),
528            pin: pin.map_into(),
529        };
530        flex.disable_interrupt();
531        unsafe { flex.inner.set_fsel_unchecked(0) };
532        flex
533    }
534
535    pub fn new_without_init(pin: impl Peripheral<P = impl Pin> + 'd) -> Self {
536        into_ref!(pin);
537        Self {
538            inner: HpsysPin::new(pin.pin_bank()),
539            pin: pin.map_into(),
540        }
541    }
542
543    pub fn disable_interrupt(&mut self) {
544        self.inner.disable_interrupt();
545        self.inner.clear_interrupt();
546    }
547
548    /// Configure pin as output
549    pub fn set_as_output(&mut self) {
550        self.inner.set_as_output();
551    }
552
553    /// Configure pin as input
554    pub fn set_as_input(&mut self) {
555        self.inner.set_as_input();
556    }
557    
558    /// Configure pin as open drain output
559    pub fn set_as_output_od(&mut self) {
560        self.inner.set_as_output_od();
561    }
562
563    /// Configure pin as analog mode
564    pub fn set_as_analog(&mut self) {
565        self.inner.set_as_analog();
566    }
567
568    /// Returns whether pin is configured as output
569    pub fn is_set_as_output(&self) -> bool {
570        self.inner.is_set_as_output()
571    }
572
573    /// Set pin level high
574    pub fn set_high(&mut self) {
575        self.inner.set_high();
576    }
577
578    /// Set pin level low 
579    pub fn set_low(&mut self) {
580        self.inner.set_low();
581    }
582
583    /// Toggle pin level
584    pub fn toggle(&mut self) {
585        self.inner.toggle();
586    }
587
588    /// Get current pin output level
589    pub fn get_output_level(&self) -> Level {
590        self.inner.get_output_level()
591    }
592
593    /// Get current pin level (either input or output depending on mode)
594    pub fn get_level(&self) -> Level {
595        self.inner.get_level()
596    }
597
598    /// Set pin level
599    pub fn set_level(&mut self, level: Level) {
600        self.inner.set_level(level)
601    }
602
603    /// Returns whether pin level is high
604    pub fn is_high(&self) -> bool {
605        self.inner.is_high()
606    }
607
608    /// Returns whether pin level is low
609    pub fn is_low(&self) -> bool {
610        self.inner.is_low()
611    }
612
613    /// Returns whether pin output level is high 
614    pub fn is_set_high(&self) -> bool {
615        self.inner.is_set_high()
616    }
617
618    /// Returns whether pin output level is low
619    pub fn is_set_low(&self) -> bool {
620        self.inner.is_set_low()
621    }
622    
623    /// Set pin pull resistor
624    pub fn set_pull(&mut self, pull: Pull) {
625        self.inner.set_pull(pull);
626    }
627
628    /// Set pin drive strength 
629    pub fn set_drive_strength(&mut self, drive: Drive) {
630        self.inner.set_drive_strength(drive);
631    }
632
633    /// Set pin slew rate
634    pub fn set_slew_rate(&mut self, slew_rate: SlewRate) {
635        self.inner.set_slew_rate(slew_rate);
636    }
637
638    /// Enable schmitt input
639    pub fn set_schmitt(&mut self, enable: bool) {
640        self.inner.set_schmitt(enable);
641    }
642
643    /// Set pin function select and attributes
644    pub fn set_function(&mut self, fsel: u8, af_type: crate::gpio::AfType) {
645        self.inner.set_function(fsel, af_type);
646    }
647
648    /// Wait until the pin is high. If it is already high, return immediately.
649    #[inline]
650    pub async fn wait_for_high(&mut self) {
651        if self.is_high() {
652            return;
653        }
654        InputFuture::new(self.pin.reborrow(), InterruptTrigger::LevelHigh).await;
655    }
656
657    /// Wait until the pin is low. If it is already low, return immediately.
658    #[inline]
659    pub async fn wait_for_low(&mut self) {
660        if self.is_low() {
661            return;
662        }
663        InputFuture::new(self.pin.reborrow(), InterruptTrigger::LevelLow).await;
664    }
665
666    /// Wait for the pin to undergo a transition from low to high.
667    #[inline]
668    pub async fn wait_for_rising_edge(&mut self) {
669        InputFuture::new(self.pin.reborrow(), InterruptTrigger::EdgeHigh).await;
670    }
671
672    /// Wait for the pin to undergo a transition from high to low.
673    #[inline]
674    pub async fn wait_for_falling_edge(&mut self) {
675        InputFuture::new(self.pin.reborrow(), InterruptTrigger::EdgeLow).await;
676    }
677
678    /// Wait for the pin to undergo any transition, i.e low to high OR high to low.
679    #[inline]
680    pub async fn wait_for_any_edge(&mut self) {
681        InputFuture::new(self.pin.reborrow(), InterruptTrigger::AnyEdge).await;
682    }
683}
684
685impl<'d> Drop for Flex<'d> {
686    #[inline]
687    fn drop(&mut self) {
688        // TODO
689        self.pin.set_as_disconnected();
690    }
691}
692
693pub struct AfType {
694    pub(crate) pull: Pull,
695}
696
697impl AfType {
698    pub(crate) fn new(pull: Pull) -> Self {
699        Self { pull }
700    }
701}
702
703pub(crate) trait SealedPin: Sized {
704    fn pin_bank(&self) -> u8;
705
706    #[inline]
707    fn _pin(&self) -> u8 {
708        self.pin_bank() & 0x7f
709    }
710
711    #[inline]
712    fn _bank(&self) -> u8 {
713        self.pin_bank() >> 7
714    }
715
716    // fn gpio(&self) -> pac::hpsys_gpio::HpsysGpio {
717    //     pac::HPSYS_GPIO
718    // }
719
720    // fn pinmux(&self) -> pac::hpsys_pinmux::HpsysPinmux {
721    //     pac::HPSYS_PINMUX
722    // }
723
724    /// Set the pin as "disconnected", ie doing nothing and consuming the lowest
725    /// amount of power possible.
726    #[inline]
727    fn set_as_disconnected(&self) {
728        let mut pin = HpsysPin::new(self.pin_bank());
729        pin.disable_interrupt();
730        pin.clear_interrupt();
731        pin.set_ipr(false, false);
732        unsafe { pin.set_fsel_unchecked(0) };
733        // pin.set_ie(false);
734        pin.set_drive_strength(Drive::Drive0);
735        pin.set_pull(Pull::None);
736    }
737
738    #[inline]
739    fn set_function(&self, fsel: u8, af_type: AfType) {
740        let mut pin = HpsysPin::new(self.pin_bank());
741        pin.set_function(fsel, af_type);
742    }
743}
744
745/// Interface for a Pin that can be configured by an [Input] or [Output] driver, or converted to an [AnyPin].
746#[allow(private_bounds)]
747pub trait Pin: Peripheral<P = Self> + Into<AnyPin> + SealedPin + Sized + 'static {
748    /// Degrade to a generic pin struct
749    fn degrade(self) -> AnyPin {
750        AnyPin {
751            pin_bank: self.pin_bank(),
752        }
753    }
754
755    /// Returns the pin number within a bank
756    #[inline]
757    fn pin(&self) -> u8 {
758        self._pin()
759    }
760
761    /// Returns the bank of this pin (PA=0, PB=1)
762    #[inline]
763    fn bank(&self) -> u8 {
764        self._bank()
765    }
766}
767
768/// Type-erased GPIO pin
769pub struct AnyPin {
770    pin_bank: u8,
771}
772
773impl AnyPin {
774    /// Unsafely create a new type-erased pin.
775    ///
776    /// # Safety
777    ///
778    /// You must ensure that you’re only using one instance of this type at a time.
779    pub unsafe fn steal(pin_bank: u8) -> Self {
780        Self { pin_bank }
781    }
782}
783
784impl_peripheral!(AnyPin);
785
786impl Pin for AnyPin {}
787impl SealedPin for AnyPin {
788    fn pin_bank(&self) -> u8 {
789        self.pin_bank
790    }
791}
792
793// ==========================
794
795macro_rules! impl_pin {
796    ($name:ident, $bank:expr, $pin_num:expr) => {
797        impl Pin for peripherals::$name {}
798        impl SealedPin for peripherals::$name {
799            #[inline]
800            fn pin_bank(&self) -> u8 {
801                ($bank as u8) * 128 + $pin_num
802            }
803        }
804
805        impl From<peripherals::$name> for crate::gpio::AnyPin {
806            fn from(val: peripherals::$name) -> Self {
807                crate::gpio::Pin::degrade(val)
808            }
809        }
810    };
811}
812
813// ====================
814
815mod eh02 {
816    use super::*;
817
818    impl<'d> embedded_hal_02::digital::v2::InputPin for Input<'d> {
819        type Error = Infallible;
820
821        fn is_high(&self) -> Result<bool, Self::Error> {
822            Ok(self.is_high())
823        }
824
825        fn is_low(&self) -> Result<bool, Self::Error> {
826            Ok(self.is_low())
827        }
828    }
829
830    impl<'d> embedded_hal_02::digital::v2::OutputPin for Output<'d> {
831        type Error = Infallible;
832
833        fn set_high(&mut self) -> Result<(), Self::Error> {
834            Ok(self.set_high())
835        }
836
837        fn set_low(&mut self) -> Result<(), Self::Error> {
838            Ok(self.set_low())
839        }
840    }
841
842    impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Output<'d> {
843        fn is_set_high(&self) -> Result<bool, Self::Error> {
844            Ok(self.is_set_high())
845        }
846
847        fn is_set_low(&self) -> Result<bool, Self::Error> {
848            Ok(self.is_set_low())
849        }
850    }
851
852    impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Output<'d> {
853        type Error = Infallible;
854        #[inline]
855        fn toggle(&mut self) -> Result<(), Self::Error> {
856            Ok(self.toggle())
857        }
858    }
859
860    impl<'d> embedded_hal_02::digital::v2::InputPin for OutputOpenDrain<'d> {
861        type Error = Infallible;
862
863        fn is_high(&self) -> Result<bool, Self::Error> {
864            Ok(self.is_high())
865        }
866
867        fn is_low(&self) -> Result<bool, Self::Error> {
868            Ok(self.is_low())
869        }
870    }
871
872    impl<'d> embedded_hal_02::digital::v2::OutputPin for OutputOpenDrain<'d> {
873        type Error = Infallible;
874
875        #[inline]
876        fn set_high(&mut self) -> Result<(), Self::Error> {
877            Ok(self.set_high())
878        }
879
880        #[inline]
881        fn set_low(&mut self) -> Result<(), Self::Error> {
882            Ok(self.set_low())
883        }
884    }
885
886    impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for OutputOpenDrain<'d> {
887        fn is_set_high(&self) -> Result<bool, Self::Error> {
888            Ok(self.is_set_high())
889        }
890
891        fn is_set_low(&self) -> Result<bool, Self::Error> {
892            Ok(self.is_set_low())
893        }
894    }
895
896    impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for OutputOpenDrain<'d> {
897        type Error = Infallible;
898        #[inline]
899        fn toggle(&mut self) -> Result<(), Self::Error> {
900            Ok(self.toggle())
901        }
902    }
903
904    impl<'d> embedded_hal_02::digital::v2::InputPin for Flex<'d> {
905        type Error = Infallible;
906
907        fn is_high(&self) -> Result<bool, Self::Error> {
908            Ok(self.is_high())
909        }
910
911        fn is_low(&self) -> Result<bool, Self::Error> {
912            Ok(self.is_low())
913        }
914    }
915
916    impl<'d> embedded_hal_02::digital::v2::OutputPin for Flex<'d> {
917        type Error = Infallible;
918
919        fn set_high(&mut self) -> Result<(), Self::Error> {
920            Ok(self.set_high())
921        }
922
923        fn set_low(&mut self) -> Result<(), Self::Error> {
924            Ok(self.set_low())
925        }
926    }
927
928    impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Flex<'d> {
929        fn is_set_high(&self) -> Result<bool, Self::Error> {
930            Ok(self.is_set_high())
931        }
932
933        fn is_set_low(&self) -> Result<bool, Self::Error> {
934            Ok(self.is_set_low())
935        }
936    }
937
938    impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Flex<'d> {
939        type Error = Infallible;
940        #[inline]
941        fn toggle(&mut self) -> Result<(), Self::Error> {
942            Ok(self.toggle())
943        }
944    }
945}
946
947impl<'d> embedded_hal_1::digital::ErrorType for Input<'d> {
948    type Error = Infallible;
949}
950
951impl<'d> embedded_hal_1::digital::InputPin for Input<'d> {
952    fn is_high(&mut self) -> Result<bool, Self::Error> {
953        Ok((*self).is_high())
954    }
955
956    fn is_low(&mut self) -> Result<bool, Self::Error> {
957        Ok((*self).is_low())
958    }
959}
960
961impl<'d> embedded_hal_1::digital::ErrorType for Output<'d> {
962    type Error = Infallible;
963}
964
965impl<'d> embedded_hal_1::digital::OutputPin for Output<'d> {
966    fn set_high(&mut self) -> Result<(), Self::Error> {
967        Ok(self.set_high())
968    }
969
970    fn set_low(&mut self) -> Result<(), Self::Error> {
971        Ok(self.set_low())
972    }
973}
974
975impl<'d> embedded_hal_1::digital::StatefulOutputPin for Output<'d> {
976    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
977        Ok((*self).is_set_high())
978    }
979
980    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
981        Ok((*self).is_set_low())
982    }
983}
984
985impl<'d> embedded_hal_1::digital::ErrorType for OutputOpenDrain<'d> {
986    type Error = Infallible;
987}
988
989impl<'d> embedded_hal_1::digital::OutputPin for OutputOpenDrain<'d> {
990    fn set_high(&mut self) -> Result<(), Self::Error> {
991        Ok(self.set_high())
992    }
993
994    fn set_low(&mut self) -> Result<(), Self::Error> {
995        Ok(self.set_low())
996    }
997}
998
999impl<'d> embedded_hal_1::digital::StatefulOutputPin for OutputOpenDrain<'d> {
1000    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
1001        Ok((*self).is_set_high())
1002    }
1003
1004    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
1005        Ok((*self).is_set_low())
1006    }
1007}
1008
1009impl<'d> embedded_hal_1::digital::InputPin for OutputOpenDrain<'d> {
1010    fn is_high(&mut self) -> Result<bool, Self::Error> {
1011        Ok((*self).is_high())
1012    }
1013
1014    fn is_low(&mut self) -> Result<bool, Self::Error> {
1015        Ok((*self).is_low())
1016    }
1017}
1018
1019impl<'d> embedded_hal_1::digital::ErrorType for Flex<'d> {
1020    type Error = Infallible;
1021}
1022
1023impl<'d> embedded_hal_1::digital::InputPin for Flex<'d> {
1024    fn is_high(&mut self) -> Result<bool, Self::Error> {
1025        Ok((*self).is_high())
1026    }
1027
1028    fn is_low(&mut self) -> Result<bool, Self::Error> {
1029        Ok((*self).is_low())
1030    }
1031}
1032
1033impl<'d> embedded_hal_1::digital::OutputPin for Flex<'d> {
1034    fn set_high(&mut self) -> Result<(), Self::Error> {
1035        Ok(self.set_high())
1036    }
1037
1038    fn set_low(&mut self) -> Result<(), Self::Error> {
1039        Ok(self.set_low())
1040    }
1041}
1042
1043impl<'d> embedded_hal_1::digital::StatefulOutputPin for Flex<'d> {
1044    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
1045        Ok((*self).is_set_high())
1046    }
1047
1048    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
1049        Ok((*self).is_set_low())
1050    }
1051}
1052
1053impl<'d> embedded_hal_async::digital::Wait for Flex<'d> {
1054    async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
1055        self.wait_for_high().await;
1056        Ok(())
1057    }
1058
1059    async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
1060        self.wait_for_low().await;
1061        Ok(())
1062    }
1063
1064    async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
1065        self.wait_for_rising_edge().await;
1066        Ok(())
1067    }
1068
1069    async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
1070        self.wait_for_falling_edge().await;
1071        Ok(())
1072    }
1073
1074    async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
1075        self.wait_for_any_edge().await;
1076        Ok(())
1077    }
1078}
1079
1080impl<'d> embedded_hal_async::digital::Wait for Input<'d> {
1081    async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
1082        self.wait_for_high().await;
1083        Ok(())
1084    }
1085
1086    async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
1087        self.wait_for_low().await;
1088        Ok(())
1089    }
1090
1091    async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
1092        self.wait_for_rising_edge().await;
1093        Ok(())
1094    }
1095
1096    async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
1097        self.wait_for_falling_edge().await;
1098        Ok(())
1099    }
1100
1101    async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
1102        self.wait_for_any_edge().await;
1103        Ok(())
1104    }
1105}
1106
1107impl<'d> embedded_hal_async::digital::Wait for OutputOpenDrain<'d> {
1108    async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
1109        self.wait_for_high().await;
1110        Ok(())
1111    }
1112
1113    async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
1114        self.wait_for_low().await;
1115        Ok(())
1116    }
1117
1118    async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
1119        self.wait_for_rising_edge().await;
1120        Ok(())
1121    }
1122
1123    async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
1124        self.wait_for_falling_edge().await;
1125        Ok(())
1126    }
1127
1128    async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
1129        self.wait_for_any_edge().await;
1130        Ok(())
1131    }
1132}
1133