diff options
| author | xoviat <[email protected]> | 2023-08-27 15:01:09 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-08-27 15:01:09 -0500 |
| commit | 531f51d0eb66af9b681b49c10e28ef25e88bbcc2 (patch) | |
| tree | e5f2577abde3bde48d04943b754ae810884a3203 | |
| parent | f28ab18d7bc05ade2130979d67b0af6ad4085d76 (diff) | |
rcc/bd: consolidate mod
| -rw-r--r-- | embassy-stm32/Cargo.toml | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/rcc/bd.rs | 107 | ||||
| -rw-r--r-- | embassy-stm32/src/rcc/wl.rs | 3 |
3 files changed, 40 insertions, 74 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 150014afe..d0ada97a3 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml | |||
| @@ -58,7 +58,7 @@ sdio-host = "0.5.0" | |||
| 58 | embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } | 58 | embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } |
| 59 | critical-section = "1.1" | 59 | critical-section = "1.1" |
| 60 | atomic-polyfill = "1.0.1" | 60 | atomic-polyfill = "1.0.1" |
| 61 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-2b87e34c661e19ff6dc603fabfe7fe99ab7261f7" } | 61 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-9a61a1f090462df8bd1751f89951f04934fdceb3" } |
| 62 | vcell = "0.1.3" | 62 | vcell = "0.1.3" |
| 63 | bxcan = "0.7.0" | 63 | bxcan = "0.7.0" |
| 64 | nb = "1.0.0" | 64 | nb = "1.0.0" |
| @@ -77,7 +77,7 @@ critical-section = { version = "1.1", features = ["std"] } | |||
| 77 | [build-dependencies] | 77 | [build-dependencies] |
| 78 | proc-macro2 = "1.0.36" | 78 | proc-macro2 = "1.0.36" |
| 79 | quote = "1.0.15" | 79 | quote = "1.0.15" |
| 80 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-2b87e34c661e19ff6dc603fabfe7fe99ab7261f7", default-features = false, features = ["metadata"]} | 80 | stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-9a61a1f090462df8bd1751f89951f04934fdceb3", default-features = false, features = ["metadata"]} |
| 81 | 81 | ||
| 82 | [features] | 82 | [features] |
| 83 | default = ["rt"] | 83 | default = ["rt"] |
diff --git a/embassy-stm32/src/rcc/bd.rs b/embassy-stm32/src/rcc/bd.rs index fd1b8d451..d56bc0330 100644 --- a/embassy-stm32/src/rcc/bd.rs +++ b/embassy-stm32/src/rcc/bd.rs | |||
| @@ -12,86 +12,67 @@ pub enum RtcClockSource { | |||
| 12 | HSE = 0b11, | 12 | HSE = 0b11, |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] | ||
| 16 | type Bdcr = crate::pac::rcc::regs::Bdcr; | ||
| 17 | |||
| 18 | #[cfg(any(rtc_v2l0, rtc_v2l1))] | ||
| 19 | type Bdcr = crate::pac::rcc::regs::Csr; | ||
| 20 | |||
| 15 | #[allow(dead_code)] | 21 | #[allow(dead_code)] |
| 16 | pub struct BackupDomain {} | 22 | pub struct BackupDomain {} |
| 17 | 23 | ||
| 18 | impl BackupDomain { | 24 | impl BackupDomain { |
| 19 | #[cfg(any( | 25 | #[cfg(any( |
| 20 | rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb | 26 | rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3, |
| 27 | rtc_v3u5 | ||
| 21 | ))] | 28 | ))] |
| 22 | #[allow(dead_code)] | 29 | #[allow(dead_code)] |
| 23 | fn unlock_registers() { | 30 | fn modify<R>(f: impl FnOnce(&mut Bdcr) -> R) -> R { |
| 24 | #[cfg(any(rtc_v2f2, rtc_v2f3, rtc_v2l1))] | 31 | #[cfg(any(rtc_v2f2, rtc_v2f3, rtc_v2l1))] |
| 25 | let cr = crate::pac::PWR.cr(); | 32 | let cr = crate::pac::PWR.cr(); |
| 26 | #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l4, rtc_v2wb))] | 33 | #[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l4, rtc_v2wb, rtc_v3, rtc_v3u5))] |
| 27 | let cr = crate::pac::PWR.cr1(); | 34 | let cr = crate::pac::PWR.cr1(); |
| 28 | 35 | ||
| 29 | // TODO: Missing from PAC for l0 and f0? | 36 | // TODO: Missing from PAC for l0 and f0? |
| 30 | #[cfg(not(any(rtc_v2f0, rtc_v2l0)))] | 37 | #[cfg(not(any(rtc_v2f0, rtc_v2l0, rtc_v3u5)))] |
| 31 | { | 38 | { |
| 32 | if !cr.read().dbp() { | 39 | cr.modify(|w| w.set_dbp(true)); |
| 33 | cr.modify(|w| w.set_dbp(true)); | 40 | while !cr.read().dbp() {} |
| 34 | while !cr.read().dbp() {} | ||
| 35 | } | ||
| 36 | } | 41 | } |
| 37 | } | ||
| 38 | 42 | ||
| 39 | #[cfg(any(rtc_v3, rtc_v3u5))] | 43 | crate::pac::RCC.bdcr().modify(|w| f(w)) |
| 40 | #[allow(dead_code)] | ||
| 41 | fn unlock_registers() { | ||
| 42 | // Unlock the backup domain | ||
| 43 | #[cfg(not(any(rtc_v3u5, rcc_wl5, rcc_wle)))] | ||
| 44 | { | ||
| 45 | if !crate::pac::PWR.cr1().read().dbp() { | ||
| 46 | crate::pac::PWR.cr1().modify(|w| w.set_dbp(true)); | ||
| 47 | while !crate::pac::PWR.cr1().read().dbp() {} | ||
| 48 | } | ||
| 49 | } | ||
| 50 | #[cfg(any(rcc_wl5, rcc_wle))] | ||
| 51 | { | ||
| 52 | use crate::pac::pwr::vals::Dbp; | ||
| 53 | |||
| 54 | if crate::pac::PWR.cr1().read().dbp() != Dbp::ENABLED { | ||
| 55 | crate::pac::PWR.cr1().modify(|w| w.set_dbp(Dbp::ENABLED)); | ||
| 56 | while crate::pac::PWR.cr1().read().dbp() != Dbp::ENABLED {} | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | 44 | } |
| 60 | 45 | ||
| 61 | #[cfg(any( | 46 | #[cfg(any( |
| 62 | rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb | 47 | rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3, |
| 48 | rtc_v3u5 | ||
| 63 | ))] | 49 | ))] |
| 64 | #[allow(dead_code)] | 50 | #[allow(dead_code)] |
| 65 | pub fn set_rtc_clock_source(clock_source: RtcClockSource) { | 51 | fn read() -> Bdcr { |
| 66 | #[cfg(not(rtc_v2wb))] | ||
| 67 | use stm32_metapac::rcc::vals::Rtcsel; | ||
| 68 | |||
| 69 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] | ||
| 70 | let cr = crate::pac::RCC.bdcr(); | ||
| 71 | #[cfg(any(rtc_v2l0, rtc_v2l1))] | 52 | #[cfg(any(rtc_v2l0, rtc_v2l1))] |
| 72 | let cr = crate::pac::RCC.csr(); | 53 | let r = crate::pac::RCC.csr().read(); |
| 73 | 54 | ||
| 74 | Self::unlock_registers(); | 55 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] |
| 56 | let r = crate::pac::RCC.bdcr().read(); | ||
| 75 | 57 | ||
| 76 | cr.modify(|w| { | 58 | r |
| 77 | // Select RTC source | ||
| 78 | #[cfg(not(rtc_v2wb))] | ||
| 79 | w.set_rtcsel(Rtcsel::from_bits(clock_source as u8)); | ||
| 80 | #[cfg(rtc_v2wb)] | ||
| 81 | w.set_rtcsel(clock_source as u8); | ||
| 82 | }); | ||
| 83 | } | 59 | } |
| 84 | 60 | ||
| 85 | #[cfg(any(rtc_v3, rtc_v3u5))] | 61 | #[cfg(any( |
| 62 | rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb, rtc_v3, | ||
| 63 | rtc_v3u5 | ||
| 64 | ))] | ||
| 86 | #[allow(dead_code)] | 65 | #[allow(dead_code)] |
| 87 | pub fn set_rtc_clock_source(clock_source: RtcClockSource) { | 66 | pub fn set_rtc_clock_source(clock_source: RtcClockSource) { |
| 88 | let clock_source = clock_source as u8; | 67 | let clock_source = clock_source as u8; |
| 89 | #[cfg(not(any(rcc_wl5, rcc_wle)))] | 68 | #[cfg(any( |
| 69 | all(not(any(rtc_v3, rtc_v3u5)), not(rtc_v2wb)), | ||
| 70 | all(any(rtc_v3, rtc_v3u5), not(any(rcc_wl5, rcc_wle))) | ||
| 71 | ))] | ||
| 90 | let clock_source = crate::pac::rcc::vals::Rtcsel::from_bits(clock_source); | 72 | let clock_source = crate::pac::rcc::vals::Rtcsel::from_bits(clock_source); |
| 91 | 73 | ||
| 92 | Self::unlock_registers(); | 74 | #[cfg(not(rtc_v2wb))] |
| 93 | 75 | Self::modify(|w| { | |
| 94 | crate::pac::RCC.bdcr().modify(|w| { | ||
| 95 | // Select RTC source | 76 | // Select RTC source |
| 96 | w.set_rtcsel(clock_source); | 77 | w.set_rtcsel(clock_source); |
| 97 | }); | 78 | }); |
| @@ -102,25 +83,16 @@ impl BackupDomain { | |||
| 102 | ))] | 83 | ))] |
| 103 | #[allow(dead_code)] | 84 | #[allow(dead_code)] |
| 104 | pub fn enable_rtc() { | 85 | pub fn enable_rtc() { |
| 105 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] | 86 | let reg = Self::read(); |
| 106 | let reg = crate::pac::RCC.bdcr().read(); | ||
| 107 | #[cfg(any(rtc_v2l0, rtc_v2l1))] | ||
| 108 | let reg = crate::pac::RCC.csr().read(); | ||
| 109 | 87 | ||
| 110 | #[cfg(any(rtc_v2h7, rtc_v2l4, rtc_v2wb))] | 88 | #[cfg(any(rtc_v2h7, rtc_v2l4, rtc_v2wb))] |
| 111 | assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet."); | 89 | assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet."); |
| 112 | 90 | ||
| 113 | if !reg.rtcen() { | 91 | if !reg.rtcen() { |
| 114 | Self::unlock_registers(); | ||
| 115 | |||
| 116 | #[cfg(not(any(rtc_v2l0, rtc_v2l1, rtc_v2f2)))] | 92 | #[cfg(not(any(rtc_v2l0, rtc_v2l1, rtc_v2f2)))] |
| 117 | crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true)); | 93 | Self::modify(|w| w.set_bdrst(true)); |
| 118 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] | ||
| 119 | let cr = crate::pac::RCC.bdcr(); | ||
| 120 | #[cfg(any(rtc_v2l0, rtc_v2l1))] | ||
| 121 | let cr = crate::pac::RCC.csr(); | ||
| 122 | 94 | ||
| 123 | cr.modify(|w| { | 95 | Self::modify(|w| { |
| 124 | // Reset | 96 | // Reset |
| 125 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] | 97 | #[cfg(not(any(rtc_v2l0, rtc_v2l1)))] |
| 126 | w.set_bdrst(false); | 98 | w.set_bdrst(false); |
| @@ -146,18 +118,13 @@ impl BackupDomain { | |||
| 146 | #[cfg(any(rtc_v3, rtc_v3u5))] | 118 | #[cfg(any(rtc_v3, rtc_v3u5))] |
| 147 | #[allow(dead_code)] | 119 | #[allow(dead_code)] |
| 148 | pub fn enable_rtc() { | 120 | pub fn enable_rtc() { |
| 149 | let bdcr = crate::pac::RCC.bdcr(); | 121 | let reg = Self::read(); |
| 150 | |||
| 151 | let reg = bdcr.read(); | ||
| 152 | assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet."); | 122 | assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet."); |
| 153 | 123 | ||
| 154 | if !reg.rtcen() { | 124 | if !reg.rtcen() { |
| 155 | Self::unlock_registers(); | 125 | Self::modify(|w| w.set_bdrst(true)); |
| 156 | |||
| 157 | bdcr.modify(|w| w.set_bdrst(true)); | ||
| 158 | 126 | ||
| 159 | bdcr.modify(|w| { | 127 | Self::modify(|w| { |
| 160 | // Reset | ||
| 161 | w.set_bdrst(false); | 128 | w.set_bdrst(false); |
| 162 | 129 | ||
| 163 | w.set_rtcen(true); | 130 | w.set_rtcen(true); |
diff --git a/embassy-stm32/src/rcc/wl.rs b/embassy-stm32/src/rcc/wl.rs index 5b1909659..e33690d10 100644 --- a/embassy-stm32/src/rcc/wl.rs +++ b/embassy-stm32/src/rcc/wl.rs | |||
| @@ -1,5 +1,4 @@ | |||
| 1 | pub use super::bus::{AHBPrescaler, APBPrescaler, VoltageScale}; | 1 | pub use super::bus::{AHBPrescaler, APBPrescaler, VoltageScale}; |
| 2 | use crate::pac::pwr::vals::Dbp; | ||
| 3 | use crate::pac::{FLASH, PWR, RCC}; | 2 | use crate::pac::{FLASH, PWR, RCC}; |
| 4 | use crate::rcc::bd::{BackupDomain, RtcClockSource}; | 3 | use crate::rcc::bd::{BackupDomain, RtcClockSource}; |
| 5 | use crate::rcc::{set_freqs, Clocks}; | 4 | use crate::rcc::{set_freqs, Clocks}; |
| @@ -212,7 +211,7 @@ pub(crate) unsafe fn init(config: Config) { | |||
| 212 | match config.rtc_mux { | 211 | match config.rtc_mux { |
| 213 | RtcClockSource::LSE => { | 212 | RtcClockSource::LSE => { |
| 214 | // 1. Unlock the backup domain | 213 | // 1. Unlock the backup domain |
| 215 | PWR.cr1().modify(|w| w.set_dbp(Dbp::ENABLED)); | 214 | PWR.cr1().modify(|w| w.set_dbp(true)); |
| 216 | 215 | ||
| 217 | // 2. Setup the LSE | 216 | // 2. Setup the LSE |
| 218 | RCC.bdcr().modify(|w| { | 217 | RCC.bdcr().modify(|w| { |
