diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-01-24 00:24:23 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-01-24 00:24:53 +0100 |
| commit | 79f60adbfba41b4abec7b9d1444bf64e5423feaa (patch) | |
| tree | 8784a972c4ea3d01d4fddec5283e4613c025b3d2 /embassy-stm32 | |
| parent | 6b0cb0609b4bfa3e464e4603ce373a9de9886103 (diff) | |
stm32: add `time-driver-any` cargo feature that automatically picks one available timer.
Diffstat (limited to 'embassy-stm32')
| -rw-r--r-- | embassy-stm32/Cargo.toml | 3 | ||||
| -rw-r--r-- | embassy-stm32/build.rs | 77 | ||||
| -rw-r--r-- | embassy-stm32/src/time_driver.rs | 8 |
3 files changed, 75 insertions, 13 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index ee5e7b173..c0c024b73 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml | |||
| @@ -28,9 +28,7 @@ stm32-metapac = { version = "0.1.0", path = "../stm32-metapac", features = ["rt" | |||
| 28 | vcell = { version = "0.1.3", optional = true } | 28 | vcell = { version = "0.1.3", optional = true } |
| 29 | bxcan = "0.6.2" | 29 | bxcan = "0.6.2" |
| 30 | nb = "1.0.0" | 30 | nb = "1.0.0" |
| 31 | |||
| 32 | seq-macro = "0.2.2" | 31 | seq-macro = "0.2.2" |
| 33 | |||
| 34 | cfg-if = "1.0.0" | 32 | cfg-if = "1.0.0" |
| 35 | 33 | ||
| 36 | [build-dependencies] | 34 | [build-dependencies] |
| @@ -46,6 +44,7 @@ exti = [] | |||
| 46 | # Features starting with `_` are for internal use only. They're not intended | 44 | # Features starting with `_` are for internal use only. They're not intended |
| 47 | # to be enabled by other crates, and are not covered by semver guarantees. | 45 | # to be enabled by other crates, and are not covered by semver guarantees. |
| 48 | _time-driver = ["embassy/time-tick-32768hz"] | 46 | _time-driver = ["embassy/time-tick-32768hz"] |
| 47 | time-driver-any = ["_time-driver"] | ||
| 49 | time-driver-tim2 = ["_time-driver"] | 48 | time-driver-tim2 = ["_time-driver"] |
| 50 | time-driver-tim3 = ["_time-driver"] | 49 | time-driver-tim3 = ["_time-driver"] |
| 51 | 50 | ||
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index 517ee57ba..f3c8cd58e 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs | |||
| @@ -4,13 +4,18 @@ use std::fs; | |||
| 4 | use std::path::PathBuf; | 4 | use std::path::PathBuf; |
| 5 | 5 | ||
| 6 | fn main() { | 6 | fn main() { |
| 7 | let chip_name = env::vars_os() | 7 | let chip_name = match env::vars() |
| 8 | .map(|(a, _)| a.to_string_lossy().to_string()) | 8 | .map(|(a, _)| a) |
| 9 | .find(|x| x.starts_with("CARGO_FEATURE_STM32")) | 9 | .filter(|x| x.starts_with("CARGO_FEATURE_STM32")) |
| 10 | .expect("No stm32xx Cargo feature enabled") | 10 | .get_one() |
| 11 | .strip_prefix("CARGO_FEATURE_") | 11 | { |
| 12 | .unwrap() | 12 | Ok(x) => x, |
| 13 | .to_ascii_lowercase(); | 13 | Err(GetOneError::None) => panic!("No stm32xx Cargo feature enabled"), |
| 14 | Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"), | ||
| 15 | } | ||
| 16 | .strip_prefix("CARGO_FEATURE_") | ||
| 17 | .unwrap() | ||
| 18 | .to_ascii_lowercase(); | ||
| 14 | 19 | ||
| 15 | struct Peripheral { | 20 | struct Peripheral { |
| 16 | kind: String, | 21 | kind: String, |
| @@ -120,5 +125,63 @@ fn main() { | |||
| 120 | println!("cargo:rustc-cfg={}", &chip_name[..chip_name.len() - 2]); | 125 | println!("cargo:rustc-cfg={}", &chip_name[..chip_name.len() - 2]); |
| 121 | } | 126 | } |
| 122 | 127 | ||
| 128 | // ======== | ||
| 129 | // Handle time-driver-XXXX features. | ||
| 130 | |||
| 131 | let time_driver = match env::vars() | ||
| 132 | .map(|(a, _)| a) | ||
| 133 | .filter(|x| x.starts_with("CARGO_FEATURE_TIME_DRIVER_")) | ||
| 134 | .get_one() | ||
| 135 | { | ||
| 136 | Ok(x) => Some( | ||
| 137 | x.strip_prefix("CARGO_FEATURE_TIME_DRIVER_") | ||
| 138 | .unwrap() | ||
| 139 | .to_ascii_lowercase(), | ||
| 140 | ), | ||
| 141 | Err(GetOneError::None) => None, | ||
| 142 | Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"), | ||
| 143 | }; | ||
| 144 | |||
| 145 | match time_driver.as_ref().map(|x| x.as_ref()) { | ||
| 146 | None => {} | ||
| 147 | Some("tim2") => println!("cargo:rustc-cfg=time_driver_tim2"), | ||
| 148 | Some("tim3") => println!("cargo:rustc-cfg=time_driver_tim3"), | ||
| 149 | Some("any") => { | ||
| 150 | if singletons.contains(&"TIM2".to_string()) { | ||
| 151 | println!("cargo:rustc-cfg=time_driver_tim2"); | ||
| 152 | } else if singletons.contains(&"TIM3".to_string()) { | ||
| 153 | println!("cargo:rustc-cfg=time_driver_tim3"); | ||
| 154 | } else { | ||
| 155 | panic!("time-driver-any requested, but the chip doesn't have TIM2 or TIM3.") | ||
| 156 | } | ||
| 157 | } | ||
| 158 | _ => panic!("unknown time_driver {:?}", time_driver), | ||
| 159 | } | ||
| 160 | |||
| 161 | // Handle time-driver-XXXX features. | ||
| 162 | if env::var("CARGO_FEATURE_TIME_DRIVER_ANY").is_ok() {} | ||
| 163 | println!("cargo:rustc-cfg={}", &chip_name[..chip_name.len() - 2]); | ||
| 164 | |||
| 123 | println!("cargo:rerun-if-changed=build.rs"); | 165 | println!("cargo:rerun-if-changed=build.rs"); |
| 124 | } | 166 | } |
| 167 | |||
| 168 | enum GetOneError { | ||
| 169 | None, | ||
| 170 | Multiple, | ||
| 171 | } | ||
| 172 | |||
| 173 | trait IteratorExt: Iterator { | ||
| 174 | fn get_one(self) -> Result<Self::Item, GetOneError>; | ||
| 175 | } | ||
| 176 | |||
| 177 | impl<T: Iterator> IteratorExt for T { | ||
| 178 | fn get_one(mut self) -> Result<Self::Item, GetOneError> { | ||
| 179 | match self.next() { | ||
| 180 | None => Err(GetOneError::None), | ||
| 181 | Some(res) => match self.next() { | ||
| 182 | Some(_) => Err(GetOneError::Multiple), | ||
| 183 | None => Ok(res), | ||
| 184 | }, | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index d20929e17..5affb1fae 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs | |||
| @@ -18,17 +18,17 @@ use self::sealed::Instance as _; | |||
| 18 | 18 | ||
| 19 | const ALARM_COUNT: usize = 3; | 19 | const ALARM_COUNT: usize = 3; |
| 20 | 20 | ||
| 21 | #[cfg(feature = "time-driver-tim2")] | 21 | #[cfg(time_driver_tim2)] |
| 22 | type T = peripherals::TIM2; | 22 | type T = peripherals::TIM2; |
| 23 | #[cfg(feature = "time-driver-tim3")] | 23 | #[cfg(time_driver_tim3)] |
| 24 | type T = peripherals::TIM3; | 24 | type T = peripherals::TIM3; |
| 25 | 25 | ||
| 26 | #[cfg(feature = "time-driver-tim2")] | 26 | #[cfg(time_driver_tim2)] |
| 27 | #[interrupt] | 27 | #[interrupt] |
| 28 | fn TIM2() { | 28 | fn TIM2() { |
| 29 | DRIVER.on_interrupt() | 29 | DRIVER.on_interrupt() |
| 30 | } | 30 | } |
| 31 | #[cfg(feature = "time-driver-tim3")] | 31 | #[cfg(time_driver_tim3)] |
| 32 | #[interrupt] | 32 | #[interrupt] |
| 33 | fn TIM3() { | 33 | fn TIM3() { |
| 34 | DRIVER.on_interrupt() | 34 | DRIVER.on_interrupt() |
