Skip to main content

device/
i2c.rs

1//! I2C device node control contract (`/devices/i2c/<name>`).
2//!
3//! Control commands (kind `b'i'`), all with a `version` field first that must
4//! equal [`ABI_VERSION`] and reserved fields that must be zero:
5//! - `SELECT_ADDRESS` (write): 7-bit address, or 10-bit when the controller
6//!   reports `ten_bit` support.
7//! - `GET_FREQUENCY` (read): configured and maximum frequency.
8//! - `SET_FREQUENCY` (write): new frequency, `0 < hz <= max_frequency_hz`.
9//! - `WRITE_READ` (write): write then read in one transaction with repeated
10//!   START, against the selected address. Lengths are at most
11//!   [`MAX_TRANSFER_LENGTH`] and at least one of them must be non-zero.
12
13use file_system::{ControlCommand, define_command};
14
15/// Version carried in the first field of every command input and output.
16pub const ABI_VERSION: u16 = 1;
17
18/// Largest write or read length of one `WRITE_READ` transfer.
19pub const MAX_TRANSFER_LENGTH: usize = 64;
20
21file_system::plain_data! {
22    #[repr(C)]
23    /// Input of `SELECT_ADDRESS`.
24    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
25    pub struct SelectAddressInput {
26        pub version: u16,
27        /// `0` selects a 7-bit address, `1` a 10-bit address.
28        pub ten_bit: u8,
29        pub reserved: u8,
30        pub address: u16,
31    }
32}
33
34file_system::plain_data! {
35    #[repr(C)]
36    /// Output of `GET_FREQUENCY`.
37    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
38    pub struct FrequencyOutput {
39        pub version: u16,
40        /// `1` when the controller supports 10-bit addresses.
41        pub ten_bit_supported: u8,
42        pub reserved: u8,
43        pub frequency_hz: u32,
44        pub max_frequency_hz: u32,
45    }
46}
47
48file_system::plain_data! {
49    #[repr(C)]
50    /// Input of `SET_FREQUENCY`.
51    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
52    pub struct SetFrequencyInput {
53        pub version: u16,
54        pub reserved: u16,
55        pub frequency_hz: u32,
56    }
57}
58
59file_system::plain_data! {
60    #[repr(C)]
61    /// Input of `WRITE_READ`. Only the first `write_length` bytes of `write` are sent.
62    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
63    pub struct WriteReadInput {
64        pub version: u16,
65        pub write_length: u8,
66        pub read_length: u8,
67        pub write: [u8; MAX_TRANSFER_LENGTH],
68    }
69}
70
71impl Default for WriteReadInput {
72    fn default() -> Self {
73        Self {
74            version: ABI_VERSION,
75            write_length: 0,
76            read_length: 0,
77            write: [0; MAX_TRANSFER_LENGTH],
78        }
79    }
80}
81
82file_system::plain_data! {
83    #[repr(C)]
84    /// Output of `WRITE_READ`. The first `read_length` bytes of `read` are valid.
85    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
86    pub struct WriteReadOutput {
87        pub version: u16,
88        pub read_length: u8,
89        pub reserved: u8,
90        pub read: [u8; MAX_TRANSFER_LENGTH],
91    }
92}
93
94impl Default for WriteReadOutput {
95    fn default() -> Self {
96        Self {
97            version: ABI_VERSION,
98            read_length: 0,
99            reserved: 0,
100            read: [0; MAX_TRANSFER_LENGTH],
101        }
102    }
103}
104
105define_command!(
106    SELECT_ADDRESS,
107    Write,
108    crate::kind::I2C,
109    0,
110    SelectAddressInput,
111    ()
112);
113define_command!(
114    GET_FREQUENCY,
115    Read,
116    crate::kind::I2C,
117    1,
118    (),
119    FrequencyOutput
120);
121define_command!(
122    SET_FREQUENCY,
123    Write,
124    crate::kind::I2C,
125    2,
126    SetFrequencyInput,
127    ()
128);
129define_command!(
130    WRITE_READ,
131    Write,
132    crate::kind::I2C,
133    3,
134    WriteReadInput,
135    WriteReadOutput
136);