diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-02-09 00:58:17 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-02-09 00:58:17 +0100 |
| commit | 8160af6af99573d3ef66aede8c4b0aac033b0ff3 (patch) | |
| tree | b48576bafeaaab3f848fc7af6000c120cfe3652e | |
| parent | d1a968042279152fd8f342406fe89cbe7ad6ce48 (diff) | |
stm32: replace `peripheral_rcc!` macrotable with build.rs
| -rw-r--r-- | embassy-stm32/build.rs | 85 | ||||
| -rw-r--r-- | embassy-stm32/src/dma/bdma.rs | 7 | ||||
| -rw-r--r-- | embassy-stm32/src/dma/dma.rs | 7 | ||||
| -rw-r--r-- | embassy-stm32/src/dma/dmamux.rs | 8 | ||||
| -rw-r--r-- | embassy-stm32/src/gpio.rs | 8 | ||||
| -rw-r--r-- | embassy-stm32/src/rcc/mod.rs | 64 | ||||
| -rw-r--r-- | stm32-metapac-gen/src/lib.rs | 30 |
7 files changed, 89 insertions, 120 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index 8889b0d01..404fb7e1f 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs | |||
| @@ -146,6 +146,91 @@ fn main() { | |||
| 146 | }); | 146 | }); |
| 147 | 147 | ||
| 148 | // ======== | 148 | // ======== |
| 149 | // Generate RccPeripheral impls | ||
| 150 | |||
| 151 | for p in METADATA.peripherals { | ||
| 152 | if !singletons.contains(&p.name.to_string()) { | ||
| 153 | continue; | ||
| 154 | } | ||
| 155 | |||
| 156 | if let Some(rcc) = &p.rcc { | ||
| 157 | let en = rcc.enable.as_ref().unwrap(); | ||
| 158 | |||
| 159 | let rst = match &rcc.reset { | ||
| 160 | Some(rst) => { | ||
| 161 | let rst_reg = format_ident!("{}", rst.register.to_ascii_lowercase()); | ||
| 162 | let set_rst_field = format_ident!("set_{}", rst.field.to_ascii_lowercase()); | ||
| 163 | quote! { | ||
| 164 | critical_section::with(|_| unsafe { | ||
| 165 | crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(true)); | ||
| 166 | crate::pac::RCC.#rst_reg().modify(|w| w.#set_rst_field(false)); | ||
| 167 | }); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | None => TokenStream::new(), | ||
| 171 | }; | ||
| 172 | |||
| 173 | let pname = format_ident!("{}", p.name); | ||
| 174 | let clk = format_ident!("{}", rcc.clock.to_ascii_lowercase()); | ||
| 175 | let en_reg = format_ident!("{}", en.register.to_ascii_lowercase()); | ||
| 176 | let set_en_field = format_ident!("set_{}", en.field.to_ascii_lowercase()); | ||
| 177 | |||
| 178 | g.extend(quote! { | ||
| 179 | impl crate::rcc::sealed::RccPeripheral for peripherals::#pname { | ||
| 180 | fn frequency() -> crate::time::Hertz { | ||
| 181 | critical_section::with(|_| unsafe { | ||
| 182 | crate::rcc::get_freqs().#clk | ||
| 183 | }) | ||
| 184 | } | ||
| 185 | fn enable() { | ||
| 186 | critical_section::with(|_| unsafe { | ||
| 187 | crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true)) | ||
| 188 | }) | ||
| 189 | } | ||
| 190 | fn disable() { | ||
| 191 | critical_section::with(|_| unsafe { | ||
| 192 | crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(false)); | ||
| 193 | }) | ||
| 194 | } | ||
| 195 | fn reset() { | ||
| 196 | #rst | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | impl crate::rcc::RccPeripheral for peripherals::#pname {} | ||
| 201 | }); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | // ======== | ||
| 206 | // Generate fns to enable GPIO, DMA in RCC | ||
| 207 | |||
| 208 | for kind in ["dma", "bdma", "dmamux", "gpio"] { | ||
| 209 | let mut gg = TokenStream::new(); | ||
| 210 | |||
| 211 | for p in METADATA.peripherals { | ||
| 212 | if p.registers.is_some() && p.registers.as_ref().unwrap().kind == kind { | ||
| 213 | if let Some(rcc) = &p.rcc { | ||
| 214 | let en = rcc.enable.as_ref().unwrap(); | ||
| 215 | let en_reg = format_ident!("{}", en.register.to_ascii_lowercase()); | ||
| 216 | let set_en_field = format_ident!("set_{}", en.field.to_ascii_lowercase()); | ||
| 217 | |||
| 218 | gg.extend(quote! { | ||
| 219 | crate::pac::RCC.#en_reg().modify(|w| w.#set_en_field(true)); | ||
| 220 | }) | ||
| 221 | } | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | let fname = format_ident!("init_{}", kind); | ||
| 226 | g.extend(quote! { | ||
| 227 | pub unsafe fn #fname(){ | ||
| 228 | #gg | ||
| 229 | } | ||
| 230 | }) | ||
| 231 | } | ||
| 232 | |||
| 233 | // ======== | ||
| 149 | // Write generated.rs | 234 | // Write generated.rs |
| 150 | 235 | ||
| 151 | let out_dir = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); | 236 | let out_dir = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); |
diff --git a/embassy-stm32/src/dma/bdma.rs b/embassy-stm32/src/dma/bdma.rs index ebb0467dc..a37de2d63 100644 --- a/embassy-stm32/src/dma/bdma.rs +++ b/embassy-stm32/src/dma/bdma.rs | |||
| @@ -9,7 +9,6 @@ use embassy::waitqueue::AtomicWaker; | |||
| 9 | use crate::dma::Request; | 9 | use crate::dma::Request; |
| 10 | use crate::pac; | 10 | use crate::pac; |
| 11 | use crate::pac::bdma::vals; | 11 | use crate::pac::bdma::vals; |
| 12 | use crate::rcc::sealed::RccPeripheral; | ||
| 13 | 12 | ||
| 14 | use super::{Word, WordSize}; | 13 | use super::{Word, WordSize}; |
| 15 | 14 | ||
| @@ -77,11 +76,7 @@ pub(crate) unsafe fn init() { | |||
| 77 | crate::interrupt::$irq::steal().enable(); | 76 | crate::interrupt::$irq::steal().enable(); |
| 78 | }; | 77 | }; |
| 79 | } | 78 | } |
| 80 | pac::peripherals! { | 79 | crate::generated::init_bdma(); |
| 81 | (bdma, $peri:ident) => { | ||
| 82 | crate::peripherals::$peri::enable(); | ||
| 83 | }; | ||
| 84 | } | ||
| 85 | } | 80 | } |
| 86 | 81 | ||
| 87 | pac::dma_channels! { | 82 | pac::dma_channels! { |
diff --git a/embassy-stm32/src/dma/dma.rs b/embassy-stm32/src/dma/dma.rs index 21623b90c..c885452b0 100644 --- a/embassy-stm32/src/dma/dma.rs +++ b/embassy-stm32/src/dma/dma.rs | |||
| @@ -7,7 +7,6 @@ use embassy::waitqueue::AtomicWaker; | |||
| 7 | use crate::interrupt; | 7 | use crate::interrupt; |
| 8 | use crate::pac; | 8 | use crate::pac; |
| 9 | use crate::pac::dma::{regs, vals}; | 9 | use crate::pac::dma::{regs, vals}; |
| 10 | use crate::rcc::sealed::RccPeripheral; | ||
| 11 | 10 | ||
| 12 | use super::{Request, Word, WordSize}; | 11 | use super::{Request, Word, WordSize}; |
| 13 | 12 | ||
| @@ -74,11 +73,7 @@ pub(crate) unsafe fn init() { | |||
| 74 | interrupt::$irq::steal().enable(); | 73 | interrupt::$irq::steal().enable(); |
| 75 | }; | 74 | }; |
| 76 | } | 75 | } |
| 77 | pac::peripherals! { | 76 | crate::generated::init_dma(); |
| 78 | (dma, $peri:ident) => { | ||
| 79 | crate::peripherals::$peri::enable(); | ||
| 80 | }; | ||
| 81 | } | ||
| 82 | } | 77 | } |
| 83 | 78 | ||
| 84 | pac::dma_channels! { | 79 | pac::dma_channels! { |
diff --git a/embassy-stm32/src/dma/dmamux.rs b/embassy-stm32/src/dma/dmamux.rs index 83dcff4e8..fd076581b 100644 --- a/embassy-stm32/src/dma/dmamux.rs +++ b/embassy-stm32/src/dma/dmamux.rs | |||
| @@ -49,11 +49,5 @@ pac::dma_channels! { | |||
| 49 | 49 | ||
| 50 | /// safety: must be called only once | 50 | /// safety: must be called only once |
| 51 | pub(crate) unsafe fn init() { | 51 | pub(crate) unsafe fn init() { |
| 52 | crate::pac::peripheral_rcc! { | 52 | crate::generated::init_dmamux(); |
| 53 | ($name:ident, dmamux, DMAMUX, $clock:ident, ($reg:ident, $field:ident, $set_field:ident), $rst:tt) => { | ||
| 54 | crate::pac::RCC.$reg().modify(|reg| { | ||
| 55 | reg.$set_field(true); | ||
| 56 | }); | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | } | 53 | } |
diff --git a/embassy-stm32/src/gpio.rs b/embassy-stm32/src/gpio.rs index d4606716c..f154a66c4 100644 --- a/embassy-stm32/src/gpio.rs +++ b/embassy-stm32/src/gpio.rs | |||
| @@ -608,13 +608,7 @@ crate::pac::pins!( | |||
| 608 | ); | 608 | ); |
| 609 | 609 | ||
| 610 | pub(crate) unsafe fn init() { | 610 | pub(crate) unsafe fn init() { |
| 611 | crate::pac::peripheral_rcc! { | 611 | crate::generated::init_gpio(); |
| 612 | ($name:ident, gpio, GPIO, $clock:ident, ($reg:ident, $field:ident, $set_field:ident), $rst:tt) => { | ||
| 613 | crate::pac::RCC.$reg().modify(|reg| { | ||
| 614 | reg.$set_field(true); | ||
| 615 | }); | ||
| 616 | }; | ||
| 617 | } | ||
| 618 | } | 612 | } |
| 619 | 613 | ||
| 620 | mod eh02 { | 614 | mod eh02 { |
diff --git a/embassy-stm32/src/rcc/mod.rs b/embassy-stm32/src/rcc/mod.rs index 541d9c148..3aab01afd 100644 --- a/embassy-stm32/src/rcc/mod.rs +++ b/embassy-stm32/src/rcc/mod.rs | |||
| @@ -1,6 +1,5 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | 2 | ||
| 3 | use crate::peripherals; | ||
| 4 | use crate::time::Hertz; | 3 | use crate::time::Hertz; |
| 5 | use core::mem::MaybeUninit; | 4 | use core::mem::MaybeUninit; |
| 6 | 5 | ||
| @@ -104,66 +103,3 @@ pub(crate) mod sealed { | |||
| 104 | } | 103 | } |
| 105 | 104 | ||
| 106 | pub trait RccPeripheral: sealed::RccPeripheral + 'static {} | 105 | pub trait RccPeripheral: sealed::RccPeripheral + 'static {} |
| 107 | |||
| 108 | crate::pac::peripheral_rcc!( | ||
| 109 | ($inst:ident, gpio, GPIO, $clk:ident, $en:tt, $rst:tt) => {}; | ||
| 110 | ($inst:ident, $module:ident, $block:ident, $clk:ident, ($en_reg:ident, $en_field:ident, $en_set_field:ident), ($rst_reg:ident, $rst_field:ident, $rst_set_field:ident)) => { | ||
| 111 | impl sealed::RccPeripheral for peripherals::$inst { | ||
| 112 | fn frequency() -> crate::time::Hertz { | ||
| 113 | critical_section::with(|_| { | ||
| 114 | unsafe { get_freqs().$clk } | ||
| 115 | }) | ||
| 116 | } | ||
| 117 | fn enable() { | ||
| 118 | critical_section::with(|_| { | ||
| 119 | unsafe { | ||
| 120 | crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(true)); | ||
| 121 | } | ||
| 122 | }) | ||
| 123 | } | ||
| 124 | fn disable() { | ||
| 125 | critical_section::with(|_| { | ||
| 126 | unsafe { | ||
| 127 | crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(false)); | ||
| 128 | } | ||
| 129 | }) | ||
| 130 | } | ||
| 131 | fn reset() { | ||
| 132 | critical_section::with(|_| { | ||
| 133 | unsafe { | ||
| 134 | crate::pac::RCC.$rst_reg().modify(|w| w.$rst_set_field(true)); | ||
| 135 | crate::pac::RCC.$rst_reg().modify(|w| w.$rst_set_field(false)); | ||
| 136 | } | ||
| 137 | }) | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | impl RccPeripheral for peripherals::$inst {} | ||
| 142 | }; | ||
| 143 | ($inst:ident, $module:ident, $block:ident, $clk:ident, ($en_reg:ident, $en_field:ident, $en_set_field:ident), _) => { | ||
| 144 | impl sealed::RccPeripheral for peripherals::$inst { | ||
| 145 | fn frequency() -> crate::time::Hertz { | ||
| 146 | critical_section::with(|_| { | ||
| 147 | unsafe { get_freqs().$clk } | ||
| 148 | }) | ||
| 149 | } | ||
| 150 | fn enable() { | ||
| 151 | critical_section::with(|_| { | ||
| 152 | unsafe { | ||
| 153 | crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(true)); | ||
| 154 | } | ||
| 155 | }) | ||
| 156 | } | ||
| 157 | fn disable() { | ||
| 158 | critical_section::with(|_| { | ||
| 159 | unsafe { | ||
| 160 | crate::pac::RCC.$en_reg().modify(|w| w.$en_set_field(false)); | ||
| 161 | } | ||
| 162 | }) | ||
| 163 | } | ||
| 164 | fn reset() {} | ||
| 165 | } | ||
| 166 | |||
| 167 | impl RccPeripheral for peripherals::$inst {} | ||
| 168 | }; | ||
| 169 | ); | ||
diff --git a/stm32-metapac-gen/src/lib.rs b/stm32-metapac-gen/src/lib.rs index ccf737eff..10d67869a 100644 --- a/stm32-metapac-gen/src/lib.rs +++ b/stm32-metapac-gen/src/lib.rs | |||
| @@ -126,7 +126,6 @@ pub fn gen_chip( | |||
| 126 | let mut interrupt_table: Vec<Vec<String>> = Vec::new(); | 126 | let mut interrupt_table: Vec<Vec<String>> = Vec::new(); |
| 127 | let mut peripherals_table: Vec<Vec<String>> = Vec::new(); | 127 | let mut peripherals_table: Vec<Vec<String>> = Vec::new(); |
| 128 | let mut peripheral_pins_table: Vec<Vec<String>> = Vec::new(); | 128 | let mut peripheral_pins_table: Vec<Vec<String>> = Vec::new(); |
| 129 | let mut peripheral_rcc_table: Vec<Vec<String>> = Vec::new(); | ||
| 130 | let mut dma_channels_table: Vec<Vec<String>> = Vec::new(); | 129 | let mut dma_channels_table: Vec<Vec<String>> = Vec::new(); |
| 131 | let mut peripheral_dma_channels_table: Vec<Vec<String>> = Vec::new(); | 130 | let mut peripheral_dma_channels_table: Vec<Vec<String>> = Vec::new(); |
| 132 | let mut peripheral_counts: BTreeMap<String, u8> = BTreeMap::new(); | 131 | let mut peripheral_counts: BTreeMap<String, u8> = BTreeMap::new(); |
| @@ -264,34 +263,6 @@ pub fn gen_chip( | |||
| 264 | } | 263 | } |
| 265 | _ => {} | 264 | _ => {} |
| 266 | } | 265 | } |
| 267 | |||
| 268 | if let Some(rcc) = &p.rcc { | ||
| 269 | let mut clock = rcc.clock.to_ascii_lowercase(); | ||
| 270 | if p.name.starts_with("TIM") { | ||
| 271 | clock = format!("{}_tim", clock) | ||
| 272 | } | ||
| 273 | |||
| 274 | let mut row = Vec::new(); | ||
| 275 | row.push(p.name.clone()); | ||
| 276 | row.push(bi.kind.clone()); | ||
| 277 | row.push(bi.block.clone()); | ||
| 278 | row.push(clock); | ||
| 279 | |||
| 280 | for reg in [&rcc.enable, &rcc.reset] { | ||
| 281 | if let Some(reg) = reg { | ||
| 282 | row.push(format!( | ||
| 283 | "({}, {}, set_{})", | ||
| 284 | reg.register.to_ascii_lowercase(), | ||
| 285 | reg.field.to_ascii_lowercase(), | ||
| 286 | reg.field.to_ascii_lowercase() | ||
| 287 | )); | ||
| 288 | } else { | ||
| 289 | row.push("_".to_string()) | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | peripheral_rcc_table.push(row); | ||
| 294 | } | ||
| 295 | } | 266 | } |
| 296 | 267 | ||
| 297 | dev.peripherals.push(ir_peri); | 268 | dev.peripherals.push(ir_peri); |
| @@ -422,7 +393,6 @@ pub fn gen_chip( | |||
| 422 | "peripheral_dma_channels", | 393 | "peripheral_dma_channels", |
| 423 | &peripheral_dma_channels_table, | 394 | &peripheral_dma_channels_table, |
| 424 | ); | 395 | ); |
| 425 | make_table(&mut data, "peripheral_rcc", &peripheral_rcc_table); | ||
| 426 | make_table(&mut data, "dma_channels", &dma_channels_table); | 396 | make_table(&mut data, "dma_channels", &dma_channels_table); |
| 427 | make_table(&mut data, "dbgmcu", &dbgmcu_table); | 397 | make_table(&mut data, "dbgmcu", &dbgmcu_table); |
| 428 | make_peripheral_counts(&mut data, &peripheral_counts); | 398 | make_peripheral_counts(&mut data, &peripheral_counts); |
