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
/// RF frequency structure.
///
/// Argument of [`set_rf_frequency`].
///
/// [`set_rf_frequency`]: super::SubGhz::set_rf_frequency
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct RfFreq {
    buf: [u8; 5],
}

impl RfFreq {
    /// 915MHz, often used in Australia and North America.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::RfFreq;
    ///
    /// assert_eq!(RfFreq::F915.freq(), 915_000_000);
    /// ```
    pub const F915: RfFreq = RfFreq::from_raw(0x39_30_00_00);

    /// 868MHz, often used in Europe.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::RfFreq;
    ///
    /// assert_eq!(RfFreq::F868.freq(), 868_000_000);
    /// ```
    pub const F868: RfFreq = RfFreq::from_raw(0x36_40_00_00);

    /// 433MHz, often used in Europe.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::RfFreq;
    ///
    /// assert_eq!(RfFreq::F433.freq(), 433_000_000);
    /// ```
    pub const F433: RfFreq = RfFreq::from_raw(0x1B_10_00_00);

    /// Create a new `RfFreq` from a raw bit value.
    ///
    /// The equation used to get the PLL frequency from the raw bits is:
    ///
    /// RF<sub>PLL</sub> = 32e6 × bits / 2<sup>25</sup>
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::RfFreq;
    ///
    /// const FREQ: RfFreq = RfFreq::from_raw(0x39300000);
    /// assert_eq!(FREQ, RfFreq::F915);
    /// ```
    pub const fn from_raw(bits: u32) -> RfFreq {
        RfFreq {
            buf: [
                super::OpCode::SetRfFrequency as u8,
                ((bits >> 24) & 0xFF) as u8,
                ((bits >> 16) & 0xFF) as u8,
                ((bits >> 8) & 0xFF) as u8,
                (bits & 0xFF) as u8,
            ],
        }
    }

    /// Create a new `RfFreq` from a PLL frequency.
    ///
    /// The equation used to get the raw bits from the PLL frequency is:
    ///
    /// bits = RF<sub>PLL</sub> * 2<sup>25</sup> / 32e6
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::RfFreq;
    ///
    /// const FREQ: RfFreq = RfFreq::from_frequency(915_000_000);
    /// assert_eq!(FREQ, RfFreq::F915);
    /// ```
    pub const fn from_frequency(freq: u32) -> RfFreq {
        Self::from_raw((((freq as u64) * (1 << 25)) / 32_000_000) as u32)
    }

    // Get the frequency bit value.
    const fn as_bits(&self) -> u32 {
        ((self.buf[1] as u32) << 24)
            | ((self.buf[2] as u32) << 16)
            | ((self.buf[3] as u32) << 8)
            | (self.buf[4] as u32)
    }

    /// Get the actual frequency.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::RfFreq;
    ///
    /// assert_eq!(RfFreq::from_raw(0x39300000).freq(), 915_000_000);
    /// ```
    pub fn freq(&self) -> u32 {
        (32_000_000 * (self.as_bits() as u64) / (1 << 25)) as u32
    }

    /// Extracts a slice containing the packet.
    ///
    /// # Example
    ///
    /// ```
    /// use stm32wlxx_hal::subghz::RfFreq;
    ///
    /// assert_eq!(RfFreq::F915.as_slice(), &[0x86, 0x39, 0x30, 0x00, 0x00]);
    /// ```
    pub const fn as_slice(&self) -> &[u8] {
        &self.buf
    }
}

#[cfg(test)]
mod test {
    use super::RfFreq;

    #[test]
    fn max() {
        assert_eq!(RfFreq::from_raw(u32::MAX).freq(), 4_095_999_999);
    }

    #[test]
    fn min() {
        assert_eq!(RfFreq::from_raw(u32::MIN).freq(), 0);
    }
}