sx126x.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*!
  2. * \file sx126x.h
  3. *
  4. * \brief SX126x driver implementation
  5. *
  6. * \copyright Revised BSD License, see section \ref LICENSE.
  7. *
  8. * \code
  9. * ______ _
  10. * / _____) _ | |
  11. * ( (____ _____ ____ _| |_ _____ ____| |__
  12. * \____ \| ___ | (_ _) ___ |/ ___) _ \
  13. * _____) ) ____| | | || |_| ____( (___| | | |
  14. * (______/|_____)_|_|_| \__)_____)\____)_| |_|
  15. * (C)2013-2017 Semtech
  16. *
  17. * \endcode
  18. *
  19. * \author Miguel Luis ( Semtech )
  20. *
  21. * \author Gregory Cristian ( Semtech )
  22. */
  23. #ifndef __SX126x_H__
  24. #define __SX126x_H__
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. #include <stdint.h>
  30. #include <stdbool.h>
  31. #include <math.h>
  32. #include "radio.h"
  33. #define SX1261 1
  34. #define SX1262 2
  35. /*!
  36. * Radio complete Wake-up Time with margin for temperature compensation
  37. */
  38. #define RADIO_WAKEUP_TIME 3 // [ms]
  39. /*!
  40. * \brief Compensation delay for SetAutoTx/Rx functions in 15.625 microseconds
  41. */
  42. #define AUTO_RX_TX_OFFSET 2
  43. /*!
  44. * \brief LFSR initial value to compute IBM type CRC
  45. */
  46. #define CRC_IBM_SEED 0xFFFF
  47. /*!
  48. * \brief LFSR initial value to compute CCIT type CRC
  49. */
  50. #define CRC_CCITT_SEED 0x1D0F
  51. /*!
  52. * \brief Polynomial used to compute IBM CRC
  53. */
  54. #define CRC_POLYNOMIAL_IBM 0x8005
  55. /*!
  56. * \brief Polynomial used to compute CCIT CRC
  57. */
  58. #define CRC_POLYNOMIAL_CCITT 0x1021
  59. /*!
  60. * \brief The address of the register holding the first byte defining the CRC seed
  61. *
  62. */
  63. #define REG_LR_CRCSEEDBASEADDR 0x06BC
  64. /*!
  65. * \brief The address of the register holding the first byte defining the CRC polynomial
  66. */
  67. #define REG_LR_CRCPOLYBASEADDR 0x06BE
  68. /*!
  69. * \brief The address of the register holding the first byte defining the whitening seed
  70. */
  71. #define REG_LR_WHITSEEDBASEADDR_MSB 0x06B8
  72. #define REG_LR_WHITSEEDBASEADDR_LSB 0x06B9
  73. /*!
  74. * \brief The address of the register holding the packet configuration
  75. */
  76. #define REG_LR_PACKETPARAMS 0x0704
  77. /*!
  78. * \brief The address of the register holding the payload size
  79. */
  80. #define REG_LR_PAYLOADLENGTH 0x0702
  81. /*!
  82. * \brief The address of the register holding the re-calculated number of symbols
  83. */
  84. #define REG_LR_SYNCH_TIMEOUT 0x0706
  85. /*!
  86. * \brief The addresses of the registers holding SyncWords values
  87. */
  88. #define REG_LR_SYNCWORDBASEADDRESS 0x06C0
  89. /*!
  90. * \brief The addresses of the register holding LoRa Modem SyncWord value
  91. */
  92. #define REG_LR_SYNCWORD 0x0740
  93. /*!
  94. * Syncword for Private LoRa networks
  95. */
  96. #define LORA_MAC_PRIVATE_SYNCWORD 0x1424
  97. /*!
  98. * Syncword for Public LoRa networks
  99. */
  100. #define LORA_MAC_PUBLIC_SYNCWORD 0x3444
  101. /*!
  102. * The address of the register giving a 32-bit random number
  103. */
  104. #define RANDOM_NUMBER_GENERATORBASEADDR 0x0819
  105. /*!
  106. * The address of the register used to disable the LNA
  107. */
  108. #define REG_ANA_LNA 0x08E2
  109. /*!
  110. * The address of the register used to disable the mixer
  111. */
  112. #define REG_ANA_MIXER 0x08E5
  113. /*!
  114. * The address of the register holding RX Gain value (0x94: power saving, 0x96: rx boosted)
  115. */
  116. #define REG_RX_GAIN 0x08AC
  117. /*!
  118. * Change the value on the device internal trimming capacitor
  119. */
  120. #define REG_XTA_TRIM 0x0911
  121. /*!
  122. * Set the current max value in the over current protection
  123. */
  124. #define REG_OCP 0x08E7
  125. /*!
  126. * \brief Structure describing the radio status
  127. */
  128. typedef union RadioStatus_u
  129. {
  130. uint8_t Value;
  131. struct
  132. { //bit order is lsb -> msb
  133. uint8_t : 1; //!< Reserved
  134. uint8_t CmdStatus : 3; //!< Command status
  135. uint8_t ChipMode : 3; //!< Chip mode
  136. uint8_t : 1; //!< Reserved
  137. }Fields;
  138. }RadioStatus_t;
  139. /*!
  140. * \brief Structure describing the error codes for callback functions
  141. */
  142. typedef enum
  143. {
  144. IRQ_HEADER_ERROR_CODE = 0x01,
  145. IRQ_SYNCWORD_ERROR_CODE = 0x02,
  146. IRQ_CRC_ERROR_CODE = 0x04,
  147. }IrqErrorCode_t;
  148. enum IrqPblSyncHeaderCode_t
  149. {
  150. IRQ_PBL_DETECT_CODE = 0x01,
  151. IRQ_SYNCWORD_VALID_CODE = 0x02,
  152. IRQ_HEADER_VALID_CODE = 0x04,
  153. };
  154. /*!
  155. * \brief Represents the operating mode the radio is actually running
  156. */
  157. typedef enum
  158. {
  159. MODE_SLEEP = 0x00, //! The radio is in sleep mode
  160. MODE_STDBY_RC, //! The radio is in standby mode with RC oscillator
  161. MODE_STDBY_XOSC, //! The radio is in standby mode with XOSC oscillator
  162. MODE_FS, //! The radio is in frequency synthesis mode
  163. MODE_TX, //! The radio is in transmit mode
  164. MODE_RX, //! The radio is in receive mode
  165. MODE_RX_DC, //! The radio is in receive duty cycle mode
  166. MODE_CAD //! The radio is in channel activity detection mode
  167. }RadioOperatingModes_t;
  168. /*!
  169. * \brief Declares the oscillator in use while in standby mode
  170. *
  171. * Using the STDBY_RC standby mode allow to reduce the energy consumption
  172. * STDBY_XOSC should be used for time critical applications
  173. */
  174. typedef enum
  175. {
  176. STDBY_RC = 0x00,
  177. STDBY_XOSC = 0x01,
  178. }RadioStandbyModes_t;
  179. /*!
  180. * \brief Declares the power regulation used to power the device
  181. *
  182. * This command allows the user to specify if DC-DC or LDO is used for power regulation.
  183. * Using only LDO implies that the Rx or Tx current is doubled
  184. */
  185. typedef enum
  186. {
  187. USE_LDO = 0x00, // default
  188. USE_DCDC = 0x01,
  189. }RadioRegulatorMode_t;
  190. /*!
  191. * \brief Represents the possible packet type (i.e. modem) used
  192. */
  193. typedef enum
  194. {
  195. PACKET_TYPE_GFSK = 0x00,
  196. PACKET_TYPE_LORA = 0x01,
  197. PACKET_TYPE_NONE = 0x0F,
  198. }RadioPacketTypes_t;
  199. /*!
  200. * \brief Represents the ramping time for power amplifier
  201. */
  202. typedef enum
  203. {
  204. RADIO_RAMP_10_US = 0x00,
  205. RADIO_RAMP_20_US = 0x01,
  206. RADIO_RAMP_40_US = 0x02,
  207. RADIO_RAMP_80_US = 0x03,
  208. RADIO_RAMP_200_US = 0x04,
  209. RADIO_RAMP_800_US = 0x05,
  210. RADIO_RAMP_1700_US = 0x06,
  211. RADIO_RAMP_3400_US = 0x07,
  212. }RadioRampTimes_t;
  213. /*!
  214. * \brief Represents the number of symbols to be used for channel activity detection operation
  215. */
  216. typedef enum
  217. {
  218. LORA_CAD_01_SYMBOL = 0x00,
  219. LORA_CAD_02_SYMBOL = 0x01,
  220. LORA_CAD_04_SYMBOL = 0x02,
  221. LORA_CAD_08_SYMBOL = 0x03,
  222. LORA_CAD_16_SYMBOL = 0x04,
  223. }RadioLoRaCadSymbols_t;
  224. /*!
  225. * \brief Represents the Channel Activity Detection actions after the CAD operation is finished
  226. */
  227. typedef enum
  228. {
  229. LORA_CAD_ONLY = 0x00,
  230. LORA_CAD_RX = 0x01,
  231. LORA_CAD_LBT = 0x10,
  232. }RadioCadExitModes_t;
  233. /*!
  234. * \brief Represents the modulation shaping parameter
  235. */
  236. typedef enum
  237. {
  238. MOD_SHAPING_OFF = 0x00,
  239. MOD_SHAPING_G_BT_03 = 0x08,
  240. MOD_SHAPING_G_BT_05 = 0x09,
  241. MOD_SHAPING_G_BT_07 = 0x0A,
  242. MOD_SHAPING_G_BT_1 = 0x0B,
  243. }RadioModShapings_t;
  244. /*!
  245. * \brief Represents the modulation shaping parameter
  246. */
  247. typedef enum
  248. {
  249. RX_BW_4800 = 0x1F,
  250. RX_BW_5800 = 0x17,
  251. RX_BW_7300 = 0x0F,
  252. RX_BW_9700 = 0x1E,
  253. RX_BW_11700 = 0x16,
  254. RX_BW_14600 = 0x0E,
  255. RX_BW_19500 = 0x1D,
  256. RX_BW_23400 = 0x15,
  257. RX_BW_29300 = 0x0D,
  258. RX_BW_39000 = 0x1C,
  259. RX_BW_46900 = 0x14,
  260. RX_BW_58600 = 0x0C,
  261. RX_BW_78200 = 0x1B,
  262. RX_BW_93800 = 0x13,
  263. RX_BW_117300 = 0x0B,
  264. RX_BW_156200 = 0x1A,
  265. RX_BW_187200 = 0x12,
  266. RX_BW_234300 = 0x0A,
  267. RX_BW_312000 = 0x19,
  268. RX_BW_373600 = 0x11,
  269. RX_BW_467000 = 0x09,
  270. }RadioRxBandwidth_t;
  271. /*!
  272. * \brief Represents the possible spreading factor values in LoRa packet types
  273. */
  274. typedef enum
  275. {
  276. LORA_SF5 = 0x05,
  277. LORA_SF6 = 0x06,
  278. LORA_SF7 = 0x07,
  279. LORA_SF8 = 0x08,
  280. LORA_SF9 = 0x09,
  281. LORA_SF10 = 0x0A,
  282. LORA_SF11 = 0x0B,
  283. LORA_SF12 = 0x0C,
  284. }RadioLoRaSpreadingFactors_t;
  285. /*!
  286. * \brief Represents the bandwidth values for LoRa packet type
  287. */
  288. typedef enum
  289. {
  290. LORA_BW_500 = 6,
  291. LORA_BW_250 = 5,
  292. LORA_BW_125 = 4,
  293. LORA_BW_062 = 3,
  294. LORA_BW_041 = 10,
  295. LORA_BW_031 = 2,
  296. LORA_BW_020 = 9,
  297. LORA_BW_015 = 1,
  298. LORA_BW_010 = 8,
  299. LORA_BW_007 = 0,
  300. }RadioLoRaBandwidths_t;
  301. /*!
  302. * \brief Represents the coding rate values for LoRa packet type
  303. */
  304. typedef enum
  305. {
  306. LORA_CR_4_5 = 0x01,
  307. LORA_CR_4_6 = 0x02,
  308. LORA_CR_4_7 = 0x03,
  309. LORA_CR_4_8 = 0x04,
  310. }RadioLoRaCodingRates_t;
  311. /*!
  312. * \brief Represents the preamble length used to detect the packet on Rx side
  313. */
  314. typedef enum
  315. {
  316. RADIO_PREAMBLE_DETECTOR_OFF = 0x00, //!< Preamble detection length off
  317. RADIO_PREAMBLE_DETECTOR_08_BITS = 0x04, //!< Preamble detection length 8 bits
  318. RADIO_PREAMBLE_DETECTOR_16_BITS = 0x05, //!< Preamble detection length 16 bits
  319. RADIO_PREAMBLE_DETECTOR_24_BITS = 0x06, //!< Preamble detection length 24 bits
  320. RADIO_PREAMBLE_DETECTOR_32_BITS = 0x07, //!< Preamble detection length 32 bit
  321. }RadioPreambleDetection_t;
  322. /*!
  323. * \brief Represents the possible combinations of SyncWord correlators activated
  324. */
  325. typedef enum
  326. {
  327. RADIO_ADDRESSCOMP_FILT_OFF = 0x00, //!< No correlator turned on, i.e. do not search for SyncWord
  328. RADIO_ADDRESSCOMP_FILT_NODE = 0x01,
  329. RADIO_ADDRESSCOMP_FILT_NODE_BROAD = 0x02,
  330. }RadioAddressComp_t;
  331. /*!
  332. * \brief Radio GFSK packet length mode
  333. */
  334. typedef enum
  335. {
  336. RADIO_PACKET_FIXED_LENGTH = 0x00, //!< The packet is known on both sides, no header included in the packet
  337. RADIO_PACKET_VARIABLE_LENGTH = 0x01, //!< The packet is on variable size, header included
  338. }RadioPacketLengthModes_t;
  339. /*!
  340. * \brief Represents the CRC length
  341. */
  342. typedef enum
  343. {
  344. RADIO_CRC_OFF = 0x01, //!< No CRC in use
  345. RADIO_CRC_1_BYTES = 0x00,
  346. RADIO_CRC_2_BYTES = 0x02,
  347. RADIO_CRC_1_BYTES_INV = 0x04,
  348. RADIO_CRC_2_BYTES_INV = 0x06,
  349. RADIO_CRC_2_BYTES_IBM = 0xF1,
  350. RADIO_CRC_2_BYTES_CCIT = 0xF2,
  351. }RadioCrcTypes_t;
  352. /*!
  353. * \brief Radio whitening mode activated or deactivated
  354. */
  355. typedef enum
  356. {
  357. RADIO_DC_FREE_OFF = 0x00,
  358. RADIO_DC_FREEWHITENING = 0x01,
  359. }RadioDcFree_t;
  360. /*!
  361. * \brief Holds the Radio lengths mode for the LoRa packet type
  362. */
  363. typedef enum
  364. {
  365. LORA_PACKET_VARIABLE_LENGTH = 0x00, //!< The packet is on variable size, header included
  366. LORA_PACKET_FIXED_LENGTH = 0x01, //!< The packet is known on both sides, no header included in the packet
  367. LORA_PACKET_EXPLICIT = LORA_PACKET_VARIABLE_LENGTH,
  368. LORA_PACKET_IMPLICIT = LORA_PACKET_FIXED_LENGTH,
  369. }RadioLoRaPacketLengthsMode_t;
  370. /*!
  371. * \brief Represents the CRC mode for LoRa packet type
  372. */
  373. typedef enum
  374. {
  375. LORA_CRC_ON = 0x01, //!< CRC activated
  376. LORA_CRC_OFF = 0x00, //!< CRC not used
  377. }RadioLoRaCrcModes_t;
  378. /*!
  379. * \brief Represents the IQ mode for LoRa packet type
  380. */
  381. typedef enum
  382. {
  383. LORA_IQ_NORMAL = 0x00,
  384. LORA_IQ_INVERTED = 0x01,
  385. }RadioLoRaIQModes_t;
  386. /*!
  387. * \brief Represents the voltage used to control the TCXO on/off from DIO3
  388. */
  389. typedef enum
  390. {
  391. TCXO_CTRL_1_6V = 0x00,
  392. TCXO_CTRL_1_7V = 0x01,
  393. TCXO_CTRL_1_8V = 0x02,
  394. TCXO_CTRL_2_2V = 0x03,
  395. TCXO_CTRL_2_4V = 0x04,
  396. TCXO_CTRL_2_7V = 0x05,
  397. TCXO_CTRL_3_0V = 0x06,
  398. TCXO_CTRL_3_3V = 0x07,
  399. }RadioTcxoCtrlVoltage_t;
  400. /*!
  401. * \brief Represents the interruption masks available for the radio
  402. *
  403. * \remark Note that not all these interruptions are available for all packet types
  404. */
  405. typedef enum
  406. {
  407. IRQ_RADIO_NONE = 0x0000,
  408. IRQ_TX_DONE = 0x0001,
  409. IRQ_RX_DONE = 0x0002,
  410. IRQ_PREAMBLE_DETECTED = 0x0004,
  411. IRQ_SYNCWORD_VALID = 0x0008,
  412. IRQ_HEADER_VALID = 0x0010,
  413. IRQ_HEADER_ERROR = 0x0020,
  414. IRQ_CRC_ERROR = 0x0040,
  415. IRQ_CAD_DONE = 0x0080,
  416. IRQ_CAD_ACTIVITY_DETECTED = 0x0100,
  417. IRQ_RX_TX_TIMEOUT = 0x0200,
  418. IRQ_RADIO_ALL = 0xFFFF,
  419. }RadioIrqMasks_t;
  420. /*!
  421. * \brief Represents all possible opcode understood by the radio
  422. */
  423. typedef enum RadioCommands_e
  424. {
  425. RADIO_GET_STATUS = 0xC0,
  426. RADIO_WRITE_REGISTER = 0x0D,
  427. RADIO_READ_REGISTER = 0x1D,
  428. RADIO_WRITE_BUFFER = 0x0E,
  429. RADIO_READ_BUFFER = 0x1E,
  430. RADIO_SET_SLEEP = 0x84,
  431. RADIO_SET_STANDBY = 0x80,
  432. RADIO_SET_FS = 0xC1,
  433. RADIO_SET_TX = 0x83,
  434. RADIO_SET_RX = 0x82,
  435. RADIO_SET_RXDUTYCYCLE = 0x94,
  436. RADIO_SET_CAD = 0xC5,
  437. RADIO_SET_TXCONTINUOUSWAVE = 0xD1,
  438. RADIO_SET_TXCONTINUOUSPREAMBLE = 0xD2,
  439. RADIO_SET_PACKETTYPE = 0x8A,
  440. RADIO_GET_PACKETTYPE = 0x11,
  441. RADIO_SET_RFFREQUENCY = 0x86,
  442. RADIO_SET_TXPARAMS = 0x8E,
  443. RADIO_SET_PACONFIG = 0x95,
  444. RADIO_SET_CADPARAMS = 0x88,
  445. RADIO_SET_BUFFERBASEADDRESS = 0x8F,
  446. RADIO_SET_MODULATIONPARAMS = 0x8B,
  447. RADIO_SET_PACKETPARAMS = 0x8C,
  448. RADIO_GET_RXBUFFERSTATUS = 0x13,
  449. RADIO_GET_PACKETSTATUS = 0x14,
  450. RADIO_GET_RSSIINST = 0x15,
  451. RADIO_GET_STATS = 0x10,
  452. RADIO_RESET_STATS = 0x00,
  453. RADIO_CFG_DIOIRQ = 0x08,
  454. RADIO_GET_IRQSTATUS = 0x12,
  455. RADIO_CLR_IRQSTATUS = 0x02,
  456. RADIO_CALIBRATE = 0x89,
  457. RADIO_CALIBRATEIMAGE = 0x98,
  458. RADIO_SET_REGULATORMODE = 0x96,
  459. RADIO_GET_ERROR = 0x17,
  460. RADIO_CLR_ERROR = 0x07,
  461. RADIO_SET_TCXOMODE = 0x97,
  462. RADIO_SET_TXFALLBACKMODE = 0x93,
  463. RADIO_SET_RFSWITCHMODE = 0x9D,
  464. RADIO_SET_STOPRXTIMERONPREAMBLE = 0x9F,
  465. RADIO_SET_LORASYMBTIMEOUT = 0xA0,
  466. }RadioCommands_t;
  467. /*!
  468. * \brief The type describing the modulation parameters for every packet types
  469. */
  470. typedef struct
  471. {
  472. RadioPacketTypes_t PacketType; //!< Packet to which the modulation parameters are referring to.
  473. struct
  474. {
  475. struct
  476. {
  477. uint32_t BitRate;
  478. uint32_t Fdev;
  479. RadioModShapings_t ModulationShaping;
  480. uint8_t Bandwidth;
  481. }Gfsk;
  482. struct
  483. {
  484. RadioLoRaSpreadingFactors_t SpreadingFactor; //!< Spreading Factor for the LoRa modulation
  485. RadioLoRaBandwidths_t Bandwidth; //!< Bandwidth for the LoRa modulation
  486. RadioLoRaCodingRates_t CodingRate; //!< Coding rate for the LoRa modulation
  487. uint8_t LowDatarateOptimize; //!< Indicates if the modem uses the low datarate optimization
  488. }LoRa;
  489. }Params; //!< Holds the modulation parameters structure
  490. }ModulationParams_t;
  491. /*!
  492. * \brief The type describing the packet parameters for every packet types
  493. */
  494. typedef struct
  495. {
  496. RadioPacketTypes_t PacketType; //!< Packet to which the packet parameters are referring to.
  497. struct
  498. {
  499. /*!
  500. * \brief Holds the GFSK packet parameters
  501. */
  502. struct
  503. {
  504. uint16_t PreambleLength; //!< The preamble Tx length for GFSK packet type in bit
  505. RadioPreambleDetection_t PreambleMinDetect; //!< The preamble Rx length minimal for GFSK packet type
  506. uint8_t SyncWordLength; //!< The synchronization word length for GFSK packet type
  507. RadioAddressComp_t AddrComp; //!< Activated SyncWord correlators
  508. RadioPacketLengthModes_t HeaderType; //!< If the header is explicit, it will be transmitted in the GFSK packet. If the header is implicit, it will not be transmitted
  509. uint8_t PayloadLength; //!< Size of the payload in the GFSK packet
  510. RadioCrcTypes_t CrcLength; //!< Size of the CRC block in the GFSK packet
  511. RadioDcFree_t DcFree;
  512. }Gfsk;
  513. /*!
  514. * \brief Holds the LoRa packet parameters
  515. */
  516. struct
  517. {
  518. uint16_t PreambleLength; //!< The preamble length is the number of LoRa symbols in the preamble
  519. RadioLoRaPacketLengthsMode_t HeaderType; //!< If the header is explicit, it will be transmitted in the LoRa packet. If the header is implicit, it will not be transmitted
  520. uint8_t PayloadLength; //!< Size of the payload in the LoRa packet
  521. RadioLoRaCrcModes_t CrcMode; //!< Size of CRC block in LoRa packet
  522. RadioLoRaIQModes_t InvertIQ; //!< Allows to swap IQ for LoRa packet
  523. }LoRa;
  524. }Params; //!< Holds the packet parameters structure
  525. }PacketParams_t;
  526. /*!
  527. * \brief Represents the packet status for every packet type
  528. */
  529. typedef struct
  530. {
  531. RadioPacketTypes_t packetType; //!< Packet to which the packet status are referring to.
  532. struct
  533. {
  534. struct
  535. {
  536. uint8_t RxStatus;
  537. int8_t RssiAvg; //!< The averaged RSSI
  538. int8_t RssiSync; //!< The RSSI measured on last packet
  539. uint32_t FreqError;
  540. }Gfsk;
  541. struct
  542. {
  543. int8_t RssiPkt; //!< The RSSI of the last packet
  544. int8_t SnrPkt; //!< The SNR of the last packet
  545. int8_t SignalRssiPkt;
  546. uint32_t FreqError;
  547. }LoRa;
  548. }Params;
  549. }PacketStatus_t;
  550. /*!
  551. * \brief Represents the Rx internal counters values when GFSK or LoRa packet type is used
  552. */
  553. typedef struct
  554. {
  555. RadioPacketTypes_t packetType; //!< Packet to which the packet status are referring to.
  556. uint16_t PacketReceived;
  557. uint16_t CrcOk;
  558. uint16_t LengthError;
  559. }RxCounter_t;
  560. /*!
  561. * \brief Represents a calibration configuration
  562. */
  563. typedef union
  564. {
  565. struct
  566. {
  567. uint8_t RC64KEnable : 1; //!< Calibrate RC64K clock
  568. uint8_t RC13MEnable : 1; //!< Calibrate RC13M clock
  569. uint8_t PLLEnable : 1; //!< Calibrate PLL
  570. uint8_t ADCPulseEnable : 1; //!< Calibrate ADC Pulse
  571. uint8_t ADCBulkNEnable : 1; //!< Calibrate ADC bulkN
  572. uint8_t ADCBulkPEnable : 1; //!< Calibrate ADC bulkP
  573. uint8_t ImgEnable : 1;
  574. uint8_t : 1;
  575. }Fields;
  576. uint8_t Value;
  577. }CalibrationParams_t;
  578. /*!
  579. * \brief Represents a sleep mode configuration
  580. */
  581. typedef union
  582. {
  583. struct
  584. {
  585. uint8_t WakeUpRTC : 1; //!< Get out of sleep mode if wakeup signal received from RTC
  586. uint8_t Reset : 1;
  587. uint8_t WarmStart : 1;
  588. uint8_t Reserved : 5;
  589. }Fields;
  590. uint8_t Value;
  591. }SleepParams_t;
  592. /*!
  593. * \brief Represents the possible radio system error states
  594. */
  595. typedef union
  596. {
  597. struct
  598. {
  599. uint8_t Rc64kCalib : 1; //!< RC 64kHz oscillator calibration failed
  600. uint8_t Rc13mCalib : 1; //!< RC 13MHz oscillator calibration failed
  601. uint8_t PllCalib : 1; //!< PLL calibration failed
  602. uint8_t AdcCalib : 1; //!< ADC calibration failed
  603. uint8_t ImgCalib : 1; //!< Image calibration failed
  604. uint8_t XoscStart : 1; //!< XOSC oscillator failed to start
  605. uint8_t PllLock : 1; //!< PLL lock failed
  606. uint8_t : 1; //!< Buck converter failed to start
  607. uint8_t PaRamp : 1; //!< PA ramp failed
  608. uint8_t : 7; //!< Reserved
  609. }Fields;
  610. uint16_t Value;
  611. }RadioError_t;
  612. /*!
  613. * Radio hardware and global parameters
  614. */
  615. typedef struct SX126x_s
  616. {
  617. PacketParams_t PacketParams;
  618. PacketStatus_t PacketStatus;
  619. ModulationParams_t ModulationParams;
  620. }SX126x_t;
  621. /*!
  622. * Hardware IO IRQ callback function definition
  623. */
  624. typedef void ( DioIrqHandler )( void* context );
  625. /*
  626. * SX126x definitions
  627. */
  628. /*!
  629. * \brief The radio callbacks structure
  630. * Holds function pointers to be called on radio interrupts
  631. */
  632. typedef struct
  633. {
  634. void ( *txDone )( void ); //!< Pointer to a function run on successful transmission
  635. void ( *rxDone )( void ); //!< Pointer to a function run on successful reception
  636. void ( *rxPreambleDetect )( void ); //!< Pointer to a function run on successful Preamble detection
  637. void ( *rxSyncWordDone )( void ); //!< Pointer to a function run on successful SyncWord reception
  638. void ( *rxHeaderDone )( bool isOk ); //!< Pointer to a function run on successful Header reception
  639. void ( *txTimeout )( void ); //!< Pointer to a function run on transmission timeout
  640. void ( *rxTimeout )( void ); //!< Pointer to a function run on reception timeout
  641. void ( *rxError )( IrqErrorCode_t errCode ); //!< Pointer to a function run on reception error
  642. void ( *cadDone )( bool cadFlag ); //!< Pointer to a function run on channel activity detected
  643. }SX126xCallbacks_t;
  644. /*!
  645. * ============================================================================
  646. * Public functions prototypes
  647. * ============================================================================
  648. */
  649. /*!
  650. * \brief Initializes the radio driver
  651. */
  652. void SX126xInit( DioIrqHandler dioIrq );
  653. /*!
  654. * \brief Wakeup the radio if it is in Sleep mode and check that Busy is low
  655. */
  656. void SX126xCheckDeviceReady( void );
  657. /*!
  658. * \brief Saves the payload to be send in the radio buffer
  659. *
  660. * \param [in] payload A pointer to the payload
  661. * \param [in] size The size of the payload
  662. */
  663. void SX126xSetPayload( uint8_t *payload, uint8_t size );
  664. /*!
  665. * \brief Reads the payload received. If the received payload is longer
  666. * than maxSize, then the method returns 1 and do not set size and payload.
  667. *
  668. * \param [out] payload A pointer to a buffer into which the payload will be copied
  669. * \param [out] size A pointer to the size of the payload received
  670. * \param [in] maxSize The maximal size allowed to copy into the buffer
  671. */
  672. uint8_t SX126xGetPayload( uint8_t *payload, uint8_t *size, uint8_t maxSize );
  673. /*!
  674. * \brief Sends a payload
  675. *
  676. * \param [in] payload A pointer to the payload to send
  677. * \param [in] size The size of the payload to send
  678. * \param [in] timeout The timeout for Tx operation
  679. */
  680. void SX126xSendPayload( uint8_t *payload, uint8_t size, uint32_t timeout );
  681. /*!
  682. * \brief Sets the Sync Word given by index used in GFSK
  683. *
  684. * \param [in] syncWord SyncWord bytes ( 8 bytes )
  685. *
  686. * \retval status [0: OK, 1: NOK]
  687. */
  688. uint8_t SX126xSetSyncWord( uint8_t *syncWord );
  689. /*!
  690. * \brief Sets the Initial value for the LFSR used for the CRC calculation
  691. *
  692. * \param [in] seed Initial LFSR value ( 2 bytes )
  693. *
  694. */
  695. void SX126xSetCrcSeed( uint16_t seed );
  696. /*!
  697. * \brief Sets the seed used for the CRC calculation
  698. *
  699. * \param [in] seed The seed value
  700. *
  701. */
  702. void SX126xSetCrcPolynomial( uint16_t polynomial );
  703. /*!
  704. * \brief Sets the Initial value of the LFSR used for the whitening in GFSK protocols
  705. *
  706. * \param [in] seed Initial LFSR value
  707. */
  708. void SX126xSetWhiteningSeed( uint16_t seed );
  709. /*!
  710. * \brief Gets a 32-bit random value generated by the radio
  711. *
  712. * \remark A valid packet type must have been configured with \ref SX126xSetPacketType
  713. * before using this command.
  714. *
  715. * \remark The radio must be in reception mode before executing this function
  716. * This code can potentially result in interrupt generation. It is the responsibility of
  717. * the calling code to disable radio interrupts before calling this function,
  718. * and re-enable them afterwards if necessary, or be certain that any interrupts
  719. * generated during this process will not cause undesired side-effects in the software.
  720. *
  721. * Please note that the random numbers produced by the generator do not have a uniform or Gaussian distribution. If
  722. * uniformity is needed, perform appropriate software post-processing.
  723. *
  724. * \retval randomValue 32 bits random value
  725. */
  726. uint32_t SX126xGetRandom( void );
  727. /*!
  728. * \brief Sets the radio in sleep mode
  729. *
  730. * \param [in] sleepConfig The sleep configuration describing data
  731. * retention and RTC wake-up
  732. */
  733. void SX126xSetSleep( SleepParams_t sleepConfig );
  734. /*!
  735. * \brief Sets the radio in configuration mode
  736. *
  737. * \param [in] mode The standby mode to put the radio into
  738. */
  739. void SX126xSetStandby( RadioStandbyModes_t mode );
  740. /*!
  741. * \brief Sets the radio in FS mode
  742. */
  743. void SX126xSetFs( void );
  744. /*!
  745. * \brief Sets the radio in transmission mode
  746. *
  747. * \param [in] timeout Structure describing the transmission timeout value
  748. */
  749. void SX126xSetTx( uint32_t timeout );
  750. /*!
  751. * \brief Sets the radio in reception mode
  752. *
  753. * \param [in] timeout Structure describing the reception timeout value
  754. */
  755. void SX126xSetRx( uint32_t timeout );
  756. /*!
  757. * \brief Sets the radio in reception mode with Boosted LNA gain
  758. *
  759. * \param [in] timeout Structure describing the reception timeout value
  760. */
  761. void SX126xSetRxBoosted( uint32_t timeout );
  762. /*!
  763. * \brief Sets the Rx duty cycle management parameters
  764. *
  765. * \param [in] rxTime Structure describing reception timeout value
  766. * \param [in] sleepTime Structure describing sleep timeout value
  767. */
  768. void SX126xSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime );
  769. /*!
  770. * \brief Sets the radio in CAD mode
  771. */
  772. void SX126xSetCad( void );
  773. /*!
  774. * \brief Sets the radio in continuous wave transmission mode
  775. */
  776. void SX126xSetTxContinuousWave( void );
  777. /*!
  778. * \brief Sets the radio in continuous preamble transmission mode
  779. */
  780. void SX126xSetTxInfinitePreamble( void );
  781. /*!
  782. * \brief Decide which interrupt will stop the internal radio rx timer.
  783. *
  784. * \param [in] enable [0: Timer stop after header/syncword detection
  785. * 1: Timer stop after preamble detection]
  786. */
  787. void SX126xSetStopRxTimerOnPreambleDetect( bool enable );
  788. /*!
  789. * \brief Set the number of symbol the radio will wait to validate a reception
  790. *
  791. * \param [in] SymbNum number of LoRa symbols
  792. */
  793. void SX126xSetLoRaSymbNumTimeout( uint8_t SymbNum );
  794. /*!
  795. * \brief Sets the power regulators operating mode
  796. *
  797. * \param [in] mode [0: LDO, 1:DC_DC]
  798. */
  799. void SX126xSetRegulatorMode( RadioRegulatorMode_t mode );
  800. /*!
  801. * \brief Calibrates the given radio block
  802. *
  803. * \param [in] calibParam The description of blocks to be calibrated
  804. */
  805. void SX126xCalibrate( CalibrationParams_t calibParam );
  806. /*!
  807. * \brief Calibrates the Image rejection depending of the frequency
  808. *
  809. * \param [in] freq The operating frequency
  810. */
  811. void SX126xCalibrateImage( uint32_t freq );
  812. /*!
  813. * \brief Activate the extention of the timeout when long preamble is used
  814. *
  815. * \param [in] enable The radio will extend the timeout to cope with long preamble
  816. */
  817. void SX126xSetLongPreamble( uint8_t enable );
  818. /*!
  819. * \brief Sets the transmission parameters
  820. *
  821. * \param [in] paDutyCycle Duty Cycle for the PA
  822. * \param [in] hpMax 0 for sx1261, 7 for sx1262
  823. * \param [in] deviceSel 1 for sx1261, 0 for sx1262
  824. * \param [in] paLut 0 for 14dBm LUT, 1 for 22dBm LUT
  825. */
  826. void SX126xSetPaConfig( uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut );
  827. /*!
  828. * \brief Defines into which mode the chip goes after a TX / RX done
  829. *
  830. * \param [in] fallbackMode The mode in which the radio goes
  831. */
  832. void SX126xSetRxTxFallbackMode( uint8_t fallbackMode );
  833. /*!
  834. * \brief Write data to the radio memory
  835. *
  836. * \param [in] address The address of the first byte to write in the radio
  837. * \param [in] buffer The data to be written in radio's memory
  838. * \param [in] size The number of bytes to write in radio's memory
  839. */
  840. void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size );
  841. /*!
  842. * \brief Read data from the radio memory
  843. *
  844. * \param [in] address The address of the first byte to read from the radio
  845. * \param [out] buffer The buffer that holds data read from radio
  846. * \param [in] size The number of bytes to read from radio's memory
  847. */
  848. void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size );
  849. /*!
  850. * \brief Write data to the buffer holding the payload in the radio
  851. *
  852. * \param [in] offset The offset to start writing the payload
  853. * \param [in] buffer The data to be written (the payload)
  854. * \param [in] size The number of byte to be written
  855. */
  856. void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size );
  857. /*!
  858. * \brief Read data from the buffer holding the payload in the radio
  859. *
  860. * \param [in] offset The offset to start reading the payload
  861. * \param [out] buffer A pointer to a buffer holding the data from the radio
  862. * \param [in] size The number of byte to be read
  863. */
  864. void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size );
  865. /*!
  866. * \brief Sets the IRQ mask and DIO masks
  867. *
  868. * \param [in] irqMask General IRQ mask
  869. * \param [in] dio1Mask DIO1 mask
  870. * \param [in] dio2Mask DIO2 mask
  871. * \param [in] dio3Mask DIO3 mask
  872. */
  873. void SX126xSetDioIrqParams( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask );
  874. /*!
  875. * \brief Returns the current IRQ status
  876. *
  877. * \retval irqStatus IRQ status
  878. */
  879. uint16_t SX126xGetIrqStatus( void );
  880. /*!
  881. * \brief Indicates if DIO2 is used to control an RF Switch
  882. *
  883. * \param [in] enable true of false
  884. */
  885. void SX126xSetDio2AsRfSwitchCtrl( uint8_t enable );
  886. /*!
  887. * \brief Indicates if the Radio main clock is supplied from a tcxo
  888. *
  889. * \param [in] tcxoVoltage voltage used to control the TCXO
  890. * \param [in] timeout time given to the TCXO to go to 32MHz
  891. */
  892. void SX126xSetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout );
  893. /*!
  894. * \brief Sets the RF frequency
  895. *
  896. * \param [in] frequency RF frequency [Hz]
  897. */
  898. void SX126xSetRfFrequency( uint32_t frequency );
  899. /*!
  900. * \brief Sets the radio for the given protocol
  901. *
  902. * \param [in] packetType [PACKET_TYPE_GFSK, PACKET_TYPE_LORA]
  903. *
  904. * \remark This method has to be called before SetRfFrequency,
  905. * SetModulationParams and SetPacketParams
  906. */
  907. void SX126xSetPacketType( RadioPacketTypes_t packetType );
  908. /*!
  909. * \brief Gets the current radio protocol
  910. *
  911. * \retval packetType [PACKET_TYPE_GFSK, PACKET_TYPE_LORA]
  912. */
  913. RadioPacketTypes_t SX126xGetPacketType( void );
  914. /*!
  915. * \brief Sets the transmission parameters
  916. *
  917. * \param [in] power RF output power [-18..13] dBm
  918. * \param [in] rampTime Transmission ramp up time
  919. */
  920. void SX126xSetTxParams( int8_t power, RadioRampTimes_t rampTime );
  921. /*!
  922. * \brief Set the modulation parameters
  923. *
  924. * \param [in] modParams A structure describing the modulation parameters
  925. */
  926. void SX126xSetModulationParams( ModulationParams_t *modParams );
  927. /*!
  928. * \brief Sets the packet parameters
  929. *
  930. * \param [in] packetParams A structure describing the packet parameters
  931. */
  932. void SX126xSetPacketParams( PacketParams_t *packetParams );
  933. /*!
  934. * \brief Sets the Channel Activity Detection (CAD) parameters
  935. *
  936. * \param [in] cadSymbolNum The number of symbol to use for CAD operations
  937. * [LORA_CAD_01_SYMBOL, LORA_CAD_02_SYMBOL,
  938. * LORA_CAD_04_SYMBOL, LORA_CAD_08_SYMBOL,
  939. * LORA_CAD_16_SYMBOL]
  940. * \param [in] cadDetPeak Limit for detection of SNR peak used in the CAD
  941. * \param [in] cadDetMin Set the minimum symbol recognition for CAD
  942. * \param [in] cadExitMode Operation to be done at the end of CAD action
  943. * [LORA_CAD_ONLY, LORA_CAD_RX, LORA_CAD_LBT]
  944. * \param [in] cadTimeout Defines the timeout value to abort the CAD activity
  945. */
  946. void SX126xSetCadParams( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout );
  947. /*!
  948. * \brief Sets the data buffer base address for transmission and reception
  949. *
  950. * \param [in] txBaseAddress Transmission base address
  951. * \param [in] rxBaseAddress Reception base address
  952. */
  953. void SX126xSetBufferBaseAddress( uint8_t txBaseAddress, uint8_t rxBaseAddress );
  954. /*!
  955. * \brief Gets the current radio status
  956. *
  957. * \retval status Radio status
  958. */
  959. RadioStatus_t SX126xGetStatus( void );
  960. /*!
  961. * \brief Returns the instantaneous RSSI value for the last packet received
  962. *
  963. * \retval rssiInst Instantaneous RSSI
  964. */
  965. int8_t SX126xGetRssiInst( void );
  966. /*!
  967. * \brief Gets the last received packet buffer status
  968. *
  969. * \param [out] payloadLength Last received packet payload length
  970. * \param [out] rxStartBuffer Last received packet buffer address pointer
  971. */
  972. void SX126xGetRxBufferStatus( uint8_t *payloadLength, uint8_t *rxStartBuffer );
  973. /*!
  974. * \brief Gets the last received packet payload length
  975. *
  976. * \param [out] pktStatus A structure of packet status
  977. */
  978. void SX126xGetPacketStatus( PacketStatus_t *pktStatus );
  979. /*!
  980. * \brief Returns the possible system errors
  981. *
  982. * \retval sysErrors Value representing the possible sys failures
  983. */
  984. RadioError_t SX126xGetDeviceErrors( void );
  985. /*!
  986. * \brief Clear all the errors in the device
  987. */
  988. void SX126xClearDeviceErrors( void );
  989. /*!
  990. * \brief Clears the IRQs
  991. *
  992. * \param [in] irq IRQ(s) to be cleared
  993. */
  994. void SX126xClearIrqStatus( uint16_t irq );
  995. #ifdef __cplusplus
  996. }
  997. #endif
  998. #endif // __SX126x_H__