Skip to main content

ch32_hal/
gpio.rs

1//! GPIO
2//!
3//! Pin can be configured as:
4//! - Floating Input
5//! - PullUp Input
6//! - PullDown Input
7//! - Analog Input
8//! - OpenDrain Output
9//! - PushPull Output
10//! - Alternate Function (input or output)
11//!
12//! Power On: Floating Input except for some Alternate Function
13
14use core::convert::Infallible;
15
16use critical_section::CriticalSection;
17use embassy_hal_internal::PeripheralType;
18use pac::gpio::vals;
19
20use crate::{exti, impl_peripheral, pac, peripherals, Peri};
21
22/// Speed, for output mode
23#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
24#[cfg_attr(feature = "defmt", derive(defmt::Format))]
25pub enum Speed {
26    /// 10MHz
27    Medium = 0b01,
28    /// 2MHz
29    Low = 0b10,
30    /// 50MHz
31    #[default]
32    High = 0b11,
33}
34
35impl Speed {
36    /// Compile-time conversion to the `MODEy` field value, family-aware.
37    /// gpio_x0 collapses every speed to OUTPUT_50MHZ — its GPIO doesn't
38    /// expose the slower modes.
39    #[cfg(any(gpio_v3, gpio_v0, gpio_h4))]
40    pub const fn to_mode(self) -> vals::Mode {
41        match self {
42            Speed::Medium => vals::Mode::OUTPUT_10MHZ,
43            Speed::Low => vals::Mode::OUTPUT_2MHZ,
44            Speed::High => vals::Mode::OUTPUT_50MHZ,
45        }
46    }
47
48    #[cfg(gpio_x0)]
49    pub const fn to_mode(self) -> vals::Mode {
50        vals::Mode::OUTPUT_50MHZ
51    }
52}
53
54impl From<Speed> for vals::Mode {
55    #[inline]
56    fn from(value: Speed) -> Self {
57        value.to_mode()
58    }
59}
60
61/// Pull setting for an input.
62#[derive(Debug, Eq, PartialEq, Copy, Clone)]
63#[cfg_attr(feature = "defmt", derive(defmt::Format))]
64pub enum Pull {
65    None,
66    Up,
67    Down,
68}
69
70impl From<Pull> for vals::Cnf {
71    fn from(value: Pull) -> Self {
72        match value {
73            Pull::None => vals::Cnf::FLOATING_IN__OPEN_DRAIN_OUT,
74            _ => vals::Cnf::PULL_IN__AF_PUSH_PULL_OUT,
75        }
76    }
77}
78
79impl Pull {
80    #[inline]
81    #[allow(unused)]
82    fn to_cnf(&self) -> u8 {
83        match self {
84            Pull::None => 0b01,
85            _ => 0b10,
86        }
87    }
88}
89
90#[derive(Debug, Eq, PartialEq, Copy, Clone)]
91#[cfg_attr(feature = "defmt", derive(defmt::Format))]
92pub enum Level {
93    Low,
94    High,
95}
96
97impl From<bool> for Level {
98    fn from(val: bool) -> Self {
99        match val {
100            true => Self::High,
101            false => Self::Low,
102        }
103    }
104}
105
106impl From<u8> for Level {
107    fn from(val: u8) -> Self {
108        match val {
109            0 => Self::Low,
110            _ => Self::High,
111        }
112    }
113}
114
115impl From<Level> for bool {
116    fn from(level: Level) -> bool {
117        match level {
118            Level::Low => false,
119            Level::High => true,
120        }
121    }
122}
123
124/// GPIO flexible pin.
125///
126/// This pin can either be a disconnected, input, or output pin, or both. The level register bit will remain
127/// set while not in output mode, so the pin's level will be 'remembered' when it is not in output
128/// mode.
129pub struct Flex<'d> {
130    pub(crate) pin: Peri<'d, AnyPin>,
131}
132
133impl<'d> Flex<'d> {
134    /// Wrap the pin in a `Flex`.
135    ///
136    /// The pin remains disconnected. The initial output level is unspecified, but can be changed
137    /// before the pin is put into output mode.
138    ///
139    #[inline]
140    pub fn new(pin: Peri<'d, impl Pin>) -> Self {
141        // Pin will be in disconnected state.
142        Self { pin: pin.into() }
143    }
144
145    /// Put the pin into input mode.
146    #[inline]
147    pub fn set_as_input(&mut self, pull: Pull) {
148        critical_section::with(|_| {
149            self.pin.set_mode_cnf(vals::Mode::INPUT, pull.into());
150            self.pin.set_pull(pull);
151        });
152    }
153
154    /// Put the pin into output mode.
155    ///
156    /// The pin level will be whatever was set before (or low by default). If you want it to begin
157    /// at a specific level, call `set_high`/`set_low` on the pin first.
158    #[inline]
159    pub fn set_as_output(&mut self, speed: Speed) {
160        critical_section::with(|_| {
161            self.pin.set_as_output(speed);
162        });
163    }
164
165    /// Put the pin into output mode with open drain.
166    ///
167    /// Pull resistor is disabled in this mode.
168    #[inline]
169    pub fn set_as_output_open_drain(&mut self, speed: Speed) {
170        // 通用开漏输出模式
171        #[cfg(not(gpio_x0))]
172        critical_section::with(|_| {
173            self.pin
174                .set_mode_cnf(speed.into(), vals::Cnf::FLOATING_IN__OPEN_DRAIN_OUT);
175        });
176        #[cfg(gpio_x0)]
177        critical_section::with(|_| {
178            self.pin
179                .set_mode_cnf(speed.into(), vals::Cnf::PULL_IN__AF_PUSH_PULL_OUT);
180            // FIXME: only i2c is auto set to open drain
181        });
182    }
183
184    #[inline]
185    pub fn is_high(&self) -> bool {
186        self.pin.block().indr().read().idr(self.pin.pin() as usize)
187    }
188
189    #[inline]
190    pub fn is_low(&self) -> bool {
191        !self.is_high()
192    }
193
194    #[inline]
195    pub fn get_level(&self) -> Level {
196        self.is_high().into()
197    }
198
199    #[inline]
200    pub fn is_set_high(&self) -> bool {
201        self.pin.block().outdr().read().odr(self.pin.pin() as usize)
202    }
203
204    /// Is the output pin set as low?
205    #[inline]
206    pub fn is_set_low(&self) -> bool {
207        !self.is_set_high()
208    }
209
210    /// What level output is set to
211    #[inline]
212    pub fn get_output_level(&self) -> Level {
213        self.is_set_high().into()
214    }
215
216    #[inline]
217    pub fn set_high(&mut self) {
218        self.pin.set_high();
219    }
220
221    /// Set the output as low.
222    #[inline]
223    pub fn set_low(&mut self) {
224        self.pin.set_low();
225    }
226
227    #[inline]
228    pub fn set_level(&mut self, level: Level) {
229        match level {
230            Level::Low => self.pin.set_low(),
231            Level::High => self.pin.set_high(),
232        }
233    }
234
235    /// Toggle pin output
236    #[inline]
237    pub fn toggle(&mut self) {
238        if self.is_set_low() {
239            self.set_high()
240        } else {
241            self.set_low()
242        }
243    }
244}
245
246/// GPIO input driver.
247pub struct Input<'d> {
248    pub(crate) pin: Flex<'d>,
249}
250
251impl<'d> Input<'d> {
252    /// Create GPIO input driver for a [Pin] with the provided [Pull] configuration.
253    #[inline]
254    pub fn new(pin: Peri<'d, impl Pin>, pull: Pull) -> Self {
255        let mut pin = Flex::new(pin);
256        pin.set_as_input(pull);
257        Self { pin }
258    }
259
260    /// Get whether the pin input level is high.
261    #[inline]
262    pub fn is_high(&self) -> bool {
263        self.pin.is_high()
264    }
265
266    /// Get whether the pin input level is low.
267    #[inline]
268    pub fn is_low(&self) -> bool {
269        self.pin.is_low()
270    }
271
272    /// Get the current pin input level.
273    #[inline]
274    pub fn get_level(&self) -> Level {
275        self.pin.get_level()
276    }
277}
278
279/// GPIO output driver.
280///
281/// Note that pins will **return to their floating state** when `Output` is dropped.
282/// If pins should retain their state indefinitely, either keep ownership of the
283/// `Output`, or pass it to [`core::mem::forget`].
284pub struct Output<'d> {
285    pub(crate) pin: Flex<'d>,
286}
287
288impl<'d> Output<'d> {
289    /// Create GPIO output driver for a [Pin] with the provided [Level] and [Speed] configuration.
290    #[inline]
291    pub fn new(pin: Peri<'d, impl Pin>, initial_output: Level, speed: Speed) -> Self {
292        let mut pin = Flex::new(pin);
293        match initial_output {
294            Level::High => pin.set_high(),
295            Level::Low => pin.set_low(),
296        }
297        pin.set_as_output(speed);
298        Self { pin }
299    }
300
301    /// Set the output as high.
302    #[inline]
303    pub fn set_high(&mut self) {
304        self.pin.set_high();
305    }
306
307    /// Set the output as low.
308    #[inline]
309    pub fn set_low(&mut self) {
310        self.pin.set_low();
311    }
312
313    /// Set the output level.
314    #[inline]
315    pub fn set_level(&mut self, level: Level) {
316        self.pin.set_level(level)
317    }
318
319    /// Is the output pin set as high?
320    #[inline]
321    pub fn is_set_high(&self) -> bool {
322        self.pin.is_set_high()
323    }
324
325    /// Is the output pin set as low?
326    #[inline]
327    pub fn is_set_low(&self) -> bool {
328        self.pin.is_set_low()
329    }
330
331    /// What level output is set to
332    #[inline]
333    pub fn get_output_level(&self) -> Level {
334        self.pin.get_output_level()
335    }
336
337    /// Toggle pin output
338    #[inline]
339    pub fn toggle(&mut self) {
340        self.pin.toggle();
341    }
342}
343
344/// GPIO output open-drain driver.
345///
346/// Note that pins will **return to their floating state** when `OutputOpenDrain` is dropped.
347/// If pins should retain their state indefinitely, either keep ownership of the
348/// `OutputOpenDrain`, or pass it to [`core::mem::forget`].
349pub struct OutputOpenDrain<'d> {
350    pub(crate) pin: Flex<'d>,
351}
352
353impl<'d> OutputOpenDrain<'d> {
354    /// Create a new GPIO open drain output driver for a [Pin] with the provided [Level] and [Speed], [Pull] configuration.
355    #[inline]
356    pub fn new(pin: Peri<'d, impl Pin>, initial_output: Level, speed: Speed) -> Self {
357        let mut pin = Flex::new(pin);
358
359        match initial_output {
360            Level::High => pin.set_high(),
361            Level::Low => pin.set_low(),
362        }
363
364        pin.set_as_output_open_drain(speed);
365        Self { pin }
366    }
367
368    /// Get whether the pin input level is high.
369    #[inline]
370    pub fn is_high(&self) -> bool {
371        !self.pin.is_low()
372    }
373
374    /// Get whether the pin input level is low.
375    #[inline]
376    pub fn is_low(&self) -> bool {
377        self.pin.is_low()
378    }
379
380    /// Get the current pin input level.
381    #[inline]
382    pub fn get_level(&self) -> Level {
383        self.pin.get_level()
384    }
385
386    /// Set the output as high.
387    #[inline]
388    pub fn set_high(&mut self) {
389        self.pin.set_high();
390    }
391
392    /// Set the output as low.
393    #[inline]
394    pub fn set_low(&mut self) {
395        self.pin.set_low();
396    }
397
398    /// Set the output level.
399    #[inline]
400    pub fn set_level(&mut self, level: Level) {
401        self.pin.set_level(level);
402    }
403
404    /// Get whether the output level is set to high.
405    #[inline]
406    pub fn is_set_high(&self) -> bool {
407        self.pin.is_set_high()
408    }
409
410    /// Get whether the output level is set to low.
411    #[inline]
412    pub fn is_set_low(&self) -> bool {
413        self.pin.is_set_low()
414    }
415
416    /// Get the current output level.
417    #[inline]
418    pub fn get_output_level(&self) -> Level {
419        self.pin.get_output_level()
420    }
421
422    /// Toggle pin output
423    #[inline]
424    pub fn toggle(&mut self) {
425        self.pin.toggle()
426    }
427}
428
429/// GPIO output type
430pub enum OutputType {
431    /// Drive the pin both high or low.
432    PushPull,
433    /// Drive the pin low, or don't drive it at all if the output level is high.
434    #[cfg(not(gpio_x0))]
435    OpenDrain,
436}
437
438impl From<OutputType> for AFType {
439    fn from(value: OutputType) -> Self {
440        match value {
441            #[cfg(not(gpio_x0))]
442            OutputType::OpenDrain => AFType::OutputOpenDrain,
443            OutputType::PushPull => AFType::OutputPushPull,
444        }
445    }
446}
447
448#[derive(Debug, Copy, Clone)]
449#[cfg_attr(feature = "defmt", derive(defmt::Format))]
450pub enum AFType {
451    /// Output, drive the pin both high or low.
452    OutputPushPull = 0b10,
453    /// Output, drive the pin low, or don't drive it at all if the output level is high.
454    #[cfg(not(gpio_x0))]
455    OutputOpenDrain = 0b11,
456}
457
458impl From<AFType> for vals::Cnf {
459    fn from(value: AFType) -> Self {
460        match value {
461            AFType::OutputPushPull => vals::Cnf::PULL_IN__AF_PUSH_PULL_OUT,
462            #[cfg(not(gpio_x0))]
463            AFType::OutputOpenDrain => vals::Cnf::AF_OPEN_DRAIN_OUT,
464        }
465    }
466}
467
468/// Bundle of mode/cnf/pull settings for an alternate-function pin.
469///
470/// Mirrors embassy-stm32's `gpio_v1::AfType`. Drivers pass a single
471/// `AfType` per pin to `Pin::set_as_af()` (via the `new_pin!` /
472/// `set_as_af!` macros) instead of juggling separate `set_as_af_output` /
473/// `set_as_input` calls. On qingke V0/V3 GPIO the AF concept is the same
474/// regardless of direction: input pins set MODE=Input + CNF=Floating/Pull,
475/// output pins set MODE=2/10/50MHz + CNF=AF_PushPull/AF_OpenDrain.
476#[derive(Copy, Clone)]
477pub struct AfType {
478    mode: vals::Mode,
479    cnf: vals::Cnf,
480    pull: Pull,
481}
482
483impl AfType {
484    /// Input AF pin (e.g. UART RX, SPI MISO).
485    pub const fn input(pull: Pull) -> Self {
486        let cnf = match pull {
487            Pull::None => vals::Cnf::FLOATING_IN__OPEN_DRAIN_OUT,
488            // PULL_IN and AF_PUSH_PULL_OUT share the same CNF encoding (0b10);
489            // for input mode this means "input with pull-up/down" and pull
490            // direction comes from ODR via `set_pull` below.
491            _ => vals::Cnf::PULL_IN__AF_PUSH_PULL_OUT,
492        };
493        Self {
494            mode: vals::Mode::INPUT,
495            cnf,
496            pull,
497        }
498    }
499
500    /// Output AF pin (e.g. UART TX, SPI MOSI/SCK, I2C SCL/SDA).
501    pub const fn output(output_type: OutputType, speed: Speed) -> Self {
502        let cnf = match output_type {
503            OutputType::PushPull => vals::Cnf::PULL_IN__AF_PUSH_PULL_OUT,
504            #[cfg(not(gpio_x0))]
505            OutputType::OpenDrain => vals::Cnf::AF_OPEN_DRAIN_OUT,
506        };
507        Self {
508            mode: speed.to_mode(),
509            cnf,
510            pull: Pull::None,
511        }
512    }
513}
514
515/// Alternate function type settings, CNF, when MODE>0b00
516
517pub(crate) trait SealedPin {
518    fn pin_port(&self) -> u8;
519
520    #[inline]
521    fn _pin(&self) -> u8 {
522        self.pin_port() % 32
523    }
524    #[inline]
525    fn _port(&self) -> u8 {
526        self.pin_port() / 32
527    }
528
529    #[inline]
530    fn block(&self) -> pac::gpio::Gpio {
531        pac::GPIO(self._port() as _)
532    }
533
534    /// Set the output as high.
535    #[inline]
536    fn set_high(&self) {
537        let n = self._pin() as _;
538        #[cfg(not(gpio_x0))]
539        self.block().bshr().write(|w| w.set_bs(n, true));
540        #[cfg(gpio_x0)]
541        {
542            if n >= 16 {
543                self.block().bsxr().write(|w| w.set_bs(n - 16, true)); // extended BS
544            } else {
545                self.block().bshr().write(|w| w.set_bs(n, true));
546            }
547        }
548    }
549
550    /// Set the output as low.
551    #[inline]
552    fn set_low(&self) {
553        let n = self._pin() as _;
554        #[cfg(not(gpio_x0))]
555        self.block().bshr().write(|w| w.set_br(n, true));
556        #[cfg(gpio_x0)]
557        {
558            if n >= 16 {
559                self.block().bsxr().write(|w| w.set_br(n - 16, true)); // extended BS
560            } else {
561                self.block().bshr().write(|w| w.set_br(n, true));
562            }
563        }
564    }
565
566    #[inline]
567    fn set_mode_cnf(&self, mode: vals::Mode, cnf: vals::Cnf) {
568        let pin = self._pin() as usize;
569        let block = self.block();
570
571        // H4 keeps `MODEy` as a plain output flag (RM §9.3.1.1) and selects the
572        // slew rate through `GPIOx_SPEED`, two bits per pin (RM §9.3.1.8). Its
573        // reset value (00b, 10 MHz) leaves the SDIO pads too slow to drive
574        // CMD: `CMDSENT` was set while the card saw nothing, so every response
575        // timed out. WCH's `GPIO_Init` programs it for every output mode, and
576        // so does this, reusing the legacy `MODEy` speed encoding.
577        #[cfg(gpio_h4)]
578        if !matches!(mode, vals::Mode::INPUT) {
579            let speed = match mode {
580                vals::Mode::OUTPUT_50MHZ => vals::Speed::MEDIUM,
581                _ => vals::Speed::LOW,
582            };
583            block.speed().modify(|w| w.set_speed(pin, speed));
584        }
585
586        match pin / 8 {
587            0 => {
588                block.cfglr().modify(|w| {
589                    w.set_cnf(pin % 8, cnf);
590                    w.set_mode(pin % 8, mode);
591                });
592            }
593            #[cfg(not(gpio_v0))]
594            1 => {
595                block.cfghr().modify(|w| {
596                    w.set_cnf(pin % 8, cnf);
597                    w.set_mode(pin % 8, mode);
598                });
599            }
600            // only for GPIO with 24 lines
601            #[cfg(gpio_x0)]
602            2 => {
603                block.cfgxr().modify(|w| {
604                    w.set_cnf(pin % 8, cnf);
605                    w.set_mode(pin % 8, mode);
606                });
607            }
608            _ => unreachable!(),
609        }
610    }
611
612    #[inline]
613    fn set_as_output(&self, speed: Speed) {
614        self.set_mode_cnf(speed.into(), vals::Cnf::ANALOG_IN__PUSH_PULL_OUT);
615    }
616
617    #[inline]
618    fn set_as_input(&self, pull: Pull) {
619        self.set_mode_cnf(vals::Mode::INPUT, pull.into());
620        self.set_pull(pull);
621    }
622
623    #[inline]
624    fn set_pull(&self, pull: Pull) {
625        let pin = self._pin() as _;
626
627        match pull {
628            Pull::Up => self.block().outdr().modify(|w| w.set_odr(pin, true)),
629            Pull::Down => self.block().outdr().modify(|w| w.set_odr(pin, false)),
630            _ => {}
631        }
632    }
633
634    /// Only one type, alternate function + push pull
635    #[inline]
636    fn set_as_af_output(&self, af_type: AFType, speed: Speed) {
637        self.set_mode_cnf(speed.into(), af_type.into());
638    }
639
640    /// Unified AF configuration — preferred by `new_pin!` / `set_as_af!`.
641    ///
642    /// On every ch32 family the basic GPIO mode/cnf is still V3-style
643    /// (the legacy AF push-pull / open-drain CNF values), so the function
644    /// signature stays the same. On CH32H4 an additional 4-bit AF number
645    /// has to be written to `AFIO.GPIO_AFR[n]` to pick which peripheral
646    /// signal connects to the pin — that's the `#[cfg(afio_h4)]` arm. On
647    /// every other chip the function selection is done indirectly via the
648    /// `AFIO.PCFR{1,2}` central remap registers (handled separately by
649    /// `pin.afio_remap()`).
650    #[inline]
651    fn set_as_af(&self, #[cfg(afio_h4)] af_num: u8, af_type: AfType) {
652        self.set_mode_cnf(af_type.mode, af_type.cnf);
653        self.set_pull(af_type.pull);
654        #[cfg(afio_h4)]
655        {
656            // AFIO.gpio_afr is a 12-element array: index = port_idx * 2 +
657            // (pin / 8), inner AFR field = pin % 8. port_idx maps A=0..F=5.
658            let port = self._port() as usize;
659            let pin = self._pin() as usize;
660            crate::pac::AFIO
661                .gpio_afr(port * 2 + (pin / 8))
662                .modify(|w| w.set_afr(pin % 8, af_num));
663        }
664    }
665
666    /// Analog mode, both input and output
667    #[inline]
668    fn set_as_analog(&self) {
669        self.set_mode_cnf(vals::Mode::INPUT, vals::Cnf::ANALOG_IN__PUSH_PULL_OUT);
670    }
671
672    /// Set the pin as "disconnected", ie doing nothing and consuming the lowest
673    /// amount of power possible.
674    ///
675    /// This is currently the same as set_as_analog but is semantically different really.
676    /// Drivers should set_as_disconnected pins when dropped.
677    #[inline]
678    fn set_as_disconnected(&self) {
679        self.set_as_analog();
680    }
681}
682
683#[allow(private_bounds)]
684pub trait Pin: PeripheralType + Into<AnyPin> + SealedPin + Sized + 'static {
685    type ExtiChannel: crate::exti::Channel;
686
687    /// Number of the pin within the port (0..31)
688    #[inline]
689    fn pin(&self) -> u8 {
690        self._pin()
691    }
692
693    /// Port of the pin
694    #[inline]
695    fn port(&self) -> u8 {
696        self._port()
697    }
698
699    /// Convert from concrete pin type PX_XX to type erased `AnyPin`.
700    #[inline]
701    fn degrade(self) -> AnyPin {
702        AnyPin {
703            pin_port: self.pin_port(),
704        }
705    }
706}
707
708// Type-erased GPIO pin
709pub struct AnyPin {
710    pin_port: u8,
711}
712
713impl AnyPin {
714    #[inline]
715    pub unsafe fn steal(pin_port: u8) -> Self {
716        Self { pin_port }
717    }
718
719    #[inline]
720    fn _port(&self) -> u8 {
721        self.pin_port / 32
722    }
723}
724
725impl_peripheral!(AnyPin);
726impl Pin for AnyPin {
727    #[cfg(exti)]
728    type ExtiChannel = exti::AnyChannel;
729}
730impl SealedPin for AnyPin {
731    #[inline]
732    fn pin_port(&self) -> u8 {
733        self.pin_port
734    }
735}
736
737foreach_pin!(
738    ($pin_name:ident, $port_name:ident, $port_num:expr, $pin_num:expr, $exti_ch:ident) => {
739        impl Pin for peripherals::$pin_name {
740            #[cfg(exti)]
741            type ExtiChannel = peripherals::$exti_ch;
742        }
743
744        impl SealedPin for peripherals::$pin_name {
745            #[inline]
746            fn pin_port(&self) -> u8 {
747                $port_num * 32 + $pin_num
748            }
749        }
750
751        impl From<peripherals::$pin_name> for AnyPin {
752            fn from(x: peripherals::$pin_name) -> Self {
753                x.degrade()
754            }
755        }
756    };
757);
758
759/// Enable the GPIO peripheral clock.
760///
761/// `reset_afio` controls the reset pulse that `enable_and_reset` sends to the
762/// AFIO block. That pulse clears `GPIO_AFR` back to AF0, so a core that does
763/// not own the pin mux must pass `false`: the boot core has already enabled the
764/// AFIO clock, and resetting it again would drop every pin the other core
765/// configured.
766pub(crate) unsafe fn init(_cs: CriticalSection, reset_afio: bool) {
767    // Gated on "the chip has an AFIO peripheral", like embassy-stm32's
768    // `gpio::init`. CH32H4 needs it too: its pin mux lives in
769    // `AFIO.GPIO_AFR`, which `set_as_af` writes for every AF pin. With the
770    // AFIO clock off those writes are silently dropped and every pin stays
771    // on AF0.
772    #[cfg(afio)]
773    if reset_afio {
774        <crate::peripherals::AFIO as crate::peripheral::SealedRccPeripheral>::enable_and_reset_with_cs(_cs);
775    }
776
777    crate::_generated::init_gpio();
778}
779
780// eh impls
781
782impl<'d> embedded_hal::digital::ErrorType for Input<'d> {
783    type Error = Infallible;
784}
785
786impl<'d> embedded_hal::digital::InputPin for Input<'d> {
787    #[inline]
788    fn is_high(&mut self) -> Result<bool, Self::Error> {
789        Ok((*self).is_high())
790    }
791
792    #[inline]
793    fn is_low(&mut self) -> Result<bool, Self::Error> {
794        Ok((*self).is_low())
795    }
796}
797
798impl<'d> embedded_hal::digital::ErrorType for Output<'d> {
799    type Error = Infallible;
800}
801
802impl<'d> embedded_hal::digital::OutputPin for Output<'d> {
803    #[inline]
804    fn set_high(&mut self) -> Result<(), Self::Error> {
805        Ok(self.set_high())
806    }
807
808    #[inline]
809    fn set_low(&mut self) -> Result<(), Self::Error> {
810        Ok(self.set_low())
811    }
812}
813
814impl<'d> embedded_hal::digital::StatefulOutputPin for Output<'d> {
815    #[inline]
816    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
817        Ok((*self).is_set_high())
818    }
819
820    /// Is the output pin set as low?
821    #[inline]
822    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
823        Ok((*self).is_set_low())
824    }
825}
826
827impl<'d> embedded_hal::digital::ErrorType for OutputOpenDrain<'d> {
828    type Error = Infallible;
829}
830
831impl<'d> embedded_hal::digital::InputPin for OutputOpenDrain<'d> {
832    #[inline]
833    fn is_high(&mut self) -> Result<bool, Self::Error> {
834        Ok((*self).is_high())
835    }
836
837    #[inline]
838    fn is_low(&mut self) -> Result<bool, Self::Error> {
839        Ok((*self).is_low())
840    }
841}
842
843impl<'d> embedded_hal::digital::OutputPin for OutputOpenDrain<'d> {
844    #[inline]
845    fn set_high(&mut self) -> Result<(), Self::Error> {
846        Ok(self.set_high())
847    }
848
849    #[inline]
850    fn set_low(&mut self) -> Result<(), Self::Error> {
851        Ok(self.set_low())
852    }
853}
854
855impl<'d> embedded_hal::digital::StatefulOutputPin for OutputOpenDrain<'d> {
856    #[inline]
857    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
858        Ok((*self).is_set_high())
859    }
860
861    /// Is the output pin set as low?
862    #[inline]
863    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
864        Ok((*self).is_set_low())
865    }
866}
867
868impl<'d> embedded_hal::digital::InputPin for Flex<'d> {
869    #[inline]
870    fn is_high(&mut self) -> Result<bool, Self::Error> {
871        Ok((*self).is_high())
872    }
873
874    #[inline]
875    fn is_low(&mut self) -> Result<bool, Self::Error> {
876        Ok((*self).is_low())
877    }
878}
879
880impl<'d> embedded_hal::digital::OutputPin for Flex<'d> {
881    #[inline]
882    fn set_high(&mut self) -> Result<(), Self::Error> {
883        Ok(self.set_high())
884    }
885
886    #[inline]
887    fn set_low(&mut self) -> Result<(), Self::Error> {
888        Ok(self.set_low())
889    }
890}
891
892impl<'d> embedded_hal::digital::ErrorType for Flex<'d> {
893    type Error = Infallible;
894}
895
896impl<'d> embedded_hal::digital::StatefulOutputPin for Flex<'d> {
897    #[inline]
898    fn is_set_high(&mut self) -> Result<bool, Self::Error> {
899        Ok((*self).is_set_high())
900    }
901
902    /// Is the output pin set as low?
903    #[inline]
904    fn is_set_low(&mut self) -> Result<bool, Self::Error> {
905        Ok((*self).is_set_low())
906    }
907}