A low cost DIY sound pressure level sensor for enabling environmental noise awareness. https://lukasschwarz.org/noise-sensor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SX1278.h 14 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/SX1278_LoRa
  7. */
  8. #ifndef __SX1278_H__
  9. #define __SX1278_H__
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include <SX1278_hw.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define SX1278_MAX_PACKET 256
  17. #define SX1278_DEFAULT_TIMEOUT 3000
  18. /* RFM98 Internal registers Address */
  19. /********************LoRa mode***************************/
  20. #define LR_RegFifo 0x00
  21. /* Common settings */
  22. #define LR_RegOpMode 0x01
  23. #define LR_RegFrMsb 0x06
  24. #define LR_RegFrMid 0x07
  25. #define LR_RegFrLsb 0x08
  26. /* Tx settings */
  27. #define LR_RegPaConfig 0x09
  28. #define LR_RegPaRamp 0x0A
  29. #define LR_RegOcp 0x0B
  30. /* Rx settings */
  31. #define LR_RegLna 0x0C
  32. /* LoRa registers */
  33. #define LR_RegFifoAddrPtr 0x0D
  34. #define LR_RegFifoTxBaseAddr 0x0E
  35. #define LR_RegFifoRxBaseAddr 0x0F
  36. #define LR_RegFifoRxCurrentaddr 0x10
  37. #define LR_RegIrqFlagsMask 0x11
  38. #define LR_RegIrqFlags 0x12
  39. #define LR_RegRxNbBytes 0x13
  40. #define LR_RegRxHeaderCntValueMsb 0x14
  41. #define LR_RegRxHeaderCntValueLsb 0x15
  42. #define LR_RegRxPacketCntValueMsb 0x16
  43. #define LR_RegRxPacketCntValueLsb 0x17
  44. #define LR_RegModemStat 0x18
  45. #define LR_RegPktSnrValue 0x19
  46. #define LR_RegPktRssiValue 0x1A
  47. #define LR_RegRssiValue 0x1B
  48. #define LR_RegHopChannel 0x1C
  49. #define LR_RegModemConfig1 0x1D
  50. #define LR_RegModemConfig2 0x1E
  51. #define LR_RegSymbTimeoutLsb 0x1F
  52. #define LR_RegPreambleMsb 0x20
  53. #define LR_RegPreambleLsb 0x21
  54. #define LR_RegPayloadLength 0x22
  55. #define LR_RegMaxPayloadLength 0x23
  56. #define LR_RegHopPeriod 0x24
  57. #define LR_RegFifoRxByteAddr 0x25
  58. #define LR_RegModemConfig3 0x26
  59. /* I/O settings */
  60. #define REG_LR_DIOMAPPING1 0x40
  61. #define REG_LR_DIOMAPPING2 0x41
  62. /* Version */
  63. #define REG_LR_VERSION 0x42
  64. /* Additional settings */
  65. #define REG_LR_PLLHOP 0x44
  66. #define REG_LR_TCXO 0x4B
  67. #define REG_LR_PADAC 0x4D
  68. #define REG_LR_FORMERTEMP 0x5B
  69. #define REG_LR_AGCREF 0x61
  70. #define REG_LR_AGCTHRESH1 0x62
  71. #define REG_LR_AGCTHRESH2 0x63
  72. #define REG_LR_AGCTHRESH3 0x64
  73. /********************FSK/ook mode***************************/
  74. #define RegFIFO 0x00
  75. #define RegOpMode 0x01
  76. #define RegBitRateMsb 0x02
  77. #define RegBitRateLsb 0x03
  78. #define RegFdevMsb 0x04
  79. #define RegFdevLsb 0x05
  80. #define RegFreqMsb 0x06
  81. #define RegFreqMid 0x07
  82. #define RegFreqLsb 0x08
  83. #define RegPaConfig 0x09
  84. #define RegPaRamp 0x0a
  85. #define RegOcp 0x0b
  86. #define RegLna 0x0c
  87. #define RegRxConfig 0x0d
  88. #define RegRssiConfig 0x0e
  89. #define RegRssiCollision 0x0f
  90. #define RegRssiThresh 0x10
  91. #define RegRssiValue 0x11
  92. #define RegRxBw 0x12
  93. #define RegAfcBw 0x13
  94. #define RegOokPeak 0x14
  95. #define RegOokFix 0x15
  96. #define RegOokAvg 0x16
  97. #define RegAfcFei 0x1a
  98. #define RegAfcMsb 0x1b
  99. #define RegAfcLsb 0x1c
  100. #define RegFeiMsb 0x1d
  101. #define RegFeiLsb 0x1e
  102. #define RegPreambleDetect 0x1f
  103. #define RegRxTimeout1 0x20
  104. #define RegRxTimeout2 0x21
  105. #define RegRxTimeout3 0x22
  106. #define RegRxDelay 0x23
  107. #define RegOsc 0x24
  108. #define RegPreambleMsb 0x25
  109. #define RegPreambleLsb 0x26
  110. #define RegSyncConfig 0x27
  111. #define RegSyncValue1 0x28
  112. #define RegSyncValue2 0x29
  113. #define RegSyncValue3 0x2a
  114. #define RegSyncValue4 0x2b
  115. #define RegSyncValue5 0x2c
  116. #define RegSyncValue6 0x2d
  117. #define RegSyncValue7 0x2e
  118. #define RegSyncValue8 0x2f
  119. #define RegPacketConfig1 0x30
  120. #define RegPacketConfig2 0x31
  121. #define RegPayloadLength 0x32
  122. #define RegNodeAdrs 0x33
  123. #define RegBroadcastAdrs 0x34
  124. #define RegFifoThresh 0x35
  125. #define RegSeqConfig1 0x36
  126. #define RegSeqConfig2 0x37
  127. #define RegTimerResol 0x38
  128. #define RegTimer1Coef 0x39
  129. #define RegSyncWord 0x39
  130. #define RegTimer2Coef 0x3a
  131. #define RegImageCal 0x3b
  132. #define RegTemp 0x3c
  133. #define RegLowBat 0x3d
  134. #define RegIrqFlags1 0x3e
  135. #define RegIrqFlags2 0x3f
  136. #define RegDioMapping1 0x40
  137. #define RegDioMapping2 0x41
  138. #define RegVersion 0x42
  139. #define RegPllHop 0x44
  140. #define RegPaDac 0x4d
  141. #define RegBitRateFrac 0x5d
  142. /**********************************************************
  143. **Parameter table define
  144. **********************************************************/
  145. #define SX1278_POWER_20DBM 0
  146. #define SX1278_POWER_17DBM 1
  147. #define SX1278_POWER_14DBM 2
  148. #define SX1278_POWER_11DBM 3
  149. static const uint8_t SX1278_Power[4] = { 0xFF, /* 20dbm */
  150. 0xFC, /* 17dbm */
  151. 0xF9, /* 14dbm */
  152. 0xF6, /* 11dbm */
  153. };
  154. #define SX1278_LORA_SF_6 0
  155. #define SX1278_LORA_SF_7 1
  156. #define SX1278_LORA_SF_8 2
  157. #define SX1278_LORA_SF_9 3
  158. #define SX1278_LORA_SF_10 4
  159. #define SX1278_LORA_SF_11 5
  160. #define SX1278_LORA_SF_12 6
  161. static const uint8_t SX1278_SpreadFactor[7] = { 6, 7, 8, 9, 10, 11, 12 };
  162. #define SX1278_LORA_BW_7_8KHZ 0
  163. #define SX1278_LORA_BW_10_4KHZ 1
  164. #define SX1278_LORA_BW_15_6KHZ 2
  165. #define SX1278_LORA_BW_20_8KHZ 3
  166. #define SX1278_LORA_BW_31_2KHZ 4
  167. #define SX1278_LORA_BW_41_7KHZ 5
  168. #define SX1278_LORA_BW_62_5KHZ 6
  169. #define SX1278_LORA_BW_125KHZ 7
  170. #define SX1278_LORA_BW_250KHZ 8
  171. #define SX1278_LORA_BW_500KHZ 9
  172. static const uint8_t SX1278_LoRaBandwidth[10] = { 0, /* 7.8KHz, */
  173. 1, /* 10.4KHz, */
  174. 2, /* 15.6KHz, */
  175. 3, /* 20.8KHz, */
  176. 4, /* 31.2KHz, */
  177. 5, /* 41.7KHz, */
  178. 6, /* 62.5KHz, */
  179. 7, /* 125.0KHz, */
  180. 8, /* 250.0KHz, */
  181. 9 /* 500.0KHz */
  182. };
  183. /* Coding rate */
  184. #define SX1278_LORA_CR_4_5 0
  185. #define SX1278_LORA_CR_4_6 1
  186. #define SX1278_LORA_CR_4_7 2
  187. #define SX1278_LORA_CR_4_8 3
  188. static const uint8_t SX1278_CodingRate[4] = { 0x01, 0x02, 0x03, 0x04 };
  189. /* CRC Enable */
  190. #define SX1278_LORA_CRC_EN 0
  191. #define SX1278_LORA_CRC_DIS 1
  192. static const uint8_t SX1278_CRC_Sum[2] = { 0x01, 0x00 };
  193. typedef enum _SX1278_STATUS {
  194. SLEEP, STANDBY, TX, RX
  195. } SX1278_Status_t;
  196. typedef struct {
  197. SX1278_hw_t *hw;
  198. uint64_t frequency;
  199. uint8_t power;
  200. uint8_t LoRa_SF;
  201. uint8_t LoRa_BW;
  202. uint8_t LoRa_CR;
  203. uint8_t LoRa_CRC_sum;
  204. uint8_t packetLength;
  205. SX1278_Status_t status;
  206. uint8_t rxBuffer[SX1278_MAX_PACKET];
  207. uint8_t readBytes;
  208. } SX1278_t;
  209. /**
  210. * \brief Read byte from LoRa module
  211. *
  212. * Reads data from LoRa module from given address.
  213. *
  214. * \param[in] module Pointer to LoRa structure
  215. * \param[in] addr Address from which data will be read
  216. *
  217. * \return Read data
  218. */
  219. uint8_t SX1278_SPIRead(SX1278_t *module, uint8_t addr);
  220. /**
  221. * \brief Write byte to LoRa module
  222. *
  223. * Writes data to LoRa module under given address.
  224. *
  225. * \param[in] module Pointer to LoRa structure
  226. * \param[in] addr Address under which data will be written
  227. * \param[in] cmd Data to write
  228. */
  229. void SX1278_SPIWrite(SX1278_t *module, uint8_t addr, uint8_t cmd);
  230. /**
  231. * \brief Read data from LoRa module
  232. *
  233. * Reads data from LoRa module from given address.
  234. *
  235. * \param[in] module Pointer to LoRa structure
  236. * \param[in] addr Address from which data will be read
  237. * \param[out] rxBuf Pointer to store read data
  238. * \param[in] length Number of bytes to read
  239. */
  240. void SX1278_SPIBurstRead(SX1278_t *module, uint8_t addr, uint8_t *rxBuf,
  241. uint8_t length);
  242. /**
  243. * \brief Write data to LoRa module
  244. *
  245. * Writes data to LoRa module under given address.
  246. *
  247. * \param[in] module Pointer to LoRa structure
  248. * \param[in] addr Address under which data will be written
  249. * \param[in] txBuf Pointer to data
  250. * \param[in] length Number of bytes to write
  251. */
  252. void SX1278_SPIBurstWrite(SX1278_t *module, uint8_t addr, uint8_t *txBuf,
  253. uint8_t length);
  254. /**
  255. * \brief Configure LoRa module
  256. *
  257. * Configure LoRa module according to parameters stored in
  258. * module structure.
  259. *
  260. * \param[in] module Pointer to LoRa structure
  261. */
  262. void SX1278_config(SX1278_t *module);
  263. /**
  264. * \brief Entry LoRa mode
  265. *
  266. * Module supports different operation mode.
  267. * To use LoRa operation mode one has to enter this
  268. * particular mode to transmit and receive data
  269. * using LoRa.
  270. *
  271. * \param[in] module Pointer to LoRa structure
  272. */
  273. void SX1278_entryLoRa(SX1278_t *module);
  274. /**
  275. * \brief Clear IRQ
  276. *
  277. * Clears interrupt flags.
  278. *
  279. * \param[in] module Pointer to LoRa structure
  280. */
  281. void SX1278_clearLoRaIrq(SX1278_t *module);
  282. /**
  283. * \brief Entry reception mode
  284. *
  285. * Entry reception mode
  286. *
  287. * \param[in] module Pointer to LoRa structure
  288. * \param[in] length Length of message to be received
  289. * \param[in] timeout Timeout in [ms]
  290. *
  291. * \return 1 if entering reception mode
  292. * 0 if timeout was exceeded
  293. */
  294. int SX1278_LoRaEntryRx(SX1278_t *module, uint8_t length, uint32_t timeout);
  295. /**
  296. * \brief Read data
  297. *
  298. * Read data and store it in module's RX buffer
  299. *
  300. * \param[in] module Pointer to LoRa structure
  301. *
  302. * \return returns number of read bytes
  303. */
  304. uint8_t SX1278_LoRaRxPacket(SX1278_t *module);
  305. /**
  306. * \brief Entry transmitter mode
  307. *
  308. * Entry transmitter mode
  309. *
  310. * \param[in] module Pointer to LoRa structure
  311. * \param[in] length Length of message to be sent
  312. * \param[in] timeout Timeout in [ms]
  313. *
  314. * \return 1 if entering reception mode
  315. * 0 if timeout was exceeded
  316. */
  317. int SX1278_LoRaEntryTx(SX1278_t *module, uint8_t length, uint32_t timeout);
  318. /**
  319. * \brief Send data
  320. *
  321. * Transmit data
  322. *
  323. * \param[in] module Pointer to LoRa structure
  324. * \param[in] txBuf Data buffer with data to be sent
  325. * \param[in] length Length of message to be sent
  326. * \param[in] timeout Timeout in [ms]
  327. *
  328. * \return 1 if entering reception mode
  329. * 0 if timeout was exceeded
  330. */
  331. int SX1278_LoRaTxPacket(SX1278_t *module, uint8_t *txBuf, uint8_t length,
  332. uint32_t timeout);
  333. /**
  334. * \brief Initialize LoRa module
  335. *
  336. * Initialize LoRa module and initialize LoRa structure.
  337. *
  338. * \param[in] module Pointer to LoRa structure
  339. * \param[in] frequency Frequency in [Hz]
  340. * \param[in] power Power level, accepts SX1278_POWER_*
  341. * \param[in] LoRa_SF LoRa spread rate, accepts SX1278_LORA_SF_*
  342. * \param[in] LoRa_BW LoRa bandwidth, accepts SX1278_LORA_BW_*
  343. * \param[in] LoRa_CR LoRa coding rate, accepts SX1278_LORA_CR_*
  344. * \param[in] LoRa_CRC_sum Hardware CRC check, SX1278_LORA_CRC_EN or
  345. * SX1278_LORA_CRC_DIS
  346. * \param[in] packetLength Package length, no more than 256 bytes
  347. */
  348. void SX1278_init(SX1278_t *module, uint64_t frequency, uint8_t power,
  349. uint8_t LoRa_SF, uint8_t LoRa_BW, uint8_t LoRa_CR,
  350. uint8_t LoRa_CRC_sum, uint8_t packetLength);
  351. /**
  352. * \brief Entry transmitter mode and send data
  353. *
  354. * Entry transmitter mode and send data.
  355. * Combination of SX1278_LoRaEntryTx() and SX1278_LoRaTxPacket().
  356. *
  357. * \param[in] module Pointer to LoRa structure
  358. * \param[in] txBuf Data buffer with data to be sent
  359. * \param[in] length Length of message to be sent
  360. * \param[in] timeout Timeout in [ms]
  361. *
  362. * \return 1 if entered TX mode and sent data
  363. * 0 if timeout was exceeded
  364. */
  365. int SX1278_transmit(SX1278_t *module, uint8_t *txBuf, uint8_t length,
  366. uint32_t timeout);
  367. /**
  368. * \brief Entry reception mode
  369. *
  370. * Same as SX1278_LoRaEntryRx()
  371. *
  372. * \param[in] module Pointer to LoRa structure
  373. * \param[in] length Length of message to be received
  374. * \param[in] timeout Timeout in [ms]
  375. *
  376. * \return 1 if entering reception mode
  377. * 0 if timeout was exceeded
  378. */
  379. int SX1278_receive(SX1278_t *module, uint8_t length, uint32_t timeout);
  380. /**
  381. * \brief Returns number of received data
  382. *
  383. * Returns the number of received data which are
  384. * held in internal buffer.
  385. * Same as SX1278_LoRaRxPacket().
  386. *
  387. * \param[in] module Pointer to LoRa structure
  388. *
  389. * \return returns number of read bytes
  390. */
  391. uint8_t SX1278_available(SX1278_t *module);
  392. /**
  393. * \brief Read received data to buffer
  394. *
  395. * Reads data from internal buffer to external
  396. * buffer. Reads exactly number of bytes which are stored in
  397. * internal buffer.
  398. *
  399. * \param[in] module Pointer to LoRa structure
  400. * \param[out] rxBuf External buffer to store data.
  401. * External buffer is terminated with '\0'
  402. * character
  403. * \param[in] length Length of message to be received
  404. *
  405. * \return returns number of read bytes
  406. */
  407. uint8_t SX1278_read(SX1278_t *module, uint8_t *rxBuf, uint8_t length);
  408. /**
  409. * \brief Returns RSSI (LoRa)
  410. *
  411. * Returns RSSI in LoRa mode.
  412. *
  413. * \param[in] module Pointer to LoRa structure
  414. *
  415. * \return RSSI value
  416. */
  417. uint8_t SX1278_RSSI_LoRa(SX1278_t *module);
  418. /**
  419. * \brief Returns RSSI
  420. *
  421. * Returns RSSI (general mode).
  422. *
  423. * \param[in] module Pointer to LoRa structure
  424. *
  425. * \return RSSI value
  426. */
  427. uint8_t SX1278_RSSI(SX1278_t *module);
  428. /**
  429. * \brief Enter standby mode
  430. *
  431. * Enters standby mode.
  432. *
  433. * \param[in] module Pointer to LoRa structure
  434. */
  435. void SX1278_standby(SX1278_t *module);
  436. /**
  437. * \brief Enter sleep mode
  438. *
  439. * Enters sleep mode.
  440. *
  441. * \param[in] module Pointer to LoRa structure
  442. */
  443. void SX1278_sleep(SX1278_t *module);
  444. #ifdef __cplusplus
  445. }
  446. #endif
  447. #endif