makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / lib / FHSS / FHSS.cpp
blobb803aea9ce20ab83fcb314a08ea53fa065a13c4d
1 #include "FHSS.h"
2 #include "logging.h"
3 #include <string.h>
5 // Our table of FHSS frequencies. Define a regulatory domain to select the correct set for your location and radio
6 #ifdef Regulatory_Domain_AU_433
7 const uint32_t FHSSfreqs[] = {
8 FREQ_HZ_TO_REG_VAL(433420000),
9 FREQ_HZ_TO_REG_VAL(433920000),
10 FREQ_HZ_TO_REG_VAL(434420000)};
11 #elif defined Regulatory_Domain_AU_915
12 const uint32_t FHSSfreqs[] = {
13 FREQ_HZ_TO_REG_VAL(915500000),
14 FREQ_HZ_TO_REG_VAL(916100000),
15 FREQ_HZ_TO_REG_VAL(916700000),
16 FREQ_HZ_TO_REG_VAL(917300000),
18 FREQ_HZ_TO_REG_VAL(917900000),
19 FREQ_HZ_TO_REG_VAL(918500000),
20 FREQ_HZ_TO_REG_VAL(919100000),
21 FREQ_HZ_TO_REG_VAL(919700000),
23 FREQ_HZ_TO_REG_VAL(920300000),
24 FREQ_HZ_TO_REG_VAL(920900000),
25 FREQ_HZ_TO_REG_VAL(921500000),
26 FREQ_HZ_TO_REG_VAL(922100000),
28 FREQ_HZ_TO_REG_VAL(922700000),
29 FREQ_HZ_TO_REG_VAL(923300000),
30 FREQ_HZ_TO_REG_VAL(923900000),
31 FREQ_HZ_TO_REG_VAL(924500000),
33 FREQ_HZ_TO_REG_VAL(925100000),
34 FREQ_HZ_TO_REG_VAL(925700000),
35 FREQ_HZ_TO_REG_VAL(926300000),
36 FREQ_HZ_TO_REG_VAL(926900000)};
37 #elif defined Regulatory_Domain_EU_868
38 /* Frequency bands taken from https://wetten.overheid.nl/BWBR0036378/2016-12-28#Bijlagen
39 * Note: these frequencies fall in the license free H-band, but in combination with 500kHz
40 * LoRa modem bandwidth used by ExpressLRS (EU allows up to 125kHz modulation BW only) they
41 * will never pass RED certification and they are ILLEGAL to use.
43 * Therefore we simply maximize the usage of available spectrum so laboratory testing of the software won't disturb existing
44 * 868MHz ISM band traffic too much.
46 const uint32_t FHSSfreqs[] = {
47 FREQ_HZ_TO_REG_VAL(863275000), // band H1, 863 - 865MHz, 0.1% duty cycle or CSMA techniques, 25mW EIRP
48 FREQ_HZ_TO_REG_VAL(863800000),
49 FREQ_HZ_TO_REG_VAL(864325000),
50 FREQ_HZ_TO_REG_VAL(864850000),
51 FREQ_HZ_TO_REG_VAL(865375000), // Band H2, 865 - 868.6MHz, 1.0% dutycycle or CSMA, 25mW EIRP
52 FREQ_HZ_TO_REG_VAL(865900000),
53 FREQ_HZ_TO_REG_VAL(866425000),
54 FREQ_HZ_TO_REG_VAL(866950000),
55 FREQ_HZ_TO_REG_VAL(867475000),
56 FREQ_HZ_TO_REG_VAL(868000000),
57 FREQ_HZ_TO_REG_VAL(868525000), // Band H3, 868.7-869.2MHz, 0.1% dutycycle or CSMA, 25mW EIRP
58 FREQ_HZ_TO_REG_VAL(869050000),
59 FREQ_HZ_TO_REG_VAL(869575000)};
60 #elif defined Regulatory_Domain_IN_866
61 /**
62 * India currently delicensed the 865-867 MHz band with a maximum of 1W Transmitter power,
63 * 4Watts Effective Radiated Power and 200Khz carrier bandwidth as per
64 * https://dot.gov.in/sites/default/files/Delicensing%20in%20865-867%20MHz%20band%20%5BGSR%20564%20%28E%29%5D_0.pdf .
65 * There is currently no mention of Direct-sequence spread spectrum,
66 * So these frequencies are a subset of Regulatory_Domain_EU_868 frequencies.
68 const uint32_t FHSSfreqs[] = {
69 FREQ_HZ_TO_REG_VAL(865375000),
70 FREQ_HZ_TO_REG_VAL(865900000),
71 FREQ_HZ_TO_REG_VAL(866425000),
72 FREQ_HZ_TO_REG_VAL(866950000)};
73 #elif defined Regulatory_Domain_EU_433
74 /* Frequency band G, taken from https://wetten.overheid.nl/BWBR0036378/2016-12-28#Bijlagen
75 * Note: As is the case with the 868Mhz band, these frequencies only comply to the license free portion
76 * of the spectrum, nothing else. As such, these are likely illegal to use.
78 const uint32_t FHSSfreqs[] = {
79 FREQ_HZ_TO_REG_VAL(433100000),
80 FREQ_HZ_TO_REG_VAL(433925000),
81 FREQ_HZ_TO_REG_VAL(434450000)};
82 #elif defined Regulatory_Domain_FCC_915
83 /* Very definitely not fully checked. An initial pass at increasing the hops
85 const uint32_t FHSSfreqs[] = {
86 FREQ_HZ_TO_REG_VAL(903500000),
87 FREQ_HZ_TO_REG_VAL(904100000),
88 FREQ_HZ_TO_REG_VAL(904700000),
89 FREQ_HZ_TO_REG_VAL(905300000),
91 FREQ_HZ_TO_REG_VAL(905900000),
92 FREQ_HZ_TO_REG_VAL(906500000),
93 FREQ_HZ_TO_REG_VAL(907100000),
94 FREQ_HZ_TO_REG_VAL(907700000),
96 FREQ_HZ_TO_REG_VAL(908300000),
97 FREQ_HZ_TO_REG_VAL(908900000),
98 FREQ_HZ_TO_REG_VAL(909500000),
99 FREQ_HZ_TO_REG_VAL(910100000),
101 FREQ_HZ_TO_REG_VAL(910700000),
102 FREQ_HZ_TO_REG_VAL(911300000),
103 FREQ_HZ_TO_REG_VAL(911900000),
104 FREQ_HZ_TO_REG_VAL(912500000),
106 FREQ_HZ_TO_REG_VAL(913100000),
107 FREQ_HZ_TO_REG_VAL(913700000),
108 FREQ_HZ_TO_REG_VAL(914300000),
109 FREQ_HZ_TO_REG_VAL(914900000),
111 FREQ_HZ_TO_REG_VAL(915500000), // as per AU..
112 FREQ_HZ_TO_REG_VAL(916100000),
113 FREQ_HZ_TO_REG_VAL(916700000),
114 FREQ_HZ_TO_REG_VAL(917300000),
116 FREQ_HZ_TO_REG_VAL(917900000),
117 FREQ_HZ_TO_REG_VAL(918500000),
118 FREQ_HZ_TO_REG_VAL(919100000),
119 FREQ_HZ_TO_REG_VAL(919700000),
121 FREQ_HZ_TO_REG_VAL(920300000),
122 FREQ_HZ_TO_REG_VAL(920900000),
123 FREQ_HZ_TO_REG_VAL(921500000),
124 FREQ_HZ_TO_REG_VAL(922100000),
126 FREQ_HZ_TO_REG_VAL(922700000),
127 FREQ_HZ_TO_REG_VAL(923300000),
128 FREQ_HZ_TO_REG_VAL(923900000),
129 FREQ_HZ_TO_REG_VAL(924500000),
131 FREQ_HZ_TO_REG_VAL(925100000),
132 FREQ_HZ_TO_REG_VAL(925700000),
133 FREQ_HZ_TO_REG_VAL(926300000),
134 FREQ_HZ_TO_REG_VAL(926900000)};
135 #elif Regulatory_Domain_ISM_2400
136 const uint32_t FHSSfreqs[] = {
137 FREQ_HZ_TO_REG_VAL(2400400000),
138 FREQ_HZ_TO_REG_VAL(2401400000),
139 FREQ_HZ_TO_REG_VAL(2402400000),
140 FREQ_HZ_TO_REG_VAL(2403400000),
141 FREQ_HZ_TO_REG_VAL(2404400000),
143 FREQ_HZ_TO_REG_VAL(2405400000),
144 FREQ_HZ_TO_REG_VAL(2406400000),
145 FREQ_HZ_TO_REG_VAL(2407400000),
146 FREQ_HZ_TO_REG_VAL(2408400000),
147 FREQ_HZ_TO_REG_VAL(2409400000),
149 FREQ_HZ_TO_REG_VAL(2410400000),
150 FREQ_HZ_TO_REG_VAL(2411400000),
151 FREQ_HZ_TO_REG_VAL(2412400000),
152 FREQ_HZ_TO_REG_VAL(2413400000),
153 FREQ_HZ_TO_REG_VAL(2414400000),
155 FREQ_HZ_TO_REG_VAL(2415400000),
156 FREQ_HZ_TO_REG_VAL(2416400000),
157 FREQ_HZ_TO_REG_VAL(2417400000),
158 FREQ_HZ_TO_REG_VAL(2418400000),
159 FREQ_HZ_TO_REG_VAL(2419400000),
161 FREQ_HZ_TO_REG_VAL(2420400000),
162 FREQ_HZ_TO_REG_VAL(2421400000),
163 FREQ_HZ_TO_REG_VAL(2422400000),
164 FREQ_HZ_TO_REG_VAL(2423400000),
165 FREQ_HZ_TO_REG_VAL(2424400000),
167 FREQ_HZ_TO_REG_VAL(2425400000),
168 FREQ_HZ_TO_REG_VAL(2426400000),
169 FREQ_HZ_TO_REG_VAL(2427400000),
170 FREQ_HZ_TO_REG_VAL(2428400000),
171 FREQ_HZ_TO_REG_VAL(2429400000),
173 FREQ_HZ_TO_REG_VAL(2430400000),
174 FREQ_HZ_TO_REG_VAL(2431400000),
175 FREQ_HZ_TO_REG_VAL(2432400000),
176 FREQ_HZ_TO_REG_VAL(2433400000),
177 FREQ_HZ_TO_REG_VAL(2434400000),
179 FREQ_HZ_TO_REG_VAL(2435400000),
180 FREQ_HZ_TO_REG_VAL(2436400000),
181 FREQ_HZ_TO_REG_VAL(2437400000),
182 FREQ_HZ_TO_REG_VAL(2438400000),
183 FREQ_HZ_TO_REG_VAL(2439400000),
185 FREQ_HZ_TO_REG_VAL(2440400000),
186 FREQ_HZ_TO_REG_VAL(2441400000),
187 FREQ_HZ_TO_REG_VAL(2442400000),
188 FREQ_HZ_TO_REG_VAL(2443400000),
189 FREQ_HZ_TO_REG_VAL(2444400000),
191 FREQ_HZ_TO_REG_VAL(2445400000),
192 FREQ_HZ_TO_REG_VAL(2446400000),
193 FREQ_HZ_TO_REG_VAL(2447400000),
194 FREQ_HZ_TO_REG_VAL(2448400000),
195 FREQ_HZ_TO_REG_VAL(2449400000),
197 FREQ_HZ_TO_REG_VAL(2450400000),
198 FREQ_HZ_TO_REG_VAL(2451400000),
199 FREQ_HZ_TO_REG_VAL(2452400000),
200 FREQ_HZ_TO_REG_VAL(2453400000),
201 FREQ_HZ_TO_REG_VAL(2454400000),
203 FREQ_HZ_TO_REG_VAL(2455400000),
204 FREQ_HZ_TO_REG_VAL(2456400000),
205 FREQ_HZ_TO_REG_VAL(2457400000),
206 FREQ_HZ_TO_REG_VAL(2458400000),
207 FREQ_HZ_TO_REG_VAL(2459400000),
209 FREQ_HZ_TO_REG_VAL(2460400000),
210 FREQ_HZ_TO_REG_VAL(2461400000),
211 FREQ_HZ_TO_REG_VAL(2462400000),
212 FREQ_HZ_TO_REG_VAL(2463400000),
213 FREQ_HZ_TO_REG_VAL(2464400000),
215 FREQ_HZ_TO_REG_VAL(2465400000),
216 FREQ_HZ_TO_REG_VAL(2466400000),
217 FREQ_HZ_TO_REG_VAL(2467400000),
218 FREQ_HZ_TO_REG_VAL(2468400000),
219 FREQ_HZ_TO_REG_VAL(2469400000),
221 FREQ_HZ_TO_REG_VAL(2470400000),
222 FREQ_HZ_TO_REG_VAL(2471400000),
223 FREQ_HZ_TO_REG_VAL(2472400000),
224 FREQ_HZ_TO_REG_VAL(2473400000),
225 FREQ_HZ_TO_REG_VAL(2474400000),
227 FREQ_HZ_TO_REG_VAL(2475400000),
228 FREQ_HZ_TO_REG_VAL(2476400000),
229 FREQ_HZ_TO_REG_VAL(2477400000),
230 FREQ_HZ_TO_REG_VAL(2478400000),
231 FREQ_HZ_TO_REG_VAL(2479400000)};
232 #else
233 #error No regulatory domain defined, please define one in user_defines.txt
234 #endif
236 // Number of FHSS frequencies in the table
237 constexpr uint32_t FHSS_FREQ_CNT = (sizeof(FHSSfreqs) / sizeof(uint32_t));
238 // Number of hops in the FHSSsequence list before circling back around, even multiple of the number of frequencies
239 constexpr uint8_t FHSS_SEQUENCE_CNT = (256 / FHSS_FREQ_CNT) * FHSS_FREQ_CNT;
240 // Actual sequence of hops as indexes into the frequency list
241 uint8_t FHSSsequence[FHSS_SEQUENCE_CNT];
242 // Which entry in the sequence we currently are on
243 uint8_t volatile FHSSptr;
244 // Channel for sync packets and initial connection establishment
245 uint_fast8_t sync_channel;
246 // Offset from the predefined frequency determined by AFC on Team900 (register units)
247 int32_t FreqCorrection;
250 Requirements:
251 1. 0 every n hops
252 2. No two repeated channels
253 3. Equal occurance of each (or as even as possible) of each channel
254 4. Pseudorandom
256 Approach:
257 Fill the sequence array with the sync channel every FHSS_FREQ_CNT
258 Iterate through the array, and for each block, swap each entry in it with
259 another random entry, excluding the sync channel.
262 void FHSSrandomiseFHSSsequence(const uint32_t seed)
264 #ifdef Regulatory_Domain_AU_915
265 INFOLN("Setting 915MHz AU Mode");
266 #elif defined Regulatory_Domain_FCC_915
267 INFOLN("Setting 915MHz FCC Mode");
268 #elif defined Regulatory_Domain_EU_868
269 INFOLN("Setting 868MHz EU Mode");
270 #elif defined Regulatory_Domain_IN_866
271 INFOLN("Setting 866MHz IN Mode");
272 #elif defined Regulatory_Domain_AU_433
273 INFOLN("Setting 433MHz AU Mode");
274 #elif defined Regulatory_Domain_EU_433
275 INFOLN("Setting 433MHz EU Mode");
276 #elif defined Regulatory_Domain_ISM_2400
277 INFOLN("Setting 2400MHz Mode");
278 #else
279 #error No regulatory domain defined, please define one in common.h
280 #endif
282 DBGLN("Number of FHSS frequencies = %u", FHSS_FREQ_CNT);
284 sync_channel = FHSS_FREQ_CNT / 2;
285 DBGLN("Sync channel = %u", sync_channel);
287 // reset the pointer (otherwise the tests fail)
288 FHSSptr = 0;
289 rngSeed(seed);
291 // initialize the sequence array
292 for (uint8_t i = 0; i < FHSS_SEQUENCE_CNT; i++)
294 if (i % FHSS_FREQ_CNT == 0) {
295 FHSSsequence[i] = sync_channel;
296 } else if (i % FHSS_FREQ_CNT == sync_channel) {
297 FHSSsequence[i] = 0;
298 } else {
299 FHSSsequence[i] = i % FHSS_FREQ_CNT;
303 for (uint8_t i=0; i < FHSS_SEQUENCE_CNT; i++)
305 // if it's not the sync channel
306 if (i % FHSS_FREQ_CNT != 0)
308 uint8_t offset = (i / FHSS_FREQ_CNT) * FHSS_FREQ_CNT; // offset to start of current block
309 uint8_t rand = rngN(FHSS_FREQ_CNT-1)+1; // random number between 1 and FHSS_FREQ_CNT
311 // switch this entry and another random entry in the same block
312 uint8_t temp = FHSSsequence[i];
313 FHSSsequence[i] = FHSSsequence[offset+rand];
314 FHSSsequence[offset+rand] = temp;
318 // output FHSS sequence
319 for (uint8_t i=0; i < FHSS_SEQUENCE_CNT; i++)
321 DBG("%u ",FHSSsequence[i]);
322 if (i % 10 == 9)
323 DBGCR;
325 DBGCR;
328 uint32_t FHSSgetChannelCount(void)
330 return FHSS_FREQ_CNT;