loraModem.h 20 KB

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