device/gpio.rs
1//! GPIO line control contract (`/devices/gpio/<name>`).
2//!
3//! # Control commands (kind `b'g'`)
4//!
5//! | Number | Command | Direction | Input | Output | Effect |
6//! |--------|-------------------|-----------|-----------------|--------------|-----------------------------------------------------|
7//! | `0x00` | `CONFIGURE` | Write | `ConfigureAbi` | `VersionAbi` | Releases the line, then requests it as input/output. |
8//! | `0x01` | `RELEASE` | Write | `VersionAbi` | `VersionAbi` | Releases the line, returning it to high impedance. |
9//! | `0x02` | `GET_CAPABILITIES`| Read | `VersionAbi` | `CapabilitiesAbi` | Reports what the line supports. |
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`. Field values outside their documented range are
14//! `InvalidArgument`. A controller refusal to take the line (unsupported
15//! option, line already in use) is reported as `InputOutput`.
16
17use file_system::{ControlCommand, define_command};
18
19/// Version of every GPIO 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 commands without payload and
25 /// as output of commands that report success.
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 /// Requested configuration of the line.
36 ///
37 /// - `direction`: `0` input, `1` output.
38 /// - `pull`: `0` none, `1` pull up, `2` pull down.
39 /// - `drive_mode`: `0` push-pull, `1` open-drain. Outputs only.
40 /// - `initial_level`: `0` low, `1` high. Outputs only.
41 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
42 pub struct ConfigureAbi {
43 pub version: u16,
44 pub direction: u8,
45 pub pull: u8,
46 pub drive_mode: u8,
47 pub initial_level: u8,
48 pub reserved: u16,
49 }
50}
51
52file_system::plain_data! {
53 #[repr(C)]
54 /// Line capabilities; each flag is `0` or `1`.
55 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
56 pub struct CapabilitiesAbi {
57 pub version: u16,
58 pub input: u8,
59 pub output: u8,
60 pub pull_up: u8,
61 pub pull_down: u8,
62 pub open_drain: u8,
63 pub reserved: u8,
64 }
65}
66
67define_command!(
68 CONFIGURE,
69 Write,
70 crate::kind::GPIO,
71 0x00,
72 ConfigureAbi,
73 VersionAbi
74);
75define_command!(
76 RELEASE,
77 Write,
78 crate::kind::GPIO,
79 0x01,
80 VersionAbi,
81 VersionAbi
82);
83define_command!(
84 GET_CAPABILITIES,
85 Read,
86 crate::kind::GPIO,
87 0x02,
88 VersionAbi,
89 CapabilitiesAbi
90);