loraModem.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // 1-channel LoRa Gateway for ESP8266 and ESP32
  2. // Copyright (c) 2016-2020 Maarten Westenberg version for ESP8266
  3. //
  4. // based on work done by Thomas Telkamp for Raspberry PI 1ch gateway
  5. // and many other contributors.
  6. //
  7. // All rights reserved. This program and the accompanying materials
  8. // are made available under the terms of the MIT License
  9. // which accompanies this distribution, and is available at
  10. // https://opensource.org/licenses/mit-license.php
  11. //
  12. // NO WARRANTY OF ANY KIND IS PROVIDED
  13. //
  14. // Author: Maarten Westenberg (mw12554@hotmail.com)
  15. //
  16. // This file contains a number of compile-time settings and declarations that are
  17. // specific to the LoRa rfm95, sx1276, sx1272 radio of the gateway.
  18. //
  19. //
  20. // ------------------------------------------------------------------------------------
  21. // ----------------------------------------
  22. // Used by REG_PAYLOAD_LENGTH to set receive payload length
  23. #define PAYLOAD_LENGTH 0x40 // 64 bytes
  24. #define MAX_PAYLOAD_LENGTH 0x80 // 128 bytes
  25. // In order to make the CAD behaviour dynamic we set a variable
  26. // when the CAD functions are defined. Value of 3 is minimum frequencies a
  27. // gateway should support to be fully LoRa compliant.
  28. // For performance reasons, 3 is the maximum as well!
  29. //
  30. #define NUM_HOPS 3
  31. // Do not change these setting for RSSI detection. They are used for CAD
  32. // Given the correction factor of 157, we can get to -122dB with this rating
  33. //
  34. #define RSSI_LIMIT 35 //
  35. // How long to wait in LoRa mode before using the RSSI value.
  36. // This period should be as short as possible, yet sufficient
  37. //
  38. #define RSSI_WAIT 6 // was 25
  39. // How long will it take when hopping before a CDONE or CDETD value
  40. // is present and can be measured.
  41. //
  42. #define EVENT_WAIT 15000 // XXX 180520 was 25 milliseconds before CDDETD timeout
  43. #define DONE_WAIT 1950 // 2000 microseconds (1/500) sec between CDDONE events
  44. // SPI setting. 8MHz seems to be the max
  45. #define SPISPEED 8000000 // Set to 8 * 10E6
  46. // Frequencies
  47. // Set center frequency. If in doubt, choose the first one, comment out all others
  48. // Each "real" gateway should support the first 3 frequencies according to LoRa spec.
  49. // NOTE: This means you have to specify at least 3 frequencies here for the single
  50. // channel gateway to work according to spec.
  51. struct vector {
  52. // Upstream messages
  53. uint32_t upFreq; // 4 bytes
  54. uint16_t upBW; // 2 bytes
  55. uint8_t upLo; // 1 bytes
  56. uint8_t upHi; // 1 bytes
  57. // Downstream messages
  58. uint32_t dwnFreq; // 4 bytes Unsigned ubt Frequency
  59. uint16_t dwnBW; // 2 bytes BW Specification
  60. uint8_t dwnLo; // 1 bytes Spreading Factor
  61. uint8_t dwnHi; // 1 bytes
  62. };
  63. // Define all the relevant LoRa Regions
  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.525 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 you 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. vector 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. uint32_t msgTime=0; // in seconds, Thru nowSeconds, now()
  162. uint32_t hopTime=0; // in micros()
  163. uint32_t detTime=0; // In micros()
  164. #if _PIN_OUT==1
  165. // ----------------------------------------------------------------------------
  166. // Definition of the GPIO pins used by the Gateway for Hallard type boards
  167. //
  168. struct pins {
  169. uint8_t dio0=15; // GPIO15 / D8. For the Hallard board shared between DIO0/DIO1/DIO2
  170. uint8_t dio1=15; // GPIO15 / D8. Used for CAD, may or not be shared with DIO0
  171. uint8_t dio2=15; // GPIO15 / D8. Used for frequency hopping, don't care
  172. uint8_t ss=16; // GPIO16 / D0. Select pin connected to GPIO16 / D0
  173. uint8_t rst=0; // GPIO 0 / D3. Reset pin not used
  174. // MISO 12 / D6
  175. // MOSI 13 / D7
  176. // CLK 14 / D5
  177. } pins;
  178. #elif _PIN_OUT==2
  179. // ----------------------------------------------------------------------------
  180. // For ComResult gateway PCB use the following settings
  181. struct pins {
  182. uint8_t dio0=5; // GPIO5 / D1. Dio0 used for one frequency and one SF
  183. uint8_t dio1=4; // GPIO4 / D2. Used for CAD, may or not be shared with DIO0
  184. uint8_t dio2=0; // GPIO0 / D3. Used for frequency hopping, don't care
  185. uint8_t ss=15; // GPIO15 / D8. Select pin connected to GPIO15
  186. uint8_t rst=0; // GPIO0 / D3. Reset pin not used
  187. } pins;
  188. #elif _PIN_OUT==3
  189. // ----------------------------------------------------------------------------
  190. // For ESP32/Wemos based board
  191. // SCK == GPIO5/ PIN5
  192. // SS == GPIO18/PIN18
  193. // MISO == GPIO19/ PIN19
  194. // MOSI == GPIO27/ PIN27
  195. // RST == GPIO14/ PIN14
  196. // This Pinning is not used and is under construction
  197. struct pins {
  198. uint8_t dio0=26; // GPIO26 / Dio0 used for one frequency and one SF
  199. uint8_t dio1=26; // GPIO26 / Used for CAD, may or not be shared with DIO0
  200. uint8_t dio2=26; // GPI2O6 / Used for frequency hopping, don't care
  201. uint8_t ss=18; // GPIO18 / Dx. Select pin connected to GPIO18
  202. uint8_t rst=14; // GPIO0 / D3. Reset pin not used
  203. } pins;
  204. #elif _PIN_OUT==4
  205. // ----------------------------------------------------------------------------
  206. // For ESP32/TTGO based board.
  207. // SCK == GPIO5/ PIN5
  208. // SS == GPIO18/ PIN18 CS
  209. // MISO == GPIO19/ PIN19
  210. // MOSI == GPIO27/ PIN27
  211. // RST == GPIO14/ PIN14
  212. struct pins {
  213. uint8_t dio0=26; // GPIO26 / Dio0 used for one frequency and one SF
  214. uint8_t dio1=33; // GPIO33 / Used for CAD, may or not be shared with DIO0
  215. uint8_t dio2=32; // GPIO32 / Used for frequency hopping, don't care
  216. uint8_t ss=18; // GPIO18 / CS. Select pin connected to GPIO18
  217. uint8_t rst=14; // GPIO14 / D3. Reset pin not used
  218. } pins;
  219. #define SCK 5
  220. #define MISO 19
  221. #define MOSI 27
  222. #define RST 14
  223. #define SS 18
  224. #if _GPS==1
  225. #define GPS_RX 15
  226. #define GPS_TX 12
  227. #endif //_GPS
  228. #elif _PIN_OUT==5
  229. // ----------------------------------------------------------------------------
  230. // For ESP32/Heltec Wifi LoRA 32(V2) HTIT-WB32LA board with 0.9" OLED
  231. //
  232. // SCK == GPIO5/ PIN5
  233. // SS == GPIO18/PIN18 CS
  234. // MISO == GPIO19/ PIN19
  235. // MOSI == GPIO27/ PIN27
  236. // RST == GPIO14/ PIN14
  237. struct pins {
  238. uint8_t dio0=26; // GPIO26 / Dio0 used for one frequency and one SF
  239. uint8_t dio1=35; // GPIO35 / Used for CAD, may or not be shared with DIO0
  240. uint8_t dio2=34; // GPIO34 / Used for frequency hopping, don't care
  241. uint8_t ss=18; // GPIO18 / Dx. Select pin connected to GPIO18
  242. uint8_t rst=14; // GPIO0 / D3. Reset pin not used
  243. } pins;
  244. #define SCK 5 // Check
  245. #define MISO 19 // Check
  246. #define MOSI 27 // Check
  247. #define RST 14 // Check
  248. #define SS 18
  249. #else
  250. // ----------------------------------------------------------------------------
  251. // Use your own pin definitions, and comment #error line below
  252. // MISO 12 / D6
  253. // MOSI 13 / D7
  254. // CLK 14 / D5
  255. // SS 16 / D0
  256. #error "Pin Definitions _PIN_OUT must be defined in loraModem.h"
  257. #endif
  258. // stat_t contains the statistics that are kept for a message.
  259. // Each time a message is received or sent the statistics are updated.
  260. // In case _STATISTICS==1 we define the last gwayConfig.maxStat message as statistics
  261. struct stat_t {
  262. uint32_t time; // Time since 1970 in seconds
  263. uint32_t node; // 4-byte DEVaddr (the only one known to gateway)
  264. uint8_t ch; // Channel index to freqs array
  265. uint8_t sf;
  266. #if RSSI==1
  267. int8_t rssi; // XXX Can be < -128
  268. #endif
  269. int8_t prssi; // XXX Can be < -128
  270. #if _LOCALSERVER==1
  271. uint8_t data[23]; // For memory purposes, only 23 chars
  272. uint8_t datal; // Length of decoded message 1 char
  273. #endif
  274. } stat_t;
  275. #if _STATISTICS >= 1
  276. // statc_c contains the statistic that are gateway related and not per
  277. // message. Example: Number of messages received on SF7 or number of (re) boots
  278. // So where statr contains the statistics gathered per packet the statc_c
  279. // contains general statistics of the node
  280. struct stat_c {
  281. uint32_t msg_ok;
  282. uint32_t msg_ttl;
  283. uint32_t msg_down;
  284. uint32_t msg_sens;
  285. // Of statistics == 2 we add spreading factor data to the statistics
  286. #if _STATISTICS >= 2 // Only if we explicitly set it higher
  287. uint32_t sf7, sf8, sf9; // Spreading factor 7, 8, 9 statistics/Count
  288. uint32_t sf10, sf11, sf12; // Spreading factor 10, 11, 12
  289. // If _STATISTICS is 3, we add statistics about the channel
  290. // When only one channel is used, we normally know the spread of these
  291. // statistics, but when HOP mode is selected we migth want to add this info
  292. #if _STATISTICS >=3
  293. uint32_t msg_ok_0, msg_ok_1, msg_ok_2;
  294. uint32_t msg_ttl_0, msg_ttl_1, msg_ttl_2;
  295. uint32_t msg_down_0, msg_down_1, msg_down_2;
  296. uint32_t msg_sens_0, msg_sens_1, msg_sens_2;
  297. uint32_t sf7_0, sf7_1, sf7_2;
  298. uint32_t sf8_0, sf8_1, sf8_2;
  299. uint32_t sf9_0, sf9_1, sf9_2;
  300. uint32_t sf10_0, sf10_1, sf10_2;
  301. uint32_t sf11_0, sf11_1, sf11_2;
  302. uint32_t sf12_0, sf12_1, sf12_2;
  303. #endif //3
  304. uint16_t boots; // Number of boots
  305. uint16_t resets;
  306. #endif // 2
  307. } stat_c;
  308. struct stat_c statc;
  309. // History of received uplink and downlink messages from nodes
  310. struct stat_t * statr;
  311. #else //_STATISTICS==0
  312. struct stat_t statr[1]; // Always have at least one element to store in
  313. #endif
  314. // Define the payload structure used to separate interrupt and SPI
  315. // processing from the loop() part
  316. uint8_t payLoad[128]; // Payload i
  317. // ====================================================================
  318. // PACKET FORWARDER
  319. // Smetech Specification
  320. // https://github.com/Lora-net/packet_forwarder/blob/master/PROTOCOL.TXT
  321. // Some parts are included both at Upstram and Downstream since
  322. // the gateway can also be used as a repeater
  323. // ====================================================================
  324. struct LoraDown {
  325. uint32_t tmst; // Timestamp (will ignore time)
  326. uint32_t tmms; // Timestamp according to GPS (sync required)
  327. uint32_t time;
  328. double_t freq; // Frequency
  329. uint8_t size;
  330. // chan = <NOT USED>
  331. bool ipol;
  332. uint8_t powe; // transmit power, normally 14, except when using special channel
  333. uint8_t crc;
  334. uint8_t iiq; // message inverted or not for node-node communiction
  335. uint8_t imme; // Immediate transfer execution
  336. uint8_t sf; // through datr
  337. uint8_t bw; // through datr
  338. uint8_t ncrc; // no CRC check
  339. uint8_t prea; // preamble
  340. uint8_t rfch; // Concentrator "RF chain" used for TX (unsigned integer)
  341. char * modu; // "LORA" os "FSCK"
  342. char * datr; // = "SF12BW125", contains both .sf and .bw parts
  343. char * codr;
  344. uint8_t * payLoad;
  345. } LoraDown;
  346. // Up buffer (from Lora sensor to UDP)
  347. // This struct contains all data of the buffer received from devices to gateway
  348. struct LoraUp {
  349. uint32_t tmst; // Timestamp of message
  350. uint32_t tmms; // <not used at the moment>
  351. uint32_t time; // <not used at the moment>
  352. double_t freq; // frequency used in HZ
  353. uint8_t size; // Length of the message Payload
  354. uint8_t chan; // Channel "IF" used for RX
  355. uint8_t sf; // Spreading Factor
  356. // modu not used
  357. // datr = "SF12BW125", contains both .sf and .bw parts
  358. int32_t snr;
  359. int16_t prssi;
  360. int16_t rssicorr;
  361. uint8_t payLoad[128];
  362. } LoraUp;
  363. // ============================================================================
  364. // Set all definitions for Gateway
  365. // ============================================================================
  366. // Register definitions. These are the addresses of the RFM95, SX1276 that we
  367. // need to set in the program.
  368. #define REG_FIFO 0x00 // rw FIFO address
  369. #define REG_OPMODE 0x01 // Operation Mode Register (Page 108)
  370. // Register 2 to 5 are unused for LoRa
  371. #define REG_FRF_MSB 0x06
  372. #define REG_FRF_MID 0x07
  373. #define REG_FRF_LSB 0x08
  374. #define REG_PAC 0x09
  375. #define REG_PARAMP 0x0A
  376. #define REG_LNA 0x0C
  377. #define REG_FIFO_ADDR_PTR 0x0D // rw SPI interface address pointer in FIFO data buffer
  378. #define REG_FIFO_TX_BASE_AD 0x0E // rw write base address in FIFO data buffer for TX modulator
  379. #define REG_FIFO_RX_BASE_AD 0x0F // rw read base address in FIFO data buffer for RX demodulator (0x00)
  380. #define REG_FIFO_RX_CURRENT_ADDR 0x10 // r Address of last packet received
  381. #define REG_IRQ_FLAGS_MASK 0x11
  382. #define REG_IRQ_FLAGS 0x12
  383. #define REG_RX_NB_BYTES 0x13
  384. #define REG_PKT_SNR_VALUE 0x19
  385. #define REG_PKT_RSSI 0x1A // latest package
  386. #define REG_RSSI 0x1B // Current RSSI, section 6.4, or 5.5.5
  387. #define REG_HOP_CHANNEL 0x1C
  388. #define REG_MODEM_CONFIG1 0x1D // LoRa: Modem PHY config 1
  389. #define REG_MODEM_CONFIG2 0x1E // LoRa: Modem PHY config 2
  390. #define REG_SYMB_TIMEOUT_LSB 0x1F
  391. #define REG_PAYLOAD_LENGTH 0x22
  392. #define REG_MAX_PAYLOAD_LENGTH 0x23
  393. #define REG_HOP_PERIOD 0x24
  394. #define REG_MODEM_CONFIG3 0x26 // Modem PHY config 3
  395. #define REG_RSSI_WIDEBAND 0x2C
  396. #define REG_INVERTIQ 0x33
  397. #define REG_DET_TRESH 0x37 // SF6
  398. #define REG_SYNC_WORD 0x39
  399. #define REG_TEMP 0x3C
  400. #define REG_DIO_MAPPING_1 0x40 // Mapping of pins DIO0 to DIO3
  401. #define REG_DIO_MAPPING_2 0x41 // Mapping of pins DIO4 and DIO5, ClkOut frequency
  402. #define REG_VERSION 0x42 // 0x12? Semtech def
  403. #define REG_PADAC 0x5A
  404. #define REG_PADAC_SX1272 0x5A
  405. #define REG_PADAC_SX1276 0x4D
  406. // ----------------------------------------
  407. // opModes
  408. #define SX72_MODE_SLEEP 0x80
  409. #define SX72_MODE_STANDBY 0x81
  410. #define SX72_MODE_FSTX 0x82
  411. #define SX72_MODE_TX 0x83 // 0x80 | 0x03
  412. #define SX72_MODE_RX_CONTINUOS 0x85
  413. // ----------------------------------------
  414. // LMIC Constants for radio registers
  415. #define OPMODE_LORA 0x80
  416. #define OPMODE_MASK 0x0F // Select LSB 8 bits 0, ignore LoRa bit for example
  417. #define OPMODE_LOWFREQ 0x08 // Should be - for 868.1 MHZ operation
  418. #define OPMODE_SLEEP 0x00
  419. #define OPMODE_STANDBY 0x01
  420. #define OPMODE_FSTX 0x02
  421. #define OPMODE_TX 0x03
  422. #define OPMODE_FSRX 0x04
  423. #define OPMODE_RX 0x05
  424. #define OPMODE_RX_SINGLE 0x06
  425. #define OPMODE_CAD 0x07
  426. // ----------------------------------------
  427. // LOW NOISE AMPLIFIER
  428. #define LNA_MAX_GAIN 0x23 // Max gain 0x20 | Boost 0x03
  429. #define LNA_OFF_GAIN 0x00
  430. #define LNA_LOW_GAIN 0x20
  431. // CONF REG
  432. #define REG1 0x0A
  433. #define REG2 0x84
  434. // ----------------------------------------
  435. // MC1 sx1276 RegModemConfig1
  436. #define SX1276_MC1_BW_125 0x70
  437. #define SX1276_MC1_BW_250 0x80
  438. #define SX1276_MC1_BW_500 0x90
  439. #define SX1276_MC1_CR_4_5 0x02 // sx1276
  440. #define SX1276_MC1_CR_4_6 0x04
  441. #define SX1276_MC1_CR_4_7 0x06
  442. #define SX1276_MC1_CR_4_8 0x08
  443. #define SX1276_MC1_IMPLICIT_HEADER_MODE_ON 0x01
  444. #define SX72_MC1_LOW_DATA_RATE_OPTIMIZE 0x01 // mandated for SF11 and SF12
  445. // ----------------------------------------
  446. // MC2 definitions
  447. #define SX72_MC2_FSK 0x00
  448. #define SX72_MC2_SF7 0x70 // SF7 == 0x07, so (SF7<<4) == SX7_MC2_SF7
  449. #define SX72_MC2_SF8 0x80
  450. #define SX72_MC2_SF9 0x90
  451. #define SX72_MC2_SF10 0xA0
  452. #define SX72_MC2_SF11 0xB0
  453. #define SX72_MC2_SF12 0xC0
  454. // ----------------------------------------
  455. // MC3
  456. #define SX1276_MC3_LOW_DATA_RATE_OPTIMIZE 0x08
  457. #define SX1276_MC3_AGCAUTO 0x04
  458. // ----------------------------------------
  459. // FRF
  460. #define FRF_MSB 0xD9 // 868.1 MHz
  461. #define FRF_MID 0x06
  462. #define FRF_LSB 0x66
  463. // ----------------------------------------
  464. // DIO function mappings D0D1D2D3
  465. #define MAP_DIO0_LORA_RXDONE 0x00 // 00------ bit 7 and 6
  466. #define MAP_DIO0_LORA_TXDONE 0x40 // 01------
  467. #define MAP_DIO0_LORA_CADDONE 0x80 // 10------
  468. #define MAP_DIO0_LORA_NOP 0xC0 // 11------
  469. #define MAP_DIO1_LORA_RXTOUT 0x00 // --00---- bit 5 and 4
  470. #define MAP_DIO1_LORA_FCC 0x10 // --01----
  471. #define MAP_DIO1_LORA_CADDETECT 0x20 // --10----
  472. #define MAP_DIO1_LORA_NOP 0x30 // --11----
  473. #define MAP_DIO2_LORA_FCC0 0x00 // ----00-- bit 3 and 2
  474. #define MAP_DIO2_LORA_FCC1 0x04 // ----01-- bit 3 and 2
  475. #define MAP_DIO2_LORA_FCC2 0x08 // ----10-- bit 3 and 2
  476. #define MAP_DIO2_LORA_NOP 0x0C // ----11-- bit 3 and 2
  477. #define MAP_DIO3_LORA_CADDONE 0x00 // ------00 bit 1 and 0
  478. #define MAP_DIO3_LORA_HEADER 0x01 // ------01
  479. #define MAP_DIO3_LORA_CRC 0x02 // ------10
  480. #define MAP_DIO3_LORA_NOP 0x03 // ------11
  481. // FSK specific
  482. #define MAP_DIO0_FSK_READY 0x00 // 00------ (packet sent / payload ready)
  483. #define MAP_DIO1_FSK_NOP 0x30 // --11----
  484. #define MAP_DIO2_FSK_TXNOP 0x04 // ----01--
  485. #define MAP_DIO2_FSK_TIMEOUT 0x08 // ----10--
  486. // ----------------------------------------
  487. // Bits masking the corresponding IRQs from the radio
  488. #define IRQ_LORA_RXTOUT_MASK 0x80 // RXTOUT
  489. #define IRQ_LORA_RXDONE_MASK 0x40 // RXDONE after receiving the header and CRC, we receive payload part
  490. #define IRQ_LORA_CRCERR_MASK 0x20 // CRC error detected. Note that RXDONE will also be set
  491. #define IRQ_LORA_HEADER_MASK 0x10 // valid HEADER mask. This interrupt is first when receiving a message
  492. #define IRQ_LORA_TXDONE_MASK 0x08 // End of Transmission
  493. #define IRQ_LORA_CDDONE_MASK 0x04 // CDDONE
  494. #define IRQ_LORA_FHSSCH_MASK 0x02
  495. #define IRQ_LORA_CDDETD_MASK 0x01 // Detect preamble channel
  496. // ----------------------------------------
  497. // Definitions for UDP message arriving from server
  498. #define PROTOCOL_VERSION 0x02
  499. #define PUSH_DATA 0x00
  500. #define PUSH_ACK 0x01
  501. #define PULL_DATA 0x02
  502. #define PULL_RESP 0x03
  503. #define PULL_ACK 0x04
  504. #define TX_ACK 0x05
  505. #define MGT_RESET 0x15 // Not a LoRa Gateway Spec message
  506. #define MGT_SET_SF 0x16
  507. #define MGT_SET_FREQ 0x17