loraModem.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. // 1-channel LoRa Gateway for ESP8266
  2. // Copyright (c) 2016, 2017, 2018, 2019 Maarten Westenberg version for ESP8266
  3. // Version 6.1.0
  4. // Date: 2019-10-20
  5. //
  6. // based on work done by Thomas Telkamp for Raspberry PI 1ch gateway
  7. // and many other contributors.
  8. //
  9. // All rights reserved. This program and the accompanying materials
  10. // are made available under the terms of the MIT License
  11. // which accompanies this distribution, and is available at
  12. // https://opensource.org/licenses/mit-license.php
  13. //
  14. // NO WARRANTY OF ANY KIND IS PROVIDED
  15. //
  16. // Author: Maarten Westenberg (mw12554@hotmail.com)
  17. //
  18. // This file contains a number of compile-time settings and declarations that are
  19. // specific to the LoRa rfm95, sx1276, sx1272 radio of the gateway.
  20. //
  21. //
  22. // ------------------------------------------------------------------------------------
  23. // ----------------------------------------
  24. // Used by REG_PAYLOAD_LENGTH to set receive payload length
  25. #define PAYLOAD_LENGTH 0x40 // 64 bytes
  26. #define MAX_PAYLOAD_LENGTH 0x80 // 128 bytes
  27. // In order to make the CAD behaviour dynamic we set a variable
  28. // when the CAD functions are defined. Value of 3 is minimum frequencies a
  29. // gateway should support to be fully LoRa compliant.
  30. // For performance reasons, 3 is the maximum as well!
  31. //
  32. #define NUM_HOPS 3
  33. // Do not change these setting for RSSI detection. They are used for CAD
  34. // Given the correction factor of 157, we can get to -122dB with this rating
  35. //
  36. #define RSSI_LIMIT 35 //
  37. // How long to wait in LoRa mode before using the RSSI value.
  38. // This period should be as short as possible, yet sufficient
  39. //
  40. #define RSSI_WAIT 6 // was 25
  41. // How long will it take when hopping before a CDONE or CDETD value
  42. // is present and can be measured.
  43. //
  44. #define EVENT_WAIT 15000 // XXX 180520 was 25 milliseconds before CDDETD timeout
  45. #define DONE_WAIT 1950 // 2000 microseconds (1/500) sec between CDDONE events
  46. // Our code should correct the server Tramission delay settings
  47. long txDelay= 0x00; // tx delay time on top of server TMST
  48. // SPI setting. 8MHz seems to be the max
  49. #define SPISPEED 8000000 // Set to 8 * 10E6
  50. // Frequencies
  51. // Set center frequency. If in doubt, choose the first one, comment all others
  52. // Each "real" gateway should support the first 3 frequencies according to LoRa spec.
  53. // NOTE: This means you have to specify at least 3 frequencies here for the single
  54. // channel gateway to work.
  55. struct vector {
  56. // Upstream messages
  57. uint32_t upFreq; // 4 bytes
  58. uint16_t upBW; // 2 bytes
  59. uint8_t upLo; // 1 bytes
  60. uint8_t upHi; // 1 bytes
  61. // Downstream messages
  62. uint32_t dwnFreq; // 4 bytes Unsigned ubt Frequency
  63. uint16_t dwnBW; // 2 bytes BW Specification
  64. uint8_t dwnLo; // 1 bytes Spreading Factor
  65. uint8_t dwnHi; // 1 bytes
  66. };
  67. #ifdef EU863_870
  68. // This the the EU863_870 format as used in most of Europe
  69. // It is also the default for most of the single channel gateway work.
  70. // For each frequency SF7-SF12 are used.
  71. vector freqs [] =
  72. {
  73. { 868100000, 125, 7, 12, 868100000, 125, 7, 12}, // Channel 0, 868.1 MHz/125 primary
  74. { 868300000, 125, 7, 12, 868300000, 125, 7, 12}, // Channel 1, 868.3 MHz/125 mandatory and (SF7BW250)
  75. { 868500000, 125, 7, 12, 868500000, 125, 7, 12}, // Channel 2, 868.5 MHz/125 mandatory
  76. { 867100000, 125, 7, 12, 867100000, 125, 7, 12}, // Channel 3, 867.1 MHz/125 Optional
  77. { 867300000, 125, 7, 12, 867300000, 125, 7, 12}, // Channel 4, 867.3 MHz/125 Optional
  78. { 867500000, 125, 7, 12, 867500000, 125, 7, 12}, // Channel 5, 867.5 MHz/125 Optional
  79. { 867700000, 125, 7, 12, 867700000, 125, 7, 12}, // Channel 6, 867.7 MHz/125 Optional
  80. { 867900000, 125, 7, 12, 867900000, 125, 7, 12}, // Channel 7, 867.9 MHz/125 Optional
  81. { 868800000, 125, 7, 12, 868800000, 125, 7, 12}, // Channel 8, 868.9 MHz/125 FSK Only
  82. { 0, 0 , 0, 0, 869525000, 125, 9, 9} // Channel 9, 869.5 MHz/125 for RX2 responses SF9(10%)
  83. // TTN defines an additional channel at 869.525 MHz using SF9 for class B. Not used
  84. };
  85. #elif defined(EU433)
  86. // The following 3 frequencies should be defined/used in an EU433
  87. // environment. The plan is not defined for TTN yet so we use this one.
  88. vector freqs [] = {
  89. { 433175000, 125, 7, 12, 433175000, 125, 7, 12}, // Channel 0, 433.175 MHz/125 primary
  90. { 433375000, 125, 7, 12, 433375000, 125, 7, 12}, // Channel 1, 433.375 MHz primary
  91. { 433575000, 125, 7, 12, 433575000, 125, 7, 12}, // Channel 2, 433.575 MHz primary
  92. { 433775000, 125, 7, 12, 433775000, 125, 7, 12}, // Channel 3, 433.775 MHz primary
  93. { 433975000, 125, 7, 12, 433975000, 125, 7, 12}, // Channel 4, 433.975 MHz primary
  94. { 434175000, 125, 7, 12, 434175000, 125, 7, 12}, // Channel 5, 434.175 MHz primary
  95. { 434375000, 125, 7, 12, 434375000, 125, 7, 12}, // Channel 6, 434.375 MHz primary
  96. { 434575000, 125, 7, 12, 434575000, 125, 7, 12}, // Channel 7, 434.575 MHz primary
  97. { 434775000, 125, 7, 12, 434775000, 125, 7, 12} // Channel 8, 434.775 MHz primary
  98. };
  99. #elif defined(US902_928)
  100. // The frequency plan for USA is a difficult one. As yout can see, the uplink protocol uses
  101. // SF7-SF10 and BW125 whereas the downlink protocol uses SF7-SF12 and BW500.
  102. // Also the number of chanels is not equal.
  103. vector freqs [] = {
  104. // Uplink
  105. { 903900000, 125, 7, 10, 923300000, 500, 7, 12}, // Up Ch 0, SF7BW125 to SF10BW125 primary
  106. { 904100000, 125, 7, 10, 923900000, 500, 7, 12}, // Up Ch 1, SF7BW125 to SF10BW125
  107. { 904300000, 125, 7, 10, 924500000, 500, 7, 12}, // Up Ch 2, SF7BW125 to SF10BW125, Dwn SF7-SF12 924,5 BW500
  108. { 904500000, 125, 7, 10, 925100000, 500, 7, 12}, // Up Ch 3, SF7BW125 to SF10BW125, Dwn SF7-SF12 925,1 BW500
  109. { 904700000, 125, 7, 10, 925700000, 500, 7, 12}, // Up Ch 3, SF7BW125 to SF10BW125, Dwn SF7-SF12 925,1
  110. { 904900000, 125, 7, 10, 926300000, 500, 7, 12}, // Up Ch 4, SF7BW125 to SF10BW125, Dwn SF7-SF12
  111. { 905100000, 125, 7, 10, 926900000, 500, 7, 12}, // Up Ch 5, SF7BW125 to SF10BW125, Dwn SF7-SF12
  112. { 905300000, 125, 7, 10, 927500000, 500, 7, 12}, // Up Ch 6, SF7BW125 to SF10BW125, Dwn SF7-SF12
  113. { 904600000, 500, 8, 8, 0 , 0, 0, 00}, // Up Ch 7, SF8BW5000, no Dwn 0 // SFxxxBW500
  114. };
  115. #elif defined(AU925_928)
  116. // Australian plan or TTN/Lora frequencies
  117. vector freqs [] = {
  118. { 916800000, 125, 7, 10, 916800000, 125, 7, 12}, // Channel 0, 916.8 MHz primary
  119. { 917000000, 125, 7, 10, 917000000, 125, 7, 12}, // Channel 1, 917.0 MHz mandatory
  120. { 917200000, 125, 7, 10, 917200000, 125, 7, 12}, // Channel 2, 917.2 MHz mandatory
  121. { 917400000, 125, 7, 10, 917400000, 125, 7, 12}, // Channel 3, 917.4 MHz Optional
  122. { 917600000, 125, 7, 10, 917600000, 125, 7, 12}, // Channel 4, 917.6 MHz Optional
  123. { 917800000, 125, 7, 10, 917800000, 125, 7, 12}, // Channel 5, 917.8 MHz Optional
  124. { 918000000, 125, 7, 10, 918000000, 125, 7, 12}, // Channel 6, 918.0 MHz Optional
  125. { 918200000, 125, 7, 10, 918200000, 125, 7, 12} , // Channel 7, 918.2 MHz Optional
  126. { 917500000, 500, 8, 8, 0, 0, 0, 0} // Channel 8, 917.5 SF8BW500 MHz Optional Uplink
  127. };
  128. #elif defined(CN470_510)
  129. // China plan for TTN frequencies
  130. vector freqs [] = {
  131. { 486300000, 125, 7, 12, 486300000, 125, 7, 12}, // 486.3 - SF7BW125 to SF12BW125
  132. { 486500000, 125, 7, 12, 486500000, 125, 7, 12}, // 486.5 - SF7BW125 to SF12BW125
  133. { 486700000, 125, 7, 12, 486700000, 125, 7, 12}, // 486.7 - SF7BW125 to SF12BW125
  134. { 486900000, 125, 7, 12, 486900000, 125, 7, 12}, // 486.9 - SF7BW125 to SF12BW125
  135. { 487100000, 125, 7, 12, 487100000, 125, 7, 12}, // 487.1 - SF7BW125 to SF12BW125
  136. { 487300000, 125, 7, 12, 487300000, 125, 7, 12}, // 487.3 - SF7BW125 to SF12BW125
  137. { 487500000, 125, 7, 12, 487500000, 125, 7, 12}, // 487.5 - SF7BW125 to SF12BW125
  138. { 487700000, 125, 7, 12, 487700000, 125, 7, 12} // 487.7 - SF7BW125 to SF12BW125
  139. };
  140. #else
  141. int freqs [] = {
  142. // Print an Error, Not supported
  143. #error "Sorry, but your frequency plan is not supported"
  144. };
  145. #endif
  146. // Set the structure for spreading factor
  147. enum sf_t { SF6=6, SF7, SF8, SF9, SF10, SF11, SF12 };
  148. // The state of the receiver. See Semtech Datasheet (rev 4, March 2015) page 43
  149. // The _state is of the enum type (and should be cast when used as a number)
  150. enum state_t { S_INIT=0, S_SCAN, S_CAD, S_RX, S_TX, S_TXDONE};
  151. volatile state_t _state=S_INIT;
  152. volatile uint8_t _event=0;
  153. // rssi is measured at specific moments and reported on others
  154. // so we need to store the current value we like to work with
  155. uint8_t _rssi;
  156. bool _cad= (bool) _CAD; // Set to true for Channel Activity Detection, only when dio 1 connected
  157. bool _hop= (bool) false;// experimental; frequency hopping. Only use when dio2 connected
  158. unsigned long nowTime=0;
  159. unsigned long msgTime=0;
  160. unsigned long hopTime=0;
  161. unsigned long detTime=0;
  162. #if _PIN_OUT==1
  163. // ----------------------------------------------------------------------------
  164. // Definition of the GPIO pins used by the Gateway for Hallard type boards
  165. //
  166. struct pins {
  167. uint8_t dio0=15; // GPIO15 / D8. For the Hallard board shared between DIO0/DIO1/DIO2
  168. uint8_t dio1=15; // GPIO15 / D8. Used for CAD, may or not be shared with DIO0
  169. uint8_t dio2=15; // GPIO15 / D8. Used for frequency hopping, don't care
  170. uint8_t ss=16; // GPIO16 / D0. Select pin connected to GPIO16 / D0
  171. uint8_t rst=0; // GPIO 0 / D3. Reset pin not used
  172. // MISO 12 / D6
  173. // MOSI 13 / D7
  174. // CLK 14 / D5
  175. } pins;
  176. #elif _PIN_OUT==2
  177. // ----------------------------------------------------------------------------
  178. // For ComResult gateway PCB use the following settings
  179. struct pins {
  180. uint8_t dio0=5; // GPIO5 / D1. Dio0 used for one frequency and one SF
  181. uint8_t dio1=4; // GPIO4 / D2. Used for CAD, may or not be shared with DIO0
  182. uint8_t dio2=0; // GPIO0 / D3. Used for frequency hopping, don't care
  183. uint8_t ss=15; // GPIO15 / D8. Select pin connected to GPIO15
  184. uint8_t rst=0; // GPIO0 / D3. Reset pin not used
  185. } pins;
  186. #elif _PIN_OUT==3
  187. // ----------------------------------------------------------------------------
  188. // For ESP32/Wemos based board
  189. // SCK == GPIO5/ PIN5
  190. // SS == GPIO18/PIN18
  191. // MISO == GPIO19/ PIN19
  192. // MOSI == GPIO27/ PIN27
  193. // RST == GPIO14/ PIN14
  194. struct pins {
  195. uint8_t dio0=26; // GPIO26 / Dio0 used for one frequency and one SF
  196. uint8_t dio1=26; // GPIO26 / Used for CAD, may or not be shared with DIO0
  197. uint8_t dio2=26; // GPI2O6 / Used for frequency hopping, don't care
  198. uint8_t ss=18; // GPIO18 / Dx. Select pin connected to GPIO18
  199. uint8_t rst=14; // GPIO0 / D3. Reset pin not used
  200. } pins;
  201. #elif _PIN_OUT==4
  202. // ----------------------------------------------------------------------------
  203. // For ESP32/TTGO based board.
  204. // SCK == GPIO5/ PIN5
  205. // SS == GPIO18/PIN18 CS
  206. // MISO == GPIO19/ PIN19
  207. // MOSI == GPIO27/ PIN27
  208. // RST == GPIO14/ PIN14
  209. struct pins {
  210. uint8_t dio0=26; // GPIO26 / Dio0 used for one frequency and one SF
  211. uint8_t dio1=33; // GPIO26 / Used for CAD, may or not be shared with DIO0
  212. uint8_t dio2=32; // GPIO26 / Used for frequency hopping, don't care
  213. uint8_t ss=18; // GPIO18 / Dx. Select pin connected to GPIO18
  214. uint8_t rst=14; // GPIO0 / D3. Reset pin not used
  215. } pins;
  216. #define SCK 5
  217. #define MISO 19
  218. #define MOSI 27
  219. #define RST 14
  220. #define SS 18
  221. #define GPS_RX 15
  222. #define GPS_TX 12
  223. #elif _PIN_OUT==5
  224. // ----------------------------------------------------------------------------
  225. // For ESP32/TTGO based board for EU32 with 0.9" OLED
  226. // NOTE: This board shoudl be same as general type TTGO (nr 4)
  227. // but for the moment we include this as a separate item
  228. //
  229. // SCK == GPIO5/ PIN5
  230. // SS == GPIO18/PIN18 CS
  231. // MISO == GPIO19/ PIN19
  232. // MOSI == GPIO27/ PIN27
  233. // RST == GPIO14/ PIN14
  234. struct pins {
  235. uint8_t dio0=26; // GPIO26 / Dio0 used for one frequency and one SF
  236. uint8_t dio1=33; // GPIO26 / Used for CAD, may or not be shared with DIO0
  237. uint8_t dio2=32; // GPIO26 / Used for frequency hopping, don't care
  238. uint8_t ss=18; // GPIO18 / Dx. Select pin connected to GPIO18
  239. uint8_t rst=14; // GPIO0 / D3. Reset pin not used
  240. } pins;
  241. #define SCK 5 // Check
  242. #define MISO 19 // Check
  243. #define MOSI 27 // Check
  244. #define RST 14 // Check
  245. #define SS 18
  246. #else
  247. // ----------------------------------------------------------------------------
  248. // Use your own pin definitions, and comment #error line below
  249. // MISO 12 / D6
  250. // MOSI 13 / D7
  251. // CLK 14 / D5
  252. // SS 16 / D0
  253. #error "Pin Definitions _PIN_OUT must be 1(HALLARD) or 2 (COMRESULT)"
  254. #endif
  255. // stat_t contains the statistics that are kept by message.
  256. // Each time a message is received or sent the statistics are updated.
  257. // In case STATISTICS==1 we define the last MAX_STAT messages as statistics
  258. struct stat_t {
  259. unsigned long tmst; // Time since 1970 in seconds
  260. unsigned long node; // 4-byte DEVaddr (the only one known to gateway)
  261. uint8_t ch; // Channel index to freqs array
  262. uint8_t sf;
  263. #if RSSI==1
  264. int8_t rssi; // XXX Can be < -128
  265. #endif
  266. int8_t prssi; // XXX Can be < -128
  267. #if _LOCALSERVER==1
  268. uint8_t data[23]; // For memory purposes, only 23 chars
  269. uint8_t datal; // Length of decoded message 1 char
  270. #endif
  271. } stat_t;
  272. #if STATISTICS >= 1
  273. // statc_c contains the statistic that are gateway related and not per
  274. // message. Example: Number of messages received on SF7 or number of (re) boots
  275. // So where statr contains the statistics gathered per packet the statc_c
  276. // contains general statistics of the node
  277. struct stat_c {
  278. unsigned long msg_ok;
  279. unsigned long msg_ttl;
  280. unsigned long msg_down;
  281. #if STATISTICS >= 2 // Only if we explicitly set it higher
  282. unsigned long sf7; // Spreading factor 7 statistics/Count
  283. unsigned long sf8; // Spreading factor 8
  284. unsigned long sf9; // Spreading factor 9
  285. unsigned long sf10; // Spreading factor 10
  286. unsigned long sf11; // Spreading factor 11
  287. unsigned long sf12; // Spreading factor 12
  288. // If STATISTICS is 3, we add statistics about the channel
  289. // When only one channel is used, we normally know the spread of
  290. // statistics, but when HOP mode is selected we migth want to add this info
  291. #if STATISTICS >=3
  292. unsigned long msg_ok_0, msg_ok_1, msg_ok_2;
  293. unsigned long msg_ttl_0, msg_ttl_1, msg_ttl_2;
  294. unsigned long msg_down_0, msg_down_1, msg_down_2;
  295. unsigned long sf7_0, sf7_1, sf7_2;
  296. unsigned long sf8_0, sf8_1, sf8_2;
  297. unsigned long sf9_0, sf9_1, sf9_2;
  298. unsigned long sf10_0, sf10_1, sf10_2;
  299. unsigned long sf11_0, sf11_1, sf11_2;
  300. unsigned long sf12_0, sf12_1, sf12_2;
  301. #endif //3
  302. uint16_t boots; // Number of boots
  303. uint16_t resets;
  304. #endif // 2
  305. } stat_c;
  306. struct stat_c statc;
  307. // History of received uplink messages from nodes
  308. struct stat_t statr[MAX_STAT];
  309. #else // STATISTICS==0
  310. struct stat_t statr[1]; // Always have at least one element to store in
  311. #endif
  312. // Define the payload structure used to separate interrupt ans SPI
  313. // processing from the loop() part
  314. uint8_t payLoad[128]; // Payload i
  315. struct LoraBuffer {
  316. uint8_t * payLoad;
  317. uint8_t payLength;
  318. uint32_t tmst; // in millis()
  319. uint8_t sfTx;
  320. uint8_t powe;
  321. uint32_t fff;
  322. uint8_t crc;
  323. uint8_t iiq;
  324. } LoraDown;
  325. // Up buffer (from Lora sensor to UDP)
  326. //
  327. struct LoraUp {
  328. uint8_t payLoad[128];
  329. uint8_t payLength;
  330. int prssi;
  331. long snr;
  332. int rssicorr;
  333. uint8_t sf;
  334. } LoraUp;
  335. // ============================================================================
  336. // Set all definitions for Gateway
  337. // ============================================================================
  338. // Register definitions. These are the addresses of the RFM95, SX1276 that we
  339. // need to set in the program.
  340. #define REG_FIFO 0x00 // rw FIFO address
  341. #define REG_OPMODE 0x01
  342. // Register 2 to 5 are unused for LoRa
  343. #define REG_FRF_MSB 0x06
  344. #define REG_FRF_MID 0x07
  345. #define REG_FRF_LSB 0x08
  346. #define REG_PAC 0x09
  347. #define REG_PARAMP 0x0A
  348. #define REG_LNA 0x0C
  349. #define REG_FIFO_ADDR_PTR 0x0D // rw SPI interface address pointer in FIFO data buffer
  350. #define REG_FIFO_TX_BASE_AD 0x0E // rw write base address in FIFO data buffer for TX modulator
  351. #define REG_FIFO_RX_BASE_AD 0x0F // rw read base address in FIFO data buffer for RX demodulator (0x00)
  352. #define REG_FIFO_RX_CURRENT_ADDR 0x10 // r Address of last packet received
  353. #define REG_IRQ_FLAGS_MASK 0x11
  354. #define REG_IRQ_FLAGS 0x12
  355. #define REG_RX_NB_BYTES 0x13
  356. #define REG_PKT_SNR_VALUE 0x19
  357. #define REG_PKT_RSSI 0x1A // latest package
  358. #define REG_RSSI 0x1B // Current RSSI, section 6.4, or 5.5.5
  359. #define REG_HOP_CHANNEL 0x1C
  360. #define REG_MODEM_CONFIG1 0x1D
  361. #define REG_MODEM_CONFIG2 0x1E
  362. #define REG_SYMB_TIMEOUT_LSB 0x1F
  363. #define REG_PAYLOAD_LENGTH 0x22
  364. #define REG_MAX_PAYLOAD_LENGTH 0x23
  365. #define REG_HOP_PERIOD 0x24
  366. #define REG_MODEM_CONFIG3 0x26
  367. #define REG_RSSI_WIDEBAND 0x2C
  368. #define REG_INVERTIQ 0x33
  369. #define REG_DET_TRESH 0x37 // SF6
  370. #define REG_SYNC_WORD 0x39
  371. #define REG_TEMP 0x3C
  372. #define REG_DIO_MAPPING_1 0x40
  373. #define REG_DIO_MAPPING_2 0x41
  374. #define REG_VERSION 0x42
  375. #define REG_PADAC 0x5A
  376. #define REG_PADAC_SX1272 0x5A
  377. #define REG_PADAC_SX1276 0x4D
  378. // ----------------------------------------
  379. // opModes
  380. #define SX72_MODE_SLEEP 0x80
  381. #define SX72_MODE_STANDBY 0x81
  382. #define SX72_MODE_FSTX 0x82
  383. #define SX72_MODE_TX 0x83 // 0x80 | 0x03
  384. #define SX72_MODE_RX_CONTINUOS 0x85
  385. // ----------------------------------------
  386. // LMIC Constants for radio registers
  387. #define OPMODE_LORA 0x80
  388. #define OPMODE_MASK 0x07
  389. #define OPMODE_SLEEP 0x00
  390. #define OPMODE_STANDBY 0x01
  391. #define OPMODE_FSTX 0x02
  392. #define OPMODE_TX 0x03
  393. #define OPMODE_FSRX 0x04
  394. #define OPMODE_RX 0x05
  395. #define OPMODE_RX_SINGLE 0x06
  396. #define OPMODE_CAD 0x07
  397. // ----------------------------------------
  398. // LOW NOISE AMPLIFIER
  399. #define LNA_MAX_GAIN 0x23 // Max gain 0x20 | Boost 0x03
  400. #define LNA_OFF_GAIN 0x00
  401. #define LNA_LOW_GAIN 0x20
  402. // CONF REG
  403. #define REG1 0x0A
  404. #define REG2 0x84
  405. // ----------------------------------------
  406. // MC1 sx1276 RegModemConfig1
  407. #define SX1276_MC1_BW_125 0x70
  408. #define SX1276_MC1_BW_250 0x80
  409. #define SX1276_MC1_BW_500 0x90
  410. #define SX1276_MC1_CR_4_5 0x02 // sx1276
  411. #define SX1276_MC1_CR_4_6 0x04
  412. #define SX1276_MC1_CR_4_7 0x06
  413. #define SX1276_MC1_CR_4_8 0x08
  414. #define SX1276_MC1_IMPLICIT_HEADER_MODE_ON 0x01
  415. #define SX72_MC1_LOW_DATA_RATE_OPTIMIZE 0x01 // mandated for SF11 and SF12
  416. // ----------------------------------------
  417. // MC2 definitions
  418. #define SX72_MC2_FSK 0x00
  419. #define SX72_MC2_SF7 0x70 // SF7 == 0x07, so (SF7<<4) == SX7_MC2_SF7
  420. #define SX72_MC2_SF8 0x80
  421. #define SX72_MC2_SF9 0x90
  422. #define SX72_MC2_SF10 0xA0
  423. #define SX72_MC2_SF11 0xB0
  424. #define SX72_MC2_SF12 0xC0
  425. // ----------------------------------------
  426. // MC3
  427. #define SX1276_MC3_LOW_DATA_RATE_OPTIMIZE 0x08
  428. #define SX1276_MC3_AGCAUTO 0x04
  429. // ----------------------------------------
  430. // FRF
  431. #define FRF_MSB 0xD9 // 868.1 MHz
  432. #define FRF_MID 0x06
  433. #define FRF_LSB 0x66
  434. // ----------------------------------------
  435. // DIO function mappings D0D1D2D3
  436. #define MAP_DIO0_LORA_RXDONE 0x00 // 00------ bit 7 and 6
  437. #define MAP_DIO0_LORA_TXDONE 0x40 // 01------
  438. #define MAP_DIO0_LORA_CADDONE 0x80 // 10------
  439. #define MAP_DIO0_LORA_NOP 0xC0 // 11------
  440. #define MAP_DIO1_LORA_RXTOUT 0x00 // --00---- bit 5 and 4
  441. #define MAP_DIO1_LORA_FCC 0x10 // --01----
  442. #define MAP_DIO1_LORA_CADDETECT 0x20 // --10----
  443. #define MAP_DIO1_LORA_NOP 0x30 // --11----
  444. #define MAP_DIO2_LORA_FCC0 0x00 // ----00-- bit 3 and 2
  445. #define MAP_DIO2_LORA_FCC1 0x04 // ----01-- bit 3 and 2
  446. #define MAP_DIO2_LORA_FCC2 0x08 // ----10-- bit 3 and 2
  447. #define MAP_DIO2_LORA_NOP 0x0C // ----11-- bit 3 and 2
  448. #define MAP_DIO3_LORA_CADDONE 0x00 // ------00 bit 1 and 0
  449. #define MAP_DIO3_LORA_HEADER 0x01 // ------01
  450. #define MAP_DIO3_LORA_CRC 0x02 // ------10
  451. #define MAP_DIO3_LORA_NOP 0x03 // ------11
  452. // FSK specific
  453. #define MAP_DIO0_FSK_READY 0x00 // 00------ (packet sent / payload ready)
  454. #define MAP_DIO1_FSK_NOP 0x30 // --11----
  455. #define MAP_DIO2_FSK_TXNOP 0x04 // ----01--
  456. #define MAP_DIO2_FSK_TIMEOUT 0x08 // ----10--
  457. // ----------------------------------------
  458. // Bits masking the corresponding IRQs from the radio
  459. #define IRQ_LORA_RXTOUT_MASK 0x80 // RXTOUT
  460. #define IRQ_LORA_RXDONE_MASK 0x40 // RXDONE after receiving the header and CRC, we receive payload part
  461. #define IRQ_LORA_CRCERR_MASK 0x20 // CRC error detected. Note that RXDONE will also be set
  462. #define IRQ_LORA_HEADER_MASK 0x10 // valid HEADER mask. This interrupt is first when receiving a message
  463. #define IRQ_LORA_TXDONE_MASK 0x08 // End of TRansmission
  464. #define IRQ_LORA_CDDONE_MASK 0x04 // CDDONE
  465. #define IRQ_LORA_FHSSCH_MASK 0x02
  466. #define IRQ_LORA_CDDETD_MASK 0x01 // Detect preamble channel
  467. // ----------------------------------------
  468. // Definitions for UDP message arriving from server
  469. #define PROTOCOL_VERSION 0x01
  470. #define PKT_PUSH_DATA 0x00
  471. #define PKT_PUSH_ACK 0x01
  472. #define PKT_PULL_DATA 0x02
  473. #define PKT_PULL_RESP 0x03
  474. #define PKT_PULL_ACK 0x04
  475. #define PKT_TX_ACK 0x05
  476. #define MGT_RESET 0x15 // Not a LoRa Gateway Spec message
  477. #define MGT_SET_SF 0x16
  478. #define MGT_SET_FREQ 0x17