loraModem.h 22 KB

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