Skip to content

Commit 13ed0e6

Browse files
committed
zephyr: Add Leds driver
Add support for Leds, and a simple augment for the pwm drivers.
1 parent 32573c2 commit 13ed0e6

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

dt-rust.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
value:
1515
raw:
1616
type: myself
17+
value:
18+
args: []
1719
device: crate::device::gpio::Gpio
1820

1921
# The gpio-leds node will have #children nodes describing each led. We'll match on the parent
@@ -50,6 +52,8 @@
5052
value:
5153
raw:
5254
type: myself
55+
value:
56+
args: []
5357
device: crate::device::flash::FlashController
5458

5559
# Flash partitions exist as children of a node compatible with "soc-nv-flash" that itself is a child
@@ -95,6 +99,8 @@
9599
value:
96100
raw:
97101
type: myself
102+
value:
103+
args: []
98104
device: "crate::device::uart::Uart"
99105
kconfig: CONFIG_SERIAL
100106

@@ -117,9 +123,28 @@
117123
value:
118124
raw:
119125
type: myself
126+
value:
127+
args: []
120128
device: "crate::device::led_strip::LedStrip"
121129
kconfig: CONFIG_LED_STRIP
122130

131+
- name: pwm-leds
132+
rules:
133+
- type: compatible
134+
value:
135+
names:
136+
- "pwm-leds"
137+
level: 0
138+
actions:
139+
- type: instance
140+
value:
141+
raw:
142+
type: myself
143+
value:
144+
args:
145+
- type: child_count
146+
device: "crate::device::led::Leds"
147+
123148
# This doesn't really belong here, and can be moved once we support modules having their own augment
124149
# files.
125150
- name: bbq-kbd-matrix

zephyr-build/src/devicetree/augment.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ impl Action {
184184
#[serde(tag = "type", rename_all = "snake_case", content = "value")]
185185
pub enum RawInfo {
186186
/// Get the raw device directly from this node.
187-
Myself,
187+
Myself {
188+
args: Vec<ArgInfo>,
189+
},
188190
/// Get the reference from a parent of this node, at a given level.
189191
Parent {
190192
/// How many levels to look up. 0 would refer to this node (but would also be an error).
@@ -209,7 +211,9 @@ impl RawInfo {
209211
};
210212

211213
match self {
212-
Self::Myself => {
214+
Self::Myself { args } => {
215+
let get_args = args.iter().map(|arg| arg.args(node));
216+
213217
let ord = node.ord;
214218
let rawdev = format_ident!("__device_dts_ord_{}", ord);
215219
quote! {
@@ -225,7 +229,7 @@ impl RawInfo {
225229
pub fn get_instance() -> Option<#device_id> {
226230
unsafe {
227231
let device = get_instance_raw();
228-
#device_id::new(&UNIQUE, device)
232+
#device_id::new(&UNIQUE, device, #(#get_args),*)
229233
}
230234
}
231235
}
@@ -291,6 +295,8 @@ impl RawInfo {
291295
pub enum ArgInfo {
292296
/// The arguments come from a 'reg' property.
293297
Reg,
298+
/// A count of the number of child nodes.
299+
ChildCount,
294300
}
295301

296302
impl ArgInfo {
@@ -303,6 +309,12 @@ impl ArgInfo {
303309
#(#reg),*
304310
}
305311
}
312+
ArgInfo::ChildCount => {
313+
let count = node.children.len();
314+
quote! {
315+
#count
316+
}
317+
}
306318
}
307319
}
308320
}

zephyr-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn main() -> Result<()> {
8282
// Each DT node has a device entry that is a static.
8383
.allowlist_item("__device_dts_ord.*")
8484
.allowlist_function("device_.*")
85-
.allowlist_function("led_strip.*")
85+
.allowlist_function("led_.*")
8686
.allowlist_function("sys_.*")
8787
.allowlist_function("z_log.*")
8888
.allowlist_function("bt_.*")

zephyr-sys/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extern int errno;
4444
#include <zephyr/drivers/flash.h>
4545
#include <zephyr/drivers/uart.h>
4646
#include <zephyr/drivers/led_strip.h>
47+
#include <zephyr/drivers/led.h>
4748

4849
/*
4950
* bindgen will only output #defined constants that resolve to simple numbers. These are some

zephyr/src/device.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::sync::atomic::{AtomicBool, Ordering};
1313
pub mod gpio;
1414
pub mod flash;
1515
pub mod uart;
16+
pub mod led;
1617
pub mod led_strip;
1718

1819
// Allow dead code, because it isn't required for a given build to have any devices.

zephyr/src/device/led.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! LED driver, with support for PWMLED driver, with support for PWM.
2+
3+
use crate::raw;
4+
use crate::error::{Result, to_result_void};
5+
6+
use super::Unique;
7+
8+
/// A simple led strip wrapper.
9+
pub struct Leds {
10+
/// The underlying device itself.
11+
#[allow(dead_code)]
12+
pub(crate) device: *const raw::device,
13+
/// How many are configured in the DT.
14+
pub(crate) count: usize,
15+
}
16+
17+
// This is send, safe with Zephyr.
18+
unsafe impl Send for Leds { }
19+
20+
impl Leds {
21+
/// Constructor, used by the devicetree generated code.
22+
#[allow(dead_code)]
23+
pub(crate) unsafe fn new(unique: &Unique, device: *const raw::device, count: usize) -> Option<Leds> {
24+
if !unique.once() {
25+
return None;
26+
}
27+
28+
Some(Leds { device, count })
29+
}
30+
31+
/// Return the number of LEDS.
32+
pub fn len(&self) -> usize {
33+
self.count
34+
}
35+
36+
/// Set the brightness of one of the described LEDs
37+
pub unsafe fn set_brightness(&mut self, index: usize, value: u8) -> Result<()> {
38+
to_result_void(unsafe {
39+
raw::led_set_brightness(self.device,
40+
index as u32,
41+
value)
42+
})
43+
}
44+
}

0 commit comments

Comments
 (0)