1use 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#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
24#[cfg_attr(feature = "defmt", derive(defmt::Format))]
25pub enum Speed {
26 Medium = 0b01,
28 Low = 0b10,
30 #[default]
32 High = 0b11,
33}
34
35impl Speed {
36 #[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#[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
124pub struct Flex<'d> {
130 pub(crate) pin: Peri<'d, AnyPin>,
131}
132
133impl<'d> Flex<'d> {
134 #[inline]
140 pub fn new(pin: Peri<'d, impl Pin>) -> Self {
141 Self { pin: pin.into() }
143 }
144
145 #[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 #[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 #[inline]
169 pub fn set_as_output_open_drain(&mut self, speed: Speed) {
170 #[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 });
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 #[inline]
206 pub fn is_set_low(&self) -> bool {
207 !self.is_set_high()
208 }
209
210 #[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 #[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 #[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
246pub struct Input<'d> {
248 pub(crate) pin: Flex<'d>,
249}
250
251impl<'d> Input<'d> {
252 #[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 #[inline]
262 pub fn is_high(&self) -> bool {
263 self.pin.is_high()
264 }
265
266 #[inline]
268 pub fn is_low(&self) -> bool {
269 self.pin.is_low()
270 }
271
272 #[inline]
274 pub fn get_level(&self) -> Level {
275 self.pin.get_level()
276 }
277}
278
279pub struct Output<'d> {
285 pub(crate) pin: Flex<'d>,
286}
287
288impl<'d> Output<'d> {
289 #[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 #[inline]
303 pub fn set_high(&mut self) {
304 self.pin.set_high();
305 }
306
307 #[inline]
309 pub fn set_low(&mut self) {
310 self.pin.set_low();
311 }
312
313 #[inline]
315 pub fn set_level(&mut self, level: Level) {
316 self.pin.set_level(level)
317 }
318
319 #[inline]
321 pub fn is_set_high(&self) -> bool {
322 self.pin.is_set_high()
323 }
324
325 #[inline]
327 pub fn is_set_low(&self) -> bool {
328 self.pin.is_set_low()
329 }
330
331 #[inline]
333 pub fn get_output_level(&self) -> Level {
334 self.pin.get_output_level()
335 }
336
337 #[inline]
339 pub fn toggle(&mut self) {
340 self.pin.toggle();
341 }
342}
343
344pub struct OutputOpenDrain<'d> {
350 pub(crate) pin: Flex<'d>,
351}
352
353impl<'d> OutputOpenDrain<'d> {
354 #[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 #[inline]
370 pub fn is_high(&self) -> bool {
371 !self.pin.is_low()
372 }
373
374 #[inline]
376 pub fn is_low(&self) -> bool {
377 self.pin.is_low()
378 }
379
380 #[inline]
382 pub fn get_level(&self) -> Level {
383 self.pin.get_level()
384 }
385
386 #[inline]
388 pub fn set_high(&mut self) {
389 self.pin.set_high();
390 }
391
392 #[inline]
394 pub fn set_low(&mut self) {
395 self.pin.set_low();
396 }
397
398 #[inline]
400 pub fn set_level(&mut self, level: Level) {
401 self.pin.set_level(level);
402 }
403
404 #[inline]
406 pub fn is_set_high(&self) -> bool {
407 self.pin.is_set_high()
408 }
409
410 #[inline]
412 pub fn is_set_low(&self) -> bool {
413 self.pin.is_set_low()
414 }
415
416 #[inline]
418 pub fn get_output_level(&self) -> Level {
419 self.pin.get_output_level()
420 }
421
422 #[inline]
424 pub fn toggle(&mut self) {
425 self.pin.toggle()
426 }
427}
428
429pub enum OutputType {
431 PushPull,
433 #[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 OutputPushPull = 0b10,
453 #[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#[derive(Copy, Clone)]
477pub struct AfType {
478 mode: vals::Mode,
479 cnf: vals::Cnf,
480 pull: Pull,
481}
482
483impl AfType {
484 pub const fn input(pull: Pull) -> Self {
486 let cnf = match pull {
487 Pull::None => vals::Cnf::FLOATING_IN__OPEN_DRAIN_OUT,
488 _ => vals::Cnf::PULL_IN__AF_PUSH_PULL_OUT,
492 };
493 Self {
494 mode: vals::Mode::INPUT,
495 cnf,
496 pull,
497 }
498 }
499
500 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
515pub(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 #[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)); } else {
545 self.block().bshr().write(|w| w.set_bs(n, true));
546 }
547 }
548 }
549
550 #[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)); } 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 #[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 #[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 #[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 #[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 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 #[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 #[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 #[inline]
689 fn pin(&self) -> u8 {
690 self._pin()
691 }
692
693 #[inline]
695 fn port(&self) -> u8 {
696 self._port()
697 }
698
699 #[inline]
701 fn degrade(self) -> AnyPin {
702 AnyPin {
703 pin_port: self.pin_port(),
704 }
705 }
706}
707
708pub 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
759pub(crate) unsafe fn init(_cs: CriticalSection, reset_afio: bool) {
767 #[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
780impl<'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 #[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 #[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 #[inline]
904 fn is_set_low(&mut self) -> Result<bool, Self::Error> {
905 Ok((*self).is_set_low())
906 }
907}