Skip to content

Commit bfb6ff5

Browse files
committed
add cortex-m0 support for testsuite
1 parent 518eb7c commit bfb6ff5

8 files changed

Lines changed: 104 additions & 34 deletions

File tree

.github/workflows/on-target.yml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ name: cortex-m on-target tests
99
jobs:
1010

1111
hil-qemu:
12+
env:
13+
PATH: /opt/qemu/bin:$PATH
1214
runs-on: ubuntu-latest
1315
steps:
1416
- uses: actions/checkout@v4
@@ -23,11 +25,14 @@ jobs:
2325
- name: Install custom QEMU into /opt
2426
run: |
2527
curl -sSL https://github.com/jonathanpallant/qemu9-for-ubuntu-2404/releases/download/qemu-9.2.3%2Bbuild0/qemu-9.2.3-ubuntu-24.04.tar.gz | sudo tar xvzf - -C /
26-
- name: Run testsuite
28+
echo "/opt/qemu/bin" >> "$GITHUB_PATH"
29+
- name: Install toolchains
2730
uses: dtolnay/rust-toolchain@stable
2831
with:
29-
targets: thumbv7em-none-eabihf
30-
- name: Run testsuite
32+
targets: thumbv7em-none-eabihf, thumbv6m-none-eabi
33+
- name: Run testsuite Cortex-M3
3134
run: |
32-
export PATH=/opt/qemu/bin:$PATH
33-
cd testsuite && cargo test --features qemu
35+
cd testsuite && cargo test --features qemu --target thumbv7em-none-eabihf
36+
- name: Run testsuite Cortex-M0
37+
run: |
38+
cd testsuite && cargo test --features qemu --target thumbv6m-none-eabi

justfile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
LOG_FORMAT := " --log-format \"{[{L}]%bold} {s} {({ff}:{l:1})%dimmed}\""
3+
QEMU_RUN_CORTEX_M3 := "qemu-run --machine lm3s6965evb --cpu cortex-m3"
4+
QEMU_RUN_CORTEX_M0 := "qemu-run --machine microbit --cpu cortex-m0"
5+
6+
[working-directory: "testsuite"]
7+
run-cortex-m0 binary:
8+
{{QEMU_RUN_CORTEX_M0}} {{binary}}
9+
10+
[working-directory: "testsuite"]
11+
run-cortex-m3 binary:
12+
{{QEMU_RUN_CORTEX_M3}} {{binary}}
13+
14+
[working-directory: "testsuite"]
15+
run-cortex-m3-test:
16+
cargo test --features qemu --target thumbv7em-none-eabihf
17+
18+
[working-directory: "testsuite"]
19+
run-cortex-m0-test:
20+
cargo test --features qemu --target thumbv6m-none-eabi --release

testsuite/.cargo/config.toml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1+
12
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
23
rustflags = [
34
"-C", "link-arg=-Tlink.x",
45
"-C", "link-arg=-Tdefmt.x",
56
]
6-
runner = [
7-
"qemu-run",
8-
"--machine",
9-
"lm3s6965evb",
10-
"--cpu",
11-
"cortex-m3",
12-
"--log-format",
13-
"{[{L}]%bold} {s} {({ff}:{l:1})%dimmed}"
14-
]
15-
# For running on actual hardware.
16-
# runner = "probe-rs run --chip <your-chip> --allow-erase-all"
7+
8+
[target.thumbv7em-none-eabihf]
9+
runner = "qemu-run --machine lm3s6965evb --cpu cortex-m3"
10+
11+
[target.thumbv6m-none-eabi]
12+
runner = "qemu-run --machine microbit --cpu cortex-m0"
1713

1814
[build]
1915
# target = "thumbv6m-none-eabi" # Cortex-M0
20-
target = "thumbv7em-none-eabihf"
16+
target = "thumbv7em-none-eabihf" # Cortex-M3
2117

2218
[env]
2319
DEFMT_LOG = "info"

testsuite/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ rust-version = "1.85"
88
[features]
99
hardware = ["dep:defmt-rtt"]
1010
qemu = ["dep:defmt-semihosting"]
11+
# microbit = ["dep:nrf-pac"]
1112

1213
[dependencies]
1314
cortex-m-rt = { path = "../cortex-m-rt" }
@@ -16,6 +17,7 @@ critical-section = "1"
1617
cortex-m-semihosting = { path = "../cortex-m-semihosting"}
1718
defmt-semihosting = { version = "0.3", optional = true }
1819
defmt-rtt = { version = "1", optional = true }
20+
# nrf-pac = { version = "0.3", features = ["nrf51", "rt"], optional = true }
1921
defmt = "1"
2022

2123
[dev-dependencies]
@@ -32,3 +34,8 @@ test = false
3234
[patch.crates-io]
3335
cortex-m = { path = "../cortex-m" }
3436
cortex-m-rt = { path = "../cortex-m-rt" }
37+
38+
[profile.release]
39+
opt-level = "z" # Optimize for size
40+
lto = true # Enable Link Time Optimization
41+
codegen-units = 1 # Better optimization at cost of compile time

testsuite/build.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use std::{
2-
env,
3-
path::{Path, PathBuf},
4-
};
1+
use std::{env, path::PathBuf};
52

3+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
4+
pub enum MemorySelect {
5+
CortexM0,
6+
CortexM3,
7+
}
68
fn main() {
79
let target = std::env::var("TARGET").unwrap();
810

@@ -13,11 +15,15 @@ fn main() {
1315
println!("cargo:rustc-check-cfg=cfg(armv8m_base)");
1416
println!("cargo:rustc-check-cfg=cfg(armv8m_main)");
1517

18+
let mut memory_select = None;
1619
if target.starts_with("thumbv6m-") {
20+
memory_select = Some(MemorySelect::CortexM0);
1721
println!("cargo:rustc-cfg=armv6m");
1822
} else if target.starts_with("thumbv7m-") {
23+
memory_select = Some(MemorySelect::CortexM3);
1924
println!("cargo:rustc-cfg=armv7m");
2025
} else if target.starts_with("thumbv7em-") {
26+
memory_select = Some(MemorySelect::CortexM3);
2127
println!("cargo:rustc-cfg=armv7m");
2228
println!("cargo:rustc-cfg=armv7em"); // (not currently used)
2329
} else if target.starts_with("thumbv8m.base") {
@@ -28,26 +34,33 @@ fn main() {
2834
println!("cargo:rustc-cfg=armv8m_main");
2935
}
3036

31-
// Put `memory.x` in our output directory and ensure it's
32-
// on the linker search path.
3337
let out = PathBuf::from(env::var_os("OUT_DIR").unwrap());
34-
let memory_x_path = out.join("memory.x");
38+
let memory_x_file = match memory_select {
39+
Some(MemorySelect::CortexM0) => {
40+
// Put `memory.x` in our output directory and ensure it's
41+
// on the linker search path.
42+
"memory_microbit.x"
43+
}
44+
Some(MemorySelect::CortexM3) => {
45+
// Put `memory.x` in our output directory and ensure it's
46+
// on the linker search path.
47+
"memory_m3.x"
48+
}
49+
// TODO: Copy memory.x if it exists?
50+
None => panic!("Unsupported target architecture: {}", target),
51+
};
52+
let target_path = out.join("memory.x");
3553

3654
// Copy memory.x from parent directory only if it doesn't exist locally
37-
if !Path::new("memory.x").exists() {
38-
std::fs::copy("../memory.x", &memory_x_path)
39-
.unwrap_or_else(|e| panic!("Failed to copy memory.x from parent: {}", e));
40-
} else {
41-
std::fs::copy("memory.x", &memory_x_path)
42-
.unwrap_or_else(|e| panic!("Failed to copy memory.x: {}", e));
43-
}
55+
std::fs::copy(memory_x_file, &target_path)
56+
.unwrap_or_else(|e| panic!("Failed to copy memory.x: {}", e));
4457

4558
println!("cargo:rustc-link-search={}", out.display());
4659

4760
// By default, Cargo will re-run a build script whenever
4861
// any file in the project changes. By specifying `memory.x`
4962
// here, we ensure the build script is only re-run when
5063
// `memory.x` is changed.
51-
println!("cargo:rerun-if-changed=memory.x");
52-
println!("cargo:rerun-if-changed=../memory.x");
64+
println!("cargo:rerun-if-changed=memory_m3.x");
65+
println!("cargo:rerun-if-changed=memory_microbit.x");
5366
}

testsuite/memory_m3.x

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Sample memory.x file used for cortex-m-rt examples and tests only.
2+
* You must provide your own memory.x with values correct for your device,
3+
* don't just copy these.
4+
*/
5+
6+
MEMORY
7+
{
8+
/* FLASH and RAM are mandatory memory regions */
9+
/* Update examples/data_overflow.rs if you change these sizes. */
10+
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
11+
RAM : ORIGIN = 0x20000000, LENGTH = 64K
12+
13+
/* More memory regions can declared: for example this is a second RAM region */
14+
/* CCRAM : ORIGIN = 0x10000000, LENGTH = 8K */
15+
}
16+
17+
/* The location of the stack can be overridden using the `_stack_start` symbol.
18+
By default it will be placed at the end of the RAM region */
19+
/* _stack_start = ORIGIN(CCRAM) + LENGTH(CCRAM); */
20+
21+
/* The location of the .text section can be overridden using the `_stext` symbol.
22+
By default it will place after .vector_table */
23+
/* _stext = ORIGIN(FLASH) + 0x40c; */

testsuite/memory_microbit.x

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
MEMORY
2+
{
3+
/* NOTE K = KiBi = 1024 bytes */
4+
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
5+
RAM : ORIGIN = 0x20000000, LENGTH = 16K
6+
}

testsuite/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ mod tests {
170170
// this test must be last!
171171
#[test]
172172
fn run_psp() {
173-
static STACK: cortex_m::psp::Stack<4096> = cortex_m::psp::Stack::new();
173+
static STACK: cortex_m::psp::Stack<2048> = cortex_m::psp::Stack::new();
174174
info!("Switching to PSP...");
175175
cortex_m::psp::switch_to_unprivileged_psp(STACK.take_handle(), crate::user_fn);
176176
}

0 commit comments

Comments
 (0)