Skip to content

Commit ec6d92e

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 b529da5 commit ec6d92e

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

samples/blinky/Cargo.toml

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 19 additions & 0 deletions
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;
@@ -37,6 +43,12 @@ extern "C" fn rust_main() {
3743

3844
// fn blink() {
3945
unsafe extern "C" fn blink(_p1: *mut c_void, _p2: *mut c_void, _p3: *mut c_void) {
46+
// Just call a "safe" rust function.
47+
do_blink();
48+
}
49+
50+
#[cfg(dt = "aliases::led0")]
51+
fn do_blink() {
4052
warn!("Inside of blinky");
4153

4254
let mut led0 = zephyr::devicetree::aliases::led0::get_instance().unwrap();
@@ -55,3 +67,10 @@ unsafe extern "C" fn blink(_p1: *mut c_void, _p2: *mut c_void, _p3: *mut c_void)
5567
sleep(duration);
5668
}
5769
}
70+
71+
#[cfg(not(dt = "aliases::led0"))]
72+
fn do_blink() {
73+
warn!("No leds configured");
74+
loop {
75+
}
76+
}

0 commit comments

Comments
 (0)