Skip to content

Commit a66f145

Browse files
committed
samples: blinky: Domonstrate conditional DT compilation
Show how an application can be conditional based on the presence of a node in the devicetree. Signed-off-by: David Brown <[email protected]>
1 parent c29684a commit a66f145

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

samples/blinky/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ crate-type = ["staticlib"]
1515
[dependencies]
1616
zephyr = "3.7.0"
1717
log = "0.4.22"
18+
19+
[build-dependencies]
20+
zephyr-build = "3.7.0"

samples/blinky/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
zephyr_build::dt_cfgs();
3+
}

samples/blinky/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
#![no_std]
55

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

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

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

@@ -61,3 +68,10 @@ fn do_blink() {
6168
sleep(duration);
6269
}
6370
}
71+
72+
#[cfg(not(dt = "aliases::led0"))]
73+
fn do_blink() {
74+
warn!("No leds configured");
75+
loop {
76+
}
77+
}

0 commit comments

Comments
 (0)