1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/// sub-GHz radio operating mode.
///
/// See `Get_Status` under section 5.8.5 "Communication status information commands"
/// in the reference manual.
///
/// This is returned by [`Status::mode`].
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum StatusMode {
    /// Standby mode with RC 13MHz.
    StandbyRc = 0x2,
    /// Standby mode with HSE32.
    StandbyHse = 0x3,
    /// Frequency Synthesis mode.
    Fs = 0x4,
    /// Receive mode.
    Rx = 0x5,
    /// Transmit mode.
    Tx = 0x6,
}

impl StatusMode {
    /// Create a new `StatusMode` from bits.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::StatusMode;
    ///
    /// assert_eq!(StatusMode::from_raw(0x2), Ok(StatusMode::StandbyRc));
    /// assert_eq!(StatusMode::from_raw(0x3), Ok(StatusMode::StandbyHse));
    /// assert_eq!(StatusMode::from_raw(0x4), Ok(StatusMode::Fs));
    /// assert_eq!(StatusMode::from_raw(0x5), Ok(StatusMode::Rx));
    /// assert_eq!(StatusMode::from_raw(0x6), Ok(StatusMode::Tx));
    /// // Other values are reserved
    /// assert_eq!(StatusMode::from_raw(0), Err(0));
    /// ```
    pub const fn from_raw(bits: u8) -> Result<Self, u8> {
        match bits {
            0x2 => Ok(StatusMode::StandbyRc),
            0x3 => Ok(StatusMode::StandbyHse),
            0x4 => Ok(StatusMode::Fs),
            0x5 => Ok(StatusMode::Rx),
            0x6 => Ok(StatusMode::Tx),
            _ => Err(bits),
        }
    }
}

/// Command status.
///
/// See `Get_Status` under section 5.8.5 "Communication status information commands"
/// in the reference manual.
///
/// This is returned by [`Status::cmd`].
#[repr(u8)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CmdStatus {
    /// Data available to host.
    ///
    /// Packet received successfully and data can be retrieved.
    Available = 0x2,
    /// Command time out.
    ///
    /// Command took too long to complete triggering a sub-GHz radio watchdog
    /// timeout.
    Timeout = 0x3,
    /// Command processing error.
    ///
    /// Invalid opcode or incorrect number of parameters.
    ProcessingError = 0x4,
    /// Command execution failure.
    ///
    /// Command successfully received but cannot be executed at this time,
    /// requested operating mode cannot be entered or requested data cannot be
    /// sent.
    ExecutionFailure = 0x5,
    /// Transmit command completed.
    ///
    /// Current packet transmission completed.
    Complete = 0x6,
}

impl CmdStatus {
    /// Create a new `CmdStatus` from bits.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::CmdStatus;
    ///
    /// assert_eq!(CmdStatus::from_raw(0x2), Ok(CmdStatus::Available));
    /// assert_eq!(CmdStatus::from_raw(0x3), Ok(CmdStatus::Timeout));
    /// assert_eq!(CmdStatus::from_raw(0x4), Ok(CmdStatus::ProcessingError));
    /// assert_eq!(CmdStatus::from_raw(0x5), Ok(CmdStatus::ExecutionFailure));
    /// assert_eq!(CmdStatus::from_raw(0x6), Ok(CmdStatus::Complete));
    /// // Other values are reserved
    /// assert_eq!(CmdStatus::from_raw(0), Err(0));
    /// ```
    pub const fn from_raw(bits: u8) -> Result<Self, u8> {
        match bits {
            0x2 => Ok(CmdStatus::Available),
            0x3 => Ok(CmdStatus::Timeout),
            0x4 => Ok(CmdStatus::ProcessingError),
            0x5 => Ok(CmdStatus::ExecutionFailure),
            0x6 => Ok(CmdStatus::Complete),
            _ => Err(bits),
        }
    }
}

/// Radio status.
///
/// This is returned by [`status`].
///
/// [`status`]: super::SubGhz::status
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct Status(u8);

impl From<u8> for Status {
    fn from(x: u8) -> Self {
        Status(x)
    }
}
impl From<Status> for u8 {
    fn from(x: Status) -> Self {
        x.0
    }
}

impl Status {
    /// Create a new `Status` from a raw `u8` value.
    ///
    /// This is the same as `Status::from(u8)`, but in a `const` function.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{CmdStatus, Status, StatusMode};
    ///
    /// const STATUS: Status = Status::from_raw(0x54_u8);
    /// assert_eq!(STATUS.mode(), Ok(StatusMode::Rx));
    /// assert_eq!(STATUS.cmd(), Ok(CmdStatus::Available));
    /// ```
    pub const fn from_raw(value: u8) -> Status {
        Status(value)
    }

    /// sub-GHz radio operating mode.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{Status, StatusMode};
    ///
    /// let status: Status = 0xACu8.into();
    /// assert_eq!(status.mode(), Ok(StatusMode::StandbyRc));
    /// ```
    pub const fn mode(&self) -> Result<StatusMode, u8> {
        StatusMode::from_raw((self.0 >> 4) & 0b111)
    }

    /// Command status.
    ///
    /// This method frequently returns reserved values such as `Err(1)`.
    /// ST support has confirmed that this is normal and should be ignored.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{CmdStatus, Status};
    ///
    /// let status: Status = 0xACu8.into();
    /// assert_eq!(status.cmd(), Ok(CmdStatus::Complete));
    /// ```
    pub const fn cmd(&self) -> Result<CmdStatus, u8> {
        CmdStatus::from_raw((self.0 >> 1) & 0b111)
    }
}

impl core::fmt::Debug for Status {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Status")
            .field("mode", &self.mode())
            .field("cmd", &self.cmd())
            .finish()
    }
}

#[cfg(feature = "defmt")]
impl defmt::Format for Status {
    fn format(&self, fmt: defmt::Formatter) {
        defmt::write!(
            fmt,
            "Status {{ mode: {}, cmd: {} }}",
            self.mode(),
            self.cmd()
        )
    }
}