Skip to content

Commit 37a4fed

Browse files
committed
zephyr-build: YAML-based augment code
Instead of hardcoding all of the augments in the code, put these augments into a data structure. Load the actual rules from a yaml file (with a future ability to extend this with per-app or per-module defined augments. Convert all of the existing hardcoded augment rules into the initial augment file.
1 parent 252e8aa commit 37a4fed

File tree

7 files changed

+435
-5
lines changed

7 files changed

+435
-5
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
set(RUST_MODULE_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE INTERNAL "")
66

7+
# Initially, we just have a single DT augment file.
8+
set(DT_AUGMENTS "${CMAKE_CURRENT_LIST_DIR}/dt-rust.yaml" CACHE INTERNAL "")
9+
710
# Zephyr targets are defined through Kconfig. We need to map these to
811
# an appropriate llvm target triple. This sets `RUST_TARGET` in the
912
# parent scope, or an error if the target is not yet supported by
@@ -134,6 +137,7 @@ INCLUDE_DIRS = \"${include_dirs}\"
134137
INCLUDE_DEFINES = \"${include_defines}\"
135138
WRAPPER_FILE = \"${WRAPPER_FILE}\"
136139
BINARY_DIR_INCLUDE_GENERATED = \"${BINARY_DIR_INCLUDE_GENERATED}\"
140+
DT_AUGMENTS = \"${DT_AUGMENTS}\"
137141
138142
[patch.crates-io]
139143
${config_paths}
@@ -152,6 +156,7 @@ ${config_paths}
152156
INCLUDE_DIRS="${include_dirs}"
153157
INCLUDE_DEFINES="${include_defines}"
154158
WRAPPER_FILE="${WRAPPER_FILE}"
159+
DT_AUGMENTS="${DT_AUGMENTS}"
155160
BINARY_DIR_INCLUDE_GENERATED="${BINARY_DIR_INCLUDE_GENERATED}"
156161
cargo build
157162
# TODO: release flag if release build

dt-rust.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Description of how to augment the devicetree for Rust.
2+
#
3+
# Each entry describes an augmentation that will be added to matching nodes in the device tree.
4+
# The full syntax is described (indirectly) in `zephyr-build/src/devicetree/config.rs`.
5+
6+
# Gpio controllers match for every node that has a `gpio-controller` property. This is one of the
7+
# few instances were we can actually just match on a property.
8+
- name: gpio-controller
9+
rules:
10+
- type: has_prop
11+
value: gpio-controller
12+
actions:
13+
- type: instance
14+
value:
15+
raw:
16+
type: myself
17+
device: crate::sys::gpio::Gpio
18+
19+
# The gpio-leds node will have #children nodes describing each led. We'll match on the parent
20+
# having this compatible property. The nodes themselves are built out of the properties associated
21+
# with each gpio.
22+
- name: gpio-leds
23+
rules:
24+
- type: compatible
25+
value:
26+
names:
27+
- gpio-leds
28+
level: 1
29+
actions:
30+
- type: instance
31+
value:
32+
raw:
33+
type: phandle
34+
value: gpios
35+
device: crate::sys::gpio::GpioPin
36+
37+
# Flash controllers don't have any particular property to identify them, so we need a list of
38+
# compatible values that should match.
39+
- name: flash-controller
40+
rules:
41+
- type: compatible
42+
value:
43+
names:
44+
- "nordic,nrf52-flash-controller"
45+
- "raspberrypi,pico-flash-controller"
46+
level: 0
47+
actions:
48+
- type: instance
49+
value:
50+
raw:
51+
type: myself
52+
device: crate::sys::flash::FlashController
53+
54+
# Flash partitions exist as children of a node compatible with "soc-nv-flash" that itself is a child
55+
# of the controller itself.
56+
# TODO: Get the write and erase property from the DT if present.
57+
- name: flash-partition
58+
rules:
59+
- type: compatible
60+
value:
61+
names:
62+
- "fixed-partitions"
63+
level: 1
64+
- type: compatible
65+
value:
66+
names:
67+
- "soc-nv-flash"
68+
level: 2
69+
actions:
70+
- type: instance
71+
value:
72+
raw:
73+
type: parent
74+
value:
75+
level: 3
76+
args:
77+
- type: reg
78+
device: "crate::sys::flash::FlashPartition"
79+
80+
# Generate a pseudo node that matches all of the labels across the tree with their nodes.
81+
- name: labels
82+
rules:
83+
- type: root
84+
actions:
85+
- type: labels
86+

zephyr-build/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@ pest = "2.6"
1919
pest_derive = "2.6"
2020
quote = "1.0"
2121
proc-macro2 = "1.0.86"
22+
serde = { version = "1.0", features = ["derive"] }
23+
serde_yaml_ng = "0.10"
24+
anyhow = "1.0.89"

zephyr-build/src/devicetree.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ use ordmap::OrdMap;
2424
use std::{cell::RefCell, collections::BTreeMap, path::Path, rc::Rc};
2525

2626
mod augment;
27+
pub mod config;
2728
mod ordmap;
2829
mod output;
2930
mod parse;
3031

32+
pub use augment::Augment;
33+
3134
pub struct DeviceTree {
3235
/// The root of the tree.
3336
root: Rc<Node>,

0 commit comments

Comments
 (0)