aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/gpio.rs
diff options
context:
space:
mode:
authoreverdrone <[email protected]>2025-09-21 17:23:39 +0200
committereverdrone <[email protected]>2025-09-21 17:23:39 +0200
commitea4e7bc3d23c3deb44fa6029f70ddcd72dfa4d35 (patch)
tree1b60c366ea73a575b6c8500d3495fdeaa6eab9c7 /embassy-stm32/src/gpio.rs
parent7b9116957a439a5e8488aa9d6f47bbb7b8a306a1 (diff)
Use `PinNumber` to accomodate chips with more than 256 pins
Diffstat (limited to 'embassy-stm32/src/gpio.rs')
-rw-r--r--embassy-stm32/src/gpio.rs37
1 files changed, 21 insertions, 16 deletions
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs
index 5a8d23183..ba5cf24c6 100644
--- a/embassy-stm32/src/gpio.rs
+++ b/embassy-stm32/src/gpio.rs
@@ -592,7 +592,7 @@ impl AfType {
592 592
593#[inline(never)] 593#[inline(never)]
594#[cfg(gpio_v1)] 594#[cfg(gpio_v1)]
595fn set_as_af(pin_port: u8, af_type: AfType) { 595fn set_as_af(pin_port: PinNumber, af_type: AfType) {
596 let pin = unsafe { AnyPin::steal(pin_port) }; 596 let pin = unsafe { AnyPin::steal(pin_port) };
597 let r = pin.block(); 597 let r = pin.block();
598 let n = pin._pin() as usize; 598 let n = pin._pin() as usize;
@@ -649,12 +649,12 @@ impl AfType {
649 649
650#[inline(never)] 650#[inline(never)]
651#[cfg(gpio_v2)] 651#[cfg(gpio_v2)]
652fn set_as_af(pin_port: u8, af_num: u8, af_type: AfType) { 652fn set_as_af(pin_port: PinNumber, af_num: u8, af_type: AfType) {
653 let pin = unsafe { AnyPin::steal(pin_port) }; 653 let pin = unsafe { AnyPin::steal(pin_port) };
654 let r = pin.block(); 654 let r = pin.block();
655 let n = pin._pin() as usize; 655 let n = pin._pin() as usize;
656 656
657 r.afr(n / 8).modify(|w| w.set_afr(n % 8, af_num)); 657 r.afr(n / 8).modify(|w| w.set_afr(n % 8, af_num as u8));
658 r.pupdr().modify(|w| w.set_pupdr(n, af_type.pupdr)); 658 r.pupdr().modify(|w| w.set_pupdr(n, af_type.pupdr));
659 r.otyper().modify(|w| w.set_ot(n, af_type.ot)); 659 r.otyper().modify(|w| w.set_ot(n, af_type.ot));
660 r.ospeedr().modify(|w| w.set_ospeedr(n, af_type.ospeedr)); 660 r.ospeedr().modify(|w| w.set_ospeedr(n, af_type.ospeedr));
@@ -663,7 +663,7 @@ fn set_as_af(pin_port: u8, af_num: u8, af_type: AfType) {
663 663
664#[inline(never)] 664#[inline(never)]
665#[cfg(gpio_v2)] 665#[cfg(gpio_v2)]
666fn set_speed(pin_port: u8, speed: Speed) { 666fn set_speed(pin_port: PinNumber, speed: Speed) {
667 let pin = unsafe { AnyPin::steal(pin_port) }; 667 let pin = unsafe { AnyPin::steal(pin_port) };
668 let r = pin.block(); 668 let r = pin.block();
669 let n = pin._pin() as usize; 669 let n = pin._pin() as usize;
@@ -672,7 +672,7 @@ fn set_speed(pin_port: u8, speed: Speed) {
672} 672}
673 673
674#[inline(never)] 674#[inline(never)]
675fn set_as_analog(pin_port: u8) { 675fn set_as_analog(pin_port: PinNumber) {
676 let pin = unsafe { AnyPin::steal(pin_port) }; 676 let pin = unsafe { AnyPin::steal(pin_port) };
677 let r = pin.block(); 677 let r = pin.block();
678 let n = pin._pin() as usize; 678 let n = pin._pin() as usize;
@@ -688,7 +688,7 @@ fn set_as_analog(pin_port: u8) {
688} 688}
689 689
690#[inline(never)] 690#[inline(never)]
691fn get_pull(pin_port: u8) -> Pull { 691fn get_pull(pin_port: PinNumber) -> Pull {
692 let pin = unsafe { AnyPin::steal(pin_port) }; 692 let pin = unsafe { AnyPin::steal(pin_port) };
693 let r = pin.block(); 693 let r = pin.block();
694 let n = pin._pin() as usize; 694 let n = pin._pin() as usize;
@@ -727,15 +727,15 @@ pub struct AfioRemapBool<const V: bool>;
727pub struct AfioRemapNotApplicable; 727pub struct AfioRemapNotApplicable;
728 728
729pub(crate) trait SealedPin { 729pub(crate) trait SealedPin {
730 fn pin_port(&self) -> u8; 730 fn pin_port(&self) -> PinNumber;
731 731
732 #[inline] 732 #[inline]
733 fn _pin(&self) -> u8 { 733 fn _pin(&self) -> PinNumber {
734 self.pin_port() % 16 734 self.pin_port() % 16
735 } 735 }
736 736
737 #[inline] 737 #[inline]
738 fn _port(&self) -> u8 { 738 fn _port(&self) -> PinNumber {
739 self.pin_port() / 16 739 self.pin_port() / 16
740 } 740 }
741 741
@@ -798,6 +798,11 @@ pub(crate) trait SealedPin {
798 } 798 }
799} 799}
800 800
801#[cfg(not(stm32n6))]
802pub type PinNumber = u8;
803#[cfg(stm32n6)]
804pub type PinNumber = u16;
805
801/// GPIO pin trait. 806/// GPIO pin trait.
802#[allow(private_bounds)] 807#[allow(private_bounds)]
803pub trait Pin: PeripheralType + Into<AnyPin> + SealedPin + Sized + 'static { 808pub trait Pin: PeripheralType + Into<AnyPin> + SealedPin + Sized + 'static {
@@ -809,20 +814,20 @@ pub trait Pin: PeripheralType + Into<AnyPin> + SealedPin + Sized + 'static {
809 814
810 /// Number of the pin within the port (0..31) 815 /// Number of the pin within the port (0..31)
811 #[inline] 816 #[inline]
812 fn pin(&self) -> u8 { 817 fn pin(&self) -> PinNumber {
813 self._pin() 818 self._pin()
814 } 819 }
815 820
816 /// Port of the pin 821 /// Port of the pin
817 #[inline] 822 #[inline]
818 fn port(&self) -> u8 { 823 fn port(&self) -> PinNumber {
819 self._port() 824 self._port()
820 } 825 }
821} 826}
822 827
823/// Type-erased GPIO pin 828/// Type-erased GPIO pin
824pub struct AnyPin { 829pub struct AnyPin {
825 pin_port: u8, 830 pin_port: PinNumber,
826} 831}
827 832
828impl AnyPin { 833impl AnyPin {
@@ -830,12 +835,12 @@ impl AnyPin {
830 /// 835 ///
831 /// `pin_port` is `port_num * 16 + pin_num`, where `port_num` is 0 for port `A`, 1 for port `B`, etc... 836 /// `pin_port` is `port_num * 16 + pin_num`, where `port_num` is 0 for port `A`, 1 for port `B`, etc...
832 #[inline] 837 #[inline]
833 pub unsafe fn steal(pin_port: u8) -> Peri<'static, Self> { 838 pub unsafe fn steal(pin_port: PinNumber) -> Peri<'static, Self> {
834 Peri::new_unchecked(Self { pin_port }) 839 Peri::new_unchecked(Self { pin_port })
835 } 840 }
836 841
837 #[inline] 842 #[inline]
838 fn _port(&self) -> u8 { 843 fn _port(&self) -> PinNumber {
839 self.pin_port / 16 844 self.pin_port / 16
840 } 845 }
841 846
@@ -854,7 +859,7 @@ impl Pin for AnyPin {
854} 859}
855impl SealedPin for AnyPin { 860impl SealedPin for AnyPin {
856 #[inline] 861 #[inline]
857 fn pin_port(&self) -> u8 { 862 fn pin_port(&self) -> PinNumber {
858 self.pin_port 863 self.pin_port
859 } 864 }
860} 865}
@@ -869,7 +874,7 @@ foreach_pin!(
869 } 874 }
870 impl SealedPin for peripherals::$pin_name { 875 impl SealedPin for peripherals::$pin_name {
871 #[inline] 876 #[inline]
872 fn pin_port(&self) -> u8 { 877 fn pin_port(&self) -> PinNumber {
873 $port_num * 16 + $pin_num 878 $port_num * 16 + $pin_num
874 } 879 }
875 } 880 }