Skip to main content

device/
spi.rs

1//! SPI device node control contract (`/devices/spi/spi<bus>.<cs>`).
2//!
3//! Configuration and multi-segment transfers use the control commands below,
4//! all with kind byte `b's'`. Every input and output struct is `#[repr(C)]`,
5//! starts with a `version` field that must equal [`SPI_ABI_VERSION`], and is
6//! validated before use.
7//!
8//! Bounds (the transaction path never allocates):
9//! - [`SPI_TRANSFER`] moves at most [`SPI_MAX_TRANSFER_BYTES`] bytes full duplex.
10//! - [`SPI_TRANSACTION`] runs at most [`SPI_MAX_SEGMENTS`] segments whose
11//!   lengths sum to at most [`SPI_MAX_TRANSACTION_BYTES`]. Segment data is laid
12//!   out back to back in both the input and output `data` arrays, so segment
13//!   `n` starts at the sum of the lengths of segments `0..n`.
14
15use file_system::{ControlCommand, ControlDirectionFlags, define_command};
16
17/// ABI version carried by every command input and output struct.
18pub const SPI_ABI_VERSION: u16 = 1;
19/// Maximum bytes moved by one [`SPI_TRANSFER`].
20pub const SPI_MAX_TRANSFER_BYTES: usize = 64;
21/// Maximum segments in one [`SPI_TRANSACTION`].
22pub const SPI_MAX_SEGMENTS: usize = 8;
23/// Maximum sum of segment lengths in one [`SPI_TRANSACTION`].
24pub const SPI_MAX_TRANSACTION_BYTES: usize = 64;
25
26/// [`SpiSegmentAbi::kind`] value for a write-only segment.
27pub const SPI_SEGMENT_WRITE: u8 = 0;
28/// [`SpiSegmentAbi::kind`] value for a read segment (zeros on MOSI).
29pub const SPI_SEGMENT_READ: u8 = 1;
30/// [`SpiSegmentAbi::kind`] value for a full-duplex segment.
31pub const SPI_SEGMENT_TRANSFER: u8 = 2;
32
33file_system::plain_data! {
34    #[repr(C)]
35    /// Device settings. Mode is 0..=3, bit order is 0 (MSB first) or 1 (LSB first).
36    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
37    pub struct SpiConfigurationAbi {
38        pub version: u16,
39        pub mode: u8,
40        pub bit_order: u8,
41        pub frequency_hz: u32,
42        /// Must be 1..=32.
43        pub bits_per_word: u8,
44        /// Must be zero.
45        pub reserved: [u8; 3],
46    }
47}
48
49file_system::plain_data! {
50    #[repr(C)]
51    /// Input of [`SPI_TRANSFER`]: `length` bytes of `tx` are sent while the same
52    /// number of bytes is received into [`SpiTransferOutputAbi::rx`].
53    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
54    pub struct SpiTransferInputAbi {
55        pub version: u16,
56        pub length: u16,
57        pub tx: [u8; SPI_MAX_TRANSFER_BYTES],
58    }
59}
60
61file_system::plain_data! {
62    #[repr(C)]
63    /// Output of [`SPI_TRANSFER`].
64    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
65    pub struct SpiTransferOutputAbi {
66        pub version: u16,
67        pub length: u16,
68        pub rx: [u8; SPI_MAX_TRANSFER_BYTES],
69    }
70}
71
72impl Default for SpiTransferOutputAbi {
73    fn default() -> Self {
74        Self {
75            version: 0,
76            length: 0,
77            rx: [0; SPI_MAX_TRANSFER_BYTES],
78        }
79    }
80}
81
82file_system::plain_data! {
83    #[repr(C)]
84    /// One segment of [`SPI_TRANSACTION`]. `kind` is one of the `SPI_SEGMENT_*`
85    /// values and `cs_change` is 0 or 1.
86    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
87    pub struct SpiSegmentAbi {
88        pub kind: u8,
89        pub cs_change: u8,
90        pub length: u16,
91    }
92}
93
94file_system::plain_data! {
95    #[repr(C)]
96    /// Input of [`SPI_TRANSACTION`]. Only the first `segment_count` segments are
97    /// read. `reserved` must be zero.
98    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
99    pub struct SpiTransactionInputAbi {
100        pub version: u16,
101        pub segment_count: u8,
102        pub reserved: u8,
103        pub segments: [SpiSegmentAbi; SPI_MAX_SEGMENTS],
104        pub data: [u8; SPI_MAX_TRANSACTION_BYTES],
105    }
106}
107
108file_system::plain_data! {
109    #[repr(C)]
110    /// Output of [`SPI_TRANSACTION`]. `length` is the total data length. Bytes of
111    /// write-only segments are zero.
112    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
113    pub struct SpiTransactionOutputAbi {
114        pub version: u16,
115        pub length: u16,
116        pub data: [u8; SPI_MAX_TRANSACTION_BYTES],
117    }
118}
119
120impl Default for SpiTransactionOutputAbi {
121    fn default() -> Self {
122        Self {
123            version: 0,
124            length: 0,
125            data: [0; SPI_MAX_TRANSACTION_BYTES],
126        }
127    }
128}
129
130// Get and set the device configuration. Getting takes no input.
131define_command!(
132    SPI_GET_CONFIGURATION,
133    Read,
134    crate::kind::SPI,
135    0,
136    (),
137    SpiConfigurationAbi
138);
139define_command!(
140    SPI_SET_CONFIGURATION,
141    Write,
142    crate::kind::SPI,
143    1,
144    SpiConfigurationAbi,
145    ()
146);
147// Full-duplex transfer of up to SPI_MAX_TRANSFER_BYTES in one chip-select cycle.
148define_command!(
149    SPI_TRANSFER,
150    ControlDirectionFlags::all(),
151    crate::kind::SPI,
152    2,
153    SpiTransferInputAbi,
154    SpiTransferOutputAbi
155);
156// Atomic multi-segment transaction, honouring cs_change between segments.
157define_command!(
158    SPI_TRANSACTION,
159    ControlDirectionFlags::all(),
160    crate::kind::SPI,
161    3,
162    SpiTransactionInputAbi,
163    SpiTransactionOutputAbi
164);