aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-08-02 19:54:41 +0200
committerGitHub <[email protected]>2021-08-02 19:54:41 +0200
commitde207764aee0dd9c23bd02f92b55a55babd47b1a (patch)
tree3562c82c26c339d6f96c3ec59b98a01f8083de64
parentc458ad52e698ae0e9810e90d094394bd3ab35c77 (diff)
parent63b32b39e10fb0677d0a980930dc83ee792cd5ca (diff)
Merge pull request #332 from bobmcwhirter/memory_x_gen
Emit a default memory.x alongside device.x from metapac.
-rw-r--r--embassy-stm32/Cargo.toml1
-rw-r--r--embassy-stm32/src/lib.rs1
-rw-r--r--examples/stm32f4/Cargo.toml2
-rw-r--r--examples/stm32f4/build.rs31
-rw-r--r--examples/stm32f4/memory.x7
-rw-r--r--examples/stm32h7/Cargo.toml2
-rw-r--r--examples/stm32h7/build.rs21
-rw-r--r--examples/stm32h7/memory.x5
m---------stm32-data0
-rw-r--r--stm32-metapac-gen/src/assets/build.rs3
-rw-r--r--stm32-metapac-gen/src/lib.rs41
-rw-r--r--stm32-metapac/Cargo.toml1
-rw-r--r--stm32-metapac/build.rs8
13 files changed, 55 insertions, 68 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index d94c9d523..6f8691c3e 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -42,6 +42,7 @@ defmt-warn = [ ]
42defmt-error = [ ] 42defmt-error = [ ]
43sdmmc-rs = ["embedded-sdmmc"] 43sdmmc-rs = ["embedded-sdmmc"]
44net = ["embassy-net", "vcell"] 44net = ["embassy-net", "vcell"]
45memory-x = ["stm32-metapac/memory-x"]
45 46
46# Reexport stm32-metapac at `embassy_stm32::pac`. 47# Reexport stm32-metapac at `embassy_stm32::pac`.
47# This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version. 48# This is unstable because semver-minor (non-breaking) releases of embassy-stm32 may major-bump (breaking) the stm32-metapac version.
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs
index 89fd86448..0567aa72a 100644
--- a/embassy-stm32/src/lib.rs
+++ b/embassy-stm32/src/lib.rs
@@ -39,6 +39,7 @@ pub mod eth;
39pub mod exti; 39pub mod exti;
40#[cfg(i2c)] 40#[cfg(i2c)]
41pub mod i2c; 41pub mod i2c;
42
42#[cfg(pwr)] 43#[cfg(pwr)]
43pub mod pwr; 44pub mod pwr;
44#[cfg(rng)] 45#[cfg(rng)]
diff --git a/examples/stm32f4/Cargo.toml b/examples/stm32f4/Cargo.toml
index 693dac545..84b1e30ee 100644
--- a/examples/stm32f4/Cargo.toml
+++ b/examples/stm32f4/Cargo.toml
@@ -19,7 +19,7 @@ defmt-error = []
19[dependencies] 19[dependencies]
20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } 20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } 21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f429zi", "unstable-pac"] } 22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32f429zi", "unstable-pac", "memory-x"] }
23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } 23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" }
24 24
25defmt = "0.2.0" 25defmt = "0.2.0"
diff --git a/examples/stm32f4/build.rs b/examples/stm32f4/build.rs
deleted file mode 100644
index d534cc3df..000000000
--- a/examples/stm32f4/build.rs
+++ /dev/null
@@ -1,31 +0,0 @@
1//! This build script copies the `memory.x` file from the crate root into
2//! a directory where the linker can always find it at build time.
3//! For many projects this is optional, as the linker always searches the
4//! project root directory -- wherever `Cargo.toml` is. However, if you
5//! are using a workspace or have a more complicated build setup, this
6//! build script becomes required. Additionally, by requesting that
7//! Cargo re-run the build script whenever `memory.x` is changed,
8//! updating `memory.x` ensures a rebuild of the application with the
9//! new memory settings.
10
11use std::env;
12use std::fs::File;
13use std::io::Write;
14use std::path::PathBuf;
15
16fn main() {
17 // Put `memory.x` in our output directory and ensure it's
18 // on the linker search path.
19 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
20 File::create(out.join("memory.x"))
21 .unwrap()
22 .write_all(include_bytes!("memory.x"))
23 .unwrap();
24 println!("cargo:rustc-link-search={}", out.display());
25
26 // By default, Cargo will re-run a build script whenever
27 // any file in the project changes. By specifying `memory.x`
28 // here, we ensure the build script is only re-run when
29 // `memory.x` is changed.
30 println!("cargo:rerun-if-changed=memory.x");
31}
diff --git a/examples/stm32f4/memory.x b/examples/stm32f4/memory.x
deleted file mode 100644
index f21e32572..000000000
--- a/examples/stm32f4/memory.x
+++ /dev/null
@@ -1,7 +0,0 @@
1MEMORY
2{
3 /* NOTE 1 K = 1 KiBi = 1024 bytes */
4 /* These values correspond to the STM32F429ZI */
5 FLASH : ORIGIN = 0x08000000, LENGTH = 2048K
6 RAM : ORIGIN = 0x20000000, LENGTH = 192K
7}
diff --git a/examples/stm32h7/Cargo.toml b/examples/stm32h7/Cargo.toml
index 278ab6f6b..8bc6781e0 100644
--- a/examples/stm32h7/Cargo.toml
+++ b/examples/stm32h7/Cargo.toml
@@ -19,7 +19,7 @@ defmt-error = []
19[dependencies] 19[dependencies]
20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } 20embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] }
21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } 21embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] }
22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi", "net"] } 22embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32h743zi", "net", "memory-x"] }
23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } 23embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" }
24embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] } 24embassy-net = { path = "../../embassy-net", default-features = false, features = ["defmt-debug", "defmt", "tcp", "medium-ethernet", "pool-16"] }
25stm32-metapac = { path = "../../stm32-metapac", features = ["stm32h743zi"] } 25stm32-metapac = { path = "../../stm32-metapac", features = ["stm32h743zi"] }
diff --git a/examples/stm32h7/build.rs b/examples/stm32h7/build.rs
deleted file mode 100644
index 555cdf687..000000000
--- a/examples/stm32h7/build.rs
+++ /dev/null
@@ -1,21 +0,0 @@
1use std::env;
2use std::fs::File;
3use std::io::Write;
4use std::path::PathBuf;
5
6fn main() {
7 // Put `memory.x` in our output directory and ensure it's
8 // on the linker search path.
9 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
10 File::create(out.join("memory.x"))
11 .unwrap()
12 .write_all(include_bytes!("memory.x"))
13 .unwrap();
14 println!("cargo:rustc-link-search={}", out.display());
15
16 // By default, Cargo will re-run a build script whenever
17 // any file in the project changes. By specifying `memory.x`
18 // here, we ensure the build script is only re-run when
19 // `memory.x` is changed.
20 println!("cargo:rerun-if-changed=memory.x");
21}
diff --git a/examples/stm32h7/memory.x b/examples/stm32h7/memory.x
deleted file mode 100644
index ef9485d12..000000000
--- a/examples/stm32h7/memory.x
+++ /dev/null
@@ -1,5 +0,0 @@
1MEMORY
2{
3 FLASH : ORIGIN = 0x08000000, LENGTH = 2048K
4 RAM : ORIGIN = 0x24000000, LENGTH = 384K
5}
diff --git a/stm32-data b/stm32-data
Subproject 0ad27b2fd1126c6c9d9f4602d1331f5d82f4aa2 Subproject af0611ff9b29f2c0ab675a4f0e51d8241a4097f
diff --git a/stm32-metapac-gen/src/assets/build.rs b/stm32-metapac-gen/src/assets/build.rs
index c16bd6407..4110fe485 100644
--- a/stm32-metapac-gen/src/assets/build.rs
+++ b/stm32-metapac-gen/src/assets/build.rs
@@ -9,6 +9,9 @@ fn main() {
9 .unwrap() 9 .unwrap()
10 .to_ascii_lowercase(); 10 .to_ascii_lowercase();
11 11
12 #[cfg(feature = "memory-x")]
13 println!("cargo:rustc-link-search=src/chips/{}/memory_x/", _chip_name);
14
12 #[cfg(feature = "rt")] 15 #[cfg(feature = "rt")]
13 println!("cargo:rustc-link-search=src/chips/{}", _chip_name); 16 println!("cargo:rustc-link-search=src/chips/{}", _chip_name);
14 17
diff --git a/stm32-metapac-gen/src/lib.rs b/stm32-metapac-gen/src/lib.rs
index f508b3658..5e0cc58d4 100644
--- a/stm32-metapac-gen/src/lib.rs
+++ b/stm32-metapac-gen/src/lib.rs
@@ -19,12 +19,24 @@ pub struct Chip {
19 pub family: String, 19 pub family: String,
20 pub line: String, 20 pub line: String,
21 pub cores: Vec<Core>, 21 pub cores: Vec<Core>,
22 pub flash: u32, 22 pub flash: Memory,
23 pub ram: u32, 23 pub ram: Memory,
24 pub packages: Vec<Package>, 24 pub packages: Vec<Package>,
25} 25}
26 26
27#[derive(Debug, Eq, PartialEq, Clone, Deserialize)] 27#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
28pub struct Memory {
29 pub bytes: u32,
30 pub regions: HashMap<String, MemoryRegion>,
31}
32
33#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
34pub struct MemoryRegion {
35 pub base: u32,
36 pub bytes: Option<u32>,
37}
38
39#[derive(Debug, Eq, PartialEq, Clone, Deserialize)]
28pub struct Core { 40pub struct Core {
29 pub name: String, 41 pub name: String,
30 pub peripherals: HashMap<String, Peripheral>, 42 pub peripherals: HashMap<String, Peripheral>,
@@ -636,6 +648,9 @@ pub fn gen(options: Options) {
636 .unwrap() 648 .unwrap()
637 .write_all(device_x.as_bytes()) 649 .write_all(device_x.as_bytes())
638 .unwrap(); 650 .unwrap();
651
652 // generate default memory.x
653 gen_memory_x(&chip_dir, &chip);
639 } 654 }
640 655
641 for (module, version) in all_peripheral_versions { 656 for (module, version) in all_peripheral_versions {
@@ -674,6 +689,7 @@ pub fn gen(options: Options) {
674 let re = Regex::new("# *! *\\[.*\\]").unwrap(); 689 let re = Regex::new("# *! *\\[.*\\]").unwrap();
675 let data = re.replace_all(&data, ""); 690 let data = re.replace_all(&data, "");
676 file.write_all(data.as_bytes()).unwrap(); 691 file.write_all(data.as_bytes()).unwrap();
692
677 } 693 }
678 694
679 // Generate src/lib_inner.rs 695 // Generate src/lib_inner.rs
@@ -734,6 +750,7 @@ pub fn gen(options: Options) {
734 750
735 // Generate build.rs 751 // Generate build.rs
736 fs::write(out_dir.join("build.rs"), include_bytes!("assets/build.rs")).unwrap(); 752 fs::write(out_dir.join("build.rs"), include_bytes!("assets/build.rs")).unwrap();
753
737} 754}
738 755
739fn bytes_find(haystack: &[u8], needle: &[u8]) -> Option<usize> { 756fn bytes_find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
@@ -741,3 +758,23 @@ fn bytes_find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
741 .windows(needle.len()) 758 .windows(needle.len())
742 .position(|window| window == needle) 759 .position(|window| window == needle)
743} 760}
761
762fn gen_memory_x(out_dir: &PathBuf, chip: &Chip) {
763 let mut memory_x = String::new();
764
765 let flash_bytes = chip.flash.regions.get("BANK_1").unwrap().bytes.unwrap_or(chip.flash.bytes);
766 let flash_origin = chip.flash.regions.get("BANK_1").unwrap().base;
767
768 let ram_bytes = chip.ram.regions.get("SRAM").unwrap().bytes.unwrap_or(chip.ram.bytes);
769 let ram_origin = chip.ram.regions.get("SRAM").unwrap().base;
770
771 write!(memory_x, "MEMORY\n{{\n").unwrap();
772 write!(memory_x, " FLASH : ORIGIN = 0x{:x}, LENGTH = {}\n", flash_origin, flash_bytes).unwrap();
773 write!(memory_x, " RAM : ORIGIN = 0x{:x}, LENGTH = {}\n", ram_origin, ram_bytes).unwrap();
774 write!(memory_x, "}}").unwrap();
775
776 fs::create_dir_all(out_dir.join("memory_x")).unwrap();
777 let mut file = File::create(out_dir.join("memory_x").join("memory.x")).unwrap();
778 file.write_all( memory_x.as_bytes() ).unwrap();
779
780}
diff --git a/stm32-metapac/Cargo.toml b/stm32-metapac/Cargo.toml
index 2e01d5183..fabd95664 100644
--- a/stm32-metapac/Cargo.toml
+++ b/stm32-metapac/Cargo.toml
@@ -17,6 +17,7 @@ regex = "1.5.4"
17 17
18[features] 18[features]
19rt = ["cortex-m-rt/device"] 19rt = ["cortex-m-rt/device"]
20memory-x = []
20 21
21# BEGIN GENERATED FEATURES 22# BEGIN GENERATED FEATURES
22# Generated by gen_features.py. DO NOT EDIT. 23# Generated by gen_features.py. DO NOT EDIT.
diff --git a/stm32-metapac/build.rs b/stm32-metapac/build.rs
index b44d98f3a..ca964c9ec 100644
--- a/stm32-metapac/build.rs
+++ b/stm32-metapac/build.rs
@@ -27,5 +27,13 @@ fn main() {
27 out_dir.display(), 27 out_dir.display(),
28 chip_name.to_ascii_lowercase() 28 chip_name.to_ascii_lowercase()
29 ); 29 );
30
31 #[cfg(feature = "memory-x")]
32 println!(
33 "cargo:rustc-link-search={}/src/chips/{}/memory_x/",
34 out_dir.display(),
35 chip_name.to_ascii_lowercase()
36 );
37
30 println!("cargo:rerun-if-changed=build.rs"); 38 println!("cargo:rerun-if-changed=build.rs");
31} 39}