diff options
| author | Harry Brooke <[email protected]> | 2024-03-11 21:45:41 +0000 |
|---|---|---|
| committer | Harry Brooke <[email protected]> | 2024-03-11 23:03:09 +0000 |
| commit | d4869b83fcc160dc9a5338e7e47c18197b9cb7f5 (patch) | |
| tree | 8b31c22bbde4d636f1759ce71ae0346802961efb | |
| parent | 096d147dce104cfb78480a8e9a6df2b067ccacf5 (diff) | |
disable -> enable. also extracted to function for easy refactoring later
| -rw-r--r-- | embassy-stm32/src/lib.rs | 100 |
1 files changed, 64 insertions, 36 deletions
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 948cde677..b548a0343 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs | |||
| @@ -190,17 +190,17 @@ pub struct Config { | |||
| 190 | #[cfg(gpdma)] | 190 | #[cfg(gpdma)] |
| 191 | pub gpdma_interrupt_priority: Priority, | 191 | pub gpdma_interrupt_priority: Priority, |
| 192 | 192 | ||
| 193 | /// Disables UCPD1 dead battery functionality. | 193 | /// Enables UCPD1 dead battery functionality. |
| 194 | /// | 194 | /// |
| 195 | /// Defaults to true (disabled). | 195 | /// Defaults to false (disabled). |
| 196 | #[cfg(ucpd)] | 196 | #[cfg(peri_ucpd1)] |
| 197 | pub disable_ucpd1_dead_battery: bool, | 197 | pub enable_ucpd1_dead_battery: bool, |
| 198 | 198 | ||
| 199 | /// Disables UCPD2 dead battery functionality. | 199 | /// Enables UCPD2 dead battery functionality. |
| 200 | /// | 200 | /// |
| 201 | /// Defaults to true (disabled). | 201 | /// Defaults to false (disabled). |
| 202 | #[cfg(all(ucpd, stm32g0x1))] | 202 | #[cfg(peri_ucpd2)] |
| 203 | pub disable_ucpd2_dead_battery: bool, | 203 | pub enable_ucpd2_dead_battery: bool, |
| 204 | } | 204 | } |
| 205 | 205 | ||
| 206 | impl Default for Config { | 206 | impl Default for Config { |
| @@ -215,10 +215,10 @@ impl Default for Config { | |||
| 215 | dma_interrupt_priority: Priority::P0, | 215 | dma_interrupt_priority: Priority::P0, |
| 216 | #[cfg(gpdma)] | 216 | #[cfg(gpdma)] |
| 217 | gpdma_interrupt_priority: Priority::P0, | 217 | gpdma_interrupt_priority: Priority::P0, |
| 218 | #[cfg(ucpd)] | 218 | #[cfg(peri_ucpd1)] |
| 219 | disable_ucpd1_dead_battery: true, | 219 | enable_ucpd1_dead_battery: false, |
| 220 | #[cfg(all(ucpd, stm32g0x1))] | 220 | #[cfg(peri_ucpd2)] |
| 221 | uisable_ucpd2_dead_battery: true, | 221 | enable_ucpd2_dead_battery: false, |
| 222 | } | 222 | } |
| 223 | } | 223 | } |
| 224 | } | 224 | } |
| @@ -270,37 +270,27 @@ pub fn init(config: Config) -> Peripherals { | |||
| 270 | #[cfg(not(any(stm32f2, stm32f4, stm32f7, stm32l0, stm32h5, stm32h7)))] | 270 | #[cfg(not(any(stm32f2, stm32f4, stm32f7, stm32l0, stm32h5, stm32h7)))] |
| 271 | peripherals::FLASH::enable_and_reset_with_cs(cs); | 271 | peripherals::FLASH::enable_and_reset_with_cs(cs); |
| 272 | 272 | ||
| 273 | // dead battery IOs are still present on g0x0 despite not having UCPD | 273 | // dead battery functionality is still present on these |
| 274 | // chips despite them not having UCPD- disable it | ||
| 274 | #[cfg(any(stm32g070, stm32g0b0))] | 275 | #[cfg(any(stm32g070, stm32g0b0))] |
| 275 | let (disable_ucpd1_dead_battery, disable_ucpd2_dead_battery) = (true, true); | ||
| 276 | #[cfg(ucpd)] | ||
| 277 | let disable_ucpd1_dead_battery = config.disable_ucpd1_dead_battery; | ||
| 278 | #[cfg(all(ucpd, stm32g0x1))] | ||
| 279 | let disable_ucpd2_dead_battery = config.disable_ucpd2_dead_battery; | ||
| 280 | |||
| 281 | #[cfg(any(stm32g070, stm32g0b0, all(ucpd, stm32g0x1)))] | ||
| 282 | { | 276 | { |
| 283 | crate::pac::SYSCFG.cfgr1().modify(|w| { | 277 | crate::pac::SYSCFG.cfgr1().modify(|w| { |
| 284 | w.set_ucpd1_strobe(disable_ucpd1_dead_battery); | 278 | w.set_ucpd1_strobe(true); |
| 285 | w.set_ucpd2_strobe(disable_ucpd2_dead_battery); | 279 | w.set_ucpd2_strobe(true); |
| 286 | }); | 280 | }); |
| 287 | } | 281 | } |
| 288 | 282 | ||
| 289 | #[cfg(all(ucpd, any(stm32g4, stm32l5)))] | ||
| 290 | { | ||
| 291 | crate::pac::PWR | ||
| 292 | .cr3() | ||
| 293 | .modify(|w| w.set_ucpd1_dbdis(disable_ucpd1_dead_battery)) | ||
| 294 | } | ||
| 295 | |||
| 296 | #[cfg(all(ucpd, any(stm32h5, stm32u5)))] | ||
| 297 | { | ||
| 298 | crate::pac::PWR | ||
| 299 | .ucpdr() | ||
| 300 | .modify(|w| w.set_ucpd1_dbdis(disable_ucpd1_dead_battery)) | ||
| 301 | } | ||
| 302 | |||
| 303 | 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 | |||
| 304 | #[cfg(feature = "_split-pins-enabled")] | 294 | #[cfg(feature = "_split-pins-enabled")] |
| 305 | crate::pac::SYSCFG.pmcr().modify(|pmcr| { | 295 | crate::pac::SYSCFG.pmcr().modify(|pmcr| { |
| 306 | #[cfg(feature = "split-pa0")] | 296 | #[cfg(feature = "split-pa0")] |
| @@ -342,3 +332,41 @@ pub fn init(config: Config) -> Peripherals { | |||
| 342 | p | 332 | p |
| 343 | }) | 333 | }) |
| 344 | } | 334 | } |
| 335 | |||
| 336 | #[cfg(ucpd)] | ||
| 337 | /// Safety: must only be called when all UCPDs are disabled (e.g. at startup) | ||
| 338 | unsafe 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 | } | ||
