Skip to content

Commit

Permalink
Merge #73
Browse files Browse the repository at this point in the history
73: refactor tests & CI r=japaric a=japaric



Co-authored-by: Jorge Aparicio <[email protected]>
  • Loading branch information
bors[bot] and japaric authored Aug 11, 2022
2 parents 7d0b16e + ade1ec6 commit 4150145
Show file tree
Hide file tree
Showing 21 changed files with 146 additions and 313 deletions.
33 changes: 14 additions & 19 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,26 @@ jobs:
host-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
override: true
profile: minimal
toolchain: stable
- uses: actions/checkout@v3

- name: Run disassembler test suite
run: cargo test

firmware-test:
env:
FW_TARGET: thumbv7m-none-eabi
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
components: rust-src
override: true
profile: minimal
target: thumbv7m-none-eabi
toolchain: nightly-2022-06-22
- run: rustup target add thumbv6m-none-eabi
- run: cargo install --path . --debug
- name: Can analyze example firmware
- uses: actions/checkout@v3

- name: Set up nightly toolchain (~1.63)
run: |
rustup default nightly-2022-08-11
rustup target add thumbv6m-none-eabi thumbv7m-none-eabi
rustup component add rust-src
- name: Install cargo-call-stack
run: cargo install --path . --debug

- name: Analyze example firmware
run: cargo test

# Refs: https://github.com/rust-lang/crater/blob/9ab6f9697c901c4a44025cf0a39b73ad5b37d198/.github/workflows/bors.yml#L125-L149
Expand Down
8 changes: 0 additions & 8 deletions cortex-m-examples/.cargo/config.toml

This file was deleted.

28 changes: 0 additions & 28 deletions cortex-m-examples/Cargo.toml

This file was deleted.

15 changes: 0 additions & 15 deletions cortex-m-examples/build.rs

This file was deleted.

59 changes: 0 additions & 59 deletions cortex-m-examples/examples/cycle.rs

This file was deleted.

24 changes: 0 additions & 24 deletions cortex-m-examples/examples/defmt.rs

This file was deleted.

26 changes: 0 additions & 26 deletions cortex-m-examples/examples/div64.rs

This file was deleted.

25 changes: 0 additions & 25 deletions cortex-m-examples/examples/fmul.rs

This file was deleted.

5 changes: 0 additions & 5 deletions cortex-m-examples/memory.x

This file was deleted.

2 changes: 2 additions & 0 deletions firmware/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
target = "thumbv7m-none-eabi"
File renamed without changes.
15 changes: 15 additions & 0 deletions firmware/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
authors = ["Jorge Aparicio <[email protected]>"]
edition = "2018"
readme = "README.md"
name = "firmware"
version = "0.1.0"

[dependencies]
panic-halt = "0.2.0"

[profile.release]
codegen-units = 1
debug = true
lto = 'fat'
opt-level = 'z'
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

use core::fmt::{self, Write as _};

use cortex_m_rt::entry;
use panic_halt as _;

#[entry]
fn main() -> ! {
#[no_mangle]
fn _start() {
write!(W, "hello, world!").ok();

loop {}
}

struct W;
Expand Down
44 changes: 44 additions & 0 deletions firmware/examples/cycle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![no_main]
#![no_std]

use core::{
arch::asm,
sync::atomic::{AtomicBool, Ordering},
};

use panic_halt as _;

#[no_mangle]
fn _start(x: &AtomicBool) {
foo(x);
quux();
}

// these three functions form a cycle that breaks when `x` is `false`
#[inline(never)]
fn foo(x: &AtomicBool) {
if x.load(Ordering::Relaxed) {
bar(x)
}
}

#[inline(never)]
fn bar(x: &AtomicBool) {
baz(x)
}

#[inline(never)]
fn baz(x: &AtomicBool) {
foo(x)
}

#[inline(never)]
fn quux() {
// spill variables onto the stack
unsafe {
asm!(
"// {0} {1} {2} {3} {4} {5}",
in(reg) 0, in(reg) 1, in(reg) 2, in(reg) 3, in(reg) 4, in(reg) 5,
);
}
}
9 changes: 9 additions & 0 deletions firmware/examples/div64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]
#![no_std]

use panic_halt as _;

#[no_mangle]
fn _start(x: u64, y: u64) -> u64 {
x / y
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
#![no_main]
#![no_std]

use core::{arch::asm, cell::Cell};
use core::arch::asm;

use cortex_m::interrupt::{self, Mutex};
use cortex_m_rt::{entry, exception};
use panic_halt as _;

static TO: interrupt::Mutex<Cell<&'static (dyn Foo + Sync)>> = Mutex::new(Cell::new(&Bar));

#[entry]
fn main() -> ! {
#[no_mangle]
fn _start(trait_object: &dyn Foo) -> (&dyn Foo, &dyn Foo) {
// trait object dispatch
let to = interrupt::free(|cs| TO.borrow(cs).get());
to.foo();
trait_object.foo();

Quux.foo();

loop {}
(&Bar, &Baz)
}

trait Foo {
Expand Down Expand Up @@ -59,20 +54,12 @@ impl Foo for Baz {
struct Quux;

impl Quux {
// not a trait method!
// not a trait method! but function name and signature matches `Foo::foo`'s
#[inline(never)]
fn foo(&self) -> bool {
// side effect to preserve function calls to this method
cortex_m::asm::nop();
unsafe { asm!("NOP") }

false
}
}

// this handler can change the trait object at any time
#[exception]
fn SysTick() {
interrupt::free(|cs| {
TO.borrow(cs).set(&Baz);
})
}
9 changes: 9 additions & 0 deletions firmware/examples/fmul.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]
#![no_std]

use panic_halt as _;

#[no_mangle]
fn _start(x: f32) -> f32 {
x * 1.1
}
Loading

0 comments on commit 4150145

Please sign in to comment.