aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2024-03-11 23:36:15 +0000
committerGitHub <[email protected]>2024-03-11 23:36:15 +0000
commit1ef02e538497d1876ec26f8acf0763d6c3263382 (patch)
tree17438d578e959cfd4a8a70eaeb8423c0d0597b00
parentb8be126a47b3ac4774e5fb4941ecdebcd7328e17 (diff)
parentd4869b83fcc160dc9a5338e7e47c18197b9cb7f5 (diff)
Merge pull request #2683 from ExplodingWaffle/ucpd-dbdis
stm32: add disable_ucpdx_dead_battery
-rw-r--r--embassy-stm32/src/lib.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index ff77399b2..b548a0343 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -189,6 +189,18 @@ pub struct Config {
189 /// Defaults to P0 (highest). 189 /// Defaults to P0 (highest).
190 #[cfg(gpdma)] 190 #[cfg(gpdma)]
191 pub gpdma_interrupt_priority: Priority, 191 pub gpdma_interrupt_priority: Priority,
192
193 /// Enables UCPD1 dead battery functionality.
194 ///
195 /// Defaults to false (disabled).
196 #[cfg(peri_ucpd1)]
197 pub enable_ucpd1_dead_battery: bool,
198
199 /// Enables UCPD2 dead battery functionality.
200 ///
201 /// Defaults to false (disabled).
202 #[cfg(peri_ucpd2)]
203 pub enable_ucpd2_dead_battery: bool,
192} 204}
193 205
194impl Default for Config { 206impl Default for Config {
@@ -203,6 +215,10 @@ impl Default for Config {
203 dma_interrupt_priority: Priority::P0, 215 dma_interrupt_priority: Priority::P0,
204 #[cfg(gpdma)] 216 #[cfg(gpdma)]
205 gpdma_interrupt_priority: Priority::P0, 217 gpdma_interrupt_priority: Priority::P0,
218 #[cfg(peri_ucpd1)]
219 enable_ucpd1_dead_battery: false,
220 #[cfg(peri_ucpd2)]
221 enable_ucpd2_dead_battery: false,
206 } 222 }
207 } 223 }
208} 224}
@@ -254,7 +270,27 @@ pub fn init(config: Config) -> Peripherals {
254 #[cfg(not(any(stm32f2, stm32f4, stm32f7, stm32l0, stm32h5, stm32h7)))] 270 #[cfg(not(any(stm32f2, stm32f4, stm32f7, stm32l0, stm32h5, stm32h7)))]
255 peripherals::FLASH::enable_and_reset_with_cs(cs); 271 peripherals::FLASH::enable_and_reset_with_cs(cs);
256 272
273 // dead battery functionality is still present on these
274 // chips despite them not having UCPD- disable it
275 #[cfg(any(stm32g070, stm32g0b0))]
276 {
277 crate::pac::SYSCFG.cfgr1().modify(|w| {
278 w.set_ucpd1_strobe(true);
279 w.set_ucpd2_strobe(true);
280 });
281 }
282
257 unsafe { 283 unsafe {
284 // TODO: refactor into mod ucpd
285 #[cfg(ucpd)]
286 ucpd_init(
287 cs,
288 #[cfg(peri_ucpd1)]
289 config.enable_ucpd1_dead_battery,
290 #[cfg(peri_ucpd2)]
291 config.enable_ucpd2_dead_battery,
292 );
293
258 #[cfg(feature = "_split-pins-enabled")] 294 #[cfg(feature = "_split-pins-enabled")]
259 crate::pac::SYSCFG.pmcr().modify(|pmcr| { 295 crate::pac::SYSCFG.pmcr().modify(|pmcr| {
260 #[cfg(feature = "split-pa0")] 296 #[cfg(feature = "split-pa0")]
@@ -296,3 +332,41 @@ pub fn init(config: Config) -> Peripherals {
296 p 332 p
297 }) 333 })
298} 334}
335
336#[cfg(ucpd)]
337/// Safety: must only be called when all UCPDs are disabled (e.g. at startup)
338unsafe fn ucpd_init(
339 _cs: critical_section::CriticalSection,
340 #[cfg(peri_ucpd1)] ucpd1_db_enable: bool,
341 #[cfg(peri_ucpd2)] ucpd2_db_enable: bool,
342) {
343 #[cfg(stm32g0x1)]
344 {
345 // according to RM0444 (STM32G0x1) section 8.1.1:
346 // when UCPD is disabled setting the strobe will disable dead battery
347 // (which is enabled after reset) but if UCPD is enabled, setting the
348 // strobe will apply the CC pin configuration from the control register
349 // (which is why we need to be careful about when we call this)
350 crate::pac::SYSCFG.cfgr1().modify(|w| {
351 w.set_ucpd1_strobe(ucpd1_db_enable);
352 w.set_ucpd2_strobe(ucpd2_db_enable);
353 });
354 }
355
356 #[cfg(any(stm32g4, stm32l5))]
357 {
358 crate::pac::PWR.cr3().modify(|w| {
359 #[cfg(stm32g4)]
360 w.set_ucpd1_dbdis(!ucpd1_db_enable);
361 #[cfg(stm32l5)]
362 w.set_ucpd_dbdis(!ucpd1_db_enable);
363 })
364 }
365
366 #[cfg(any(stm32h5, stm32u5))]
367 {
368 crate::pac::PWR.ucpdr().modify(|w| {
369 w.set_ucpd_dbdis(!ucpd1_db_enable);
370 })
371 }
372}