|
1 | 1 | use std::{env, fs::File, io::prelude::*, path::PathBuf};
|
2 | 2 |
|
3 | 3 | fn main() {
|
| 4 | + check_device_feature(); |
4 | 5 | if cfg!(feature = "ld") {
|
5 | 6 | gen_memory_x();
|
6 | 7 | }
|
7 | 8 | println!("cargo:rerun-if-changed=build.rs");
|
8 | 9 | }
|
9 | 10 |
|
| 11 | +/// Check device feature selection |
| 12 | +fn check_device_feature() { |
| 13 | + if !cfg!(feature = "device-selected") { |
| 14 | + if cfg!(feature = "direct-call-deprecated") { |
| 15 | + eprintln!( |
| 16 | + "The feature you selected is deprecated, because it was split up into sub-devices. |
| 17 | +
|
| 18 | +Example: The STM32F3Discovery board has a STM32F303VCT6 chip. |
| 19 | +You probably used to use `stm32f303` but now functionalities for the sub-device were added. |
| 20 | +In this case replace it with `stm32f303xc` to make your code build again. |
| 21 | +
|
| 22 | +For more information, see \x1b]8;;https://github.com/stm32-rs/stm32f3xx-hal#selecting-the-right-chip\x1b\\README -> Selecting the right chip\x1b]8;;\x1b\\." |
| 23 | + ); |
| 24 | + } else { |
| 25 | + eprintln!( |
| 26 | + "This crate requires you to specify your target chip as a feature. |
| 27 | +
|
| 28 | +Please select one of the following (`x` denotes any character in [a-z]): |
| 29 | +
|
| 30 | + stm32f301x6 stm32f301x8 |
| 31 | + stm32f318x8 |
| 32 | + stm32f302x6 stm32f302x8 stm32f302xb stm32f302xc stm32f302xd stm32f302xe |
| 33 | + stm32f303x6 stm32f303x8 stm32f303xb stm32f303xc stm32f303xd stm32f303xe |
| 34 | + stm32f328x8 |
| 35 | + stm32f358xc |
| 36 | + stm32f398xe |
| 37 | + stm32f373x8 stm32f373xb stm32f373xc |
| 38 | + stm32f378xc |
| 39 | + stm32f334x4 stm32f334x6 stm32f334x8 |
| 40 | +
|
| 41 | +Example: The STM32F3Discovery board has a STM32F303VCT6 chip. |
| 42 | +So you need to specify stm32f303xc in your Cargo.toml (note that VC → xc). |
| 43 | +
|
| 44 | +For more information, see \x1b]8;;https://github.com/stm32-rs/stm32f3xx-hal#selecting-the-right-chip\x1b\\README -> Selecting the right chip\x1b]8;;\x1b\\." |
| 45 | + ); |
| 46 | + } |
| 47 | + std::process::exit(1); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// Generate `memory.x` for selected device |
| 52 | +/// |
| 53 | +/// Available RAM/CCMRAM/FLASH value is extracted from RM0313/RM0316/RM0364/RM0365/RM0366 |
10 | 54 | fn gen_memory_x() {
|
11 |
| - #![allow(clippy::unneeded_wildcard_pattern)] |
| 55 | + enum Mem { |
| 56 | + _4, |
| 57 | + _6, |
| 58 | + _8, |
| 59 | + B, |
| 60 | + C, |
| 61 | + D, |
| 62 | + E, |
| 63 | + } |
12 | 64 |
|
13 |
| - let mem_cfg_set = ( |
14 |
| - cfg!(feature = "mem-4"), |
15 |
| - cfg!(feature = "mem-6"), |
16 |
| - cfg!(feature = "mem-8"), |
17 |
| - cfg!(feature = "mem-b"), |
18 |
| - cfg!(feature = "mem-c"), |
19 |
| - cfg!(feature = "mem-d"), |
20 |
| - cfg!(feature = "mem-e"), |
21 |
| - ); |
22 |
| - let flash = match mem_cfg_set { |
23 |
| - (true, ..) => 16, |
24 |
| - (_, true, ..) => 32, |
25 |
| - (_, _, true, ..) => 64, |
26 |
| - (.., true, _, _, _) => 128, |
27 |
| - (.., _, true, _, _) => 256, |
28 |
| - (.., _, _, true, _) => 384, |
29 |
| - (.., _, _, _, true) => 512, |
30 |
| - _ => unreachable!(), |
| 65 | + let mem = if cfg!(feature = "mem-4") { |
| 66 | + Mem::_4 |
| 67 | + } else if cfg!(feature = "mem-6") { |
| 68 | + Mem::_6 |
| 69 | + } else if cfg!(feature = "mem-8") { |
| 70 | + Mem::_8 |
| 71 | + } else if cfg!(feature = "mem-b") { |
| 72 | + Mem::B |
| 73 | + } else if cfg!(feature = "mem-c") { |
| 74 | + Mem::C |
| 75 | + } else if cfg!(feature = "mem-d") { |
| 76 | + Mem::D |
| 77 | + } else if cfg!(feature = "mem-e") { |
| 78 | + Mem::E |
| 79 | + } else { |
| 80 | + eprintln!( |
| 81 | + "Memory size unknown. |
| 82 | +This may be due to incorrect feature configuration in Cargo.toml or stm32f3xx-hal's internal issue." |
| 83 | + ); |
| 84 | + std::process::exit(1); |
| 85 | + }; |
| 86 | + |
| 87 | + let flash = match mem { |
| 88 | + Mem::_4 => 16, |
| 89 | + Mem::_6 => 32, |
| 90 | + Mem::_8 => 64, |
| 91 | + Mem::B => 128, |
| 92 | + Mem::C => 256, |
| 93 | + Mem::D => 384, |
| 94 | + Mem::E => 512, |
31 | 95 | };
|
32 | 96 | let ccmram = if cfg!(feature = "svd-f303") || cfg!(feature = "svd-f3x4") {
|
33 |
| - match mem_cfg_set { |
34 |
| - (true, ..) | (_, true, ..) | (_, _, true, ..) => 4, |
35 |
| - (.., true, _, _, _) | (.., _, true, _, _) => 8, |
36 |
| - (.., _, _, true, _) | (.., _, _, _, true) => 16, |
37 |
| - _ => unreachable!(), |
| 97 | + match mem { |
| 98 | + Mem::_4 | Mem::_6 | Mem::_8 => 4, |
| 99 | + Mem::B | Mem::C => 8, |
| 100 | + Mem::D | Mem::E => 16, |
38 | 101 | }
|
39 | 102 | } else {
|
40 | 103 | 0
|
41 | 104 | };
|
42 |
| - let ram = match mem_cfg_set { |
43 |
| - (true, ..) | (_, true, ..) | (_, _, true, ..) => 16, |
44 |
| - (.., true, _, _, _) if cfg!(feature = "svd-f373") => 24, |
45 |
| - (.., true, _, _, _) if cfg!(feature = "svd-f302") => 32, |
46 |
| - (.., _, true, _, _) if cfg!(feature = "svd-f373") => 32, |
47 |
| - (.., true, _, _, _) if cfg!(feature = "svd-f303") => 40, |
48 |
| - (.., _, true, _, _) if cfg!(feature = "svd-f302") => 40, |
49 |
| - (.., _, true, _, _) if cfg!(feature = "svd-f303") => 48, |
50 |
| - (.., _, _, true, _) | (.., _, _, _, true) if cfg!(feature = "svd-f302") => 64, |
51 |
| - (.., _, _, true, _) | (.., _, _, _, true) if cfg!(feature = "svd-f303") => 80, |
52 |
| - _ => unreachable!(), |
| 105 | + let ram = match mem { |
| 106 | + Mem::_4 | Mem::_6 | Mem::_8 => 16, |
| 107 | + Mem::B if cfg!(feature = "svd-f373") => 24, |
| 108 | + Mem::B if cfg!(feature = "svd-f302") => 32, |
| 109 | + Mem::C if cfg!(feature = "svd-f373") => 32, |
| 110 | + Mem::B if cfg!(feature = "svd-f303") => 40, |
| 111 | + Mem::C if cfg!(feature = "svd-f302") => 40, |
| 112 | + Mem::C if cfg!(feature = "svd-f303") => 48, |
| 113 | + Mem::D | Mem::E if cfg!(feature = "svd-f302") => 64, |
| 114 | + Mem::D | Mem::E if cfg!(feature = "svd-f303") => 80, |
| 115 | + _ => { |
| 116 | + eprintln!( |
| 117 | + "Memory size unknown. |
| 118 | +This may be due to incorrect feature configuration in Cargo.toml or stm32f3xx-hal's internal issue." |
| 119 | + ); |
| 120 | + std::process::exit(1); |
| 121 | + } |
53 | 122 | } - ccmram;
|
54 | 123 |
|
55 | 124 | let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
|
|
0 commit comments