Skip to content

Commit 3c4c5f1

Browse files
no111u3mvertescher
authored andcommittedJul 6, 2020
Add correct memory sizes selection for linker script
1 parent 471ca4d commit 3c4c5f1

File tree

6 files changed

+195
-1
lines changed

6 files changed

+195
-1
lines changed
 

‎build.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
use std::{
2+
env,
3+
fs::File,
4+
io::{self, prelude::*},
5+
path::PathBuf,
6+
};
7+
8+
fn main() -> Result<(), Error> {
9+
let target = Target::read();
10+
11+
copy_memory_config(target)?;
12+
13+
println!("cargo:rerun-if-changed=build.rs");
14+
15+
Ok(())
16+
}
17+
18+
/// Make `memory.x` available to dependent crates
19+
fn copy_memory_config(target: Target) -> Result<(), Error> {
20+
let memory_x = match target.sub_family {
21+
SubFamily::Stm32f722
22+
| SubFamily::Stm32f723
23+
| SubFamily::Stm32f732
24+
| SubFamily::Stm32f733 => include_bytes!("memory_512_176.x").as_ref(),
25+
SubFamily::Stm32f745 | SubFamily::Stm32f746 | SubFamily::Stm32f756 => {
26+
include_bytes!("memory_1024_240.x").as_ref()
27+
}
28+
SubFamily::Stm32f765
29+
| SubFamily::Stm32f767
30+
| SubFamily::Stm32f769
31+
| SubFamily::Stm32f777
32+
| SubFamily::Stm32f778
33+
| SubFamily::Stm32f779 => include_bytes!("memory_2048_368.x").as_ref(),
34+
SubFamily::Stm32f730 => include_bytes!("memory_64_176.x").as_ref(),
35+
};
36+
37+
let out_dir = env::var("OUT_DIR")?;
38+
let out_dir = PathBuf::from(out_dir);
39+
40+
File::create(out_dir.join("memory.x"))?.write_all(memory_x)?;
41+
42+
// Tell Cargo where to find the file.
43+
println!("cargo:rustc-link-search={}", out_dir.display());
44+
45+
println!("cargo:rerun-if-changed=memory_1024_240.x");
46+
println!("cargo:rerun-if-changed=memory_2048_368.x");
47+
println!("cargo:rerun-if-changed=memory_512_176.x");
48+
println!("cargo:rerun-if-changed=memory_64_176.x");
49+
println!("cargo:rerun-if-changed=memory_64_240.x");
50+
51+
Ok(())
52+
}
53+
54+
#[derive(Clone, Copy)]
55+
struct Target {
56+
sub_family: SubFamily,
57+
}
58+
59+
impl Target {
60+
fn read() -> Self {
61+
let sub_family = SubFamily::read();
62+
63+
Self { sub_family }
64+
}
65+
}
66+
67+
#[derive(Clone, Copy)]
68+
enum SubFamily {
69+
Stm32f722,
70+
Stm32f723,
71+
Stm32f730,
72+
Stm32f732,
73+
Stm32f733,
74+
Stm32f745,
75+
Stm32f746,
76+
Stm32f756,
77+
Stm32f765,
78+
Stm32f767,
79+
Stm32f769,
80+
Stm32f777,
81+
Stm32f778,
82+
Stm32f779,
83+
}
84+
85+
impl SubFamily {
86+
fn read() -> Self {
87+
if cfg!(feature = "stm32f722") {
88+
SubFamily::Stm32f722
89+
} else if cfg!(feature = "stm32f723") {
90+
SubFamily::Stm32f723
91+
} else if cfg!(feature = "stm32f730") {
92+
SubFamily::Stm32f730
93+
} else if cfg!(feature = "stm32f732") {
94+
SubFamily::Stm32f732
95+
} else if cfg!(feature = "stm32f733") {
96+
SubFamily::Stm32f733
97+
} else if cfg!(feature = "stm32f745") {
98+
SubFamily::Stm32f745
99+
} else if cfg!(feature = "stm32f746") {
100+
SubFamily::Stm32f746
101+
} else if cfg!(feature = "stm32f756") {
102+
SubFamily::Stm32f756
103+
} else if cfg!(feature = "stm32f765") {
104+
SubFamily::Stm32f765
105+
} else if cfg!(feature = "stm32f767") {
106+
SubFamily::Stm32f767
107+
} else if cfg!(feature = "stm32f769") {
108+
SubFamily::Stm32f769
109+
} else if cfg!(feature = "stm32f777") {
110+
SubFamily::Stm32f777
111+
} else if cfg!(feature = "stm32f778") {
112+
SubFamily::Stm32f778
113+
} else if cfg!(feature = "stm32f779") {
114+
SubFamily::Stm32f779
115+
} else {
116+
error("You must select a target.
117+
If you added Stm32f7xx HAL as a dependency to your crate, you can select a target by enabling the respective feature in `Cargo.toml`.
118+
If you're running an example from the repository, select a target by passing the desired target as a command-line argument, for example `--features=stm32f746`.
119+
Please refer to the documentation for more details."
120+
)
121+
}
122+
}
123+
}
124+
125+
#[derive(Debug)]
126+
enum Error {
127+
Env(env::VarError),
128+
Io(io::Error),
129+
}
130+
131+
impl From<env::VarError> for Error {
132+
fn from(error: env::VarError) -> Self {
133+
Self::Env(error)
134+
}
135+
}
136+
137+
impl From<io::Error> for Error {
138+
fn from(error: io::Error) -> Self {
139+
Self::Io(error)
140+
}
141+
}
142+
143+
fn error(message: &str) -> ! {
144+
panic!("\n\n\n{}\n\n\n", message);
145+
}

‎memory.x renamed to ‎memory_1024_240.x

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
/* For STM32F745,446,756 devices */
12
MEMORY
23
{
34
/* NOTE K = KiBi = 1024 bytes */
45
FLASH : ORIGIN = 0x08000000, LENGTH = 1M
5-
RAM : ORIGIN = 0x20000000, LENGTH = 128K
6+
RAM : ORIGIN = 0x20000000, LENGTH = 240K + 16K
67
}
78

89
/* This is where the call stack will be allocated. */

‎memory_2048_368.x

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* For STM32F765,767,768,769,777,778,779 devices */
2+
MEMORY
3+
{
4+
/* NOTE K = KiBi = 1024 bytes */
5+
FLASH : ORIGIN = 0x08000000, LENGTH = 2M
6+
RAM : ORIGIN = 0x20000000, LENGTH = 368K + 16K
7+
}
8+
9+
/* This is where the call stack will be allocated. */
10+
/* The stack is of the full descending type. */
11+
/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */
12+
_stack_start = ORIGIN(RAM) + LENGTH(RAM);

‎memory_512_176.x

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* For STM32F722,723,732,733 devices */
2+
MEMORY
3+
{
4+
/* NOTE K = KiBi = 1024 bytes */
5+
FLASH : ORIGIN = 0x08000000, LENGTH = 512K
6+
RAM : ORIGIN = 0x20000000, LENGTH = 176K + 16K
7+
}
8+
9+
/* This is where the call stack will be allocated. */
10+
/* The stack is of the full descending type. */
11+
/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */
12+
_stack_start = ORIGIN(RAM) + LENGTH(RAM);

‎memory_64_176.x

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* For STM32F730 devices */
2+
MEMORY
3+
{
4+
/* NOTE K = KiBi = 1024 bytes */
5+
FLASH : ORIGIN = 0x08000000, LENGTH = 64k
6+
RAM : ORIGIN = 0x20000000, LENGTH = 176K + 16K
7+
}
8+
9+
/* This is where the call stack will be allocated. */
10+
/* The stack is of the full descending type. */
11+
/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */
12+
_stack_start = ORIGIN(RAM) + LENGTH(RAM);

‎memory_64_240.x

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* For STM32F750 devices */
2+
MEMORY
3+
{
4+
/* NOTE K = KiBi = 1024 bytes */
5+
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
6+
RAM : ORIGIN = 0x20000000, LENGTH = 240K + 16K
7+
}
8+
9+
/* This is where the call stack will be allocated. */
10+
/* The stack is of the full descending type. */
11+
/* NOTE Do NOT modify `_stack_start` unless you know what you are doing */
12+
_stack_start = ORIGIN(RAM) + LENGTH(RAM);

0 commit comments

Comments
 (0)
Please sign in to comment.