From 575c747519c369ae940f36cd9355681e0f241464 Mon Sep 17 00:00:00 2001 From: Piroro-hs Date: Thu, 22 Apr 2021 01:52:00 +0900 Subject: [PATCH] Auto generate memory.x --- Cargo.toml | 81 ++++++++++++++++++++++++++++++------------------------ build.rs | 65 +++++++++++++++++++++++++++++++++++++++++++ memory.x | 9 ------ 3 files changed, 110 insertions(+), 45 deletions(-) create mode 100644 build.rs delete mode 100644 memory.x diff --git a/Cargo.toml b/Cargo.toml index d04b50326..b08c97940 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -66,6 +66,7 @@ default = ["unproven"] unproven = ["embedded-hal/unproven"] device-selected = [] direct-call-deprecated = [] +ld = [] rt = ["stm32f3/rt"] can = ["embedded-hal-can"] @@ -75,6 +76,14 @@ svd-f303 = ["stm32f3/stm32f303"] svd-f373 = ["stm32f3/stm32f373"] svd-f3x4 = ["stm32f3/stm32f3x4"] +mem-4 = [] +mem-6 = [] +mem-8 = [] +mem-b = [] +mem-c = [] +mem-d = [] +mem-e = [] + gpio-f302 = [] gpio-f303 = [] gpio-f303e = [] @@ -84,40 +93,40 @@ gpio-f373 = [] # Any changes here should be mirrored in README.md, src/lib.rs, and # .github/workflows/ci.yml. stm32f301 = ["svd-f301", "direct-call-deprecated"] -stm32f301x6 = ["stm32f301", "gpio-f302", "device-selected"] -stm32f301x8 = ["stm32f301", "gpio-f302", "device-selected"] +stm32f301x6 = ["stm32f301", "mem-6", "gpio-f302", "device-selected"] +stm32f301x8 = ["stm32f301", "mem-8", "gpio-f302", "device-selected"] stm32f318 = ["svd-f301", "direct-call-deprecated"] -stm32f318x8 = ["stm32f318", "gpio-f302", "device-selected"] +stm32f318x8 = ["stm32f318", "mem-8", "gpio-f302", "device-selected"] stm32f302 = ["svd-f302", "direct-call-deprecated"] -stm32f302x6 = ["stm32f302", "gpio-f302", "device-selected"] -stm32f302x8 = ["stm32f302", "gpio-f302", "device-selected"] -stm32f302xb = ["stm32f302", "gpio-f303", "device-selected"] -stm32f302xc = ["stm32f302", "gpio-f303", "device-selected"] -stm32f302xd = ["stm32f302", "gpio-f303e", "device-selected"] -stm32f302xe = ["stm32f302", "gpio-f303e", "device-selected"] +stm32f302x6 = ["stm32f302", "mem-6", "gpio-f302", "device-selected"] +stm32f302x8 = ["stm32f302", "mem-8", "gpio-f302", "device-selected"] +stm32f302xb = ["stm32f302", "mem-b", "gpio-f303", "device-selected"] +stm32f302xc = ["stm32f302", "mem-c", "gpio-f303", "device-selected"] +stm32f302xd = ["stm32f302", "mem-d", "gpio-f303e", "device-selected"] +stm32f302xe = ["stm32f302", "mem-e", "gpio-f303e", "device-selected"] stm32f303 = ["svd-f303", "direct-call-deprecated"] -stm32f303x6 = ["stm32f303", "gpio-f333", "device-selected"] -stm32f303x8 = ["stm32f303", "gpio-f333", "device-selected"] -stm32f303xb = ["stm32f303", "gpio-f303", "device-selected"] -stm32f303xc = ["stm32f303", "gpio-f303", "device-selected"] -stm32f303xd = ["stm32f303", "gpio-f303e", "device-selected"] -stm32f303xe = ["stm32f303", "gpio-f303e", "device-selected"] +stm32f303x6 = ["stm32f303", "mem-6", "gpio-f333", "device-selected"] +stm32f303x8 = ["stm32f303", "mem-8", "gpio-f333", "device-selected"] +stm32f303xb = ["stm32f303", "mem-b", "gpio-f303", "device-selected"] +stm32f303xc = ["stm32f303", "mem-c", "gpio-f303", "device-selected"] +stm32f303xd = ["stm32f303", "mem-d", "gpio-f303e", "device-selected"] +stm32f303xe = ["stm32f303", "mem-e", "gpio-f303e", "device-selected"] stm32f328 = ["svd-f303", "direct-call-deprecated"] -stm32f328x8 = ["stm32f328", "gpio-f333", "device-selected"] +stm32f328x8 = ["stm32f328", "mem-8", "gpio-f333", "device-selected"] stm32f358 = ["svd-f303", "direct-call-deprecated"] -stm32f358xc = ["stm32f358", "gpio-f303", "device-selected"] +stm32f358xc = ["stm32f358", "mem-c", "gpio-f303", "device-selected"] stm32f398 = ["svd-f303", "direct-call-deprecated"] -stm32f398xe = ["stm32f398", "gpio-f303e", "device-selected"] +stm32f398xe = ["stm32f398", "mem-e", "gpio-f303e", "device-selected"] stm32f373 = ["svd-f373", "direct-call-deprecated"] -stm32f373x8 = ["stm32f373", "gpio-f373", "device-selected"] -stm32f373xb = ["stm32f373", "gpio-f373", "device-selected"] -stm32f373xc = ["stm32f373", "gpio-f373", "device-selected"] +stm32f373x8 = ["stm32f373", "mem-8", "gpio-f373", "device-selected"] +stm32f373xb = ["stm32f373", "mem-b", "gpio-f373", "device-selected"] +stm32f373xc = ["stm32f373", "mem-c", "gpio-f373", "device-selected"] stm32f378 = ["svd-f373", "direct-call-deprecated"] -stm32f378xc = ["stm32f378", "gpio-f373", "device-selected"] +stm32f378xc = ["stm32f378", "mem-c", "gpio-f373", "device-selected"] stm32f334 = ["svd-f3x4", "direct-call-deprecated"] -stm32f334x4 = ["stm32f334", "gpio-f333", "device-selected"] -stm32f334x6 = ["stm32f334", "gpio-f333", "device-selected"] -stm32f334x8 = ["stm32f334", "gpio-f333", "device-selected"] +stm32f334x4 = ["stm32f334", "mem-4", "gpio-f333", "device-selected"] +stm32f334x6 = ["stm32f334", "mem-6", "gpio-f333", "device-selected"] +stm32f334x8 = ["stm32f334", "mem-8", "gpio-f333", "device-selected"] defmt-default = ["defmt"] defmt-trace = ["defmt"] @@ -137,45 +146,45 @@ opt-level = "s" [[example]] name = "pwm" -required-features = ["stm32f303xc"] +required-features = ["ld", "stm32f303xc"] [[example]] name = "toggle" -required-features = ["rt", "stm32f303xc"] +required-features = ["ld", "stm32f303xc"] [[example]] name = "usb_serial" -required-features = ["rt", "stm32f303xc", "stm32-usbd"] +required-features = ["ld", "stm32f303xc", "stm32-usbd"] [[example]] name = "spi" -required-features = ["stm32f303"] +required-features = ["ld", "stm32f303"] [[example]] name = "can" -required-features = ["rt", "can", "stm32f303"] +required-features = ["ld", "can", "stm32f303"] [[example]] name = "serial_dma" -required-features = ["stm32f303"] +required-features = ["ld", "stm32f303"] [[example]] name = "adc" -required-features = ["stm32f303"] +required-features = ["ld", "stm32f303"] [[example]] name = "i2c_scanner" -required-features = ["stm32f303xc"] +required-features = ["ld", "stm32f303xc"] [[example]] name = "gpio_erased" -required-features = ["rt", "stm32f303xc"] +required-features = ["ld", "stm32f303xc"] [[example]] name = "gpio_interrupts" -required-features = ["rt", "stm32f303xc"] +required-features = ["ld", "rt", "stm32f303xc"] [[test]] name = "rcc" -required-features = ["rt", "defmt"] +required-features = ["defmt"] harness = false diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..1cb4d54e3 --- /dev/null +++ b/build.rs @@ -0,0 +1,65 @@ +use std::{env, fs::File, io::prelude::*, path::PathBuf}; + +fn main() { + if cfg!(feature = "ld") { + gen_memory_x(); + } + println!("cargo:rerun-if-changed=build.rs"); +} + +fn gen_memory_x() { + #![allow(clippy::unneeded_wildcard_pattern)] + + let mem_cfg_set = ( + cfg!(feature = "mem-4"), + cfg!(feature = "mem-6"), + cfg!(feature = "mem-8"), + cfg!(feature = "mem-b"), + cfg!(feature = "mem-c"), + cfg!(feature = "mem-d"), + cfg!(feature = "mem-e"), + ); + let flash = match mem_cfg_set { + (true, ..) => 16, + (_, true, ..) => 32, + (_, _, true, ..) => 64, + (.., true, _, _, _) => 128, + (.., _, true, _, _) => 256, + (.., _, _, true, _) => 384, + (.., _, _, _, true) => 512, + _ => unreachable!(), + }; + let ccmram = if cfg!(feature = "svd-f303") || cfg!(feature = "svd-f3x4") { + match mem_cfg_set { + (true, ..) | (_, true, ..) | (_, _, true, ..) => 4, + (.., true, _, _, _) | (.., _, true, _, _) => 8, + (.., _, _, true, _) | (.., _, _, _, true) => 16, + _ => unreachable!(), + } + } else { + 0 + }; + let ram = match mem_cfg_set { + (true, ..) | (_, true, ..) | (_, _, true, ..) => 16, + (.., true, _, _, _) if cfg!(feature = "svd-f373") => 24, + (.., true, _, _, _) if cfg!(feature = "svd-f302") => 32, + (.., _, true, _, _) if cfg!(feature = "svd-f373") => 32, + (.., true, _, _, _) if cfg!(feature = "svd-f303") => 40, + (.., _, true, _, _) if cfg!(feature = "svd-f302") => 40, + (.., _, true, _, _) if cfg!(feature = "svd-f303") => 48, + (.., _, _, true, _) | (.., _, _, _, true) if cfg!(feature = "svd-f302") => 64, + (.., _, _, true, _) | (.., _, _, _, true) if cfg!(feature = "svd-f303") => 80, + _ => unreachable!(), + } - ccmram; + + let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + let mut file = File::create(out_dir.join("memory.x")).unwrap(); + writeln!(file, "MEMORY {{").unwrap(); + writeln!(file, " FLASH (rx) : o = 0x8000000, l = {}K", flash).unwrap(); + if ccmram > 0 { + writeln!(file, " CCMRAM (rwx) : o = 0x10000000, l = {}K", ccmram).unwrap(); + } + writeln!(file, " RAM (rwx) : o = 0x20000000, l = {}K", ram).unwrap(); + writeln!(file, "}}").unwrap(); + println!("cargo:rustc-link-search={}", out_dir.display()); +} diff --git a/memory.x b/memory.x deleted file mode 100644 index 1362d6b30..000000000 --- a/memory.x +++ /dev/null @@ -1,9 +0,0 @@ -/* Linker script for the STM32F303VCT6 */ -MEMORY -{ - CCRAM : ORIGIN = 0x10000000, LENGTH = 8K - FLASH : ORIGIN = 0x08000000, LENGTH = 256K - RAM : ORIGIN = 0x20000000, LENGTH = 40K -} - -_stack_start = ORIGIN(CCRAM) + LENGTH(CCRAM);