Skip to content

Commit

Permalink
samples: blinky: Domonstrate conditional DT compilation
Browse files Browse the repository at this point in the history
Show how an application can be conditional based on the presence of a
node in the devicetree.

Signed-off-by: David Brown <[email protected]>
  • Loading branch information
d3zd3z committed Oct 18, 2024
1 parent dfd483d commit bb03fb7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions samples/blinky/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ crate-type = ["staticlib"]
[dependencies]
zephyr = "3.7.0"
log = "0.4.22"

[build-dependencies]
zephyr-build = "3.7.0"
3 changes: 3 additions & 0 deletions samples/blinky/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
zephyr_build::dt_cfgs();
}
14 changes: 14 additions & 0 deletions samples/blinky/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

#![no_std]

// Sigh. The check config system requires that the compiler be told what possible config values
// there might be. This is completely impossible with both Kconfig and the DT configs, since the
// whole point is that we likely need to check for configs that aren't otherwise present in the
// build. So, this is just always necessary.
#![allow(unexpected_cfgs)]

use log::warn;

use core::ffi::c_void;
Expand Down Expand Up @@ -41,6 +47,7 @@ unsafe extern "C" fn blink(_p1: *mut c_void, _p2: *mut c_void, _p3: *mut c_void)
do_blink();
}

#[cfg(dt = "aliases::led0")]
fn do_blink() {
warn!("Inside of blinky");

Expand All @@ -61,3 +68,10 @@ fn do_blink() {
sleep(duration);
}
}

#[cfg(not(dt = "aliases::led0"))]
fn do_blink() {
warn!("No leds configured");
loop {
}
}

0 comments on commit bb03fb7

Please sign in to comment.