aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-04-16 17:47:25 -0500
committerxoviat <[email protected]>2023-04-16 17:47:25 -0500
commit776e001b5befc50ff409b5cd8252bbb61fbb5acc (patch)
tree4b6f918251a844719a9b33e79c2b4119d4afc0b3
parent1fdce6e52a51de89f48f002d5c92139f58029575 (diff)
stm32: remove TIMX singleton when used on timer driver
fixes #1316.
-rw-r--r--embassy-hal-common/src/macros.rs25
-rw-r--r--embassy-stm32/build.rs114
2 files changed, 92 insertions, 47 deletions
diff --git a/embassy-hal-common/src/macros.rs b/embassy-hal-common/src/macros.rs
index 5e62e048a..f06b46002 100644
--- a/embassy-hal-common/src/macros.rs
+++ b/embassy-hal-common/src/macros.rs
@@ -1,5 +1,5 @@
1#[macro_export] 1#[macro_export]
2macro_rules! peripherals { 2macro_rules! peripherals_definition {
3 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => { 3 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
4 /// Types for the peripheral singletons. 4 /// Types for the peripheral singletons.
5 pub mod peripherals { 5 pub mod peripherals {
@@ -26,7 +26,12 @@ macro_rules! peripherals {
26 $crate::impl_peripheral!($name); 26 $crate::impl_peripheral!($name);
27 )* 27 )*
28 } 28 }
29 };
30}
29 31
32#[macro_export]
33macro_rules! peripherals_struct {
34 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
30 /// Struct containing all the peripheral singletons. 35 /// Struct containing all the peripheral singletons.
31 /// 36 ///
32 /// To obtain the peripherals, you must initialize the HAL, by calling [`crate::init`]. 37 /// To obtain the peripherals, you must initialize the HAL, by calling [`crate::init`].
@@ -77,6 +82,24 @@ macro_rules! peripherals {
77} 82}
78 83
79#[macro_export] 84#[macro_export]
85macro_rules! peripherals {
86 ($($(#[$cfg:meta])? $name:ident),*$(,)?) => {
87 $crate::peripherals_definition!(
88 $(
89 $(#[$cfg])?
90 $name,
91 )*
92 );
93 $crate::peripherals_struct!(
94 $(
95 $(#[$cfg])?
96 $name,
97 )*
98 );
99 };
100}
101
102#[macro_export]
80macro_rules! into_ref { 103macro_rules! into_ref {
81 ($($name:ident),*) => { 104 ($($name:ident),*) => {
82 $( 105 $(
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index 73bd29fcf..bb6c99963 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -81,11 +81,74 @@ fn main() {
81 singletons.push(c.name.to_string()); 81 singletons.push(c.name.to_string());
82 } 82 }
83 83
84 // ========
85 // Handle time-driver-XXXX features.
86
87 let time_driver = match env::vars()
88 .map(|(a, _)| a)
89 .filter(|x| x.starts_with("CARGO_FEATURE_TIME_DRIVER_"))
90 .get_one()
91 {
92 Ok(x) => Some(
93 x.strip_prefix("CARGO_FEATURE_TIME_DRIVER_")
94 .unwrap()
95 .to_ascii_lowercase(),
96 ),
97 Err(GetOneError::None) => None,
98 Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"),
99 };
100
101 let time_driver_singleton = match time_driver.as_ref().map(|x| x.as_ref()) {
102 None => "",
103 Some("tim2") => "TIM2",
104 Some("tim3") => "TIM3",
105 Some("tim4") => "TIM4",
106 Some("tim5") => "TIM5",
107 Some("tim12") => "TIM12",
108 Some("tim15") => "TIM15",
109 Some("any") => {
110 if singletons.contains(&"TIM2".to_string()) {
111 "TIM2"
112 } else if singletons.contains(&"TIM3".to_string()) {
113 "TIM3"
114 } else if singletons.contains(&"TIM4".to_string()) {
115 "TIM4"
116 } else if singletons.contains(&"TIM5".to_string()) {
117 "TIM5"
118 } else if singletons.contains(&"TIM12".to_string()) {
119 "TIM12"
120 } else if singletons.contains(&"TIM15".to_string()) {
121 "TIM15"
122 } else {
123 panic!("time-driver-any requested, but the chip doesn't have TIM2, TIM3, TIM4, TIM5, TIM12 or TIM15.")
124 }
125 }
126 _ => panic!("unknown time_driver {:?}", time_driver),
127 };
128
129 if time_driver_singleton != "" {
130 println!("cargo:rustc-cfg=time_driver_{}", time_driver_singleton.to_lowercase());
131 }
132
133 // ========
134 // Write singletons
135
84 let mut g = TokenStream::new(); 136 let mut g = TokenStream::new();
85 137
86 let singleton_tokens: Vec<_> = singletons.iter().map(|s| format_ident!("{}", s)).collect(); 138 let singleton_tokens: Vec<_> = singletons.iter().map(|s| format_ident!("{}", s)).collect();
139
87 g.extend(quote! { 140 g.extend(quote! {
88 embassy_hal_common::peripherals!(#(#singleton_tokens),*); 141 embassy_hal_common::peripherals_definition!(#(#singleton_tokens),*);
142 });
143
144 let singleton_tokens: Vec<_> = singletons
145 .iter()
146 .filter(|s| *s != &time_driver_singleton.to_string())
147 .map(|s| format_ident!("{}", s))
148 .collect();
149
150 g.extend(quote! {
151 embassy_hal_common::peripherals_struct!(#(#singleton_tokens),*);
89 }); 152 });
90 153
91 // ======== 154 // ========
@@ -536,6 +599,10 @@ fn main() {
536 let pin_name = format_ident!("{}", pin.pin); 599 let pin_name = format_ident!("{}", pin.pin);
537 let af = pin.af.unwrap_or(0); 600 let af = pin.af.unwrap_or(0);
538 601
602 if peri == time_driver_singleton {
603 continue;
604 }
605
539 // MCO is special 606 // MCO is special
540 if pin.signal.starts_with("MCO_") { 607 if pin.signal.starts_with("MCO_") {
541 // Supported in H7 only for now 608 // Supported in H7 only for now
@@ -838,51 +905,6 @@ fn main() {
838 println!("cargo:rustc-cfg={}x", &chip_name[..8]); // stm32f42x 905 println!("cargo:rustc-cfg={}x", &chip_name[..8]); // stm32f42x
839 println!("cargo:rustc-cfg={}x{}", &chip_name[..7], &chip_name[8..9]); // stm32f4x9 906 println!("cargo:rustc-cfg={}x{}", &chip_name[..7], &chip_name[8..9]); // stm32f4x9
840 907
841 // ========
842 // Handle time-driver-XXXX features.
843
844 let time_driver = match env::vars()
845 .map(|(a, _)| a)
846 .filter(|x| x.starts_with("CARGO_FEATURE_TIME_DRIVER_"))
847 .get_one()
848 {
849 Ok(x) => Some(
850 x.strip_prefix("CARGO_FEATURE_TIME_DRIVER_")
851 .unwrap()
852 .to_ascii_lowercase(),
853 ),
854 Err(GetOneError::None) => None,
855 Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"),
856 };
857
858 match time_driver.as_ref().map(|x| x.as_ref()) {
859 None => {}
860 Some("tim2") => println!("cargo:rustc-cfg=time_driver_tim2"),
861 Some("tim3") => println!("cargo:rustc-cfg=time_driver_tim3"),
862 Some("tim4") => println!("cargo:rustc-cfg=time_driver_tim4"),
863 Some("tim5") => println!("cargo:rustc-cfg=time_driver_tim5"),
864 Some("tim12") => println!("cargo:rustc-cfg=time_driver_tim12"),
865 Some("tim15") => println!("cargo:rustc-cfg=time_driver_tim15"),
866 Some("any") => {
867 if singletons.contains(&"TIM2".to_string()) {
868 println!("cargo:rustc-cfg=time_driver_tim2");
869 } else if singletons.contains(&"TIM3".to_string()) {
870 println!("cargo:rustc-cfg=time_driver_tim3");
871 } else if singletons.contains(&"TIM4".to_string()) {
872 println!("cargo:rustc-cfg=time_driver_tim4");
873 } else if singletons.contains(&"TIM5".to_string()) {
874 println!("cargo:rustc-cfg=time_driver_tim5");
875 } else if singletons.contains(&"TIM12".to_string()) {
876 println!("cargo:rustc-cfg=time_driver_tim12");
877 } else if singletons.contains(&"TIM15".to_string()) {
878 println!("cargo:rustc-cfg=time_driver_tim15");
879 } else {
880 panic!("time-driver-any requested, but the chip doesn't have TIM2, TIM3, TIM4, TIM5, TIM12 or TIM15.")
881 }
882 }
883 _ => panic!("unknown time_driver {:?}", time_driver),
884 }
885
886 // Handle time-driver-XXXX features. 908 // Handle time-driver-XXXX features.
887 if env::var("CARGO_FEATURE_TIME_DRIVER_ANY").is_ok() {} 909 if env::var("CARGO_FEATURE_TIME_DRIVER_ANY").is_ok() {}
888 println!("cargo:rustc-cfg={}", &chip_name[..chip_name.len() - 2]); 910 println!("cargo:rustc-cfg={}", &chip_name[..chip_name.len() - 2]);