Skip to content

Commit 95a68e1

Browse files
committed
samples: rust: Add spi_dummy
Signed-off-by: Esteban Blanc <[email protected]>
1 parent d77db21 commit 95a68e1

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

samples/rust/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ config SAMPLE_RUST_HOSTPROGS
3737

3838
If unsure, say N.
3939

40+
config SAMPLE_RUST_SPI_DUMMY
41+
tristate "SPI dummy"
42+
help
43+
This option builds the Rust SPI dummy sample.
44+
45+
To compile this as a module, choose M here:
46+
the module will be called rust_spi_dummy.
47+
48+
If unsure, say N.
49+
4050
endif # SAMPLES_RUST

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
44
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
5+
obj-$(CONFIG_SAMPLE_RUST_SPI_DUMMY) += spi_dummy.o
56

67
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs

samples/rust/spi_dummy.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
use alloc::boxed::Box;
4+
use core::pin::Pin;
5+
use kernel::prelude::*;
6+
use kernel::spi::*;
7+
use kernel::c_str;
8+
9+
module! {
10+
type: SPIDummy,
11+
name: "rust_spi_dummy",
12+
author: "ks0n",
13+
description: "SPI Dummy Driver",
14+
license: "GPL",
15+
}
16+
17+
struct SPIDummyMethods;
18+
19+
#[vtable]
20+
impl SpiMethods for SPIDummyMethods {
21+
fn probe(spi_device: &mut SpiDevice) -> Result<i32, Error> {
22+
pr_info!("[SPI-RS] SPI Registered\n");
23+
pr_info!(
24+
"[SPI-RS] SPI Registered, spi_device = {:#?}\n",
25+
spi_device.to_ptr()
26+
);
27+
28+
Ok(0)
29+
}
30+
}
31+
32+
struct SPIDummy {
33+
_spi: Pin<Box<DriverRegistration<SPIDummyMethods>>>,
34+
}
35+
36+
impl kernel::Module for SPIDummy {
37+
fn init(module: &'static ThisModule) -> Result<Self> {
38+
pr_info!("[SPI-RS] Init\n");
39+
40+
let spi = DriverRegistration::new_pinned(module, c_str!("SPIDummy"))?;
41+
42+
Ok(SPIDummy { _spi: spi })
43+
}
44+
}
45+
46+
impl Drop for SPIDummy {
47+
fn drop(&mut self) {
48+
pr_info!("[SPI-RS] Exit\n");
49+
}
50+
}

0 commit comments

Comments
 (0)