diff options
| author | xoviat <[email protected]> | 2025-12-13 23:19:46 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-13 23:19:46 +0000 |
| commit | 574868282518ceb81bddcb03bee38fc5b6208a5a (patch) | |
| tree | 2c9ba989a39bbca1d8a452419319648f424bd2e0 | |
| parent | d2740f8fad566f30bed24df970f1297209005126 (diff) | |
| parent | edc58c3f1122196313dada19f0068bb948775f12 (diff) | |
Merge pull request #5059 from liebman/stm32-init-gpio-analog
stm32: set pins to ANALOG on init
| -rw-r--r-- | embassy-stm32/build.rs | 54 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/adc.rs | 18 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/blinky.rs | 22 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/button_exti.rs | 18 | ||||
| -rw-r--r-- | examples/stm32wle5/src/bin/i2c.rs | 20 |
5 files changed, 57 insertions, 75 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index 46d6290e7..a3b863340 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs | |||
| @@ -950,6 +950,60 @@ fn main() { | |||
| 950 | } | 950 | } |
| 951 | } | 951 | } |
| 952 | 952 | ||
| 953 | if kind == "gpio" { | ||
| 954 | for p in METADATA.peripherals { | ||
| 955 | // set all GPIOs to analog mode except for PA13 and PA14 which are SWDIO and SWDCLK | ||
| 956 | if p.registers.is_some() | ||
| 957 | && p.registers.as_ref().unwrap().kind == "gpio" | ||
| 958 | && p.registers.as_ref().unwrap().version != "v1" | ||
| 959 | { | ||
| 960 | let port = format_ident!("{}", p.name); | ||
| 961 | if p.name == "GPIOA" { | ||
| 962 | gg.extend(quote! { | ||
| 963 | // leave PA13 and PA14 as unchanged | ||
| 964 | crate::pac::#port.moder().modify(|w| { | ||
| 965 | w.set_moder(0, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 966 | w.set_moder(1, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 967 | w.set_moder(2, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 968 | w.set_moder(3, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 969 | w.set_moder(4, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 970 | w.set_moder(5, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 971 | w.set_moder(6, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 972 | w.set_moder(7, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 973 | w.set_moder(8, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 974 | w.set_moder(9, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 975 | w.set_moder(10, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 976 | w.set_moder(11, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 977 | w.set_moder(12, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 978 | w.set_moder(15, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 979 | }); | ||
| 980 | }); | ||
| 981 | } else { | ||
| 982 | gg.extend(quote! { | ||
| 983 | crate::pac::#port.moder().modify(|w| { | ||
| 984 | w.set_moder(0, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 985 | w.set_moder(1, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 986 | w.set_moder(2, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 987 | w.set_moder(3, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 988 | w.set_moder(4, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 989 | w.set_moder(5, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 990 | w.set_moder(6, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 991 | w.set_moder(7, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 992 | w.set_moder(8, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 993 | w.set_moder(9, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 994 | w.set_moder(10, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 995 | w.set_moder(11, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 996 | w.set_moder(12, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 997 | w.set_moder(13, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 998 | w.set_moder(14, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 999 | w.set_moder(15, crate::pac::gpio::vals::Moder::ANALOG); | ||
| 1000 | }); | ||
| 1001 | }); | ||
| 1002 | } | ||
| 1003 | } | ||
| 1004 | } | ||
| 1005 | } | ||
| 1006 | |||
| 953 | let fname = format_ident!("init_{}", kind); | 1007 | let fname = format_ident!("init_{}", kind); |
| 954 | g.extend(quote! { | 1008 | g.extend(quote! { |
| 955 | pub unsafe fn #fname(){ | 1009 | pub unsafe fn #fname(){ |
diff --git a/examples/stm32wle5/src/bin/adc.rs b/examples/stm32wle5/src/bin/adc.rs index ea91fb063..8cc84ccdf 100644 --- a/examples/stm32wle5/src/bin/adc.rs +++ b/examples/stm32wle5/src/bin/adc.rs | |||
| @@ -34,24 +34,6 @@ async fn async_main(_spawner: Spawner) { | |||
| 34 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | 34 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) |
| 35 | let p = embassy_stm32::init(config); | 35 | let p = embassy_stm32::init(config); |
| 36 | 36 | ||
| 37 | // start with all GPIOs as analog to reduce power consumption | ||
| 38 | for r in [ | ||
| 39 | embassy_stm32::pac::GPIOA, | ||
| 40 | embassy_stm32::pac::GPIOB, | ||
| 41 | embassy_stm32::pac::GPIOC, | ||
| 42 | embassy_stm32::pac::GPIOH, | ||
| 43 | ] { | ||
| 44 | r.moder().modify(|w| { | ||
| 45 | for i in 0..16 { | ||
| 46 | // don't reset these if probe-rs should stay connected! | ||
| 47 | #[cfg(feature = "defmt-rtt")] | ||
| 48 | if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { | ||
| 49 | continue; | ||
| 50 | } | ||
| 51 | w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); | ||
| 52 | } | ||
| 53 | }); | ||
| 54 | } | ||
| 55 | #[cfg(feature = "defmt-serial")] | 37 | #[cfg(feature = "defmt-serial")] |
| 56 | { | 38 | { |
| 57 | use embassy_stm32::mode::Blocking; | 39 | use embassy_stm32::mode::Blocking; |
diff --git a/examples/stm32wle5/src/bin/blinky.rs b/examples/stm32wle5/src/bin/blinky.rs index 9f0c04672..3b7eb7761 100644 --- a/examples/stm32wle5/src/bin/blinky.rs +++ b/examples/stm32wle5/src/bin/blinky.rs | |||
| @@ -32,24 +32,6 @@ async fn async_main(_spawner: Spawner) { | |||
| 32 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | 32 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) |
| 33 | let p = embassy_stm32::init(config); | 33 | let p = embassy_stm32::init(config); |
| 34 | 34 | ||
| 35 | // start with all GPIOs as analog to reduce power consumption | ||
| 36 | for r in [ | ||
| 37 | embassy_stm32::pac::GPIOA, | ||
| 38 | embassy_stm32::pac::GPIOB, | ||
| 39 | embassy_stm32::pac::GPIOC, | ||
| 40 | embassy_stm32::pac::GPIOH, | ||
| 41 | ] { | ||
| 42 | r.moder().modify(|w| { | ||
| 43 | for i in 0..16 { | ||
| 44 | // don't reset these if probe-rs should stay connected! | ||
| 45 | #[cfg(feature = "defmt-rtt")] | ||
| 46 | if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { | ||
| 47 | continue; | ||
| 48 | } | ||
| 49 | w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); | ||
| 50 | } | ||
| 51 | }); | ||
| 52 | } | ||
| 53 | #[cfg(feature = "defmt-serial")] | 35 | #[cfg(feature = "defmt-serial")] |
| 54 | { | 36 | { |
| 55 | use embassy_stm32::mode::Blocking; | 37 | use embassy_stm32::mode::Blocking; |
| @@ -67,10 +49,10 @@ async fn async_main(_spawner: Spawner) { | |||
| 67 | loop { | 49 | loop { |
| 68 | info!("low"); | 50 | info!("low"); |
| 69 | led.set_low(); | 51 | led.set_low(); |
| 70 | Timer::after_millis(500).await; | 52 | Timer::after_millis(15000).await; |
| 71 | 53 | ||
| 72 | info!("high"); | 54 | info!("high"); |
| 73 | led.set_high(); | 55 | led.set_high(); |
| 74 | Timer::after_millis(500).await; | 56 | Timer::after_millis(15000).await; |
| 75 | } | 57 | } |
| 76 | } | 58 | } |
diff --git a/examples/stm32wle5/src/bin/button_exti.rs b/examples/stm32wle5/src/bin/button_exti.rs index f248b6147..9ffc39948 100644 --- a/examples/stm32wle5/src/bin/button_exti.rs +++ b/examples/stm32wle5/src/bin/button_exti.rs | |||
| @@ -39,24 +39,6 @@ async fn async_main(_spawner: Spawner) { | |||
| 39 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | 39 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) |
| 40 | let p = embassy_stm32::init(config); | 40 | let p = embassy_stm32::init(config); |
| 41 | 41 | ||
| 42 | // start with all GPIOs as analog to reduce power consumption | ||
| 43 | for r in [ | ||
| 44 | embassy_stm32::pac::GPIOA, | ||
| 45 | embassy_stm32::pac::GPIOB, | ||
| 46 | embassy_stm32::pac::GPIOC, | ||
| 47 | embassy_stm32::pac::GPIOH, | ||
| 48 | ] { | ||
| 49 | r.moder().modify(|w| { | ||
| 50 | for i in 0..16 { | ||
| 51 | // don't reset these if probe-rs should stay connected! | ||
| 52 | #[cfg(feature = "defmt-rtt")] | ||
| 53 | if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); | ||
| 57 | } | ||
| 58 | }); | ||
| 59 | } | ||
| 60 | #[cfg(feature = "defmt-serial")] | 42 | #[cfg(feature = "defmt-serial")] |
| 61 | { | 43 | { |
| 62 | use embassy_stm32::mode::Blocking; | 44 | use embassy_stm32::mode::Blocking; |
diff --git a/examples/stm32wle5/src/bin/i2c.rs b/examples/stm32wle5/src/bin/i2c.rs index 68c17a672..8e7a6e2d8 100644 --- a/examples/stm32wle5/src/bin/i2c.rs +++ b/examples/stm32wle5/src/bin/i2c.rs | |||
| @@ -40,24 +40,6 @@ async fn async_main(_spawner: Spawner) { | |||
| 40 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) | 40 | // Initialize STM32WL peripherals (use default config like wio-e5-async example) |
| 41 | let p = embassy_stm32::init(config); | 41 | let p = embassy_stm32::init(config); |
| 42 | 42 | ||
| 43 | // start with all GPIOs as analog to reduce power consumption | ||
| 44 | for r in [ | ||
| 45 | embassy_stm32::pac::GPIOA, | ||
| 46 | embassy_stm32::pac::GPIOB, | ||
| 47 | embassy_stm32::pac::GPIOC, | ||
| 48 | embassy_stm32::pac::GPIOH, | ||
| 49 | ] { | ||
| 50 | r.moder().modify(|w| { | ||
| 51 | for i in 0..16 { | ||
| 52 | // don't reset these if probe-rs should stay connected! | ||
| 53 | #[cfg(feature = "defmt-rtt")] | ||
| 54 | if config.enable_debug_during_sleep && r == embassy_stm32::pac::GPIOA && [13, 14].contains(&i) { | ||
| 55 | continue; | ||
| 56 | } | ||
| 57 | w.set_moder(i, embassy_stm32::pac::gpio::vals::Moder::ANALOG); | ||
| 58 | } | ||
| 59 | }); | ||
| 60 | } | ||
| 61 | #[cfg(feature = "defmt-serial")] | 43 | #[cfg(feature = "defmt-serial")] |
| 62 | { | 44 | { |
| 63 | use embassy_stm32::mode::Blocking; | 45 | use embassy_stm32::mode::Blocking; |
| @@ -79,7 +61,7 @@ async fn async_main(_spawner: Spawner) { | |||
| 79 | let mut i2c = I2c::new(p.I2C2, p.PB15, p.PA15, IrqsI2C, p.DMA1_CH6, p.DMA1_CH7, { | 61 | let mut i2c = I2c::new(p.I2C2, p.PB15, p.PA15, IrqsI2C, p.DMA1_CH6, p.DMA1_CH7, { |
| 80 | let mut config = i2c::Config::default(); | 62 | let mut config = i2c::Config::default(); |
| 81 | config.frequency = Hertz::khz(100); | 63 | config.frequency = Hertz::khz(100); |
| 82 | config.timeout = Duration::from_millis(500); | 64 | config.timeout = Duration::from_millis(1000); |
| 83 | config | 65 | config |
| 84 | }); | 66 | }); |
| 85 | 67 | ||
