Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ This script does the following steps for you:
- create a TAB (tock application bundle)
- if you have a J-Link compatible board connected: flash this TAB to your board (using tockloader)

## Configuration of Memory Sizes

The configuration of different memory parameters can be configured using
environment variables:

- `APP_HEAP_SIZE` sets the size of the app heap and defaults to 1024.
- `APP_STACK_SIZE` sets the size of the app stack and default to 2048.
- `KERNEL_HEAP_SIZE` sets the size of the kernel heap and default to 1024.

## License

Expand Down
2 changes: 1 addition & 1 deletion boards/layout_hail.ld → boards/layout_hail.ld.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MEMORY {
* Any change to STACK_SIZE should be accompanied by a corresponding change to
* `elf2tab`'s `--stack` option
*/
STACK_SIZE = 2048;
STACK_SIZE = {{stack_size}};

MPU_MIN_ALIGN = 8K;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ MEMORY {
* Any change to STACK_SIZE should be accompanied by a corresponding change to
* `elf2tab`'s `--stack` option
*/
STACK_SIZE = 2048;
STACK_SIZE = {{stack_size}};

MPU_MIN_ALIGN = 1K;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MEMORY {
* Any change to STACK_SIZE should be accompanied by a corresponding change to
* `elf2tab`'s `--stack` option
*/
STACK_SIZE = 2048;
STACK_SIZE = {{stack_size}};

MPU_MIN_ALIGN = 8K;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ MEMORY {
* Any change to STACK_SIZE should be accompanied by a corresponding change to
* `elf2tab`'s `--stack` option
*/
STACK_SIZE = 2048;
STACK_SIZE = {{stack_size}};

MPU_MIN_ALIGN = 8K;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ MEMORY {
* Any change to STACK_SIZE should be accompanied by a corresponding change to
* `elf2tab`'s `--stack` option
*/
STACK_SIZE = 2048;
STACK_SIZE = {{stack_size}};

MPU_MIN_ALIGN = 1K;

Expand Down
47 changes: 41 additions & 6 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::env;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufRead;
use std::io::BufReader;
use std::path::Path;
use std::process;

static LAYOUT_FILE_NAME: &str = "layout.ld";
static APP_STACK_SIZE: &str = "APP_STACK_SIZE";
static DEFAULT_STACK_SIZE_BYTES: &str = "2048";

fn main() {
static PLATFORM_ENV_VAR: &str = "PLATFORM";
Expand All @@ -16,15 +19,28 @@ fn main() {

println!("cargo:rerun-if-env-changed={}", PLATFORM_ENV_VAR);
println!("cargo:rerun-if-env-changed={}", APP_HEAP_SIZE);
println!("cargo:rerun-if-env-changed={}", APP_STACK_SIZE);
println!("cargo:rerun-if-env-changed={}", KERNEL_HEAP_SIZE);
println!("cargo:rerun-if-changed={}", PLATFORM_FILE_NAME);
println!("cargo:rerun-if-changed={}", LAYOUT_FILE_NAME);

let stack_size = read_env_var(APP_STACK_SIZE);
if stack_size.is_none() {
println!(
"cargo:warning=No explicit stack size specified. \
Using default of 2048 bytes. \
Set this explicitly using the \
environment variable {}.
",
APP_STACK_SIZE
);
}

let platform_name =
read_env_var(PLATFORM_ENV_VAR).or_else(|| read_board_name_from_file(PLATFORM_FILE_NAME));
if let Some(platform_name) = platform_name {
println!("cargo:rustc-env={}={}", PLATFORM_ENV_VAR, platform_name);
copy_linker_file(platform_name.trim());
create_linker_file(platform_name.trim(), stack_size);
} else {
println!(
"cargo:warning=No platform specified. \
Expand All @@ -34,13 +50,14 @@ fn main() {

set_default_env(APP_HEAP_SIZE, "1024");
set_default_env(KERNEL_HEAP_SIZE, "1024");
set_default_env(APP_STACK_SIZE, DEFAULT_STACK_SIZE_BYTES);
}

fn set_default_env(env_var: &str, default: &str) {
if let Some(s) = read_env_var(env_var) {
println!("cargo:rustc-env={}={}", env_var, s);
} else {
// Just use a default of 1024 if nothing is passed in
// Use the provided default value if nothing is passed in
println!("cargo:rustc-env={}={}", env_var, default);
}
}
Expand All @@ -63,12 +80,30 @@ fn read_board_name_from_file(file_name: &str) -> Option<String> {
Some(board_name)
}

fn copy_linker_file(platform_name: &str) {
let linker_file_name = format!("boards/layout_{}.ld", platform_name);
fn create_linker_file(platform_name: &str, stack_size: Option<String>) {
let linker_file_name = format!("boards/layout_{}.ld.handlebars", platform_name);

let path = Path::new(&linker_file_name);
if !path.exists() {
println!("Cannot find layout file {:?}", path);
println!("Cannot find layout template file {:?}", path);
process::exit(1);
}
fs::copy(linker_file_name, LAYOUT_FILE_NAME).unwrap();

create_linker_file_from_template(
path,
&stack_size.unwrap_or_else(|| DEFAULT_STACK_SIZE_BYTES.to_string()),
)
}

fn create_linker_file_from_template(path: &Path, stack_size: &str) {
let mut linker_template = File::open(path).expect("Failed to open file.");
let mut template_content = String::new();
linker_template
.read_to_string(&mut template_content)
.expect("Could not read linke file to string.");

let processed_template = template_content.replace("{{stack_size}}", &stack_size);

let out_path = Path::new(LAYOUT_FILE_NAME);
fs::write(out_path, processed_template).expect("Failed to write linker file.");
}
2 changes: 1 addition & 1 deletion tools/flash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ tab_file_name="${libtock_target_path}.tab"
mkdir -p "${libtock_target_path}"
cp "$1" "${elf_file_name}"

elf2tab -n "${artifact}" -o "${tab_file_name}" "${elf_file_name}" --stack 2048 --app-heap $APP_HEAP_SIZE --kernel-heap $KERNEL_HEAP_SIZE --protected-region-size=64
elf2tab -n "${artifact}" -o "${tab_file_name}" "${elf_file_name}" --stack $APP_STACK_SIZE --app-heap $APP_HEAP_SIZE --kernel-heap $KERNEL_HEAP_SIZE --protected-region-size=64

if [ $tockload == "n" ]; then
echo "Skipping flashing for platform \"${PLATFORM}\""
Expand Down