Skip to content

Commit 488336f

Browse files
Merge pull request #17 from ferrous-systems/windows-fixes
Windows fixes
2 parents f869e34 + 9572326 commit 488336f

File tree

7 files changed

+93
-21
lines changed

7 files changed

+93
-21
lines changed

qemu-cortex-r5-app/.cargo/config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ rustflags = [
77
"-Clink-arg=-Tdefmt.x",
88
"-Ctarget-cpu=cortex-r5",
99
]
10-
runner = "./qemu_run.sh"
10+
runner = "./qemu_run.cmd"
1111

1212
[build]
1313
target = ["armv7r-none-eabihf"]
14+
15+
[env]
16+
DEFMT_LOG = "debug"

qemu-cortex-r5-app/README.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,75 @@ This application is for the Arm Versatile Application Board, which includes an
77
Arm Cortex-R5 processor. This board was chosen because it can be emulated by
88
QEMU.
99

10-
To build and run this project, simply run:
10+
## Pre-requisites
11+
12+
To build this demo you must:
13+
14+
1. Run `git submodule update --init` to check-out the ThreadX source code
15+
2. Install `defmt-print` with `cargo install defmt-print`
16+
3. Install `qemu-system-arm` - see [the QEMU website](https://www.qemu.org/download/) or via `winget install --id=SoftwareFreedomConservancy.QEMU`
17+
4. Install `arm-none-eabi-gcc` - such as from the [Arm GNU Toolchain](https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain) or via `winget install gcc-arm-embedded`
18+
5. Install `libclang`, as specified in [the `bindgen` documentation](https://rust-lang.github.io/rust-bindgen/requirements.html) or via `winget install LLVM.LLVM`
19+
6. Add the `armv7r-none-eabihf` target with `rustup target add armv7r-none-eabih`:
20+
21+
```bash
22+
rustup target add armv7r-none-eabihf --toolchain=stable
23+
```
24+
25+
This demo was tested on Ferrocene 25.05 and Rust 1.89. It may work with earlier versions.
26+
27+
## Building
28+
29+
Once you have the pre-requisites, to build and run this project, simply run:
1130

1231
```bash
13-
cargo run
32+
cargo run --release
1433
```
1534

1635
You must have `qemu-system-arm` in your system's PATH. You will also need
1736
`arm-none-eabi-gcc` in your system's PATH, so this project can automatically
1837
compile ThreadX (which is expected in `../threadx`). Our runner also requires `defmt-print`.
1938

39+
If you have Ferrocene available, you can also do:
40+
41+
```bash
42+
criticalup install
43+
criticalup run cargo run --release
44+
```
45+
2046
You will see something like:
2147

2248
```console
23-
$ DEFMT_LOG=info cargo run
24-
Compiling defmt-macros v1.0.1
25-
Compiling defmt v1.0.1
26-
Compiling defmt v0.3.100
27-
Compiling defmt-semihosting v0.3.0
28-
Compiling cortex-ar v0.2.0
29-
Compiling qemu-cortex-r5-app v0.1.0 (threadx-experiments/qemu-cortex-r5-app)
30-
Finished `dev` profile [unoptimized + debuginfo] target(s) in 1.17s
31-
Running `threadx-experiments/qemu-cortex-r5-app/./qemu_run.sh target/armv7r-none-eabihf/debug/qemu-cortex-r5-app`
32-
ELF_BINARY=target/armv7r-none-eabihf/debug/qemu-cortex-r5-app
49+
$ cargo run --release
50+
Finished `release` profile [optimized + debuginfo] target(s) in 0.3s
51+
Running `threadx-experiments/qemu-cortex-r5-app/./qemu_run.cmd target/armv7r-none-eabihf/release/qemu-cortex-r5-app`
52+
ELF_BINARY=target/armv7r-none-eabihf/release/qemu-cortex-r5-app
3353
Running on '-cpu cortex-r5f -machine versatileab'...
3454
------------------------------------------------------------------------
3555
[INFO ] Hello, this is version unknown! (src/main.rs:156)
3656
[INFO ] In tx_application_define()... (src/main.rs:27)
57+
[DEBUG] Stack allocated @ 0x000074f8 (src/main.rs:60)
58+
[DEBUG] Thread spawned (entry=12345678) @ 0x000134f4 (src/main.rs:86)
59+
[DEBUG] Stack allocated @ 0x0000b500 (src/main.rs:103)
60+
[DEBUG] Thread spawned (entry=aabbccdd) @ 0x000135ac (src/main.rs:129)
3761
[INFO ] I am my_thread(12345678) (src/main.rs:138)
3862
[INFO ] I am my_thread(aabbccdd) (src/main.rs:138)
3963
[INFO ] I am my_thread(12345678), count = 1 (src/main.rs:147)
4064
[INFO ] I am my_thread(aabbccdd), count = 1 (src/main.rs:147)
4165
[INFO ] I am my_thread(12345678), count = 2 (src/main.rs:147)
4266
[INFO ] I am my_thread(aabbccdd), count = 2 (src/main.rs:147)
67+
^Cqemu-system-arm: terminating on signal 2 from pid 56574 (<unknown process>)
68+
------------------------------------------------------------------------
4369
```
4470

45-
Press `Ctrl-C` to quit QEMU.
71+
Press `Ctrl-C` to quit QEMU, or if that doesn't work, try `Ctrl+A, X`.
4672

4773
Console output appears through `defmt` which is transported over QEMU's
4874
semihosting interface into `defmt-print` on the host.
4975

50-
If you wish to debug the program, run:
76+
## Debugging
77+
78+
If you wish to debug the program, add `-- -s -S` to the `cargo run` command, like:
5179

5280
```bash
5381
cargo run -- -s -S

qemu-cortex-r5-app/build.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,22 @@ fn main() -> Result<(), Box<dyn Error>> {
220220
println!("cargo:rustc-link-search={}", out_dir.display());
221221
println!("cargo:rerun-if-changed=linker.ld");
222222

223-
// Build our ThreadX static library
223+
// Set up common paths
224224
let tx_common_dir = crate_dir.join("../threadx/common/src");
225225
let tx_common_inc = crate_dir.join("../threadx/common/inc");
226226
let tx_port_dir = crate_dir.join("../threadx/ports/cortex_r5/gnu/src");
227227
let tx_port_inc = crate_dir.join("../threadx/ports/cortex_r5/gnu/inc");
228+
229+
// Check for ThreadX source code
230+
for file in TX_PORT_FILES.iter().map(|&s| tx_port_dir.join(s)) {
231+
if !std::fs::exists(&file)? {
232+
eprintln!("Cannot find ThreadX file {}!", file.display());
233+
eprintln!("Did you run `git submodule update --init`?");
234+
panic!("Missing ThreadX source code");
235+
}
236+
}
237+
238+
// Build our ThreadX static library
228239
cc::Build::new()
229240
.include(&tx_common_inc)
230241
.include(&tx_port_inc)

qemu-cortex-r5-app/qemu_run.bat

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@echo off
2+
3+
rem SPDX-FileCopyrightText: Copyright (c) 2025 Ferrous Systems
4+
rem SPDX-License-Identifier: CC0-1.0
5+
6+
rem This requires you to previously run `cargo install defmt-print`
7+
8+
set ELF_BINARY=%1
9+
set MACHINE=-cpu cortex-r5f -machine versatileab
10+
set QEMU_PATH=%ProgramFiles%\qemu
11+
echo ELF_BINARY=%ELF_BINARY%
12+
echo Running on '%MACHINE%'...
13+
echo ------------------------------------------------------------------------
14+
"%QEMU_PATH%\qemu-system-arm" %MACHINE% -semihosting-config enable=on -nographic -kernel %* | defmt-print -e %ELF_BINARY% --log-format="{[{L}]%%bold} {s} {({ff}:{l:1})%%dimmed}"
15+
echo ------------------------------------------------------------------------

qemu-cortex-r5-app/qemu_run.cmd

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
: # This is a special script which intermixes both sh
2+
: # and cmd code. It is written this way because it is
3+
: # set as the cargo runner and needs to work on Windows
4+
: # and POSIX platforms. See https://stackoverflow.com/questions/17510688
5+
: # for details.
6+
: #
7+
: # SPDX-FileCopyrightText: Copyright (c) 2025 Ferrous Systems
8+
: # SPDX-License-Identifier: CC0-1.0
9+
:; ./qemu_run.sh $* ; exit
10+
@ECHO OFF
11+
call qemu_run.bat %*

qemu-cortex-r5-app/qemu_run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ LOG_FORMAT='{[{L}]%bold} {s} {({ff}:{l:1})%dimmed}'
1212
echo "ELF_BINARY=$ELF_BINARY"
1313
echo "Running on '$MACHINE'..."
1414
echo "------------------------------------------------------------------------"
15-
qemu-system-arm $MACHINE -semihosting-config enable=on,target=native -kernel $ELF_BINARY $* | defmt-print -e $ELF_BINARY --log-format="$LOG_FORMAT"
15+
qemu-system-arm $MACHINE -semihosting-config enable=on,target=native -nographic -kernel $ELF_BINARY $* | defmt-print -e $ELF_BINARY --log-format="$LOG_FORMAT"
1616
echo "------------------------------------------------------------------------"

qemu-cortex-r5-app/src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,24 +127,28 @@ extern "C" fn tx_application_define(_first_unused_memory: *mut core::ffi::c_void
127127
}
128128
};
129129
defmt::debug!(
130-
"Thread spawned (entry={:08x}) @ 0x{=usize:08x}",
130+
"Thread spawned entry={=u32:08x} @ 0x{=usize:08x}",
131131
entry,
132132
thread1 as *const _ as usize
133133
);
134134
}
135135

136136
/// A function we execute in its own thread.
137137
extern "C" fn my_thread(value: u32) {
138-
defmt::info!("I am my_thread({:08x})", value);
139-
let mut thread_counter = 0;
138+
defmt::info!("I am my_thread({=u32:08x})", value);
139+
let mut thread_counter: u64 = 0;
140140
loop {
141141
thread_counter += 1;
142142

143143
unsafe {
144144
threadx_sys::_tx_thread_sleep(100);
145145
}
146146

147-
defmt::info!("I am my_thread({:08x}), count = {}", value, thread_counter);
147+
defmt::info!(
148+
"I am my_thread({=u32:08x}), count = {=u64}",
149+
value,
150+
thread_counter
151+
);
148152
}
149153
}
150154

0 commit comments

Comments
 (0)