SX1276.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**
  2. * Author Wojciech Domski <Wojciech.Domski@gmail.com>
  3. * www: www.Domski.pl
  4. *
  5. * work based on DORJI.COM sample code and
  6. * https://github.com/realspinner/SX1276_LoRa
  7. */
  8. #ifndef __SX1276_H__
  9. #define __SX1276_H__
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include "main.h"
  13. #define SX1276_MAX_PACKET 256
  14. #define SX1276_DEFAULT_TIMEOUT 3000
  15. //Error Coding rate (CR)setting
  16. #define SX1276_CR_4_5
  17. //#define SX1276_CR_4_6
  18. //#define SX1276_CR_4_7
  19. //#define SX1276_CR_4_8
  20. #ifdef SX1276_CR_4_5
  21. #define SX1276_CR 0x01
  22. #else
  23. #ifdef SX1276_CR_4_6
  24. #define SX1276_CR 0x02
  25. #else
  26. #ifdef SX1276_CR_4_7
  27. #define SX1276_CR 0x03
  28. #else
  29. #ifdef SX1276_CR_4_8
  30. #define SX1276_CR 0x04
  31. #endif
  32. #endif
  33. #endif
  34. #endif
  35. //CRC Enable
  36. #define SX1276_CRC_EN
  37. #ifdef SX1276_CRC_EN
  38. #define SX1276_CRC 0x01
  39. #else
  40. #define SX1276_CRC 0x00
  41. #endif
  42. //RFM98 Internal registers Address
  43. /********************LoRa mode***************************/
  44. #define LR_RegFifo 0x00
  45. // Common settings
  46. #define LR_RegOpMode 0x01
  47. #define LR_RegFrMsb 0x06
  48. #define LR_RegFrMid 0x07
  49. #define LR_RegFrLsb 0x08
  50. // Tx settings
  51. #define LR_RegPaConfig 0x09
  52. #define LR_RegPaRamp 0x0A
  53. #define LR_RegOcp 0x0B
  54. // Rx settings
  55. #define LR_RegLna 0x0C
  56. // LoRa registers
  57. #define LR_RegFifoAddrPtr 0x0D
  58. #define LR_RegFifoTxBaseAddr 0x0E
  59. #define LR_RegFifoRxBaseAddr 0x0F
  60. #define LR_RegFifoRxCurrentaddr 0x10
  61. #define LR_RegIrqFlagsMask 0x11
  62. #define LR_RegIrqFlags 0x12
  63. #define LR_RegRxNbBytes 0x13
  64. #define LR_RegRxHeaderCntValueMsb 0x14
  65. #define LR_RegRxHeaderCntValueLsb 0x15
  66. #define LR_RegRxPacketCntValueMsb 0x16
  67. #define LR_RegRxPacketCntValueLsb 0x17
  68. #define LR_RegModemStat 0x18
  69. #define LR_RegPktSnrValue 0x19
  70. #define LR_RegPktRssiValue 0x1A
  71. #define LR_RegRssiValue 0x1B
  72. #define LR_RegHopChannel 0x1C
  73. #define LR_RegModemConfig1 0x1D
  74. #define LR_RegModemConfig2 0x1E
  75. #define LR_RegSymbTimeoutLsb 0x1F
  76. #define LR_RegPreambleMsb 0x20
  77. #define LR_RegPreambleLsb 0x21
  78. #define LR_RegPayloadLength 0x22
  79. #define LR_RegMaxPayloadLength 0x23
  80. #define LR_RegHopPeriod 0x24
  81. #define LR_RegFifoRxByteAddr 0x25
  82. // I/O settings
  83. #define REG_LR_DIOMAPPING1 0x40
  84. #define REG_LR_DIOMAPPING2 0x41
  85. // Version
  86. #define REG_LR_VERSION 0x42
  87. // Additional settings
  88. #define REG_LR_PLLHOP 0x44
  89. #define REG_LR_TCXO 0x4B
  90. #define REG_LR_PADAC 0x4D
  91. #define REG_LR_FORMERTEMP 0x5B
  92. #define REG_LR_AGCREF 0x61
  93. #define REG_LR_AGCTHRESH1 0x62
  94. #define REG_LR_AGCTHRESH2 0x63
  95. #define REG_LR_AGCTHRESH3 0x64
  96. /********************FSK/ook mode***************************/
  97. #define RegFIFO 0x00
  98. #define RegOpMode 0x01
  99. #define RegBitRateMsb 0x02
  100. #define RegBitRateLsb 0x03
  101. #define RegFdevMsb 0x04
  102. #define RegFdevLsb 0x05
  103. #define RegFreqMsb 0x06
  104. #define RegFreqMid 0x07
  105. #define RegFreqLsb 0x08
  106. #define RegPaConfig 0x09
  107. #define RegPaRamp 0x0a
  108. #define RegOcp 0x0b
  109. #define RegLna 0x0c
  110. #define RegRxConfig 0x0d
  111. #define RegRssiConfig 0x0e
  112. #define RegRssiCollision 0x0f
  113. #define RegRssiThresh 0x10
  114. #define RegRssiValue 0x11
  115. #define RegRxBw 0x12
  116. #define RegAfcBw 0x13
  117. #define RegOokPeak 0x14
  118. #define RegOokFix 0x15
  119. #define RegOokAvg 0x16
  120. #define RegAfcFei 0x1a
  121. #define RegAfcMsb 0x1b
  122. #define RegAfcLsb 0x1c
  123. #define RegFeiMsb 0x1d
  124. #define RegFeiLsb 0x1e
  125. #define RegPreambleDetect 0x1f
  126. #define RegRxTimeout1 0x20
  127. #define RegRxTimeout2 0x21
  128. #define RegRxTimeout3 0x22
  129. #define RegRxDelay 0x23
  130. #define RegOsc 0x24
  131. #define RegPreambleMsb 0x25
  132. #define RegPreambleLsb 0x26
  133. #define RegSyncConfig 0x27
  134. #define RegSyncValue1 0x28
  135. #define RegSyncValue2 0x29
  136. #define RegSyncValue3 0x2a
  137. #define RegSyncValue4 0x2b
  138. #define RegSyncValue5 0x2c
  139. #define RegSyncValue6 0x2d
  140. #define RegSyncValue7 0x2e
  141. #define RegSyncValue8 0x2f
  142. #define RegPacketConfig1 0x30
  143. #define RegPacketConfig2 0x31
  144. #define RegPayloadLength 0x32
  145. #define RegNodeAdrs 0x33
  146. #define RegBroadcastAdrs 0x34
  147. #define RegFifoThresh 0x35
  148. #define RegSeqConfig1 0x36
  149. #define RegSeqConfig2 0x37
  150. #define RegTimerResol 0x38
  151. #define RegTimer1Coef 0x39
  152. #define RegTimer2Coef 0x3a
  153. #define RegImageCal 0x3b
  154. #define RegTemp 0x3c
  155. #define RegLowBat 0x3d
  156. #define RegIrqFlags1 0x3e
  157. #define RegIrqFlags2 0x3f
  158. #define RegDioMapping1 0x40
  159. #define RegDioMapping2 0x41
  160. #define RegVersion 0x42
  161. #define RegPllHop 0x44
  162. #define RegPaDac 0x4d
  163. #define RegBitRateFrac 0x5d
  164. /**********************************************************
  165. **Parameter table define
  166. **********************************************************/
  167. typedef enum{
  168. SX1276_917MHZ =0,
  169. SX1276_917_5MHZ ,
  170. SX1276_918MHZ ,
  171. SX1276_918_5MHZ ,
  172. SX1276_919MHZ ,
  173. SX1276_919_5MHZ ,
  174. SX1276_920MHZ ,
  175. SX1276_920_5MHZ ,
  176. SX1276_921MHZ ,
  177. SX1276_921_5MHZ ,
  178. SX1276_922MHZ ,
  179. SX1276_922_5MHZ ,
  180. SX1276_923MHZ ,
  181. SX1276_923_5MHZ ,
  182. };
  183. /*
  184. 917.000000 : e5 40 26
  185. 917.500000 : e5 60 26
  186. 918.000000 : e5 80 26
  187. 918.500000 : e5 a0 26
  188. 919.000000 : e5 c0 26
  189. 919.500000 : e5 e0 26
  190. 920.000000 : e6 00 26
  191. 920.500000 : e6 20 26
  192. 921.000000 : e6 40 26
  193. 921.500000 : e6 60 26
  194. 922.000000 : e6 80 26
  195. 922.500000 : e6 a0 26
  196. 923.000000 : e6 c0 26
  197. 923.500000 : e6 e0 26
  198. */
  199. static const uint8_t SX1276_Frequency[14][3] = {
  200. { 0xE5, 0x40, 0x26 }, //About 917
  201. { 0xE5, 0x60, 0x26 }, //917.5
  202. { 0xE5, 0x80, 0x26 }, //918
  203. { 0xE5, 0xa0, 0x26 }, //918.5
  204. { 0xE5, 0xc0, 0x26 }, //919
  205. { 0xE5, 0xe0, 0x26 }, //919.5
  206. { 0xE6, 0x00, 0x26 }, //920
  207. { 0xE6, 0x20, 0x26 }, //920.5
  208. { 0xE6, 0x40, 0x26 }, //921
  209. { 0xE6, 0x60, 0x26 }, //921.5
  210. { 0xE6, 0x80, 0x26 }, //922
  211. { 0xE6, 0xa0, 0x26 }, //922.5
  212. { 0xE6, 0xc0, 0x26 }, //923
  213. { 0xE6, 0xe0, 0x26 }, //923.5
  214. };/*°ø½Ä : 61.035 * 0xE5 40 26 = 916.9Mhz// ê³µì‹� = ì›�하는 주파수 /61.035(recommad Freq/61.035)
  215. (À̰ªÀÌ º¯¼ö) */
  216. #define SX1276_POWER_20DBM 0
  217. #define SX1276_POWER_17DBM 1
  218. #define SX1276_POWER_14DBM 2
  219. #define SX1276_POWER_11DBM 3
  220. static const uint8_t SX1276_Power[4] = {
  221. 0xFF, //20dbm
  222. 0xFC, //17dbm
  223. 0xF9, //14dbm
  224. 0xF6, //11dbm
  225. };
  226. typedef enum{
  227. SX1276_0dBm = 0,
  228. SX1276_1dBm,
  229. SX1276_2dBm,
  230. SX1276_3dBm,
  231. SX1276_4dBm,
  232. SX1276_5dBm,
  233. SX1276_6dBm,
  234. SX1276_7dBm,
  235. SX1276_8dBm,
  236. SX1276_9dBm,
  237. SX1276_10dBm,
  238. SX1276_11dBm,
  239. SX1276_12dBm,
  240. SX1276_13dBm,
  241. SX1276_14dBm,
  242. SX1276_15dBm,
  243. }SX1276_dBm_t;
  244. typedef enum{
  245. SX1276_LORA_SF_6 =0,
  246. SX1276_LORA_SF_7 =1,
  247. SX1276_LORA_SF_8 =2,
  248. SX1276_LORA_SF_9 =3,
  249. SX1276_LORA_SF_10 =4,
  250. SX1276_LORA_SF_11 =5,
  251. SX1276_LORA_SF_12 =6,
  252. }SX1276_Sf_t;
  253. typedef enum{
  254. SX1276_LORA_G1 = 1 ,
  255. SX1276_LORA_G2 ,
  256. SX1276_LORA_G3 ,
  257. SX1276_LORA_G4 ,
  258. SX1276_LORA_G5 ,
  259. SX1276_LORA_G6 ,
  260. }SX1276_Lna_t;
  261. static const uint8_t SX1276_SpreadFactor[7] = { 6, 7, 8, 9, 10, 11, 12 };
  262. typedef enum{
  263. SX1276_LORA_BW_7_8KHZ = 0,
  264. SX1276_LORA_BW_10_4KHZ ,
  265. SX1276_LORA_BW_15_6KHZ ,
  266. SX1276_LORA_BW_20_8KHZ ,
  267. SX1276_LORA_BW_31_2KHZ ,
  268. SX1276_LORA_BW_41_7KHZ ,
  269. SX1276_LORA_BW_62_5KHZ ,
  270. SX1276_LORA_BW_125KHZ ,
  271. SX1276_LORA_BW_250KHZ ,
  272. SX1276_LORA_BW_500KHZ ,
  273. }SX1276_Bw_t;
  274. static const uint8_t SX1276_LoRaBandwidth[10] = { 0, // 7.8KHz,
  275. 1, // 10.4KHz,
  276. 2, // 15.6KHz,
  277. 3, // 20.8KHz,
  278. 4, // 31.2KHz,
  279. 5, // 41.7KHz,
  280. 6, // 62.5KHz,
  281. 7, // 125.0KHz,
  282. 8, // 250.0KHz,
  283. 9 // 500.0KHz
  284. };
  285. typedef enum _SX1276_STATUS {
  286. SLEEP, STANDBY, TX, RX
  287. } SX1276_Status_t;
  288. typedef struct {
  289. int pin;
  290. void * port;
  291. } SX1276_hw_dio_t;
  292. typedef struct {
  293. SX1276_hw_dio_t reset;
  294. SX1276_hw_dio_t dio0;
  295. SX1276_hw_dio_t nss;
  296. void * spi;
  297. } SX1276_hw_t;
  298. typedef struct {
  299. SX1276_hw_t * hw;
  300. uint8_t frequency;
  301. uint8_t power;
  302. uint8_t LoRa_Rate;
  303. uint8_t LoRa_BW;
  304. uint8_t LoRa_Lna;
  305. uint8_t LoRa_Pa_boost;
  306. uint8_t packetLength;
  307. SX1276_Status_t status;
  308. uint8_t rxBuffer[SX1276_MAX_PACKET];
  309. uint8_t readBytes;
  310. } SX1276_t;
  311. typedef struct {
  312. uint8_t frequency;
  313. uint8_t power;
  314. uint8_t LoRa_Rate;
  315. uint8_t LoRa_BW;
  316. uint8_t LoRa_Lna;
  317. uint8_t LoRa_Pa_boost;
  318. } Default_SX1276_t;
  319. //hardware
  320. __weak void SX1276_hw_init(SX1276_hw_t * hw);
  321. __weak void SX1276_hw_SetNSS(SX1276_hw_t * hw, int value);
  322. __weak void SX1276_hw_Reset(SX1276_hw_t * hw);
  323. __weak void SX1276_hw_SPICommand(SX1276_hw_t * hw, uint8_t cmd);
  324. __weak uint8_t SX1276_hw_SPIReadByte(SX1276_hw_t * hw);
  325. __weak void SX1276_hw_DelayMs(uint32_t msec);
  326. __weak int SX1276_hw_GetDIO0(SX1276_hw_t * hw);
  327. //logic
  328. uint8_t SX1276_SPIRead(SX1276_t * module, uint8_t addr);
  329. void SX1276_SPIWrite(SX1276_t * module, uint8_t addr, uint8_t cmd);
  330. void SX1276_SPIBurstRead(SX1276_t * module, uint8_t addr, uint8_t *rxBuf,
  331. uint8_t length);
  332. void SX1276_SPIBurstWrite(SX1276_t * module, uint8_t addr, uint8_t *txBuf,
  333. uint8_t length);
  334. void SX1276_DIO0_InterruptHandler(SX1276_t * module);
  335. void SX1276_config(SX1276_t * module, uint8_t frequency, uint8_t power,
  336. uint8_t LoRa_Rate, uint8_t LoRa_BW,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost);
  337. void SX1276_defaultConfig(SX1276_t * module);
  338. void SX1276_entryLoRa(SX1276_t * module);
  339. void SX1276_clearLoRaIrq(SX1276_t * module);
  340. int SX1276_LoRaEntryRx(SX1276_t * module, uint8_t length, uint32_t timeout);
  341. uint8_t SX1276_LoRaRxPacket(SX1276_t * module);
  342. int SX1276_LoRaEntryTx(SX1276_t * module, uint8_t length, uint32_t timeout);
  343. int SX1276_LoRaTxModeSet(SX1276_t * module, uint8_t length, uint32_t timeout);
  344. int SX1276_LoRaRxModeSet(SX1276_t * module, uint8_t length, uint32_t timeout);
  345. int SX1276_LoRaTxPacket(SX1276_t * module, uint8_t *txBuf, uint8_t length,
  346. uint32_t timeout);
  347. void SX1276_begin(SX1276_t * module, uint8_t frequency, uint8_t power,
  348. uint8_t LoRa_Rate, uint8_t LoRa_BW, uint8_t packetLength,uint8_t LoRa_Lna,uint8_t LoRa_PaBoost);
  349. int SX1276_transmit(SX1276_t * module, uint8_t *txBuf, uint8_t length,
  350. uint32_t timeout);
  351. int SX1276_(SX1276_t * module, uint8_t length, uint32_t timeoutT);
  352. uint8_t SX1276_available(SX1276_t * module);
  353. uint8_t SX1276_read(SX1276_t * module, uint8_t *rxBuf, uint8_t length);
  354. uint8_t SX1276_RSSI_LoRa(SX1276_t * module);
  355. uint8_t SX1276_RSSI(SX1276_t * module);
  356. void SX1276_standby(SX1276_t * module);
  357. void SX1276_sleep(SX1276_t * module);
  358. uint8_t SX1276_SPIIDRead(SX1276_t * module, uint8_t addr);
  359. #endif