diff options
| author | Dion Dokter <[email protected]> | 2025-04-24 11:23:11 +0200 |
|---|---|---|
| committer | Dion Dokter <[email protected]> | 2025-04-24 11:23:11 +0200 |
| commit | 7259f20fe211f2833ffb4aa1b44e6ddd60145853 (patch) | |
| tree | 03b5e75cafc55d7985c9288ff456225b08bd631f /embassy-stm32/build.rs | |
| parent | fb5ce05b26ae0c90a872a8e0787c9419178d475a (diff) | |
Add configurable bank support in build script
Diffstat (limited to 'embassy-stm32/build.rs')
| -rw-r--r-- | embassy-stm32/build.rs | 67 |
1 files changed, 51 insertions, 16 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index f9f03c51b..2a3213c0c 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs | |||
| @@ -50,6 +50,48 @@ fn main() { | |||
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | // ======== | 52 | // ======== |
| 53 | // Select the memory variant to use | ||
| 54 | let memory = { | ||
| 55 | let single_bank_selected = env::var("CARGO_FEATURE_SINGLE_BANK").is_ok(); | ||
| 56 | let dual_bank_selected = env::var("CARGO_FEATURE_DUAL_BANK").is_ok(); | ||
| 57 | |||
| 58 | let single_bank_memory = METADATA.memory.iter().find(|mem| { | ||
| 59 | mem.iter() | ||
| 60 | .filter(|region| region.kind == MemoryRegionKind::Flash) | ||
| 61 | .count() | ||
| 62 | == 1 | ||
| 63 | }); | ||
| 64 | |||
| 65 | let dual_bank_memory = METADATA.memory.iter().find(|mem| { | ||
| 66 | mem.iter() | ||
| 67 | .filter(|region| region.kind == MemoryRegionKind::Flash) | ||
| 68 | .count() | ||
| 69 | == 2 | ||
| 70 | }); | ||
| 71 | |||
| 72 | cfgs.set( | ||
| 73 | "bank_setup_configurable", | ||
| 74 | single_bank_memory.is_some() && dual_bank_memory.is_some(), | ||
| 75 | ); | ||
| 76 | |||
| 77 | match (single_bank_selected, dual_bank_selected) { | ||
| 78 | (true, true) => panic!("Both 'single-bank' and 'dual-bank' features enabled"), | ||
| 79 | (true, false) => { | ||
| 80 | single_bank_memory.expect("The 'single-bank' feature is not supported on this dual bank chip") | ||
| 81 | } | ||
| 82 | (false, true) => { | ||
| 83 | dual_bank_memory.expect("The 'dual-bank' feature is not supported on this single bank chip") | ||
| 84 | } | ||
| 85 | (false, false) => { | ||
| 86 | if METADATA.memory.len() != 1 { | ||
| 87 | panic!("Chip supports single and dual bank configuration. No Cargo feature to select one is enabled. Use the 'single-bank' or 'dual-bank' feature to make your selection") | ||
| 88 | } | ||
| 89 | METADATA.memory[0] | ||
| 90 | } | ||
| 91 | } | ||
| 92 | }; | ||
| 93 | |||
| 94 | // ======== | ||
| 53 | // Generate singletons | 95 | // Generate singletons |
| 54 | 96 | ||
| 55 | let mut singletons: Vec<String> = Vec::new(); | 97 | let mut singletons: Vec<String> = Vec::new(); |
| @@ -290,8 +332,7 @@ fn main() { | |||
| 290 | // ======== | 332 | // ======== |
| 291 | // Generate FLASH regions | 333 | // Generate FLASH regions |
| 292 | let mut flash_regions = TokenStream::new(); | 334 | let mut flash_regions = TokenStream::new(); |
| 293 | let flash_memory_regions: Vec<_> = METADATA | 335 | let flash_memory_regions: Vec<_> = memory |
| 294 | .memory | ||
| 295 | .iter() | 336 | .iter() |
| 296 | .filter(|x| x.kind == MemoryRegionKind::Flash && x.settings.is_some()) | 337 | .filter(|x| x.kind == MemoryRegionKind::Flash && x.settings.is_some()) |
| 297 | .collect(); | 338 | .collect(); |
| @@ -1616,8 +1657,7 @@ fn main() { | |||
| 1616 | let mut pins_table: Vec<Vec<String>> = Vec::new(); | 1657 | let mut pins_table: Vec<Vec<String>> = Vec::new(); |
| 1617 | let mut adc_table: Vec<Vec<String>> = Vec::new(); | 1658 | let mut adc_table: Vec<Vec<String>> = Vec::new(); |
| 1618 | 1659 | ||
| 1619 | for m in METADATA | 1660 | for m in memory |
| 1620 | .memory | ||
| 1621 | .iter() | 1661 | .iter() |
| 1622 | .filter(|m| m.kind == MemoryRegionKind::Flash && m.settings.is_some()) | 1662 | .filter(|m| m.kind == MemoryRegionKind::Flash && m.settings.is_some()) |
| 1623 | { | 1663 | { |
| @@ -1855,8 +1895,7 @@ fn main() { | |||
| 1855 | // ======== | 1895 | // ======== |
| 1856 | // Generate flash constants | 1896 | // Generate flash constants |
| 1857 | 1897 | ||
| 1858 | let flash_regions: Vec<&MemoryRegion> = METADATA | 1898 | let flash_regions: Vec<&MemoryRegion> = memory |
| 1859 | .memory | ||
| 1860 | .iter() | 1899 | .iter() |
| 1861 | .filter(|x| x.kind == MemoryRegionKind::Flash && x.name.starts_with("BANK_")) | 1900 | .filter(|x| x.kind == MemoryRegionKind::Flash && x.name.starts_with("BANK_")) |
| 1862 | .collect(); | 1901 | .collect(); |
| @@ -1981,7 +2020,7 @@ fn main() { | |||
| 1981 | println!("cargo:rerun-if-changed=build.rs"); | 2020 | println!("cargo:rerun-if-changed=build.rs"); |
| 1982 | 2021 | ||
| 1983 | if cfg!(feature = "memory-x") { | 2022 | if cfg!(feature = "memory-x") { |
| 1984 | gen_memory_x(out_dir); | 2023 | gen_memory_x(memory, out_dir); |
| 1985 | println!("cargo:rustc-link-search={}", out_dir.display()); | 2024 | println!("cargo:rustc-link-search={}", out_dir.display()); |
| 1986 | } | 2025 | } |
| 1987 | } | 2026 | } |
| @@ -2070,11 +2109,11 @@ fn rustfmt(path: impl AsRef<Path>) { | |||
| 2070 | } | 2109 | } |
| 2071 | } | 2110 | } |
| 2072 | 2111 | ||
| 2073 | fn gen_memory_x(out_dir: &Path) { | 2112 | fn gen_memory_x(memory: &[MemoryRegion], out_dir: &Path) { |
| 2074 | let mut memory_x = String::new(); | 2113 | let mut memory_x = String::new(); |
| 2075 | 2114 | ||
| 2076 | let flash = get_memory_range(MemoryRegionKind::Flash); | 2115 | let flash = get_memory_range(memory, MemoryRegionKind::Flash); |
| 2077 | let ram = get_memory_range(MemoryRegionKind::Ram); | 2116 | let ram = get_memory_range(memory, MemoryRegionKind::Ram); |
| 2078 | 2117 | ||
| 2079 | write!(memory_x, "MEMORY\n{{\n").unwrap(); | 2118 | write!(memory_x, "MEMORY\n{{\n").unwrap(); |
| 2080 | writeln!( | 2119 | writeln!( |
| @@ -2098,12 +2137,8 @@ fn gen_memory_x(out_dir: &Path) { | |||
| 2098 | std::fs::write(out_dir.join("memory.x"), memory_x.as_bytes()).unwrap(); | 2137 | std::fs::write(out_dir.join("memory.x"), memory_x.as_bytes()).unwrap(); |
| 2099 | } | 2138 | } |
| 2100 | 2139 | ||
| 2101 | fn get_memory_range(kind: MemoryRegionKind) -> (u32, u32, String) { | 2140 | fn get_memory_range(memory: &[MemoryRegion], kind: MemoryRegionKind) -> (u32, u32, String) { |
| 2102 | let mut mems: Vec<_> = METADATA | 2141 | let mut mems: Vec<_> = memory.iter().filter(|m| m.kind == kind && m.size != 0).collect(); |
| 2103 | .memory | ||
| 2104 | .iter() | ||
| 2105 | .filter(|m| m.kind == kind && m.size != 0) | ||
| 2106 | .collect(); | ||
| 2107 | mems.sort_by_key(|m| m.address); | 2142 | mems.sort_by_key(|m| m.address); |
| 2108 | 2143 | ||
| 2109 | let mut start = u32::MAX; | 2144 | let mut start = u32::MAX; |
