8000 Add SPI Abstraction by Skallwar · Pull Request #264 · Rust-for-Linux/linux · GitHub
[go: up one dir, main page]

Skip to content

Add SPI Abstraction #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: rust-next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
samples: rust: Add spi_dummy
Signed-off-by: Esteban Blanc <estblcsk@gmail.com>
  • Loading branch information
Skallwar committed Mar 19, 2024
commit 7dd3aec39d89023446f1b19fd43dbd64433d33e1
10 changes: 10 additions & 0 deletions samples/rust/Kconfig
10000
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,14 @@ config SAMPLE_RUST_HOSTPROGS

If unsure, say N.

config SAMPLE_RUST_SPI_DUMMY
tristate "SPI dummy"
help
This option builds the Rust SPI dummy sample.

To compile this as a module, choose M here:
the module will be called rust_spi_dummy.

If unsure, say N.

endif # SAMPLES_RUST
1 change: 1 addition & 0 deletions samples/rust/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
obj-$(CONFIG_SAMPLE_RUST_SPI_DUMMY) += spi_dummy.o

subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs
62 changes: 62 additions & 0 deletions samples/rust/spi_dummy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: GPL-2.0

use alloc::boxed::Box;
use core::pin::Pin;
use kernel::c_str;
use kernel::prelude::*;
use kernel::spi::*;

module! {
type: SPIDummy,
name: "rust_spi_dummy",
author: "ks0n",
description: "SPI Dummy Driver",
license: "GPL",
}

struct SPIDummyMethods;

#[vtable]
impl SpiMethods for SPIDummyMethods {
fn probe(spi_device: &mut SpiDevice) -> Result<i32, Error> {
pr_info!("[SPI-RS] SPI Registered\n");
pr_info!(
"[SPI-RS] SPI Registered, spi_device = {:#?}\n",
spi_device.to_ptr()
);

Ok(0)
}
}

struct SPIDummy {
_spi: Pin<Box<DriverRegistration<SPIDummyMethods>>>,
}

const TEST: u8 = 0;
const fn test() -> usize {
42
}

static ID_TABLE: &[SpiDeviceId] = &[
SpiDeviceId::new(c_str!("test1")).with_driver_data_pointer(&TEST),
SpiDeviceId::new(c_str!("test2")).with_driver_data_pointer(&test),
SpiDeviceId::new(c_str!("test3")).with_driver_data_number(42),
SpiDeviceId::sentinel(),
];

impl kernel::Module for SPIDummy {
fn init(module: &'static ThisModule) -> Result<Self> {
pr_info!("[SPI-RS] Init\n");

let spi = DriverRegistration::new_pinned(module, c_str!("SPIDummy"), Some(&ID_TABLE))?;

Ok(SPIDummy { _spi: spi })
}
}

impl Drop for SPIDummy {
fn drop(&mut self) {
pr_info!("[SPI-RS] Exit\n");
}
}
0