sx126x.h 38 KB

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