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