From c1d8c8cf36e3d13daf0eb93b56d8e149acf55b27 Mon Sep 17 00:00:00 2001 From: Quentin Smith Date: Tue, 16 Aug 2022 01:17:28 -0400 Subject: [PATCH] Add example of rtos-trace / SystemView --- examples/nrf/Cargo.toml | 68 ++++++++++++++++++++++++------ examples/nrf/build.rs | 1 + examples/nrf/src/bin/rtos_trace.rs | 64 ++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 examples/nrf/src/bin/rtos_trace.rs diff --git a/examples/nrf/Cargo.toml b/examples/nrf/Cargo.toml index 91edbd36..2f877b6a 100644 --- a/examples/nrf/Cargo.toml +++ b/examples/nrf/Cargo.toml @@ -4,28 +4,72 @@ name = "embassy-nrf-examples" version = "0.1.0" [features] -default = ["nightly"] +default = ["defmt", "nightly"] nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embassy-nrf/unstable-traits", "embassy-usb", "embassy-usb-serial", "embassy-usb-hid", "embassy-usb-ncm", "embedded-io/async", "embassy-net"] +defmt = [ + "dep:defmt", + "dep:defmt-rtt", + "embassy-util/defmt", + "embassy-executor/defmt", + "embassy-executor/defmt-timestamp-uptime", + "embassy-nrf/defmt", + "embassy-net/defmt", + "embassy-usb/defmt", + "embassy-usb-serial/defmt", + "embassy-usb-hid/defmt", + "embassy-usb-ncm/defmt", + "panic-probe/print-defmt", +] +log = [ + "dep:log", + "embassy-util/log", + "embassy-executor/log", + "embassy-nrf/log", + "embassy-net/log", + "embassy-usb-ncm/log", + # Currently broken: + # "embassy-usb/log", + # "embassy-usb-serial/log", + # "embassy-usb-hid/log", +] +rtos-trace = [ + "dep:rtos-trace", + "dep:systemview-target", + "log", + "embassy-executor/rtos-trace", + "embassy-executor/rtos-trace-interrupt", +] [dependencies] -embassy-util = { version = "0.1.0", path = "../../embassy-util", features = ["defmt"] } -embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "defmt-timestamp-uptime"] } -embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } -embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } -embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"], optional = true } -embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", features = ["defmt"], optional = true } -embassy-usb-hid = { version = "0.1.0", path = "../../embassy-usb-hid", features = ["defmt"], optional = true } -embassy-usb-ncm = { version = "0.1.0", path = "../../embassy-usb-ncm", features = ["defmt"], optional = true } +embassy-util = { version = "0.1.0", path = "../../embassy-util" } +embassy-executor = { version = "0.1.0", path = "../../embassy-executor" } +embassy-nrf = { version = "0.1.0", path = "../../embassy-nrf", features = ["nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] } +embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["tcp", "dhcpv4", "medium-ethernet", "pool-16"], optional = true } +embassy-usb = { version = "0.1.0", path = "../../embassy-usb", optional = true } +embassy-usb-serial = { version = "0.1.0", path = "../../embassy-usb-serial", optional = true } +embassy-usb-hid = { version = "0.1.0", path = "../../embassy-usb-hid", optional = true } +embassy-usb-ncm = { version = "0.1.0", path = "../../embassy-usb-ncm", optional = true } embedded-io = "0.3.0" -defmt = "0.3" -defmt-rtt = "0.3" +defmt = { version = "0.3", optional = true } +defmt-rtt = { version = "0.3", optional = true } cortex-m = "0.7.3" cortex-m-rt = "0.7.0" -panic-probe = { version = "0.3", features = ["print-defmt"] } +panic-probe = { version = "0.3" } futures = { version = "0.3.17", default-features = false, features = ["async-await"] } rand = { version = "0.8.4", default-features = false } embedded-storage = "0.3.0" usbd-hid = "0.5.2" serde = { version = "1.0.136", default-features = false } +rtos-trace = { version = "0.1.3", optional = true } +systemview-target = { version = "0.1.1", optional = true, features = ["callbacks-app", "callbacks-os", "log", "cortex-m"] } +log = { version = "0.4.17", optional = true } + +[[bin]] +name = "rtos_trace" +required-features = ["nightly", "rtos-trace", "log"] + +[patch.crates-io] +rtos-trace = { git = "https://gitlab.com/quentinmit/rtos-trace.git", branch = "build-fix" } +systemview-target = { git = "https://gitlab.com/quentinmit/rtos-trace.git", branch = "build-fix" } diff --git a/examples/nrf/build.rs b/examples/nrf/build.rs index 30691aa9..36cdb65a 100644 --- a/examples/nrf/build.rs +++ b/examples/nrf/build.rs @@ -31,5 +31,6 @@ fn main() { println!("cargo:rustc-link-arg-bins=--nmagic"); println!("cargo:rustc-link-arg-bins=-Tlink.x"); + #[cfg(feature = "defmt")] println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); } diff --git a/examples/nrf/src/bin/rtos_trace.rs b/examples/nrf/src/bin/rtos_trace.rs new file mode 100644 index 00000000..8b4f5b75 --- /dev/null +++ b/examples/nrf/src/bin/rtos_trace.rs @@ -0,0 +1,64 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use core::task::Poll; + +use embassy_executor::executor::Spawner; +use embassy_executor::time::{Duration, Instant, Timer}; +use embassy_nrf::Peripherals; + +// N.B. systemview_target cannot be used at the same time as defmt_rtt. + +use rtos_trace; +use systemview_target::SystemView; +use panic_probe as _; +use log::*; + +static LOGGER: systemview_target::SystemView = systemview_target::SystemView::new(); +rtos_trace::global_trace!{SystemView} + +struct TraceInfo(); + +impl rtos_trace::RtosTraceApplicationCallbacks for TraceInfo { + fn system_description() {} + fn sysclock() -> u32 { + 64000000 + } +} +rtos_trace::global_application_callbacks!{TraceInfo} + +#[embassy_executor::task] +async fn run1() { + loop { + info!("DING DONG"); + Timer::after(Duration::from_ticks(16000)).await; + } +} + +#[embassy_executor::task] +async fn run2() { + loop { + Timer::at(Instant::from_ticks(0)).await; + } +} + +#[embassy_executor::task] +async fn run3() { + futures::future::poll_fn(|cx| { + cx.waker().wake_by_ref(); + Poll::<()>::Pending + }) + .await; +} + +#[embassy_executor::main] +async fn main(spawner: Spawner, _p: Peripherals) { + LOGGER.init(); + ::log::set_logger(&LOGGER).ok(); + ::log::set_max_level(::log::LevelFilter::Trace); + + spawner.spawn(run1()).unwrap(); + spawner.spawn(run2()).unwrap(); + spawner.spawn(run3()).unwrap(); +}