1#![macro_use]
3
4use core::convert::Infallible;
5use core::hint::unreachable_unchecked;
6
7use cfg_if::cfg_if;
8use embassy_hal_internal::{impl_peripheral, Peri, PeripheralType};
9
10use crate::pac;
11use crate::pac::common::{Reg, RW};
12use crate::pac::gpio;
13use crate::pac::gpio::vals;
14#[cfg(not(feature = "_nrf51"))]
15use crate::pac::shared::{regs::Psel, vals::Connect};
16
17#[derive(Debug, Eq, PartialEq)]
19pub enum Port {
20 Port0,
22
23 #[cfg(feature = "_gpio-p1")]
25 Port1,
26
27 #[cfg(feature = "_gpio-p2")]
29 Port2,
30}
31
32#[derive(Clone, Copy, Debug, Eq, PartialEq)]
34#[cfg_attr(feature = "defmt", derive(defmt::Format))]
35pub enum Pull {
36 None,
38 Up,
40 Down,
42}
43
44pub struct Input<'d> {
46 pub(crate) pin: Flex<'d>,
47}
48
49impl<'d> Input<'d> {
50 #[inline]
52 pub fn new(pin: Peri<'d, impl Pin>, pull: Pull) -> Self {
53 let mut pin = Flex::new(pin);
54 pin.set_as_input(pull);
55
56 Self { pin }
57 }
58
59 #[inline]
61 pub fn is_high(&self) -> bool {
62 self.pin.is_high()
63 }
64
65 #[inline]
67 pub fn is_low(&self) -> bool {
68 self.pin.is_low()
69 }
70
71 #[inline]
73 pub fn get_level(&self) -> Level {
74 self.pin.get_level()
75 }
76}
77
78#[derive(Clone, Copy, Debug, Eq, PartialEq)]
80#[cfg_attr(feature = "defmt", derive(defmt::Format))]
81pub enum Level {
82 Low,
84 High,
86}
87
88impl From<bool> for Level {
89 fn from(val: bool) -> Self {
90 match val {
91 true => Self::High,
92 false => Self::Low,
93 }
94 }
95}
96
97impl From<Level> for bool {
98 fn from(level: Level) -> bool {
99 match level {
100 Level::Low => false,
101 Level::High => true,
102 }
103 }
104}
105
106#[cfg(feature = "_nrf54l")]
109#[derive(Clone, Copy, Debug, PartialEq)]
110#[cfg_attr(feature = "defmt", derive(defmt::Format))]
111#[repr(u8)]
112pub enum LevelDrive {
113 Disconnect = 2,
115 Standard = 0,
117 High = 1,
119 ExtraHigh = 3,
121}
122
123#[cfg(feature = "_nrf54l")]
128#[derive(Clone, Copy, Debug, PartialEq)]
129#[cfg_attr(feature = "defmt", derive(defmt::Format))]
130pub struct OutputDrive {
131 low: LevelDrive,
132 high: LevelDrive,
133}
134
135#[cfg(feature = "_nrf54l")]
136#[allow(non_upper_case_globals)]
137impl OutputDrive {
138 pub const Standard: Self = Self {
140 low: LevelDrive::Standard,
141 high: LevelDrive::Standard,
142 };
143 pub const HighDrive0Standard1: Self = Self {
145 low: LevelDrive::High,
146 high: LevelDrive::Standard,
147 };
148 pub const Standard0HighDrive1: Self = Self {
150 low: LevelDrive::Standard,
151 high: LevelDrive::High,
152 };
153 pub const HighDrive: Self = Self {
155 low: LevelDrive::High,
156 high: LevelDrive::High,
157 };
158 pub const Disconnect0Standard1: Self = Self {
160 low: LevelDrive::Disconnect,
161 high: LevelDrive::Standard,
162 };
163 pub const Disconnect0HighDrive1: Self = Self {
165 low: LevelDrive::Disconnect,
166 high: LevelDrive::High,
167 };
168 pub const Standard0Disconnect1: Self = Self {
170 low: LevelDrive::Standard,
171 high: LevelDrive::Disconnect,
172 };
173 pub const HighDrive0Disconnect1: Self = Self {
175 low: LevelDrive::High,
176 high: LevelDrive::Disconnect,
177 };
178}
179
180#[cfg(not(feature = "_nrf54l"))]
183#[derive(Clone, Copy, Debug, PartialEq)]
184#[cfg_attr(feature = "defmt", derive(defmt::Format))]
185#[repr(u8)]
186pub enum OutputDrive {
187 Standard = 0,
189 HighDrive0Standard1 = 1,
191 Standard0HighDrive1 = 2,
193 HighDrive = 3,
195 Disconnect0Standard1 = 4,
197 Disconnect0HighDrive1 = 5,
199 Standard0Disconnect1 = 6,
201 HighDrive0Disconnect1 = 7,
203}
204
205pub struct Output<'d> {
207 pub(crate) pin: Flex<'d>,
208}
209
210impl<'d> Output<'d> {
211 #[inline]
213 pub fn new(pin: Peri<'d, impl Pin>, initial_output: Level, drive: OutputDrive) -> Self {
214 let mut pin = Flex::new(pin);
215 match initial_output {
216 Level::High => pin.set_high(),
217 Level::Low => pin.set_low(),
218 }
219 pin.set_as_output(drive);
220
221 Self { pin }
222 }
223
224 #[inline]
226 pub fn set_high(&mut self) {
227 self.pin.set_high()
228 }
229
230 #[inline]
232 pub fn set_low(&mut self) {
233 self.pin.set_low()
234 }
235
236 #[inline]
238 pub fn toggle(&mut self) {
239 self.pin.toggle()
240 }
241
242 #[inline]
244 pub fn set_level(&mut self, level: Level) {
245 self.pin.set_level(level)
246 }
247
248 #[inline]
250 pub fn is_set_high(&self) -> bool {
251 self.pin.is_set_high()
252 }
253
254 #[inline]
256 pub fn is_set_low(&self) -> bool {
257 self.pin.is_set_low()
258 }
259
260 #[inline]
262 pub fn get_output_level(&self) -> Level {
263 self.pin.get_output_level()
264 }
265}
266
267pub(crate) fn convert_drive(w: &mut pac::gpio::regs::PinCnf, drive: OutputDrive) {
268 #[cfg(not(feature = "_nrf54l"))]
269 {
270 let drive = match drive {
271 OutputDrive::Standard => vals::Drive::S0S1,
272 OutputDrive::HighDrive0Standard1 => vals::Drive::H0S1,
273 OutputDrive::Standard0HighDrive1 => vals::Drive::S0H1,
274 OutputDrive::HighDrive => vals::Drive::H0H1,
275 OutputDrive::Disconnect0Standard1 => vals::Drive::D0S1,
276 OutputDrive::Disconnect0HighDrive1 => vals::Drive::D0H1,
277 OutputDrive::Standard0Disconnect1 => vals::Drive::S0D1,
278 OutputDrive::HighDrive0Disconnect1 => vals::Drive::H0D1,
279 };
280 w.set_drive(drive);
281 }
282
283 #[cfg(feature = "_nrf54l")]
284 {
285 fn convert(d: LevelDrive) -> vals::Drive {
286 match d {
287 LevelDrive::Disconnect => vals::Drive::D,
288 LevelDrive::Standard => vals::Drive::S,
289 LevelDrive::High => vals::Drive::H,
290 LevelDrive::ExtraHigh => vals::Drive::E,
291 }
292 }
293
294 w.set_drive0(convert(drive.low));
295 w.set_drive1(convert(drive.high));
296 }
297}
298
299fn convert_pull(pull: Pull) -> vals::Pull {
300 match pull {
301 Pull::None => vals::Pull::DISABLED,
302 Pull::Up => vals::Pull::PULLUP,
303 Pull::Down => vals::Pull::PULLDOWN,
304 }
305}
306
307pub struct Flex<'d> {
313 pub(crate) pin: Peri<'d, AnyPin>,
314}
315
316impl<'d> Flex<'d> {
317 #[inline]
322 pub fn new(pin: Peri<'d, impl Pin>) -> Self {
323 Self { pin: pin.into() }
325 }
326
327 #[inline]
329 pub fn set_as_input(&mut self, pull: Pull) {
330 self.pin.conf().write(|w| {
331 w.set_dir(vals::Dir::INPUT);
332 w.set_input(vals::Input::CONNECT);
333 w.set_pull(convert_pull(pull));
334 convert_drive(w, OutputDrive::Standard);
335 w.set_sense(vals::Sense::DISABLED);
336 });
337 }
338
339 #[inline]
344 pub fn set_as_output(&mut self, drive: OutputDrive) {
345 self.pin.conf().write(|w| {
346 w.set_dir(vals::Dir::OUTPUT);
347 w.set_input(vals::Input::DISCONNECT);
348 w.set_pull(vals::Pull::DISABLED);
349 convert_drive(w, drive);
350 w.set_sense(vals::Sense::DISABLED);
351 });
352 }
353
354 #[inline]
364 pub fn set_as_input_output(&mut self, pull: Pull, drive: OutputDrive) {
365 self.pin.conf().write(|w| {
366 w.set_dir(vals::Dir::OUTPUT);
367 w.set_input(vals::Input::CONNECT);
368 w.set_pull(convert_pull(pull));
369 convert_drive(w, drive);
370 w.set_sense(vals::Sense::DISABLED);
371 });
372 }
373
374 #[inline]
376 pub fn set_as_disconnected(&mut self) {
377 self.pin.conf().write(|w| {
378 w.set_input(vals::Input::DISCONNECT);
379 });
380 }
381
382 #[inline]
384 pub fn is_high(&self) -> bool {
385 self.pin.block().in_().read().pin(self.pin.pin() as _)
386 }
387
388 #[inline]
390 pub fn is_low(&self) -> bool {
391 !self.is_high()
392 }
393
394 #[inline]
396 pub fn get_level(&self) -> Level {
397 self.is_high().into()
398 }
399
400 #[inline]
402 pub fn set_high(&mut self) {
403 self.pin.set_high()
404 }
405
406 #[inline]
408 pub fn set_low(&mut self) {
409 self.pin.set_low()
410 }
411
412 #[inline]
414 pub fn toggle(&mut self) {
415 if self.is_set_low() {
416 self.set_high()
417 } else {
418 self.set_low()
419 }
420 }
421
422 #[inline]
424 pub fn set_level(&mut self, level: Level) {
425 match level {
426 Level::Low => self.pin.set_low(),
427 Level::High => self.pin.set_high(),
428 }
429 }
430
431 #[inline]
433 pub fn is_set_high(&self) -> bool {
434 self.pin.block().out().read().pin(self.pin.pin() as _)
435 }
436
437 #[inline]
439 pub fn is_set_low(&self) -> bool {
440 !self.is_set_high()
441 }
442
443 #[inline]
445 pub fn get_output_level(&self) -> Level {
446 self.is_set_high().into()
447 }
448}
449
450impl<'d> Drop for Flex<'d> {
451 fn drop(&mut self) {
452 self.set_as_disconnected();
453 }
454}
455
456pub(crate) trait SealedPin {
457 fn pin_port(&self) -> u8;
458
459 #[inline]
460 fn _pin(&self) -> u8 {
461 cfg_if! {
462 if #[cfg(feature = "_gpio-p1")] {
463 self.pin_port() % 32
464 } else {
465 self.pin_port()
466 }
467 }
468 }
469
470 #[inline]
471 fn block(&self) -> gpio::Gpio {
472 match self.pin_port() / 32 {
473 #[cfg(feature = "_nrf51")]
474 0 => pac::GPIO,
475 #[cfg(not(feature = "_nrf51"))]
476 0 => pac::P0,
477 #[cfg(feature = "_gpio-p1")]
478 1 => pac::P1,
479 #[cfg(feature = "_gpio-p2")]
480 2 => pac::P2,
481 _ => unsafe { unreachable_unchecked() },
482 }
483 }
484
485 #[inline]
486 fn conf(&self) -> Reg<gpio::regs::PinCnf, RW> {
487 self.block().pin_cnf(self._pin() as usize)
488 }
489
490 #[inline]
492 fn set_high(&self) {
493 self.block().outset().write(|w| w.set_pin(self._pin() as _, true))
494 }
495
496 #[inline]
498 fn set_low(&self) {
499 self.block().outclr().write(|w| w.set_pin(self._pin() as _, true))
500 }
501}
502
503#[allow(private_bounds)]
505pub trait Pin: PeripheralType + Into<AnyPin> + SealedPin + Sized + 'static {
506 #[inline]
508 fn pin(&self) -> u8 {
509 self._pin()
510 }
511
512 #[inline]
514 fn port(&self) -> Port {
515 match self.pin_port() / 32 {
516 0 => Port::Port0,
517 #[cfg(feature = "_gpio-p1")]
518 1 => Port::Port1,
519 #[cfg(feature = "_gpio-p2")]
520 2 => Port::Port2,
521 _ => unsafe { unreachable_unchecked() },
522 }
523 }
524
525 #[inline]
527 #[cfg(not(feature = "_nrf51"))]
528 fn psel_bits(&self) -> pac::shared::regs::Psel {
529 pac::shared::regs::Psel(self.pin_port() as u32)
530 }
531}
532
533pub struct AnyPin {
535 pub(crate) pin_port: u8,
536}
537
538impl AnyPin {
539 #[inline]
544 pub unsafe fn steal(pin_port: u8) -> Peri<'static, Self> {
545 Peri::new_unchecked(Self { pin_port })
546 }
547}
548
549impl_peripheral!(AnyPin);
550impl Pin for AnyPin {}
551impl SealedPin for AnyPin {
552 #[inline]
553 fn pin_port(&self) -> u8 {
554 self.pin_port
555 }
556}
557
558#[cfg(not(feature = "_nrf51"))]
561#[cfg_attr(feature = "_nrf54l", allow(unused))] pub(crate) trait PselBits {
563 fn psel_bits(&self) -> pac::shared::regs::Psel;
564}
565
566#[cfg(not(feature = "_nrf51"))]
567impl<'a, P: Pin> PselBits for Option<Peri<'a, P>> {
568 #[inline]
569 fn psel_bits(&self) -> pac::shared::regs::Psel {
570 match self {
571 Some(pin) => pin.psel_bits(),
572 None => DISCONNECTED,
573 }
574 }
575}
576
577#[cfg(not(feature = "_nrf51"))]
578#[cfg_attr(feature = "_nrf54l", allow(unused))] pub(crate) const DISCONNECTED: Psel = Psel(1 << 31);
580
581#[cfg(not(feature = "_nrf51"))]
582#[allow(dead_code)]
583pub(crate) fn deconfigure_pin(psel: Psel) {
584 if psel.connect() == Connect::DISCONNECTED {
585 return;
586 }
587 unsafe { AnyPin::steal(psel.0 as _) }.conf().write(|w| {
588 w.set_input(vals::Input::DISCONNECT);
589 })
590}
591
592macro_rules! impl_pin {
595 ($type:ident, $port_num:expr, $pin_num:expr) => {
596 impl crate::gpio::Pin for peripherals::$type {}
597 impl crate::gpio::SealedPin for peripherals::$type {
598 #[inline]
599 fn pin_port(&self) -> u8 {
600 $port_num * 32 + $pin_num
601 }
602 }
603
604 impl From<peripherals::$type> for crate::gpio::AnyPin {
605 fn from(_val: peripherals::$type) -> Self {
606 Self {
607 pin_port: $port_num * 32 + $pin_num,
608 }
609 }
610 }
611 };
612}
613
614mod eh02 {
617 use super::*;
618
619 impl<'d> embedded_hal_02::digital::v2::InputPin for Input<'d> {
620 type Error = Infallible;
621
622 fn is_high(&self) -> Result<bool, Self::Error> {
623 Ok(self.is_high())
624 }
625
626 fn is_low(&self) -> Result<bool, Self::Error> {
627 Ok(self.is_low())
628 }
629 }
630
631 impl<'d> embedded_hal_02::digital::v2::OutputPin for Output<'d> {
632 type Error = Infallible;
633
634 fn set_high(&mut self) -> Result<(), Self::Error> {
635 self.set_high();
636 Ok(())
637 }
638
639 fn set_low(&mut self) -> Result<(), Self::Error> {
640 self.set_low();
641 Ok(())
642 }
643 }
644
645 impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Output<'d> {
646 fn is_set_high(&self) -> Result<bool, Self::Error> {
647 Ok(self.is_set_high())
648 }
649
650 fn is_set_low(&self) -> Result<bool, Self::Error> {
651 Ok(self.is_set_low())
652 }
653 }
654
655 impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Output<'d> {
656 type Error = Infallible;
657 #[inline]
658 fn toggle(&mut self) -> Result<(), Self::Error> {
659 self.toggle();
660 Ok(())
661 }
662 }
663
664 impl<'d> embedded_hal_02::digital::v2::InputPin for Flex<'d> {
668 type Error = Infallible;
669
670 fn is_high(&self) -> Result<bool, Self::Error> {
671 Ok(self.is_high())
672 }
673
674 fn is_low(&self) -> Result<bool, Self::Error> {
675 Ok(self.is_low())
676 }
677 }
678
679 impl<'d> embedded_hal_02::digital::v2::OutputPin for Flex<'d> {
680 type Error = Infallible;
681
682 fn set_high(&mut self) -> Result<(), Self::Error> {
683 self.set_high();
684 Ok(())
685 }
686
687 fn set_low(&mut self) -> Result<(), Self::Error> {
688 self.set_low();
689 Ok(())
690 }
691 }
692
693 impl<'d> embedded_hal_02::digital::v2::StatefulOutputPin for Flex<'d> {
694 fn is_set_high(&self) -> Result<bool, Self::Error> {
695 Ok(self.is_set_high())
696 }
697
698 fn is_set_low(&self) -> Result<bool, Self::Error> {
699 Ok(self.is_set_low())
700 }
701 }
702
703 impl<'d> embedded_hal_02::digital::v2::ToggleableOutputPin for Flex<'d> {
704 type Error = Infallible;
705 #[inline]
706 fn toggle(&mut self) -> Result<(), Self::Error> {
707 self.toggle();
708 Ok(())
709 }
710 }
711}
712
713impl<'d> embedded_hal_1::digital::ErrorType for Input<'d> {
714 type Error = Infallible;
715}
716
717impl<'d> embedded_hal_1::digital::InputPin for Input<'d> {
718 fn is_high(&mut self) -> Result<bool, Self::Error> {
719 Ok((*self).is_high())
720 }
721
722 fn is_low(&mut self) -> Result<bool, Self::Error> {
723 Ok((*self).is_low())
724 }
725}
726
727impl<'d> embedded_hal_1::digital::ErrorType for Output<'d> {
728 type Error = Infallible;
729}
730
731impl<'d> embedded_hal_1::digital::OutputPin for Output<'d> {
732 fn set_high(&mut self) -> Result<(), Self::Error> {
733 self.set_high();
734 Ok(())
735 }
736
737 fn set_low(&mut self) -> Result<(), Self::Error> {
738 self.set_low();
739 Ok(())
740 }
741}
742
743impl<'d> embedded_hal_1::digital::StatefulOutputPin for Output<'d> {
744 fn is_set_high(&mut self) -> Result<bool, Self::Error> {
745 Ok((*self).is_set_high())
746 }
747
748 fn is_set_low(&mut self) -> Result<bool, Self::Error> {
749 Ok((*self).is_set_low())
750 }
751}
752
753impl<'d> embedded_hal_1::digital::ErrorType for Flex<'d> {
754 type Error = Infallible;
755}
756
757impl<'d> embedded_hal_1::digital::InputPin for Flex<'d> {
761 fn is_high(&mut self) -> Result<bool, Self::Error> {
762 Ok((*self).is_high())
763 }
764
765 fn is_low(&mut self) -> Result<bool, Self::Error> {
766 Ok((*self).is_low())
767 }
768}
769
770impl<'d> embedded_hal_1::digital::OutputPin for Flex<'d> {
771 fn set_high(&mut self) -> Result<(), Self::Error> {
772 self.set_high();
773 Ok(())
774 }
775
776 fn set_low(&mut self) -> Result<(), Self::Error> {
777 self.set_low();
778 Ok(())
779 }
780}
781
782impl<'d> embedded_hal_1::digital::StatefulOutputPin for Flex<'d> {
783 fn is_set_high(&mut self) -> Result<bool, Self::Error> {
784 Ok((*self).is_set_high())
785 }
786
787 fn is_set_low(&mut self) -> Result<bool, Self::Error> {
788 Ok((*self).is_set_low())
789 }
790}