radio.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. ______ _
  3. / _____) _ | |
  4. ( (____ _____ ____ _| |_ _____ ____| |__
  5. \____ \| ___ | (_ _) ___ |/ ___) _ \
  6. _____) ) ____| | | || |_| ____( (___| | | |
  7. (______/|_____)_|_|_| \__)_____)\____)_| |_|
  8. (C)2016 Semtech
  9. Description: Handling of the node configuration protocol
  10. License: Revised BSD License, see LICENSE.TXT file include in the project
  11. Maintainer: Miguel Luis, Gregory Cristian and Matthieu Verdy
  12. */
  13. #ifndef __RADIO_H__
  14. #define __RADIO_H__
  15. #include "sx1280.h"
  16. /*!
  17. * \brief Class holding the basic communications with a radio
  18. *
  19. * It sets the functions to read/write registers, send commands and read/write
  20. * payload.
  21. * It also provides functions to run callback functions depending on the
  22. * interrupts generated from the radio.
  23. */
  24. struct Radio_s
  25. {
  26. /*!
  27. * \brief Initializes the radio
  28. *
  29. * \param [IN] callbacks Structure containing the driver callback functions
  30. */
  31. void ( *Init )( RadioCallbacks_t *callbacks );
  32. /*!
  33. * \brief Resets the radio
  34. */
  35. void ( *Reset )( void );
  36. /*!
  37. * \brief Gets the current radio status
  38. *
  39. * \retval status Radio status
  40. */
  41. RadioStatus_t ( *GetStatus )( void );
  42. /*!
  43. * \brief Writes the given command to the radio
  44. *
  45. * \param [in] opcode Command opcode
  46. * \param [in] buffer Command parameters byte array
  47. * \param [in] size Command parameters byte array size
  48. */
  49. void ( *WriteCommand )( RadioCommands_t opcode, uint8_t *buffer, uint16_t size );
  50. /*!
  51. * \brief Reads the given command from the radio
  52. *
  53. * \param [in] opcode Command opcode
  54. * \param [in] buffer Command parameters byte array
  55. * \param [in] size Command parameters byte array size
  56. */
  57. void ( *ReadCommand )( RadioCommands_t opcode, uint8_t *buffer, uint16_t size );
  58. /*!
  59. * \brief Writes multiple radio registers starting at address
  60. *
  61. * \param [in] address First Radio register address
  62. * \param [in] buffer Buffer containing the new register's values
  63. * \param [in] size Number of registers to be written
  64. */
  65. void ( *WriteRegisters )( uint16_t address, uint8_t *buffer, uint16_t size );
  66. /*!
  67. * \brief Writes the radio register at the specified address
  68. *
  69. * \param [in] address Register address
  70. * \param [in] value New register value
  71. */
  72. void ( *WriteRegister )( uint16_t address, uint8_t value );
  73. /*!
  74. * \brief Reads multiple radio registers starting at address
  75. *
  76. * \param [in] address First Radio register address
  77. * \param [out] buffer Buffer where to copy the registers data
  78. * \param [in] size Number of registers to be read
  79. */
  80. void ( *ReadRegisters )( uint16_t address, uint8_t *buffer, uint16_t size );
  81. /*!
  82. * \brief Reads the radio register at the specified address
  83. *
  84. * \param [in] address Register address
  85. *
  86. * \retval value The register value
  87. */
  88. uint8_t ( *ReadRegister )( uint16_t address );
  89. /*!
  90. * \brief Writes Radio Data Buffer with buffer of size starting at offset.
  91. *
  92. * \param [in] offset Offset where to start writing
  93. * \param [in] buffer Buffer pointer
  94. * \param [in] size Buffer size
  95. */
  96. void ( *WriteBuffer )( uint8_t offset, uint8_t *buffer, uint8_t size );
  97. /*!
  98. * \brief Reads Radio Data Buffer at offset to buffer of size
  99. *
  100. * \param [in] offset Offset where to start reading
  101. * \param [out] buffer Buffer pointer
  102. * \param [in] size Buffer size
  103. */
  104. void ( *ReadBuffer )( uint8_t offset, uint8_t *buffer, uint8_t size );
  105. /*!
  106. * \brief Gets the current status of the radio DIOs
  107. *
  108. * \retval status [Bit #3: DIO3, Bit #2: DIO2,
  109. * Bit #1: DIO1, Bit #0: BUSY]
  110. */
  111. uint8_t ( *GetDioStatus )( void );
  112. /*!
  113. * \brief Return firmware version
  114. *
  115. * \retval firmware The firmware version
  116. */
  117. uint16_t ( *GetFirmwareVersion )( void );
  118. /*!
  119. * \brief Sets the power regulators operating mode
  120. *
  121. * \param [in] mode [0: LDO, 1:DC_DC]
  122. */
  123. void ( *SetRegulatorMode )( RadioRegulatorModes_t mode );
  124. /*!
  125. * \brief Sets the radio in configuration mode
  126. *
  127. * \param [in] mode The standby mode to put the radio into
  128. */
  129. void ( *SetStandby )( RadioStandbyModes_t mode );
  130. /*!
  131. * \brief Sets the radio for the given protocol
  132. *
  133. * \param [in] packetType [PACKET_TYPE_GFSK, PACKET_TYPE_LORA,
  134. * PACKET_TYPE_RANGING, PACKET_TYPE_FLRC,
  135. * PACKET_TYPE_BLE]
  136. *
  137. * \remark This method has to be called before SetRfFrequency,
  138. * SetModulationParams and SetPacketParams
  139. */
  140. void ( *SetPacketType )( RadioPacketTypes_t packetType );
  141. /*!
  142. * \brief Set the modulation parameters
  143. *
  144. * \param [in] modParams A structure describing the modulation parameters
  145. */
  146. void ( *SetModulationParams )( ModulationParams_t *modParams );
  147. /*!
  148. * \brief Sets the packet parameters
  149. *
  150. * \param [in] packetParams A structure describing the packet parameters
  151. */
  152. void ( *SetPacketParams )( PacketParams_t *packetParams );
  153. /*!
  154. * \brief Sets the RF frequency
  155. *
  156. * \param [in] frequency RF frequency [Hz]
  157. */
  158. void ( *SetRfFrequency )( uint32_t frequency );
  159. /*!
  160. * \brief Sets the data buffer base address for transmission and reception
  161. *
  162. * \param [in] txBaseAddress Transmission base address
  163. * \param [in] rxBaseAddress Reception base address
  164. */
  165. void ( *SetBufferBaseAddresses )( uint8_t txBaseAddress, uint8_t rxBaseAddress );
  166. /*!
  167. * \brief Sets the transmission parameters
  168. *
  169. * \param [in] power RF output power [-18..13] dBm
  170. * \param [in] rampTime Transmission ramp up time
  171. */
  172. void ( *SetTxParams )( int8_t power, RadioRampTimes_t rampTime );
  173. /*!
  174. * \brief Sets the IRQ mask and DIO masks
  175. *
  176. * \param [in] irqMask General IRQ mask
  177. * \param [in] dio1Mask DIO1 mask
  178. * \param [in] dio2Mask DIO2 mask
  179. * \param [in] dio3Mask DIO3 mask
  180. */
  181. void ( *SetDioIrqParams )( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask );
  182. /*!
  183. * \brief Sets the Sync Word given by index used in GFSK, FLRC and BLE protocols
  184. *
  185. * \remark 5th byte isn't used in FLRC and BLE protocols
  186. *
  187. * \param [in] syncWordIdx Index of SyncWord to be set [1..3]
  188. * \param [in] syncWord SyncWord bytes ( 5 bytes )
  189. *
  190. * \retval status [0: OK, 1: NOK]
  191. */
  192. uint8_t ( *SetSyncWord )( uint8_t syncWordIdx, uint8_t *syncWord );
  193. /*!
  194. * \brief Sets the radio in reception mode
  195. *
  196. * \param [in] timeout Structure describing the reception timeout value
  197. */
  198. void ( *SetRx )( TickTime_t timeout );
  199. /*!
  200. * \brief Reads the payload received. If the received payload is longer
  201. * than maxSize, then the method returns 1 and do not set size and payload.
  202. *
  203. * \param [out] payload A pointer to a buffer into which the payload will be copied
  204. * \param [out] size A pointer to the size of the payload received
  205. * \param [in] maxSize The maximal size allowed to copy into the buffer
  206. */
  207. uint8_t ( *GetPayload )( uint8_t *payload, uint8_t *size, uint8_t maxSize );
  208. /*!
  209. * \brief Sends a payload
  210. *
  211. * \param [in] payload A pointer to the payload to send
  212. * \param [in] size The size of the payload to send
  213. * \param [in] timeout The timeout for Tx operation
  214. */
  215. void ( *SendPayload )( uint8_t *payload, uint8_t size, TickTime_t timeout );
  216. /*!
  217. * \brief Set the role of the radio during ranging operations
  218. *
  219. * \param [in] role Role of the radio
  220. */
  221. void ( *SetRangingRole )( RadioRangingRoles_t role );
  222. /*!
  223. * \brief Set the driver in polling mode.
  224. *
  225. * In polling mode the application is responsible to call ProcessIrqs( ) to
  226. * execute callbacks functions.
  227. * The default mode is Interrupt Mode.
  228. * @code
  229. * // Initializations and callbacks declaration/definition
  230. * radio = SX1280( mosi, miso, sclk, nss, busy, int1, int2, int3, rst, &callbacks );
  231. * radio.Init( );
  232. * radio.SetPollingMode( );
  233. *
  234. * while( true )
  235. * {
  236. * // IRQ processing is automatically done
  237. * radio.ProcessIrqs( ); // <-- here, as well as callback functions
  238. * // calls
  239. * // Do some applicative work
  240. * }
  241. * @endcode
  242. *
  243. * \see SX1280SetInterruptMode
  244. */
  245. void ( *SetPollingMode )( void );
  246. /*!
  247. * \brief Set the driver in interrupt mode.
  248. *
  249. * In interrupt mode, the driver communicate with the radio during the
  250. * interruption by direct calls to ProcessIrqs( ). The main advantage is
  251. * the possibility to have low power application architecture.
  252. * This is the default mode.
  253. * @code
  254. * // Initializations and callbacks declaration/definition
  255. * radio = SX1280( mosi, miso, sclk, nss, busy, int1, int2, int3, rst, &callbacks );
  256. * radio.Init( );
  257. * radio.SetInterruptMode( ); // Optionnal. Driver default behavior
  258. *
  259. * while( true )
  260. * {
  261. * // Do some applicative work
  262. * }
  263. * @endcode
  264. *
  265. * \see SX1280SetPollingMode
  266. */
  267. void ( *SetInterruptMode )( void );
  268. /*!
  269. * \brief Initializes the radio registers to the recommended default values
  270. */
  271. void ( *SetRegistersDefault )( void );
  272. /*!
  273. * \brief Gets the current Operation Mode of the Radio
  274. *
  275. * \retval RadioOperatingModes_t last operating mode
  276. */
  277. RadioOperatingModes_t ( *GetOpMode )( void );
  278. /*!
  279. * \brief Sets the radio in sleep mode
  280. *
  281. * \param [in] sleepConfig The sleep configuration describing data
  282. * retention and RTC wake-up
  283. */
  284. void ( *SetSleep )( SleepParams_t sleepConfig );
  285. /*!
  286. * \brief Sets the radio in FS mode
  287. */
  288. void ( *SetFs )( void );
  289. /*!
  290. * \brief Sets the radio in transmission mode
  291. *
  292. * \param [in] timeout Structure describing the transmission timeout value
  293. */
  294. void ( *SetTx )( TickTime_t timeout );
  295. /*!
  296. * \brief Sets the Rx duty cycle management parameters
  297. *
  298. * \param [in] rxTime Structure describing reception timeout value
  299. * \param [in] sleepTime Structure describing sleep timeout value
  300. */
  301. void ( *SetRxDutyCycle )( RadioTickSizes_t Step, uint16_t NbStepRx, uint16_t RxNbStepSleep );
  302. /*!
  303. * \brief Sets the radio in CAD mode
  304. *
  305. * \see SX1280::SetCadParams
  306. */
  307. void ( *SetCad )( void );
  308. /*!
  309. * \brief Sets the radio in continuous wave transmission mode
  310. */
  311. void ( *SetTxContinuousWave )( void );
  312. /*!
  313. * \brief Sets the radio in continuous preamble transmission mode
  314. */
  315. void ( *SetTxContinuousPreamble )( void );
  316. /*!
  317. * \brief Gets the current radio protocol
  318. *
  319. * \retval packetType [PACKET_TYPE_GFSK, PACKET_TYPE_LORA,
  320. * PACKET_TYPE_RANGING, PACKET_TYPE_FLRC,
  321. * PACKET_TYPE_BLE, PACKET_TYPE_NONE]
  322. */
  323. RadioPacketTypes_t ( *GetPacketType )( void );
  324. /*!
  325. * \brief Sets the number of symbols to be used for Channel Activity
  326. * Detection operation
  327. *
  328. * \param [in] cadSymbolNum The number of symbol to use for Channel Activity
  329. * Detection operations [LORA_CAD_01_SYMBOL, LORA_CAD_02_SYMBOL,
  330. * LORA_CAD_04_SYMBOL, LORA_CAD_08_SYMBOL, LORA_CAD_16_SYMBOL]
  331. */
  332. void ( *SetCadParams )( RadioLoRaCadSymbols_t cadSymbolNum );
  333. /*!
  334. * \brief Gets the last received packet buffer status
  335. *
  336. * \param [out] payloadLength Last received packet payload length
  337. * \param [out] rxStartBuffer Last received packet buffer address pointer
  338. */
  339. void ( *GetRxBufferStatus )( uint8_t *payloadLength, uint8_t *rxStartBuffer );
  340. /*!
  341. * \brief Gets the last received packet payload length
  342. *
  343. * \param [out] pktStatus A structure of packet status
  344. */
  345. void ( *GetPacketStatus )( PacketStatus_t *pktStatus );
  346. /*!
  347. * \brief Returns the instantaneous RSSI value for the last packet received
  348. *
  349. * \retval rssiInst Instantaneous RSSI
  350. */
  351. int8_t ( *GetRssiInst )( void );
  352. /*!
  353. * \brief Returns the current IRQ status
  354. *
  355. * \retval irqStatus IRQ status
  356. */
  357. uint16_t ( *GetIrqStatus )( void );
  358. /*!
  359. * \brief Clears the IRQs
  360. *
  361. * \param [in] irq IRQ(s) to be cleared
  362. */
  363. void ( *ClearIrqStatus )( uint16_t irq );
  364. /*!
  365. * \brief Calibrates the given radio block
  366. *
  367. * \param [in] calibParam The description of blocks to be calibrated
  368. */
  369. void ( *Calibrate )( CalibrationParams_t calibParam );
  370. /*!
  371. * \brief Saves the current selected modem configuration into data RAM
  372. */
  373. void ( *SetSaveContext )( void );
  374. /*!
  375. * \brief Sets the chip to automatically send a packet after the end of a packet reception
  376. *
  377. * \remark The offset is automatically compensated inside the function
  378. *
  379. * \param [in] time The delay in us after which a Tx is done
  380. */
  381. void ( *SetAutoTx )( uint16_t time );
  382. /*!
  383. * \brief Sets the chip to automatically receive a packet after the end of a packet transmission
  384. *
  385. * \remark The offset is automatically compensated inside the function
  386. *
  387. * \param [in] time The delay in us after which a Rx is done
  388. */
  389. void ( *SetAutoFS )( uint8_t enable );
  390. /*!
  391. * \brief Enables or disables long preamble detection mode
  392. *
  393. * \param [in] enable [0: Disable, 1: Enable]
  394. */
  395. void ( *SetLongPreamble )( uint8_t enable );
  396. /*!
  397. * \brief Saves the payload to be send in the radio buffer
  398. *
  399. * \param [in] payload A pointer to the payload
  400. * \param [in] size The size of the payload
  401. */
  402. void ( *SetPayload )( uint8_t *payload, uint8_t size );
  403. /*!
  404. * \brief Sets the Sync Word given by index used in GFSK, FLRC and BLE protocols
  405. *
  406. * \remark 5th byte isn't used in FLRC and BLE protocols
  407. *
  408. * \param [in] syncWordIdx Index of SyncWord to be set [1..3]
  409. * \param [in] syncWord SyncWord bytes ( 5 bytes )
  410. *
  411. * \retval status [0: OK, 1: NOK]
  412. */
  413. void ( *SetSyncWordErrorTolerance )( uint8_t errorBits );
  414. /*!
  415. * \brief Sets the Initial value for the LFSR used for the CRC calculation
  416. *
  417. * \param [in] seed Initial LFSR value ( 4 bytes )
  418. *
  419. */
  420. void ( *SetCrcSeed )( uint16_t seed );
  421. /*!
  422. * \brief Set the Access Address field of BLE packet
  423. *
  424. * \param [in] accessAddress The access address to be used for next BLE packet sent
  425. *
  426. * \see SX1280::SetBleAdvertizerAccessAddress
  427. */
  428. void ( *SetBleAccessAddress )( uint32_t accessAddress );
  429. /*!
  430. * \brief Set the Access Address for Advertizer BLE packets
  431. *
  432. * All advertizer BLE packets must use a particular value for Access
  433. * Address field. This method sets it.
  434. *
  435. * \see SX1280::SetBleAccessAddress
  436. */
  437. void ( *SetBleAdvertizerAccessAddress )( void );
  438. /*!
  439. * \brief Sets the seed used for the CRC calculation
  440. *
  441. * \param [in] seed The seed value
  442. *
  443. */
  444. void ( *SetCrcPolynomial )( uint16_t seed );
  445. /*!
  446. * \brief Sets the Initial value of the LFSR used for the whitening in GFSK, FLRC and BLE protocols
  447. *
  448. * \param [in] seed Initial LFSR value
  449. */
  450. void ( *SetWhiteningSeed )( uint8_t seed );
  451. /*!
  452. * \brief Sets the number of bits used to check that ranging request match ranging ID
  453. *
  454. * \param [in] length [0: 8 bits, 1: 16 bits,
  455. * 2: 24 bits, 3: 32 bits]
  456. */
  457. void ( *SetRangingIdLength )( RadioRangingIdCheckLengths_t length );
  458. /*!
  459. * \brief Sets ranging device id
  460. *
  461. * \param [in] address Device address
  462. */
  463. void ( *SetDeviceRangingAddress )( uint32_t address );
  464. /*!
  465. * \brief Sets the device id to ping in a ranging request
  466. *
  467. * \param [in] address Address of the device to ping
  468. */
  469. void ( *SetRangingRequestAddress )( uint32_t address );
  470. /*!
  471. * \brief Return the ranging result value
  472. *
  473. * \param [in] resultType Specifies the type of result.
  474. * [0: RAW, 1: Averaged,
  475. * 2: De-biased, 3:Filtered]
  476. *
  477. * \retval ranging The ranging measure filtered according to resultType [m]
  478. */
  479. double ( *GetRangingResult )( RadioRangingResultTypes_t resultType );
  480. /*!
  481. * \brief Sets the standard processing delay between Master and Slave
  482. *
  483. * \param [in] cal RxTx delay offset for correcting ranging bias.
  484. *
  485. * The calibration value reflects the group delay of the radio front end and
  486. * must be re-performed for each new SX1280 PCB design. The value is obtained
  487. * empirically by either conducted measurement in a known electrical length
  488. * coaxial RF cable (where the design is connectorised) or by radiated
  489. * measurement, at a known distance, where an antenna is present.
  490. * The result of the calibration process is that the SX1280 ranging result
  491. * accurately reflects the physical range, the calibration procedure therefore
  492. * removes the average timing error from the time-of-flight measurement for a
  493. * given design.
  494. *
  495. * The values are Spreading Factor dependents, and depend also of the board
  496. * design. Some typical values are provided in the next table.
  497. *
  498. * Spreading Factor | Calibration Value
  499. * ---------------- | -----------------
  500. * SF5 | 12200
  501. * SF6 | 12200
  502. * SF7 | 12400
  503. * SF8 | 12650
  504. * SF9 | 12940
  505. * SF10 | 13000
  506. * SF11 | 13060
  507. * SF12 | 13120
  508. */
  509. void ( *SetRangingCalibration )( uint16_t cal );
  510. /*!
  511. * \brief Clears the ranging filter
  512. */
  513. void ( *RangingClearFilterResult )( void );
  514. /*!
  515. * \brief Set the number of samples considered in the built-in filter
  516. *
  517. * \param [in] numSample The number of samples to use built-in filter
  518. * [8..255]
  519. *
  520. * \remark Value inferior to 8 will be silently set to 8
  521. */
  522. void ( *RangingSetFilterNumSamples )( uint8_t numSample );
  523. /*!
  524. * \brief Return the Estimated Frequency Error in LORA and RANGING operations
  525. *
  526. * \retval efe The estimated frequency error [Hz]
  527. */
  528. double ( *GetFrequencyError )( void );
  529. };
  530. /*!
  531. * \brief Radio driver
  532. *
  533. * \remark This variable is defined and initialized in the specific radio
  534. * board implementation
  535. */
  536. extern const struct Radio_s Radio;
  537. #endif // __RADIO_H__