Skip to content

Commit 180b337

Browse files
committed
Build two binaries
0 parents  commit 180b337

20 files changed

+9246
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/result*
2+

app/.cargo/config.toml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[target.thumbv7m-none-eabi]
2+
# uncomment this to make `cargo run` execute programs on QEMU
3+
# runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
4+
5+
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
6+
# uncomment ONE of these three option to make `cargo run` start a GDB session
7+
# which option to pick depends on your system
8+
# runner = "arm-none-eabi-gdb -q -x openocd.gdb"
9+
# runner = "gdb-multiarch -q -x openocd.gdb"
10+
# runner = "gdb -q -x openocd.gdb"
11+
12+
rustflags = [
13+
# This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x
14+
# See https://github.com/rust-embedded/cortex-m-quickstart/pull/95
15+
"-C", "link-arg=--nmagic",
16+
17+
# LLD (shipped with the Rust toolchain) is used as the default linker
18+
"-C", "link-arg=-Tlink.x",
19+
20+
# if you run into problems with LLD switch to the GNU linker by commenting out
21+
# this line
22+
"-C", "linker=lld",
23+
24+
# if you need to link to pre-compiled C libraries provided by a C toolchain
25+
# use GCC as the linker by commenting out both lines above and then
26+
# uncommenting the three lines below
27+
# "-C", "linker=arm-none-eabi-gcc",
28+
# "-C", "link-arg=-Wl,-Tlink.x",
29+
# "-C", "link-arg=-nostartfiles",
30+
]
31+
32+
[build]
33+
# Pick ONE of these compilation targets
34+
# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
35+
target = "thumbv7m-none-eabi" # Cortex-M3
36+
# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
37+
# target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
38+
# target = "thumbv8m.base-none-eabi" # Cortex-M23
39+
# target = "thumbv8m.main-none-eabi" # Cortex-M33 (no FPU)
40+
# target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)

app/.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
**/*.rs.bk
2+
.#*
3+
.gdb_history
4+
target/
5+
6+
# editor files
7+
.vscode/*
8+
!.vscode/*.md
9+
!.vscode/*.svd
10+
!.vscode/launch.json
11+
!.vscode/tasks.json
12+
!.vscode/extensions.json

app/Cargo.lock

+180
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Cargo.toml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[package]
2+
authors = ["Alex Martens <alex@thinglab.org>"]
3+
edition = "2018"
4+
readme = "README.md"
5+
name = "app"
6+
version = "0.1.0"
7+
8+
[dependencies]
9+
cortex-m = "0.7"
10+
cortex-m-rt = "0.7"
11+
cortex-m-semihosting = "0.5.0"
12+
panic-halt = "0.2.0"
13+
14+
# Uncomment for the panic example.
15+
# panic-itm = "0.4.1"
16+
17+
# Uncomment for the allocator example.
18+
# alloc-cortex-m = "0.4.0"
19+
20+
# Uncomment for the device example.
21+
# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`,
22+
# and then use `cargo build --examples device` to build it.
23+
# [dependencies.stm32f3]
24+
# features = ["stm32f303", "rt"]
25+
# version = "0.7.1"
26+
27+
# this lets you use `cargo fix`!
28+
[[bin]]
29+
name = "app"
30+
test = false
31+
bench = false
32+
33+
[profile.release]
34+
codegen-units = 1 # better optimizations
35+
debug = true # symbols are nice and they don't increase the size on Flash
36+
lto = true # better optimizations

app/build.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
11+
use std::env;
12+
use std::fs::File;
13+
use std::io::Write;
14+
use std::path::PathBuf;
15+
16+
fn 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+
}

app/default.nix

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{ lib
2+
, rustPlatform
3+
, lld
4+
}:
5+
6+
let
7+
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
8+
in
9+
rustPlatform.buildRustPackage {
10+
inherit (cargoToml.package) version;
11+
pname = cargoToml.package.name;
12+
13+
src = ./.;
14+
15+
cargoLock.lockFile = ./Cargo.lock;
16+
17+
nativeBuildInputs = [
18+
lld
19+
];
20+
21+
# no tests for no_std
22+
doCheck = false;
23+
}

app/examples/allocator.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! How to use the heap and a dynamic memory allocator
2+
//!
3+
//! This example depends on the alloc-cortex-m crate so you'll have to add it to your Cargo.toml:
4+
//!
5+
//! ``` text
6+
//! # or edit the Cargo.toml file manually
7+
//! $ cargo add alloc-cortex-m
8+
//! ```
9+
//!
10+
//! ---
11+
12+
#![feature(alloc_error_handler)]
13+
#![no_main]
14+
#![no_std]
15+
16+
extern crate alloc;
17+
use panic_halt as _;
18+
19+
use self::alloc::vec;
20+
use core::alloc::Layout;
21+
22+
use alloc_cortex_m::CortexMHeap;
23+
use cortex_m::asm;
24+
use cortex_m_rt::entry;
25+
use cortex_m_semihosting::{hprintln, debug};
26+
27+
// this is the allocator the application will use
28+
#[global_allocator]
29+
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
30+
31+
const HEAP_SIZE: usize = 1024; // in bytes
32+
33+
#[entry]
34+
fn main() -> ! {
35+
// Initialize the allocator BEFORE you use it
36+
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
37+
38+
// Growable array allocated on the heap
39+
let xs = vec![0, 1, 2];
40+
41+
hprintln!("{:?}", xs).unwrap();
42+
43+
// exit QEMU
44+
// NOTE do not run this on hardware; it can corrupt OpenOCD state
45+
debug::exit(debug::EXIT_SUCCESS);
46+
47+
loop {}
48+
}
49+
50+
// define what happens in an Out Of Memory (OOM) condition
51+
#[alloc_error_handler]
52+
fn alloc_error(_layout: Layout) -> ! {
53+
asm::bkpt();
54+
55+
loop {}
56+
}

0 commit comments

Comments
 (0)