Skip to content

Commit c7fdc10

Browse files
committed
FIXME: rust: spi: Add SpiTransfer and SpiMessage
Signed-off-by: Esteban Blanc <[email protected]>
1 parent 7dd3aec commit c7fdc10

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

rust/kernel/spi.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,49 @@ impl SpiDeviceId {
255255
}
256256
}
257257

258+
/// Wrapper struct around the kernel's `struct spi_transfer`.
259+
pub struct SpiTransfer(bindings::spi_transfer);
260+
261+
impl SpiTransfer {
262+
pub fn new() -> Self {
263+
SpiTransfer(bindings::spi_transfer::default())
264+
}
265+
266+
pub fn with_tx_buf(mut self, buf: &[u8]) -> Self {
267+
// Make sure no tx_buf nor rx_buf has been provided before
268+
// TODO: replace with a `Result`
269+
assert!(self.0.len == 0);
270+
271+
self.0.tx_buf = buf.as_ptr() as *const core::ffi::c_void;
272+
self.0.len = buf.len() as core::ffi::c_uint;
273+
274+
self
275+
}
276+
277+
pub fn with_rx_buf(mut self, buf: &mut[u8]) -> Self {
278+
// Make sure no tx_buf nor rx_buf has been provided before
279+
// TODO: replace with a `Result`
280+
assert!(self.0.len == 0);
281+
282+
self.0.rx_buf = buf.as_mut_ptr() as *mut core::ffi::c_void;
283+
self.0.len = buf.len() as core::ffi::c_uint;
284+
285+
self
286+
}
287+
}
288+
289+
#[derive(Default)]
290+
pub struct SpiMessage(bindings::spi_message);
291+
292+
impl SpiMessage {
293+
pub fn new() -> Self {
294+
Self::default()
295+
}
296+
/// Equivalent of `spi_message_init`
297+
pub fn spi_message_init(&mut self) {
298+
}
299+
}
300+
258301
/// High level abstraction over the kernel's SPI functions such as `spi_write_then_read`.
259302
// TODO this should be a mod, right?
260303
pub struct Spi;

0 commit comments

Comments
 (0)