Skip to main content

device/
adc.rs

1//! ADC device node control contract (`/devices/adc/<name>`).
2//!
3//! # Control commands (kind `b'a'`)
4//!
5//! | Number | Command           | Direction | Input              | Output             | Effect                                        |
6//! |--------|-------------------|-----------|--------------------|--------------------|-----------------------------------------------|
7//! | `0x00` | `GET_INFO`        | Read      | `VersionAbi`       | `InfoAbi`          | Returns channels, resolution and reference.   |
8//! | `0x01` | `SELECT_CHANNEL`  | Write     | `SelectChannelAbi` | `VersionAbi`       | Selects the channel used by plain reads.      |
9//! | `0x02` | `READ_MILLIVOLTS` | Read      | `VersionAbi`       | `MillivoltsAbi`    | Converts the selected channel to millivolts.  |
10//!
11//! Every struct starts with a `version` field equal to [`ABI_VERSION`] and its
12//! reserved fields must be zero; otherwise the command fails with
13//! `InvalidArgument`. A channel outside `0..channels` is `InvalidArgument`,
14//! and `READ_MILLIVOLTS` on a converter without a reference voltage is
15//! `Unsupported`.
16
17use file_system::{ControlCommand, define_command};
18
19/// Version of every ADC control ABI struct in this module.
20pub const ABI_VERSION: u16 = 1;
21
22file_system::plain_data! {
23    #[repr(C)]
24    /// Carries only the ABI version. Used as input of queries and output of commands
25    /// that have no other payload.
26    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
27    pub struct VersionAbi {
28        pub version: u16,
29        pub reserved: u16,
30    }
31}
32
33file_system::plain_data! {
34    #[repr(C)]
35    /// Converter geometry. `reference_millivolts` is meaningful only when
36    /// `reference_present` is 1.
37    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
38    pub struct InfoAbi {
39        pub version: u16,
40        pub channels: u8,
41        pub resolution_bits: u8,
42        pub reference_millivolts: u32,
43        pub reference_present: u8,
44        pub reserved: [u8; 3],
45    }
46}
47
48file_system::plain_data! {
49    #[repr(C)]
50    /// Selects the channel used by plain reads.
51    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
52    pub struct SelectChannelAbi {
53        pub version: u16,
54        pub channel: u8,
55        pub reserved: u8,
56    }
57}
58
59file_system::plain_data! {
60    #[repr(C)]
61    /// One conversion in millivolts.
62    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
63    pub struct MillivoltsAbi {
64        pub version: u16,
65        pub reserved: u16,
66        pub millivolts: u32,
67    }
68}
69
70define_command!(GET_INFO, Read, crate::kind::ADC, 0x00, VersionAbi, InfoAbi);
71define_command!(
72    SELECT_CHANNEL,
73    Write,
74    crate::kind::ADC,
75    0x01,
76    SelectChannelAbi,
77    VersionAbi
78);
79define_command!(
80    READ_MILLIVOLTS,
81    Read,
82    crate::kind::ADC,
83    0x02,
84    VersionAbi,
85    MillivoltsAbi
86);