Skip to content

Commit 48d11ea

Browse files
committed
zephyr: device: Add led-strip wrapper
Add a simple wrapper for the led strip driver. Signed-off-by: David Brown <[email protected]>
1 parent c35db55 commit 48d11ea

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

dt-rust.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@
9797
device: "crate::device::uart::Uart"
9898
kconfig: CONFIG_SERIAL
9999

100+
- name: led-strip
101+
rules:
102+
- type: or
103+
value:
104+
- type: compatible
105+
value:
106+
names:
107+
- "worldsemi,wd2812-spi"
108+
level: 0
109+
- type: compatible
110+
value:
111+
names:
112+
- "worldsemi,ws2812-rpi_pico-pio"
113+
level: 1
114+
actions:
115+
- type: instance
116+
value:
117+
raw:
118+
type: myself
119+
device: "crate::device::led_strip::LedStrip"
120+
kconfig: CONFIG_LED_STRIP
121+
100122
# Generate a pseudo node that matches all of the labels across the tree with their nodes.
101123
- name: labels
102124
rules:

zephyr-sys/build.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn main() -> Result<()> {
6767
// one from the minimal libc.
6868
.clang_arg("-DRUST_BINDGEN")
6969
.clang_arg(format!("-I{}/lib/libc/minimal/include", zephyr_base))
70-
.derive_copy(false)
70+
.derive_copy(true)
7171
.allowlist_function("k_.*")
7272
.allowlist_function("gpio_.*")
7373
.allowlist_function("flash_.*")
@@ -82,6 +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.*")
8586
.allowlist_function("sys_.*")
8687
.allowlist_function("uart_.*")
8788
.allowlist_item("E.*")

zephyr-sys/wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern int errno;
4141
#include <zephyr/drivers/gpio.h>
4242
#include <zephyr/drivers/flash.h>
4343
#include <zephyr/drivers/uart.h>
44+
#include <zephyr/drivers/led_strip.h>
4445

4546
/*
4647
* 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
@@ -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_strip;
1314

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

zephyr/src/device/led_strip.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Simple led strip driver
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 LedStrip {
10+
/// The underlying device itself.
11+
#[allow(dead_code)]
12+
pub(crate) device: *const raw::device,
13+
}
14+
15+
// This is send, safe with Zephyr.
16+
unsafe impl Send for LedStrip { }
17+
18+
impl LedStrip {
19+
/// Constructor, used by the devicetree generated code.
20+
#[allow(dead_code)]
21+
pub(crate) unsafe fn new(unique: &Unique, device: *const raw::device) -> Option<LedStrip> {
22+
if !unique.once() {
23+
return None;
24+
}
25+
26+
Some(LedStrip { device })
27+
}
28+
29+
/// Return the number of LEDS in the chain.
30+
pub fn chain_len(&self) -> usize {
31+
unsafe { raw::led_strip_length(self.device) as usize}
32+
}
33+
34+
/// Update the state of the LEDs.
35+
///
36+
/// It is unclear from the API docs whether this is supposed to be an array of rgb_led
37+
/// (which is what the samples assume), or packed rgb data.
38+
pub unsafe fn update(&mut self, channels: &[raw::led_rgb]) -> Result<()> {
39+
to_result_void(unsafe {
40+
raw::led_strip_update_rgb(self.device,
41+
channels.as_ptr() as *mut _,
42+
channels.len())
43+
})
44+
}
45+
}

zephyr/src/sys.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ pub fn uptime_get() -> i64 {
4141
}
4242
}
4343

44+
/// Busy wait.
45+
///
46+
/// Busy wait for a give number of microseconds. This directly calls `zephyr_sys::k_busy_wait`.
47+
///
48+
/// Zephyr has numerous caveats on configurations where this function doesn't work.
49+
pub use zephyr_sys::k_busy_wait as busy_wait;
50+
4451
pub mod critical {
4552
//! Zephyr implementation of critical sections.
4653
//!

0 commit comments

Comments
 (0)