Skip to content

Commit 1694ceb

Browse files
committed
zephyr: Add Leds driver
Add support for Leds, and a simple augment for the pwm drivers.
1 parent a18aa91 commit 1694ceb

File tree

6 files changed

+87
-4
lines changed

6 files changed

+87
-4
lines changed

dt-rust.yaml

+25
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
@@ -49,6 +51,8 @@
4951
value:
5052
raw:
5153
type: myself
54+
value:
55+
args: []
5256
device: crate::device::flash::FlashController
5357

5458
# Flash partitions exist as children of a node compatible with "soc-nv-flash" that itself is a child
@@ -94,6 +98,8 @@
9498
value:
9599
raw:
96100
type: myself
101+
value:
102+
args: []
97103
device: "crate::device::uart::Uart"
98104
kconfig: CONFIG_SERIAL
99105

@@ -116,9 +122,28 @@
116122
value:
117123
raw:
118124
type: myself
125+
value:
126+
args: []
119127
device: "crate::device::led_strip::LedStrip"
120128
kconfig: CONFIG_LED_STRIP
121129

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

zephyr-build/src/devicetree/augment.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ fn decode_gpios_gpio(unique: &Ident, entry: &Value) -> TokenStream {
247247
#[serde(tag = "type", rename_all = "snake_case", content = "value")]
248248
pub enum RawInfo {
249249
/// Get the raw device directly from this node.
250-
Myself,
250+
Myself {
251+
args: Vec<ArgInfo>,
252+
},
251253
/// Get the reference from a parent of this node, at a given level.
252254
Parent {
253255
/// How many levels to look up. 0 would refer to this node (but would also be an error).
@@ -272,7 +274,9 @@ impl RawInfo {
272274
};
273275

274276
match self {
275-
RawInfo::Myself => {
277+
RawInfo::Myself { args } => {
278+
let get_args = args.iter().map(|arg| arg.args(node));
279+
276280
let ord = node.ord;
277281
let rawdev = format_ident!("__device_dts_ord_{}", ord);
278282
quote! {
@@ -288,7 +292,7 @@ impl RawInfo {
288292
pub fn get_instance() -> Option<#device_id> {
289293
unsafe {
290294
let device = get_instance_raw();
291-
#device_id::new(&UNIQUE, device)
295+
#device_id::new(&UNIQUE, device, #(#get_args),*)
292296
}
293297
}
294298
}
@@ -354,6 +358,8 @@ impl RawInfo {
354358
pub enum ArgInfo {
355359
/// The arguments come from a 'reg' property.
356360
Reg,
361+
/// A count of the number of child nodes.
362+
ChildCount,
357363
}
358364

359365
impl ArgInfo {
@@ -366,6 +372,12 @@ impl ArgInfo {
366372
#(#reg),*
367373
}
368374
}
375+
ArgInfo::ChildCount => {
376+
let count = node.children.len();
377+
quote! {
378+
#count
379+
}
380+
}
369381
}
370382
}
371383
}

zephyr-sys/build.rs

+1-1
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("uart_.*")
8888
.allowlist_item("E.*")

zephyr-sys/wrapper.h

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ extern int errno;
4242
#include <zephyr/drivers/flash.h>
4343
#include <zephyr/drivers/uart.h>
4444
#include <zephyr/drivers/led_strip.h>
45+
#include <zephyr/drivers/led.h>
4546

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

zephyr/src/device.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::sync::atomic::{AtomicUsize, Ordering};
1010
pub mod gpio;
1111
pub mod flash;
1212
pub mod uart;
13+
pub mod led;
1314
pub mod led_strip;
1415

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

zephyr/src/device/led.rs

+44
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)