From 733a38a272cff812b643a4adb7a1b3f96c85e232 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 10 Nov 2021 03:46:56 +0000 Subject: [PATCH 1/8] update defmt deps to v0.3 (#56) * bump defmt to v0.3 remove Cargo features which are no longer needed * remove `timestamp!` it's no longer mandatory and a monotonic counter does not add much value given that `defmt-rtt` works by disabling interrupts so one won't see logs out of order anyways (which would the case for a lock-free multi-channel `Logger`) * use println! in most the examples so that all examples print something when `DEFMT_LOG` is unset * make overflow example terminate in less steps also include an estimate of the stack pointer to show the stack grows downwards towards the RAM boundary --- Cargo.toml | 25 +++++-------------------- src/bin/bitfield.rs | 2 +- src/bin/format.rs | 9 ++++++--- src/bin/hello.rs | 2 +- src/bin/levels.rs | 2 ++ src/bin/overflow.rs | 9 ++++++--- src/bin/panic.rs | 2 +- src/lib.rs | 11 +---------- testsuite/Cargo.toml | 28 ++++++---------------------- 9 files changed, 29 insertions(+), 61 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fe68fa8..cecfd70 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,29 +9,14 @@ version = "0.1.0" members = ["testsuite"] [dependencies] -cortex-m = "0.7.1" -cortex-m-rt = "0.6.13" -defmt = "0.2.0" -defmt-rtt = "0.2.0" -panic-probe = { version = "0.2.0", features = ["print-defmt"] } +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" +defmt = "0.3.0" +defmt-rtt = "0.3.0" +panic-probe = { version = "0.3.0", features = ["print-defmt"] } # TODO(4) enter your HAL here # some-hal = "1.2.3" -[features] -# set logging levels here -default = [ - "defmt-default", - # "dependency-a/defmt-trace", -] - -# do NOT modify these features -defmt-default = [] -defmt-trace = [] -defmt-debug = [] -defmt-info = [] -defmt-warn = [] -defmt-error = [] - # cargo build/run [profile.dev] codegen-units = 1 diff --git a/src/bin/bitfield.rs b/src/bin/bitfield.rs index d9889b1..1e41fcb 100644 --- a/src/bin/bitfield.rs +++ b/src/bin/bitfield.rs @@ -7,7 +7,7 @@ use {{crate_name}} as _; // global logger + panicking-behavior + memory layout fn main() -> ! { // value of the FREQUENCY register (nRF52840 device; RADIO peripheral) let frequency: u32 = 276; - defmt::debug!("FREQUENCY: {0=0..7}, MAP: {0=8..9}", frequency); + defmt::println!("FREQUENCY: {0=0..7}, MAP: {0=8..9}", frequency); {{crate_name}}::exit() } diff --git a/src/bin/format.rs b/src/bin/format.rs index 30476f6..08ef2fc 100644 --- a/src/bin/format.rs +++ b/src/bin/format.rs @@ -17,10 +17,13 @@ struct S2 { #[cortex_m_rt::entry] fn main() -> ! { - let s = S1 { x: 42, y: S2 { z: 43 } }; - defmt::info!("s={:?}", s); + let s = S1 { + x: 42, + y: S2 { z: 43 }, + }; + defmt::println!("s={:?}", s); let x = 42; - defmt::info!("x={=u8}", x); + defmt::println!("x={=u8}", x); {{crate_name}}::exit() } diff --git a/src/bin/hello.rs b/src/bin/hello.rs index 4a39f24..d67bae6 100644 --- a/src/bin/hello.rs +++ b/src/bin/hello.rs @@ -5,7 +5,7 @@ use {{crate_name}} as _; // global logger + panicking-behavior + memory layout #[cortex_m_rt::entry] fn main() -> ! { - defmt::info!("Hello, world!"); + defmt::println!("Hello, world!"); {{crate_name}}::exit() } diff --git a/src/bin/levels.rs b/src/bin/levels.rs index a944130..612d2a4 100644 --- a/src/bin/levels.rs +++ b/src/bin/levels.rs @@ -5,6 +5,8 @@ use {{crate_name}} as _; // global logger + panicking-behavior + memory layout #[cortex_m_rt::entry] fn main() -> ! { + // try setting the DEFMT_LOG environment variable + // e.g. `export DEFMT_LOG=info` or `DEFMT_LOG=trace cargo rb levels` defmt::info!("info"); defmt::trace!("trace"); defmt::warn!("warn"); diff --git a/src/bin/overflow.rs b/src/bin/overflow.rs index 4e85b95..75ad68e 100644 --- a/src/bin/overflow.rs +++ b/src/bin/overflow.rs @@ -10,12 +10,15 @@ fn main() -> ! { } fn ack(m: u32, n: u32) -> u32 { - defmt::info!("ack(m={=u32}, n={=u32})", m, n); - let mut big = [2; 512]; + // waste stack space to trigger a stack overflow + let mut buffer = [0u8; 16 * 1024]; + // estimate of the Stack Pointer register + let sp = buffer.as_mut_ptr(); + defmt::println!("ack(m={=u32}, n={=u32}, SP={:x})", m, n, sp); + if m == 0 { n + 1 } else { - big[100] += 1; if n == 0 { ack(m - 1, 1) } else { diff --git a/src/bin/panic.rs b/src/bin/panic.rs index de9298f..587fdc1 100644 --- a/src/bin/panic.rs +++ b/src/bin/panic.rs @@ -5,7 +5,7 @@ use {{crate_name}} as _; // global logger + panicking-behavior + memory layout #[cortex_m_rt::entry] fn main() -> ! { - defmt::info!("main"); + defmt::println!("main"); defmt::panic!() } diff --git a/src/lib.rs b/src/lib.rs index 8b34265..506b5fd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,7 @@ #![no_std] -use core::sync::atomic::{AtomicUsize, Ordering}; - use defmt_rtt as _; // global logger + // TODO(5) adjust HAL import // use some_hal as _; // memory layout @@ -15,14 +14,6 @@ fn panic() -> ! { cortex_m::asm::udf() } -static COUNT: AtomicUsize = AtomicUsize::new(0); -defmt::timestamp!("{=usize}", { - // NOTE(no-CAS) `timestamps` runs with interrupts disabled - let n = COUNT.load(Ordering::Relaxed); - COUNT.store(n + 1, Ordering::Relaxed); - n -}); - /// Terminates the application and makes `probe-run` exit with exit-code = 0 pub fn exit() -> ! { loop { diff --git a/testsuite/Cargo.toml b/testsuite/Cargo.toml index 3567356..c81ac48 100644 --- a/testsuite/Cargo.toml +++ b/testsuite/Cargo.toml @@ -15,25 +15,9 @@ harness = false [dependencies] {{project-name}} = { path = ".." } -cortex-m = "0.7.1" -cortex-m-rt = "0.6.12" -defmt = "0.2.0" -defmt-rtt = "0.2.0" -defmt-test = "0.2.0" -panic-probe = { version = "0.2.0", features = ["print-defmt"] } - -[features] -# set logging levels here -default = [ - # in tests, enable all logs - "defmt-trace", - # "dependency-a/defmt-trace", -] - -# do NOT modify these features -defmt-default = [] -defmt-trace = [] -defmt-debug = [] -defmt-info = [] -defmt-warn = [] -defmt-error = [] +cortex-m = "0.7.3" +cortex-m-rt = "0.7.0" +defmt = "0.3.0" +defmt-rtt = "0.3.0" +defmt-test = "0.3.0" +panic-probe = { version = "0.3.0", features = ["print-defmt"] } From 09cb051ef0e6863e805a3fc65d509147b031f411 Mon Sep 17 00:00:00 2001 From: BriocheBerlin Date: Wed, 24 Nov 2021 16:45:48 +0100 Subject: [PATCH 2/8] Updates nrf-hal version to 0.14.0 to resolve dependency conflicts. (#57) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8dae452..7384e42 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ For the nRF52840 you'll want to use the [`nrf52840-hal`]. # Cargo.toml [dependencies] -# some-hal = "1.2.3" -+nrf52840-hal = "0.12.0" ++nrf52840-hal = "0.14.0" ``` #### 5. Import your HAL From d82fad8c965d91fcabd859cc072b92ff322d34bf Mon Sep 17 00:00:00 2001 From: Anatol Ulrich <45840+spookyvision@users.noreply.github.com> Date: Fri, 26 Nov 2021 18:35:42 +0100 Subject: [PATCH 3/8] Add documentation for https://github.com/knurling-rs/defmt/issues/628 (#58) --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 7384e42..1f92047 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,12 @@ $ echo $? 0 ``` +If you're running out of memory (`flip-link` bails with an overflow error), you can decrease the size of the device memory buffer by setting the `DEFMT_RTT_BUFFER_SIZE` environment variable. The default value is 1024 bytes, and powers of two should be used for optimal performance: + +``` console +$ DEFMT_RTT_BUFFER_SIZE=64 cargo rb hello +``` + #### (8. Set `rust-analyzer.linkedProjects`) If you are using [rust-analyzer] with VS Code for IDE-like features you can add following configuration to your `.vscode/settings.json` to make it work transparently across workspaces. Find the details of this option in the [RA docs]. From 47277bbf9da54049a1cf2e067ea578fcdeb3e63b Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 3 Jan 2022 10:29:08 +0000 Subject: [PATCH 4/8] add unit tests (#60) also, flatten the workspace into a single crate. I forget what the original reason for the workspace was -- I think it was added because `cargo t -p testsuite` is nicer than running `cargo t --test $filename` N times. However, a single crate removes the need to repeat the list of dependencies in `testsuite/Cargo.toml` also, document the two sets of tests included with the template and how to run them in the README file --- Cargo.toml | 12 ++++++++++-- README.md | 33 +++++++++++++++++++++++++++++++-- src/lib.rs | 15 +++++++++++++++ tests/integration.rs | 16 ++++++++++++++++ testsuite/Cargo.toml | 23 ----------------------- testsuite/src/lib.rs | 7 ------- testsuite/tests/test.rs | 21 --------------------- 7 files changed, 72 insertions(+), 55 deletions(-) create mode 100644 tests/integration.rs delete mode 100644 testsuite/Cargo.toml delete mode 100644 testsuite/src/lib.rs delete mode 100644 testsuite/tests/test.rs diff --git a/Cargo.toml b/Cargo.toml index cecfd70..5fcd52e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,13 @@ name = "{{project-name}}" edition = "2018" version = "0.1.0" -[workspace] -members = ["testsuite"] +[lib] +harness = false + +# needed for each integration test +[[test]] +name = "integration" +harness = false [dependencies] cortex-m = "0.7.3" @@ -17,6 +22,9 @@ panic-probe = { version = "0.3.0", features = ["print-defmt"] } # TODO(4) enter your HAL here # some-hal = "1.2.3" +[dev-dependencies] +defmt-test = "0.3.0" + # cargo build/run [profile.dev] codegen-units = 1 diff --git a/README.md b/README.md index 1f92047..fad2063 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ $ echo $? ``` If you're running out of memory (`flip-link` bails with an overflow error), you can decrease the size of the device memory buffer by setting the `DEFMT_RTT_BUFFER_SIZE` environment variable. The default value is 1024 bytes, and powers of two should be used for optimal performance: - + ``` console $ DEFMT_RTT_BUFFER_SIZE=64 cargo rb hello ``` @@ -154,12 +154,41 @@ If you are using [rust-analyzer] with VS Code for IDE-like features you can add "Cargo.toml", "firmware/Cargo.toml", ] -} +} ``` [RA docs]: https://rust-analyzer.github.io/manual.html#configuration [rust-analyzer]: https://rust-analyzer.github.io/ +## Running tests + +The template comes configured for running unit tests and integration tests on the target. + +Unit tests reside in the library crate and can test private API; the initial set of unit tests are in `src/lib.rs`. +`cargo test --lib` will run those unit tests. + +``` console +$ cargo test --lib +(1/1) running `it_works`... +└─ app::unit_tests::__defmt_test_entry @ src/lib.rs:33 +all tests passed! +└─ app::unit_tests::__defmt_test_entry @ src/lib.rs:28 +``` + +Integration tests reside in the `tests` directory; the initial set of integration tests are in `tests/integration.rs`. +`cargo test --test integration` will run those integration tests. +Note that the argument of the `--test` flag must match the name of the test file in the `tests` directory. + +``` console +$ cargo test --test integration +(1/1) running `it_works`... +└─ integration::tests::__defmt_test_entry @ tests/integration.rs:13 +all tests passed! +└─ integration::tests::__defmt_test_entry @ tests/integration.rs:8 +``` + +Note that to add a new test file to the `tests` directory you also need to add a new `[[test]]` section to `Cargo.toml`. + ## Trying out the git version of defmt This template is configured to use the latest crates.io release (the "stable" release) of the `defmt` framework. diff --git a/src/lib.rs b/src/lib.rs index 506b5fd..2aef086 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![no_main] #![no_std] use defmt_rtt as _; // global logger @@ -20,3 +21,17 @@ pub fn exit() -> ! { cortex_m::asm::bkpt(); } } + +// defmt-test 0.3.0 has the limitation that this `#[tests]` attribute can only be used +// once within a crate. the module can be in any file but there can only be at most +// one `#[tests]` module in this library crate +#[cfg(test)] +#[defmt_test::tests] +mod unit_tests { + use defmt::assert; + + #[test] + fn it_works() { + assert!(true) + } +} diff --git a/tests/integration.rs b/tests/integration.rs new file mode 100644 index 0000000..149ab9c --- /dev/null +++ b/tests/integration.rs @@ -0,0 +1,16 @@ +#![no_std] +#![no_main] + +use {{crate_name}} as _; // memory layout + panic handler + +// See https://crates.io/crates/defmt-test/0.3.0 for more documentation (e.g. about the 'state' +// feature) +#[defmt_test::tests] +mod tests { + use defmt::assert; + + #[test] + fn it_works() { + assert!(true) + } +} diff --git a/testsuite/Cargo.toml b/testsuite/Cargo.toml deleted file mode 100644 index c81ac48..0000000 --- a/testsuite/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -[package] -# TODO(1) fix `authors` if you didn't use `cargo-generate` -authors = ["{{authors}}"] -name = "testsuite" -publish = false -edition = "2018" -version = "0.1.0" - -[lib] -harness = false - -[[test]] -name = "test" -harness = false - -[dependencies] -{{project-name}} = { path = ".." } -cortex-m = "0.7.3" -cortex-m-rt = "0.7.0" -defmt = "0.3.0" -defmt-rtt = "0.3.0" -defmt-test = "0.3.0" -panic-probe = { version = "0.3.0", features = ["print-defmt"] } diff --git a/testsuite/src/lib.rs b/testsuite/src/lib.rs deleted file mode 100644 index d4dd9ce..0000000 --- a/testsuite/src/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -#![no_std] -#![cfg_attr(test, no_main)] - -use {{crate_name}} as _; // memory layout + panic handler - -#[defmt_test::tests] -mod tests {} diff --git a/testsuite/tests/test.rs b/testsuite/tests/test.rs deleted file mode 100644 index b26f3bf..0000000 --- a/testsuite/tests/test.rs +++ /dev/null @@ -1,21 +0,0 @@ -#![no_std] -#![no_main] - -use {{crate_name}} as _; // memory layout + panic handler - -// See https://crates.io/crates/defmt-test/0.1.0 for more documentation (e.g. about the 'state' -// feature) -#[defmt_test::tests] -mod tests { - use defmt::{assert, assert_eq}; - - #[test] - fn assert_true() { - assert!(true) - } - - #[test] - fn assert_eq() { - assert_eq!(24, 42, "TODO: write actual tests") - } -} From b4327d02599a77e3b6f544d133d4d772cfb8c768 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 5 Jan 2022 11:36:38 +0000 Subject: [PATCH 5/8] Disable LTO (#61) to work around rust-lang/rust#90357 --- Cargo.toml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5fcd52e..fe911aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,10 @@ codegen-units = 1 debug = 2 debug-assertions = false # <- incremental = false -lto = 'fat' +# NOTE disabled to work around issue rust-lang/rust#90357 +# the bug results in log messages not having location information +# (the line printed below the log message that contains the file-line location) +# lto = 'fat' opt-level = 3 # <- overflow-checks = false # <- @@ -59,7 +62,8 @@ codegen-units = 1 debug = 2 debug-assertions = false # <- incremental = false -lto = 'fat' +# see comment in the profile.release section +lto = 'false' opt-level = 3 # <- overflow-checks = false # <- From 5413c2c24ba87ce9e22edf76ff0cac51b7b8d27d Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Mon, 27 Jun 2022 14:46:28 +0200 Subject: [PATCH 6/8] Fix `lto` option in cargo profile (#64) Raised in https://github.com/knurling-rs/app-template/pull/61#issuecomment-1166687101. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fe911aa..00c94e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -63,7 +63,7 @@ debug = 2 debug-assertions = false # <- incremental = false # see comment in the profile.release section -lto = 'false' +lto = false opt-level = 3 # <- overflow-checks = false # <- From 12bd48a88dac2edd2ba8f7d6f3d7324344e302f7 Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Tue, 18 Oct 2022 17:25:46 +0200 Subject: [PATCH 7/8] Update `Cargo.toml` (#67) * Update `defmt-rtt` * Omit patch version from dependencies * Format `Cargo.toml` * Switch to rust 2021 edition * Use `opt-level = 'z'` again * Re-enable `lto` --- Cargo.toml | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 00c94e8..2e19b68 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ # TODO(1) fix `authors` and `name` if you didn't use `cargo-generate` authors = ["{{authors}}"] name = "{{project-name}}" -edition = "2018" +edition = "2021" version = "0.1.0" [lib] @@ -14,16 +14,16 @@ name = "integration" harness = false [dependencies] -cortex-m = "0.7.3" -cortex-m-rt = "0.7.0" -defmt = "0.3.0" -defmt-rtt = "0.3.0" -panic-probe = { version = "0.3.0", features = ["print-defmt"] } +cortex-m = { version = "0.7", features = ["critical-section-single-core"] } +cortex-m-rt = "0.7" +defmt = "0.3" +defmt-rtt = "0.4" +panic-probe = { version = "0.3", features = ["print-defmt"] } # TODO(4) enter your HAL here # some-hal = "1.2.3" [dev-dependencies] -defmt-test = "0.3.0" +defmt-test = "0.3" # cargo build/run [profile.dev] @@ -31,8 +31,8 @@ codegen-units = 1 debug = 2 debug-assertions = true # <- incremental = false -opt-level = 3 # <- -overflow-checks = true # <- +opt-level = 'z' # <- +overflow-checks = true # <- # cargo test [profile.test] @@ -40,8 +40,8 @@ codegen-units = 1 debug = 2 debug-assertions = true # <- incremental = false -opt-level = 3 # <- -overflow-checks = true # <- +opt-level = 3 # <- +overflow-checks = true # <- # cargo build/run --release [profile.release] @@ -49,12 +49,9 @@ codegen-units = 1 debug = 2 debug-assertions = false # <- incremental = false -# NOTE disabled to work around issue rust-lang/rust#90357 -# the bug results in log messages not having location information -# (the line printed below the log message that contains the file-line location) -# lto = 'fat' -opt-level = 3 # <- -overflow-checks = false # <- +lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- # cargo test --release [profile.bench] @@ -62,10 +59,9 @@ codegen-units = 1 debug = 2 debug-assertions = false # <- incremental = false -# see comment in the profile.release section -lto = false -opt-level = 3 # <- -overflow-checks = false # <- +lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- # uncomment this to switch from the crates.io version of defmt to its git version # check app-template's README for instructions @@ -73,4 +69,4 @@ overflow-checks = false # <- # defmt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } # defmt-rtt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } # defmt-test = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } -# panic-probe = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } \ No newline at end of file +# panic-probe = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } From 141f45e8a924934e4a98328ea65f20c893a0746b Mon Sep 17 00:00:00 2001 From: Johann Hemmann Date: Tue, 7 Feb 2023 11:30:40 +0100 Subject: [PATCH 8/8] Add note about RP2040 BSPs (#69) --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index fad2063..d96db64 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,10 @@ For the nRF52840 you'll want to use the [`nrf52840-hal`]. +nrf52840-hal = "0.14.0" ``` +⚠️ Note for RP2040 users ⚠️ + +You will need to not just specify the `rp-hal` HAL, but a BSP (board support crate) which includes a second stage bootloader. Please find a list of available BSPs [here](https://github.com/rp-rs/rp-hal-boards#packages). + #### 5. Import your HAL Now that you have selected a HAL, fix the HAL import in `src/lib.rs`