Skip to main content

device/
pwm.rs

1//! PWM device node control contract (`/devices/pwm/<name>`).
2//!
3//! # Control commands (kind `b'p'`)
4//!
5//! | Number | Command            | Direction | Input              | Output        | Effect                                        |
6//! |--------|--------------------|-----------|--------------------|---------------|-----------------------------------------------|
7//! | `0x00` | `GET_INFO`         | Read      | `PwmVersionAbi`    | `PwmInfoAbi`  | Returns channel count, max duty and period.   |
8//! | `0x01` | `SET_FREQUENCY`    | Write     | `PwmFrequencyAbi`  | `PwmInfoAbi`  | Sets the channel period in hertz.             |
9//! | `0x02` | `SET_DUTY_PERMILLE`| Write     | `PwmDutyPermilleAbi`| `PwmInfoAbi` | Sets duty as 0..=1000 per mille of max duty.  |
10//!
11//! Every struct starts with a `version` field equal to [`ABI_VERSION`] and
12//! carries zeroed `reserved` fields. A wrong version, a non-zero reserved field
13//! or an out-of-range value is `InvalidArgument`. A frequency the controller
14//! cannot express, or a fixed-period controller, is `Unsupported`.
15
16use file_system::{ControlCommand, define_command};
17
18/// Version of every PWM control ABI struct in this module.
19pub const ABI_VERSION: u16 = 1;
20
21/// Largest value accepted by [`SET_DUTY_PERMILLE`].
22pub const PERMILLE_FULL_SCALE: u16 = 1_000;
23
24file_system::plain_data! {
25    #[repr(C)]
26    /// Carries only the ABI version and a reserved field. Input of [`GET_INFO`].
27    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
28    pub struct PwmVersionAbi {
29        pub version: u16,
30        pub reserved: u16,
31    }
32}
33
34file_system::plain_data! {
35    #[repr(C)]
36    /// Channel description returned by every PWM command.
37    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
38    pub struct PwmInfoAbi {
39        pub version: u16,
40        /// Number of channels on the controller.
41        pub channel_count: u16,
42        /// Duty value that means 100 percent on this channel.
43        pub max_duty_cycle: u16,
44        pub reserved: u16,
45        /// Current period of this channel in hertz.
46        pub frequency_hz: u32,
47    }
48}
49
50file_system::plain_data! {
51    #[repr(C)]
52    /// Input of [`SET_FREQUENCY`].
53    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
54    pub struct PwmFrequencyAbi {
55        pub version: u16,
56        pub reserved: u16,
57        pub frequency_hz: u32,
58    }
59}
60
61file_system::plain_data! {
62    #[repr(C)]
63    /// Input of [`SET_DUTY_PERMILLE`]. `permille` is 0..=[`PERMILLE_FULL_SCALE`].
64    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
65    pub struct PwmDutyPermilleAbi {
66        pub version: u16,
67        pub permille: u16,
68    }
69}
70
71define_command!(
72    GET_INFO,
73    Read,
74    crate::kind::PWM,
75    0x00,
76    PwmVersionAbi,
77    PwmInfoAbi
78);
79define_command!(
80    SET_FREQUENCY,
81    Write,
82    crate::kind::PWM,
83    0x01,
84    PwmFrequencyAbi,
85    PwmInfoAbi
86);
87define_command!(
88    SET_DUTY_PERMILLE,
89    Write,
90    crate::kind::PWM,
91    0x02,
92    PwmDutyPermilleAbi,
93    PwmInfoAbi
94);