Skip to content

Commit 7dd3aec

Browse files
committed
samples: rust: Add spi_dummy
Signed-off-by: Esteban Blanc <[email protected]>
1 parent eb5852f commit 7dd3aec

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

samples/rust/Kconfig

+10
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

+1
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

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
use alloc::boxed::Box;
4+
use core::pin::Pin;
5+
use kernel::c_str;
6+
use kernel::prelude::*;
7+
use kernel::spi::*;
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+
const TEST: u8 = 0;
37+
const fn test() -> usize {
38+
42
39+
}
40+
41+
static ID_TABLE: &[SpiDeviceId] = &[
42+
SpiDeviceId::new(c_str!("test1")).with_driver_data_pointer(&TEST),
43+
SpiDeviceId::new(c_str!("test2")).with_driver_data_pointer(&test),
44+
SpiDeviceId::new(c_str!("test3")).with_driver_data_number(42),
45+
SpiDeviceId::sentinel(),
46+
];
47+
48+
impl kernel::Module for SPIDummy {
49+
fn init(module: &'static ThisModule) -> Result<Self> {
50+
pr_info!("[SPI-RS] Init\n");
51+
52+
let spi = DriverRegistration::new_pinned(module, c_str!("SPIDummy"), Some(&ID_TABLE))?;
53+
54+
Ok(SPIDummy { _spi: spi })
55+
}
56+
}
57+
58+
impl Drop for SPIDummy {
59+
fn drop(&mut self) {
60+
pr_info!("[SPI-RS] Exit\n");
61+
}
62+
}

0 commit comments

Comments
 (0)