aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32
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
parent7b9116957a439a5e8488aa9d6f47bbb7b8a306a1 (diff)
Use `PinNumber` to accomodate chips with more than 256 pins
Diffstat (limited to 'embassy-stm32')
-rw-r--r--embassy-stm32/src/exti.rs16
-rw-r--r--embassy-stm32/src/gpio.rs37
2 files changed, 29 insertions, 24 deletions
diff --git a/embassy-stm32/src/exti.rs b/embassy-stm32/src/exti.rs
index 376fdccb8..bc4ecd1cc 100644
--- a/embassy-stm32/src/exti.rs
+++ b/embassy-stm32/src/exti.rs
@@ -8,7 +8,7 @@ use core::task::{Context, Poll};
8use embassy_hal_internal::{impl_peripheral, PeripheralType}; 8use embassy_hal_internal::{impl_peripheral, PeripheralType};
9use embassy_sync::waitqueue::AtomicWaker; 9use embassy_sync::waitqueue::AtomicWaker;
10 10
11use crate::gpio::{AnyPin, Input, Level, Pin as GpioPin, Pull}; 11use crate::gpio::{AnyPin, Input, Level, Pin as GpioPin, PinNumber, Pull};
12use crate::pac::exti::regs::Lines; 12use crate::pac::exti::regs::Lines;
13use crate::pac::EXTI; 13use crate::pac::EXTI;
14use crate::{interrupt, pac, peripherals, Peri}; 14use crate::{interrupt, pac, peripherals, Peri};
@@ -226,12 +226,12 @@ impl<'d> embedded_hal_async::digital::Wait for ExtiInput<'d> {
226 226
227#[must_use = "futures do nothing unless you `.await` or poll them"] 227#[must_use = "futures do nothing unless you `.await` or poll them"]
228struct ExtiInputFuture<'a> { 228struct ExtiInputFuture<'a> {
229 pin: u8, 229 pin: PinNumber,
230 phantom: PhantomData<&'a mut AnyPin>, 230 phantom: PhantomData<&'a mut AnyPin>,
231} 231}
232 232
233impl<'a> ExtiInputFuture<'a> { 233impl<'a> ExtiInputFuture<'a> {
234 fn new(pin: u8, port: u8, rising: bool, falling: bool) -> Self { 234 fn new(pin: PinNumber, port: PinNumber, rising: bool, falling: bool) -> Self {
235 critical_section::with(|_| { 235 critical_section::with(|_| {
236 let pin = pin as usize; 236 let pin = pin as usize;
237 exticr_regs().exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port)); 237 exticr_regs().exticr(pin / 4).modify(|w| w.set_exti(pin % 4, port));
@@ -334,20 +334,20 @@ trait SealedChannel {}
334#[allow(private_bounds)] 334#[allow(private_bounds)]
335pub trait Channel: PeripheralType + SealedChannel + Sized { 335pub trait Channel: PeripheralType + SealedChannel + Sized {
336 /// Get the EXTI channel number. 336 /// Get the EXTI channel number.
337 fn number(&self) -> u8; 337 fn number(&self) -> PinNumber;
338} 338}
339 339
340/// Type-erased EXTI channel. 340/// Type-erased EXTI channel.
341/// 341///
342/// This represents ownership over any EXTI channel, known at runtime. 342/// This represents ownership over any EXTI channel, known at runtime.
343pub struct AnyChannel { 343pub struct AnyChannel {
344 number: u8, 344 number: PinNumber,
345} 345}
346 346
347impl_peripheral!(AnyChannel); 347impl_peripheral!(AnyChannel);
348impl SealedChannel for AnyChannel {} 348impl SealedChannel for AnyChannel {}
349impl Channel for AnyChannel { 349impl Channel for AnyChannel {
350 fn number(&self) -> u8 { 350 fn number(&self) -> PinNumber {
351 self.number 351 self.number
352 } 352 }
353} 353}
@@ -356,7 +356,7 @@ macro_rules! impl_exti {
356 ($type:ident, $number:expr) => { 356 ($type:ident, $number:expr) => {
357 impl SealedChannel for peripherals::$type {} 357 impl SealedChannel for peripherals::$type {}
358 impl Channel for peripherals::$type { 358 impl Channel for peripherals::$type {
359 fn number(&self) -> u8 { 359 fn number(&self) -> PinNumber {
360 $number 360 $number
361 } 361 }
362 } 362 }
@@ -364,7 +364,7 @@ macro_rules! impl_exti {
364 impl From<peripherals::$type> for AnyChannel { 364 impl From<peripherals::$type> for AnyChannel {
365 fn from(val: peripherals::$type) -> Self { 365 fn from(val: peripherals::$type) -> Self {
366 Self { 366 Self {
367 number: val.number() as u8, 367 number: val.number() as PinNumber,
368 } 368 }
369 } 369 }
370 } 370 }
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 }