diff --git a/examples/arm-app/Cargo.toml b/examples/arm-app/Cargo.toml index 515aabd..1b50f7c 100644 --- a/examples/arm-app/Cargo.toml +++ b/examples/arm-app/Cargo.toml @@ -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"] diff --git a/examples/arm-app/README.md b/examples/arm-app/README.md index d37dd91..aa3e6d0 100644 --- a/examples/arm-app/README.md +++ b/examples/arm-app/README.md @@ -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 ` 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`. diff --git a/examples/arm-app/src/main.rs b/examples/arm-app/src/main.rs index bf18268..f507aaf 100644 --- a/examples/arm-app/src/main.rs +++ b/examples/arm-app/src/main.rs @@ -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}, @@ -26,6 +27,7 @@ use proto::raw_::*; #[entry] fn main() -> ! { + #[cfg(feature = "formatting")] hprintln!( "size of Packet = {}, Packet_::Msg = {}, LogBundle = {}, Log = {}", size_of::(), @@ -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 {} }