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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/// Interrupt lines.
///
/// Argument of [`CfgIrq::irq_enable`] and [`CfgIrq::irq_disable`].
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum IrqLine {
    /// Global interrupt.
    Global,
    /// Interrupt line 1.
    ///
    /// This will output to the [`RfIrq0`](crate::gpio::RfIrq0) pin.
    Line1,
    /// Interrupt line 2.
    ///
    /// This will output to the [`RfIrq1`](crate::gpio::RfIrq1) pin.
    Line2,
    /// Interrupt line 3.
    ///
    /// This will output to the [`RfIrq2`](crate::gpio::RfIrq2) pin.
    Line3,
}

impl IrqLine {
    pub(super) const fn offset(&self) -> usize {
        match self {
            IrqLine::Global => 1,
            IrqLine::Line1 => 3,
            IrqLine::Line2 => 5,
            IrqLine::Line3 => 7,
        }
    }
}

/// IRQ bit mapping
///
/// See table 37 "IRQ bit mapping and definition" in the reference manual for
/// more information.
#[repr(u16)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Irq {
    /// Packet transmission finished.
    ///
    /// * Packet type: LoRa and GFSK
    /// * Operation: TX
    TxDone = (1 << 0),
    /// Packet reception finished.
    ///
    /// * Packet type: LoRa and GFSK
    /// * Operation: RX
    RxDone = (1 << 1),
    /// Preamble detected.
    ///
    /// * Packet type: LoRa and GFSK
    /// * Operation: RX
    PreambleDetected = (1 << 2),
    /// Synchronization word valid.
    ///
    /// * Packet type: GFSK
    /// * Operation: RX
    SyncDetected = (1 << 3),
    /// Header valid.
    ///
    /// * Packet type: LoRa
    /// * Operation: RX
    HeaderValid = (1 << 4),
    /// Header CRC error.
    ///
    /// * Packet type: LoRa
    /// * Operation: RX
    HeaderErr = (1 << 5),
    /// Dual meaning error.
    ///
    /// For GFSK RX this indicates a preamble, syncword, address, CRC, or length
    /// error.
    ///
    /// For LoRa RX this indicates a CRC error.
    Err = (1 << 6),
    /// Channel activity detection finished.
    ///
    /// * Packet type: LoRa
    /// * Operation: CAD
    CadDone = (1 << 7),
    /// Channel activity detected.
    ///
    /// * Packet type: LoRa
    /// * Operation: CAD
    CadDetected = (1 << 8),
    /// RX or TX timeout.
    ///
    /// * Packet type: LoRa and GFSK
    /// * Operation: RX and TX
    Timeout = (1 << 9),
}

impl Irq {
    /// Get the bitmask for an IRQ.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::Irq;
    ///
    /// assert_eq!(Irq::TxDone.mask(), 0x0001);
    /// assert_eq!(Irq::Timeout.mask(), 0x0200);
    /// ```
    pub const fn mask(self) -> u16 {
        self as u16
    }
}

/// Argument for [`set_irq_cfg`].
///
/// [`set_irq_cfg`]: crate::subghz::SubGhz::set_irq_cfg
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct CfgIrq {
    buf: [u8; 9],
}

impl CfgIrq {
    /// Create a new `CfgIrq`.
    ///
    /// This is the same as `default`, but in a `const` function.
    ///
    /// The default value has all interrupts disabled on all lines.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::CfgIrq;
    ///
    /// const IRQ_CFG: CfgIrq = CfgIrq::new();
    /// ```
    pub const fn new() -> CfgIrq {
        CfgIrq {
            buf: [
                super::OpCode::CfgDioIrq as u8,
                0x00,
                0x00,
                0x00,
                0x00,
                0x00,
                0x00,
                0x00,
                0x00,
            ],
        }
    }

    /// Enable an interrupt.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{CfgIrq, Irq, IrqLine};
    ///
    /// const IRQ_CFG: CfgIrq = CfgIrq::new()
    ///     .irq_enable(IrqLine::Global, Irq::TxDone)
    ///     .irq_enable(IrqLine::Global, Irq::Timeout);
    /// # assert_eq!(IRQ_CFG.as_slice()[1], 0x02);
    /// # assert_eq!(IRQ_CFG.as_slice()[2], 0x01);
    /// # assert_eq!(IRQ_CFG.as_slice()[3], 0x00);
    /// ```
    #[must_use = "irq_enable returns a modified CfgIrq"]
    pub const fn irq_enable(mut self, line: IrqLine, irq: Irq) -> CfgIrq {
        let mask: u16 = irq as u16;
        let offset: usize = line.offset();
        self.buf[offset] |= ((mask >> 8) & 0xFF) as u8;
        self.buf[offset + 1] |= (mask & 0xFF) as u8;
        self
    }

    /// Enable an interrupt on all lines.
    ///
    /// As far as I can tell with empirical testing all IRQ lines need to be
    /// enabled for the internal interrupt to be pending in the NVIC.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{CfgIrq, Irq};
    ///
    /// const IRQ_CFG: CfgIrq = CfgIrq::new()
    ///     .irq_enable_all(Irq::TxDone)
    ///     .irq_enable_all(Irq::Timeout);
    /// # assert_eq!(IRQ_CFG.as_slice()[1], 0x02);
    /// # assert_eq!(IRQ_CFG.as_slice()[2], 0x01);
    /// # assert_eq!(IRQ_CFG.as_slice()[3], 0x02);
    /// # assert_eq!(IRQ_CFG.as_slice()[4], 0x01);
    /// # assert_eq!(IRQ_CFG.as_slice()[5], 0x02);
    /// # assert_eq!(IRQ_CFG.as_slice()[6], 0x01);
    /// # assert_eq!(IRQ_CFG.as_slice()[7], 0x02);
    /// # assert_eq!(IRQ_CFG.as_slice()[8], 0x01);
    /// ```
    #[must_use = "irq_enable_all returns a modified CfgIrq"]
    pub const fn irq_enable_all(mut self, irq: Irq) -> CfgIrq {
        let mask: [u8; 2] = irq.mask().to_be_bytes();

        self.buf[1] |= mask[0];
        self.buf[2] |= mask[1];
        self.buf[3] |= mask[0];
        self.buf[4] |= mask[1];
        self.buf[5] |= mask[0];
        self.buf[6] |= mask[1];
        self.buf[7] |= mask[0];
        self.buf[8] |= mask[1];

        self
    }

    /// Disable an interrupt.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{CfgIrq, Irq, IrqLine};
    ///
    /// const IRQ_CFG: CfgIrq = CfgIrq::new()
    ///     .irq_enable(IrqLine::Global, Irq::TxDone)
    ///     .irq_enable(IrqLine::Global, Irq::Timeout)
    ///     .irq_disable(IrqLine::Global, Irq::TxDone)
    ///     .irq_disable(IrqLine::Global, Irq::Timeout);
    /// # assert_eq!(IRQ_CFG.as_slice()[1], 0x00);
    /// # assert_eq!(IRQ_CFG.as_slice()[2], 0x00);
    /// # assert_eq!(IRQ_CFG.as_slice()[3], 0x00);
    /// ```
    #[must_use = "irq_disable returns a modified CfgIrq"]
    pub const fn irq_disable(mut self, line: IrqLine, irq: Irq) -> CfgIrq {
        let mask: u16 = !(irq as u16);
        let offset: usize = line.offset();
        self.buf[offset] &= ((mask >> 8) & 0xFF) as u8;
        self.buf[offset + 1] &= (mask & 0xFF) as u8;
        self
    }

    /// Disable an interrupt on all lines.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{CfgIrq, Irq};
    ///
    /// const IRQ_CFG: CfgIrq = CfgIrq::new()
    ///     .irq_enable_all(Irq::TxDone)
    ///     .irq_enable_all(Irq::Timeout)
    ///     .irq_disable_all(Irq::TxDone)
    ///     .irq_disable_all(Irq::Timeout);
    /// # assert_eq!(IRQ_CFG, CfgIrq::new());
    /// ```
    #[must_use = "irq_disable_all returns a modified CfgIrq"]
    pub const fn irq_disable_all(mut self, irq: Irq) -> CfgIrq {
        let mask: [u8; 2] = (!irq.mask()).to_be_bytes();

        self.buf[1] &= mask[0];
        self.buf[2] &= mask[1];
        self.buf[3] &= mask[0];
        self.buf[4] &= mask[1];
        self.buf[5] &= mask[0];
        self.buf[6] &= mask[1];
        self.buf[7] &= mask[0];
        self.buf[8] &= mask[1];

        self
    }

    /// Extracts a slice containing the packet.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::{CfgIrq, Irq};
    ///
    /// const IRQ_CFG: CfgIrq = CfgIrq::new()
    ///     .irq_enable_all(Irq::TxDone)
    ///     .irq_enable_all(Irq::Timeout);
    ///
    /// assert_eq!(
    ///     IRQ_CFG.as_slice(),
    ///     &[0x08, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01, 0x02, 0x01]
    /// );
    /// ```
    pub const fn as_slice(&self) -> &[u8] {
        &self.buf
    }
}

impl Default for CfgIrq {
    fn default() -> Self {
        Self::new()
    }
}