Skip to content

Commit

Permalink
Gate extra formatting behind a feature flag in arm-app
Browse files Browse the repository at this point in the history
  • Loading branch information
YuhanLiin committed Jul 16, 2024
1 parent 5946783 commit c352ba1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
6 changes: 5 additions & 1 deletion examples/arm-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ version = "0.0.0"
[dependencies]
cortex-m = "0.6.0"
cortex-m-rt = "0.6.10"
cortex-m-semihosting = "0.3.3"
cortex-m-semihosting = { version = "0.3.3", optional = true }
panic-halt = "0.2.0"

micropb = { path = "../../micropb", features = ["container-heapless"] }

[build-dependencies]
micropb-gen = { path = "../../micropb-gen" }

[features]
default = ["formatting"]
formatting = ["dep:cortex-m-semihosting"]
4 changes: 2 additions & 2 deletions examples/arm-app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ To build, run `cargo build --profile release-lto`. This will build for the `thum

To run the code, run `qemu-system-arm -cpu cortex-m4 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel <executable-file>` after building.

To check the size of the binary, run `cargo size --profile release-lto`. The current size is about 12.5 kB.
To check the size of the binary without including formatting/printing code, run `cargo size --profile release-lto --no-default-features 00 -- -A`. The current size of the `.text` section is 8.7 kB.

To disassemble the binary, run `cargo objdump --profile release-lto -- -disassemble`.
To disassemble the binary without formatting/printing code, run `cargo objdump --profile release-lto --no-default-features -- --disassemble`.
29 changes: 13 additions & 16 deletions examples/arm-app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![no_std]
#![no_main]

use core::{mem::size_of, str::FromStr};
use core::{hint::black_box, mem::size_of, str::FromStr};

#[cfg(feature = "formatting")]
use cortex_m_semihosting::{debug, hprintln};
use micropb::{
heapless::{String, Vec},
Expand All @@ -26,6 +27,7 @@ use proto::raw_::*;

#[entry]
fn main() -> ! {
#[cfg(feature = "formatting")]
hprintln!(
"size of Packet = {}, Packet_::Msg = {}, LogBundle = {}, Log = {}",
size_of::<Packet>(),
Expand Down Expand Up @@ -102,23 +104,18 @@ fn main() -> ! {
let mut logs_pkt_out = Packet::default();
logs_pkt_out.decode_len_delimited(&mut decoder).unwrap();

// Don't use assert to check message equality, because debug printing logic bloats code size
if init_pkt != init_pkt_out {
// Uncomment to debug panic
//hprintln!("init_pkt = {init_pkt:?}\ninit_pkt_out = {init_pkt_out:?}").unwrap();
panic!("init package fails roundtrip test");
}
if logs_pkt != logs_pkt_out {
// Uncomment to debug panic
//hprintln!("logs_pkt = {logs_pkt:?}\nlogs_pkt_out = {logs_pkt_out:?}").unwrap();
panic!("log package fails roundtrip test");
}
#[cfg(feature = "formatting")]
{
assert_eq!(init_pkt, init_pkt_out);
assert_eq!(logs_pkt, logs_pkt_out);
hprintln!("Example complete").unwrap();

hprintln!("Example complete").unwrap();
// exit QEMU
// NOTE do not run this on hardware; it can corrupt OpenOCD state
debug::exit(debug::EXIT_SUCCESS);
}

// exit QEMU
// NOTE do not run this on hardware; it can corrupt OpenOCD state
debug::exit(debug::EXIT_SUCCESS);
black_box((init_pkt, init_pkt_out, logs_pkt, logs_pkt_out));

loop {}
}

0 comments on commit c352ba1

Please sign in to comment.