radio.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. /*!
  2. * \file radio.c
  3. *
  4. * \brief Radio driver API definition
  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. #include <math.h>
  24. #include <string.h>
  25. #include "delay.h"
  26. #include "radio.h"
  27. #include "sx126x.h"
  28. #include "sx126x_board.h"
  29. #include "project_config.h"
  30. #include "BSP.h"
  31. #include "lora.h"
  32. //scx
  33. static double RadioLoRaSymbTime[3][6] = {{ 32.768, 16.384, 8.192, 4.096, 2.048, 1.024 }, // 125 KHz
  34. { 16.384, 8.192, 4.096, 2.048, 1.024, 0.512 }, // 250 KHz
  35. { 8.192, 4.096, 2.048, 1.024, 0.512, 0.256 }}; // 500 KHz
  36. /*!
  37. * Defines the time required for the TCXO to wakeup [ms].
  38. */
  39. #define BOARD_TCXO_WAKEUP_TIME 0
  40. /*!
  41. * \brief Initializes the radio
  42. *
  43. * \param [IN] events Structure containing the driver callback functions
  44. */
  45. void RadioInit( RadioEvents_t *events );
  46. /*!
  47. * Return current radio status
  48. *
  49. * \param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
  50. */
  51. RadioState_t RadioGetStatus( void );
  52. /*!
  53. * \brief Configures the radio with the given modem
  54. *
  55. * \param [IN] modem Modem to be used [0: FSK, 1: LoRa]
  56. */
  57. void RadioSetModem( RadioModems_t modem );
  58. /*!
  59. * \brief Sets the channel frequency
  60. *
  61. * \param [IN] freq Channel RF frequency
  62. */
  63. void RadioSetChannel( uint32_t freq );
  64. /*!
  65. * \brief Checks if the channel is free for the given time
  66. *
  67. * \remark The FSK modem is always used for this task as we can select the Rx bandwidth at will.
  68. *
  69. * \param [IN] freq Channel RF frequency in Hertz
  70. * \param [IN] rxBandwidth Rx bandwidth in Hertz
  71. * \param [IN] rssiThresh RSSI threshold in dBm
  72. * \param [IN] maxCarrierSenseTime Max time in milliseconds while the RSSI is measured
  73. *
  74. * \retval isFree [true: Channel is free, false: Channel is not free]
  75. */
  76. bool RadioIsChannelFree( uint32_t freq, uint32_t rxBandwidth, int16_t rssiThresh, uint32_t maxCarrierSenseTime );
  77. /*!
  78. * \brief Generates a 32 bits random value based on the RSSI readings
  79. *
  80. * \remark This function sets the radio in LoRa modem mode and disables
  81. * all interrupts.
  82. * After calling this function either Radio.SetRxConfig or
  83. * Radio.SetTxConfig functions must be called.
  84. *
  85. * \retval randomValue 32 bits random value
  86. */
  87. uint32_t RadioRandom( void );
  88. /*!
  89. * \brief Sets the reception parameters
  90. *
  91. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  92. * \param [IN] bandwidth Sets the bandwidth
  93. * FSK : >= 2600 and <= 250000 Hz
  94. * LoRa: [0: 125 kHz, 1: 250 kHz,
  95. * 2: 500 kHz, 3: Reserved]
  96. * \param [IN] datarate Sets the Datarate
  97. * FSK : 600..300000 bits/s
  98. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  99. * 10: 1024, 11: 2048, 12: 4096 chips]
  100. * \param [IN] coderate Sets the coding rate (LoRa only)
  101. * FSK : N/A ( set to 0 )
  102. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  103. * \param [IN] bandwidthAfc Sets the AFC Bandwidth (FSK only)
  104. * FSK : >= 2600 and <= 250000 Hz
  105. * LoRa: N/A ( set to 0 )
  106. * \param [IN] preambleLen Sets the Preamble length
  107. * FSK : Number of bytes
  108. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  109. * \param [IN] symbTimeout Sets the RxSingle timeout value
  110. * FSK : timeout in number of bytes
  111. * LoRa: timeout in symbols
  112. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  113. * \param [IN] payloadLen Sets payload length when fixed length is used
  114. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  115. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  116. * FSK : N/A ( set to 0 )
  117. * LoRa: [0: OFF, 1: ON]
  118. * \param [IN] HopPeriod Number of symbols between each hop
  119. * FSK : N/A ( set to 0 )
  120. * LoRa: Number of symbols
  121. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  122. * FSK : N/A ( set to 0 )
  123. * LoRa: [0: not inverted, 1: inverted]
  124. * \param [IN] rxContinuous Sets the reception in continuous mode
  125. * [false: single mode, true: continuous mode]
  126. */
  127. void RadioSetRxConfig( RadioModems_t modem, uint32_t bandwidth,
  128. uint32_t datarate, uint8_t coderate,
  129. uint32_t bandwidthAfc, uint16_t preambleLen,
  130. uint16_t symbTimeout, bool fixLen,
  131. uint8_t payloadLen,
  132. bool crcOn, bool FreqHopOn, uint8_t HopPeriod,
  133. bool iqInverted, bool rxContinuous );
  134. /*!
  135. * \brief Sets the transmission parameters
  136. *
  137. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  138. * \param [IN] power Sets the output power [dBm]
  139. * \param [IN] fdev Sets the frequency deviation (FSK only)
  140. * FSK : [Hz]
  141. * LoRa: 0
  142. * \param [IN] bandwidth Sets the bandwidth (LoRa only)
  143. * FSK : 0
  144. * LoRa: [0: 125 kHz, 1: 250 kHz,
  145. * 2: 500 kHz, 3: Reserved]
  146. * \param [IN] datarate Sets the Datarate
  147. * FSK : 600..300000 bits/s
  148. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  149. * 10: 1024, 11: 2048, 12: 4096 chips]
  150. * \param [IN] coderate Sets the coding rate (LoRa only)
  151. * FSK : N/A ( set to 0 )
  152. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  153. * \param [IN] preambleLen Sets the preamble length
  154. * FSK : Number of bytes
  155. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  156. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  157. * \param [IN] crcOn Enables disables the CRC [0: OFF, 1: ON]
  158. * \param [IN] FreqHopOn Enables disables the intra-packet frequency hopping
  159. * FSK : N/A ( set to 0 )
  160. * LoRa: [0: OFF, 1: ON]
  161. * \param [IN] HopPeriod Number of symbols between each hop
  162. * FSK : N/A ( set to 0 )
  163. * LoRa: Number of symbols
  164. * \param [IN] iqInverted Inverts IQ signals (LoRa only)
  165. * FSK : N/A ( set to 0 )
  166. * LoRa: [0: not inverted, 1: inverted]
  167. * \param [IN] timeout Transmission timeout [ms]
  168. */
  169. void RadioSetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
  170. uint32_t bandwidth, uint32_t datarate,
  171. uint8_t coderate, uint16_t preambleLen,
  172. bool fixLen, bool crcOn, bool FreqHopOn,
  173. uint8_t HopPeriod, bool iqInverted, uint32_t timeout );
  174. /*!
  175. * \brief Checks if the given RF frequency is supported by the hardware
  176. *
  177. * \param [IN] frequency RF frequency to be checked
  178. * \retval isSupported [true: supported, false: unsupported]
  179. */
  180. bool RadioCheckRfFrequency( uint32_t frequency );
  181. /*!
  182. * \brief Computes the packet time on air in ms for the given payload
  183. *
  184. * \Remark Can only be called once SetRxConfig or SetTxConfig have been called
  185. *
  186. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  187. * \param [IN] bandwidth Sets the bandwidth
  188. * FSK : >= 2600 and <= 250000 Hz
  189. * LoRa: [0: 125 kHz, 1: 250 kHz,
  190. * 2: 500 kHz, 3: Reserved]
  191. * \param [IN] datarate Sets the Datarate
  192. * FSK : 600..300000 bits/s
  193. * LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
  194. * 10: 1024, 11: 2048, 12: 4096 chips]
  195. * \param [IN] coderate Sets the coding rate (LoRa only)
  196. * FSK : N/A ( set to 0 )
  197. * LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  198. * \param [IN] preambleLen Sets the Preamble length
  199. * FSK : Number of bytes
  200. * LoRa: Length in symbols (the hardware adds 4 more symbols)
  201. * \param [IN] fixLen Fixed length packets [0: variable, 1: fixed]
  202. * \param [IN] payloadLen Sets payload length when fixed length is used
  203. * \param [IN] crcOn Enables/Disables the CRC [0: OFF, 1: ON]
  204. *
  205. * \retval airTime Computed airTime (ms) for the given packet payload length
  206. */
  207. uint32_t RadioTimeOnAir( RadioModems_t modem, uint32_t bandwidth,
  208. uint32_t datarate, uint8_t coderate,
  209. uint16_t preambleLen, bool fixLen, uint8_t payloadLen,
  210. bool crcOn );
  211. /*!
  212. * \brief Sends the buffer of size. Prepares the packet to be sent and sets
  213. * the radio in transmission
  214. *
  215. * \param [IN]: buffer Buffer pointer
  216. * \param [IN]: size Buffer size
  217. */
  218. void RadioSend( uint8_t *buffer, uint8_t size );
  219. /*!
  220. * \brief Sets the radio in sleep mode
  221. */
  222. void RadioSleep( void );
  223. /*!
  224. * \brief Sets the radio in standby mode
  225. */
  226. void RadioStandby( void );
  227. /*!
  228. * \brief Sets the radio in reception mode for the given time
  229. * \param [IN] timeout Reception timeout [ms]
  230. * [0: continuous, others timeout]
  231. */
  232. void RadioRx( uint32_t timeout );
  233. /*!
  234. * \brief Start a Channel Activity Detection
  235. */
  236. void RadioStartCad( void );
  237. /*!
  238. * \brief Sets the radio in continuous wave transmission mode
  239. *
  240. * \param [IN]: freq Channel RF frequency
  241. * \param [IN]: power Sets the output power [dBm]
  242. * \param [IN]: time Transmission mode timeout [s]
  243. */
  244. void RadioSetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time );
  245. /*!
  246. * \brief Reads the current RSSI value
  247. *
  248. * \retval rssiValue Current RSSI value in [dBm]
  249. */
  250. int16_t RadioRssi( RadioModems_t modem );
  251. /*!
  252. * \brief Writes the radio register at the specified address
  253. *
  254. * \param [IN]: addr Register address
  255. * \param [IN]: data New register value
  256. */
  257. void RadioWrite( uint32_t addr, uint8_t data );
  258. /*!
  259. * \brief Reads the radio register at the specified address
  260. *
  261. * \param [IN]: addr Register address
  262. * \retval data Register value
  263. */
  264. uint8_t RadioRead( uint32_t addr );
  265. /*!
  266. * \brief Writes multiple radio registers starting at address
  267. *
  268. * \param [IN] addr First Radio register address
  269. * \param [IN] buffer Buffer containing the new register's values
  270. * \param [IN] size Number of registers to be written
  271. */
  272. void RadioWriteBuffer( uint32_t addr, uint8_t *buffer, uint8_t size );
  273. /*!
  274. * \brief Reads multiple radio registers starting at address
  275. *
  276. * \param [IN] addr First Radio register address
  277. * \param [OUT] buffer Buffer where to copy the registers data
  278. * \param [IN] size Number of registers to be read
  279. */
  280. void RadioReadBuffer( uint32_t addr, uint8_t *buffer, uint8_t size );
  281. /*!
  282. * \brief Sets the maximum payload length.
  283. *
  284. * \param [IN] modem Radio modem to be used [0: FSK, 1: LoRa]
  285. * \param [IN] max Maximum payload length in bytes
  286. */
  287. void RadioSetMaxPayloadLength( RadioModems_t modem, uint8_t max );
  288. /*!
  289. * \brief Sets the network to public or private. Updates the sync byte.
  290. *
  291. * \remark Applies to LoRa modem only
  292. *
  293. * \param [IN] enable if true, it enables a public network
  294. */
  295. void RadioSetPublicNetwork( bool enable );
  296. /*!
  297. * \brief Gets the time required for the board plus radio to get out of sleep.[ms]
  298. *
  299. * \retval time Radio plus board wakeup time in ms.
  300. */
  301. uint32_t RadioGetWakeupTime( void );
  302. /*!
  303. * \brief Process radio irq
  304. */
  305. void RadioIrqProcess( void );
  306. /*!
  307. * \brief Sets the radio in reception mode with Max LNA gain for the given time
  308. * \param [IN] timeout Reception timeout [ms]
  309. * [0: continuous, others timeout]
  310. */
  311. void RadioRxBoosted( uint32_t timeout );
  312. /*!
  313. * \brief Sets the Rx duty cycle management parameters
  314. *
  315. * \param [in] rxTime Structure describing reception timeout value
  316. * \param [in] sleepTime Structure describing sleep timeout value
  317. */
  318. void RadioSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime );
  319. /*!
  320. * Radio driver structure initialization
  321. */
  322. const struct Radio_s Radio =
  323. {
  324. RadioInit,
  325. RadioGetStatus,
  326. RadioSetModem,
  327. RadioSetChannel,
  328. RadioIsChannelFree,
  329. RadioRandom,
  330. RadioSetRxConfig,
  331. RadioSetTxConfig,
  332. RadioCheckRfFrequency,
  333. RadioTimeOnAir,
  334. RadioSend,
  335. RadioSleep,
  336. RadioStandby,
  337. RadioRx,
  338. RadioStartCad,
  339. RadioSetTxContinuousWave,
  340. RadioRssi,
  341. RadioWrite,
  342. RadioRead,
  343. RadioWriteBuffer,
  344. RadioReadBuffer,
  345. RadioSetMaxPayloadLength,
  346. RadioSetPublicNetwork,
  347. RadioGetWakeupTime,
  348. RadioIrqProcess,
  349. // Available on SX126x only
  350. RadioRxBoosted,
  351. RadioSetRxDutyCycle
  352. };
  353. /*
  354. * Local types definition
  355. */
  356. /*!
  357. * FSK bandwidth definition
  358. */
  359. typedef struct
  360. {
  361. uint32_t bandwidth;
  362. uint8_t RegValue;
  363. }FskBandwidth_t;
  364. /*!
  365. * Precomputed FSK bandwidth registers values
  366. */
  367. const FskBandwidth_t FskBandwidths[] =
  368. {
  369. { 4800 , 0x1F },
  370. { 5800 , 0x17 },
  371. { 7300 , 0x0F },
  372. { 9700 , 0x1E },
  373. { 11700 , 0x16 },
  374. { 14600 , 0x0E },
  375. { 19500 , 0x1D },
  376. { 23400 , 0x15 },
  377. { 29300 , 0x0D },
  378. { 39000 , 0x1C },
  379. { 46900 , 0x14 },
  380. { 58600 , 0x0C },
  381. { 78200 , 0x1B },
  382. { 93800 , 0x13 },
  383. { 117300, 0x0B },
  384. { 156200, 0x1A },
  385. { 187200, 0x12 },
  386. { 234300, 0x0A },
  387. { 312000, 0x19 },
  388. { 373600, 0x11 },
  389. { 467000, 0x09 },
  390. { 500000, 0x00 }, // Invalid Bandwidth
  391. };
  392. const RadioLoRaBandwidths_t Bandwidths[] = { LORA_BW_125, LORA_BW_250, LORA_BW_500 };
  393. uint8_t MaxPayloadLength = 0xFF;
  394. uint32_t TxTimeout = 0;
  395. uint32_t RxTimeout = 0;
  396. bool RxContinuous = false;
  397. PacketStatus_t RadioPktStatus;
  398. uint8_t RadioRxPayload[255];
  399. volatile bool IrqFired = false;
  400. /*
  401. * SX126x DIO IRQ callback functions prototype
  402. */
  403. /*!
  404. * \brief DIO 0 IRQ callback
  405. */
  406. void RadioOnDioIrq( void* context );
  407. /*
  408. * Private global variables
  409. */
  410. /*!
  411. * Holds the current network type for the radio
  412. */
  413. typedef struct
  414. {
  415. bool Previous;
  416. bool Current;
  417. }RadioPublicNetwork_t;
  418. static RadioPublicNetwork_t RadioPublicNetwork = { false };
  419. /*!
  420. * Radio callbacks variable
  421. */
  422. static RadioEvents_t* RadioEvents;
  423. /*
  424. * Public global variables
  425. */
  426. /*!
  427. * Radio hardware and global parameters
  428. */
  429. SX126x_t SX126x;
  430. /*!
  431. * \brief Holds the internal operating mode of the radio
  432. */
  433. static RadioOperatingModes_t OperatingMode;
  434. //------------------------ board code start 从 board 移动过来的代码 -----------------
  435. RadioOperatingModes_t SX126xGetOperatingMode( void )
  436. {
  437. return OperatingMode;
  438. }
  439. void SX126xSetOperatingMode( RadioOperatingModes_t mode )
  440. {
  441. OperatingMode = mode;
  442. // printf("lora mode :%X\n",mode);
  443. }
  444. void SX126xIoTcxoInit( void )
  445. {
  446. // No TCXO component available on this board design.
  447. }
  448. uint32_t SX126xGetBoardTcxoWakeupTime( void )
  449. {
  450. return BOARD_TCXO_WAKEUP_TIME;
  451. }
  452. //设置DIO2位RF控制脚
  453. void SX126xIoRfSwitchInit( void )
  454. {
  455. SX126xSetDio2AsRfSwitchCtrl( true );
  456. }
  457. //这里天线由内部自动控制,不用专门设置
  458. void SX126xAntSwOn( void )
  459. {
  460. //GpioInit( &AntPow, RADIO_ANT_SWITCH_POWER, PIN_OUTPUT, PIN_PUSH_PULL, PIN_PULL_UP, 1 );
  461. }
  462. //这里天线由内部自动控制,不用专门设置
  463. void SX126xAntSwOff( void )
  464. {
  465. //GpioInit( &AntPow, RADIO_ANT_SWITCH_POWER, PIN_ANALOGIC, PIN_PUSH_PULL, PIN_NO_PULL, 0 );
  466. }
  467. uint8_t SX126xGetDeviceId( void )
  468. {
  469. return SX1262;
  470. }
  471. void SX126xSetRfTxPower( int8_t power )
  472. {
  473. SX126xSetTxParams( power, RADIO_RAMP_40_US );
  474. }
  475. void SX126xWriteRegister( uint16_t address, uint8_t value )
  476. {
  477. SX126xWriteRegisters( address, &value, 1 );
  478. }
  479. uint8_t SX126xReadRegister( uint16_t address )
  480. {
  481. uint8_t data;
  482. SX126xReadRegisters( address, &data, 1 );
  483. return data;
  484. }
  485. void SX126xWakeup( void )
  486. {
  487. uint8_t status;
  488. CRITICAL_SECTION_BEGIN( );
  489. SX126xSetNss(0);
  490. status = SX126xSpiInOut(RADIO_GET_STATUS );
  491. printf("status = %d\r\n", status);
  492. SX126xSpiInOut(0x00 );
  493. SX126xSetNss(1);
  494. // Wait for chip to be ready.
  495. SX126xWaitOnBusy( );
  496. // Update operating mode context variable
  497. SX126xSetOperatingMode( MODE_STDBY_RC );
  498. CRITICAL_SECTION_END( );
  499. }
  500. void SX126xWriteCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
  501. {
  502. uint16_t i = 0;
  503. SX126xCheckDeviceReady( );
  504. SX126xSetNss(0);
  505. SX126xSpiInOut(( uint8_t )command);
  506. for( i = 0; i < size; i++ ){
  507. SX126xSpiInOut(buffer[i]);
  508. }
  509. SX126xSetNss(1);
  510. if( command != RADIO_SET_SLEEP ){
  511. SX126xWaitOnBusy( );
  512. }
  513. // printf("lora mode write:%x\n",command);
  514. }
  515. uint8_t SX126xReadCommand( RadioCommands_t command, uint8_t *buffer, uint16_t size )
  516. {
  517. uint8_t status = 0;
  518. uint16_t i = 0;
  519. SX126xCheckDeviceReady( );
  520. SX126xSetNss(0);
  521. SX126xSpiInOut(( uint8_t )command);
  522. status = SX126xSpiInOut(0x00);
  523. for( i = 0; i < size; i++ ){
  524. buffer[i] = SX126xSpiInOut(0);
  525. }
  526. SX126xSetNss(1);
  527. SX126xWaitOnBusy( );
  528. return status;
  529. }
  530. void SX126xWriteRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
  531. {
  532. uint16_t i = 0;
  533. SX126xCheckDeviceReady( );
  534. SX126xSetNss(0);
  535. SX126xSpiInOut(RADIO_WRITE_REGISTER );
  536. SX126xSpiInOut(( address & 0xFF00 ) >> 8 );
  537. SX126xSpiInOut( address & 0x00FF );
  538. for( i = 0; i < size; i++ ){
  539. SX126xSpiInOut(buffer[i] );
  540. }
  541. SX126xSetNss(1);
  542. SX126xWaitOnBusy( );
  543. }
  544. void SX126xReadRegisters( uint16_t address, uint8_t *buffer, uint16_t size )
  545. {
  546. uint16_t i = 0;
  547. SX126xCheckDeviceReady( );
  548. SX126xSetNss(0);
  549. SX126xSpiInOut(RADIO_READ_REGISTER);
  550. SX126xSpiInOut(( address & 0xFF00 ) >> 8);
  551. SX126xSpiInOut(address & 0x00FF);
  552. SX126xSpiInOut(0);
  553. for( i = 0; i < size; i++ ){
  554. buffer[i] = SX126xSpiInOut(0);
  555. }
  556. SX126xSetNss(1);
  557. SX126xWaitOnBusy( );
  558. }
  559. void SX126xWriteBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
  560. {
  561. uint16_t i = 0;
  562. SX126xCheckDeviceReady( );
  563. SX126xSetNss(0);
  564. SX126xSpiInOut(RADIO_WRITE_BUFFER );
  565. SX126xSpiInOut(offset );
  566. for( i = 0; i < size; i++ ){
  567. SX126xSpiInOut(buffer[i] );
  568. }
  569. SX126xSetNss(1);
  570. SX126xWaitOnBusy( );
  571. }
  572. void SX126xReadBuffer( uint8_t offset, uint8_t *buffer, uint8_t size )
  573. {
  574. uint16_t i = 0;
  575. SX126xCheckDeviceReady( );
  576. SX126xSetNss(0);
  577. SX126xSpiInOut(RADIO_READ_BUFFER );
  578. SX126xSpiInOut(offset );
  579. SX126xSpiInOut(0 );
  580. for( i = 0; i < size; i++ ){
  581. buffer[i] = SX126xSpiInOut(0);
  582. }
  583. SX126xSetNss(1);
  584. SX126xWaitOnBusy( );
  585. }
  586. //------------------------ board code end -----------------
  587. /*!
  588. * Returns the known FSK bandwidth registers value
  589. *
  590. * \param [IN] bandwidth Bandwidth value in Hz
  591. * \retval regValue Bandwidth register value.
  592. */
  593. static uint8_t RadioGetFskBandwidthRegValue( uint32_t bandwidth )
  594. {
  595. uint8_t i;
  596. if( bandwidth == 0 )
  597. {
  598. return( 0x1F );
  599. }
  600. for( i = 0; i < ( sizeof( FskBandwidths ) / sizeof( FskBandwidth_t ) ) - 1; i++ )
  601. {
  602. if( ( bandwidth >= FskBandwidths[i].bandwidth ) && ( bandwidth < FskBandwidths[i + 1].bandwidth ) )
  603. {
  604. return FskBandwidths[i+1].RegValue;
  605. }
  606. }
  607. // ERROR: Value not found
  608. while( 1 );
  609. }
  610. void RadioInit( RadioEvents_t *events )
  611. {
  612. RadioEvents = events;
  613. SX126xIoInit();
  614. SX126xInit( RadioOnDioIrq );
  615. SX126xSetStandby( STDBY_RC );
  616. SX126xSetRegulatorMode( USE_DCDC );
  617. SX126xSetBufferBaseAddress( 0x00, 0x00 );
  618. SX126xSetTxParams( 0, RADIO_RAMP_200_US );
  619. SX126xSetDioIrqParams( IRQ_RADIO_ALL, IRQ_RADIO_ALL, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  620. SX126xTimerInit();
  621. IrqFired = false;
  622. }
  623. RadioState_t RadioGetStatus( void )
  624. {
  625. switch( SX126xGetOperatingMode( ) )
  626. {
  627. case MODE_TX:
  628. return RF_TX_RUNNING;
  629. case MODE_RX:
  630. return RF_RX_RUNNING;
  631. case MODE_CAD:
  632. return RF_CAD;
  633. default:
  634. return RF_IDLE;
  635. }
  636. }
  637. void RadioSetModem( RadioModems_t modem )
  638. {
  639. switch( modem )
  640. {
  641. default:
  642. case MODEM_FSK:
  643. SX126xSetPacketType( PACKET_TYPE_GFSK );
  644. // When switching to GFSK mode the LoRa SyncWord register value is reset
  645. // Thus, we also reset the RadioPublicNetwork variable
  646. RadioPublicNetwork.Current = false;
  647. break;
  648. case MODEM_LORA:
  649. SX126xSetPacketType( PACKET_TYPE_LORA );
  650. // Public/Private network register is reset when switching modems
  651. if( RadioPublicNetwork.Current != RadioPublicNetwork.Previous )
  652. {
  653. RadioPublicNetwork.Current = RadioPublicNetwork.Previous;
  654. RadioSetPublicNetwork( RadioPublicNetwork.Current );
  655. }
  656. break;
  657. }
  658. }
  659. void RadioSetChannel( uint32_t freq )
  660. {
  661. SX126xSetRfFrequency( freq );
  662. }
  663. bool RadioIsChannelFree( uint32_t freq, uint32_t rxBandwidth, int16_t rssiThresh, uint32_t maxCarrierSenseTime )
  664. {
  665. bool status = true;
  666. int16_t rssi = 0;
  667. uint32_t count = 0;
  668. RadioSetModem( MODEM_FSK );
  669. RadioSetChannel( freq );
  670. // Set Rx bandwidth. Other parameters are not used.
  671. RadioSetRxConfig( MODEM_FSK, rxBandwidth, 600, 0, rxBandwidth, 3, 0, false,
  672. 0, false, 0, 0, false, true );
  673. RadioRx( 0 );
  674. SX126xDelayMs( 1 );
  675. // Perform carrier sense for maxCarrierSenseTime
  676. count = 0;
  677. while( count < maxCarrierSenseTime )
  678. {
  679. rssi = RadioRssi( MODEM_FSK );
  680. if( rssi > rssiThresh )
  681. {
  682. status = false;
  683. break;
  684. }
  685. SX126xDelayMs( 1 );
  686. count++;
  687. }
  688. RadioSleep( );
  689. return status;
  690. }
  691. uint32_t RadioRandom( void )
  692. {
  693. uint32_t rnd = 0;
  694. /*
  695. * Radio setup for random number generation
  696. */
  697. // Set LoRa modem ON
  698. RadioSetModem( MODEM_LORA );
  699. // Disable LoRa modem interrupts
  700. SX126xSetDioIrqParams( IRQ_RADIO_NONE, IRQ_RADIO_NONE, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  701. rnd = SX126xGetRandom( );
  702. return rnd;
  703. }
  704. void RadioSetRxConfig( RadioModems_t modem, uint32_t bandwidth,
  705. uint32_t datarate, uint8_t coderate,
  706. uint32_t bandwidthAfc, uint16_t preambleLen,
  707. uint16_t symbTimeout, bool fixLen,
  708. uint8_t payloadLen,
  709. bool crcOn, bool freqHopOn, uint8_t hopPeriod,
  710. bool iqInverted, bool rxContinuous )
  711. {
  712. uint8_t syncWordBuf[]={ 0xC1, 0x94, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00 };
  713. RxContinuous = rxContinuous;
  714. if( rxContinuous == true )
  715. {
  716. symbTimeout = 0;
  717. }
  718. if( fixLen == true )
  719. {
  720. MaxPayloadLength = payloadLen;
  721. }
  722. else
  723. {
  724. MaxPayloadLength = 0xFF;
  725. }
  726. switch( modem )
  727. {
  728. case MODEM_FSK:
  729. SX126xSetStopRxTimerOnPreambleDetect( false );
  730. SX126x.ModulationParams.PacketType = PACKET_TYPE_GFSK;
  731. SX126x.ModulationParams.Params.Gfsk.BitRate = datarate;
  732. SX126x.ModulationParams.Params.Gfsk.ModulationShaping = MOD_SHAPING_G_BT_1;
  733. SX126x.ModulationParams.Params.Gfsk.Bandwidth = RadioGetFskBandwidthRegValue( bandwidth << 1 ); // SX126x badwidth is double sided
  734. SX126x.PacketParams.PacketType = PACKET_TYPE_GFSK;
  735. SX126x.PacketParams.Params.Gfsk.PreambleLength = ( preambleLen << 3 ); // convert byte into bit
  736. SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_08_BITS;
  737. SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3; // convert byte into bit
  738. SX126x.PacketParams.Params.Gfsk.AddrComp = RADIO_ADDRESSCOMP_FILT_OFF;
  739. SX126x.PacketParams.Params.Gfsk.HeaderType = ( fixLen == true ) ? RADIO_PACKET_FIXED_LENGTH : RADIO_PACKET_VARIABLE_LENGTH;
  740. SX126x.PacketParams.Params.Gfsk.PayloadLength = MaxPayloadLength;
  741. if( crcOn == true )
  742. {
  743. SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_2_BYTES_CCIT;
  744. }
  745. else
  746. {
  747. SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
  748. }
  749. SX126x.PacketParams.Params.Gfsk.DcFree = RADIO_DC_FREEWHITENING;
  750. RadioStandby( );
  751. RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  752. SX126xSetModulationParams( &SX126x.ModulationParams );
  753. SX126xSetPacketParams( &SX126x.PacketParams );
  754. SX126xSetSyncWord( syncWordBuf );
  755. SX126xSetWhiteningSeed( 0x01FF );
  756. RxTimeout = ( uint32_t )symbTimeout * 8000UL / datarate;
  757. break;
  758. case MODEM_LORA:
  759. SX126xSetStopRxTimerOnPreambleDetect( false );
  760. SX126x.ModulationParams.PacketType = PACKET_TYPE_LORA;
  761. SX126x.ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t )datarate;
  762. SX126x.ModulationParams.Params.LoRa.Bandwidth = Bandwidths[bandwidth];
  763. SX126x.ModulationParams.Params.LoRa.CodingRate = ( RadioLoRaCodingRates_t )coderate;
  764. if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
  765. ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
  766. {
  767. SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
  768. }
  769. else
  770. {
  771. SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x00;
  772. }
  773. SX126x.PacketParams.PacketType = PACKET_TYPE_LORA;
  774. if( ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
  775. ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
  776. {
  777. if( preambleLen < 12 )
  778. {
  779. SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
  780. }
  781. else
  782. {
  783. SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  784. }
  785. }
  786. else
  787. {
  788. SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  789. }
  790. SX126x.PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )fixLen;
  791. SX126x.PacketParams.Params.LoRa.PayloadLength = MaxPayloadLength;
  792. SX126x.PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t )crcOn;
  793. SX126x.PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t )iqInverted;
  794. RadioStandby( );
  795. RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  796. SX126xSetModulationParams( &SX126x.ModulationParams );
  797. SX126xSetPacketParams( &SX126x.PacketParams );
  798. SX126xSetLoRaSymbNumTimeout( symbTimeout );
  799. // WORKAROUND - Optimizing the Inverted IQ Operation, see DS_SX1261-2_V1.2 datasheet chapter 15.4
  800. if( SX126x.PacketParams.Params.LoRa.InvertIQ == LORA_IQ_INVERTED )
  801. {
  802. // RegIqPolaritySetup = @address 0x0736
  803. SX126xWriteRegister( 0x0736, SX126xReadRegister( 0x0736 ) & ~( 1 << 2 ) );
  804. }
  805. else
  806. {
  807. // RegIqPolaritySetup @address 0x0736
  808. SX126xWriteRegister( 0x0736, SX126xReadRegister( 0x0736 ) | ( 1 << 2 ) );
  809. }
  810. // WORKAROUND END
  811. // Timeout Max, Timeout handled directly in SetRx function
  812. RxTimeout = 0xFFFF;
  813. break;
  814. }
  815. }
  816. void RadioSetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
  817. uint32_t bandwidth, uint32_t datarate,
  818. uint8_t coderate, uint16_t preambleLen,
  819. bool fixLen, bool crcOn, bool freqHopOn,
  820. uint8_t hopPeriod, bool iqInverted, uint32_t timeout )
  821. {
  822. uint8_t syncWordBuf[] = { 0xC1, 0x94, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x00 };
  823. switch( modem )
  824. {
  825. case MODEM_FSK:
  826. SX126x.ModulationParams.PacketType = PACKET_TYPE_GFSK;
  827. SX126x.ModulationParams.Params.Gfsk.BitRate = datarate;
  828. SX126x.ModulationParams.Params.Gfsk.ModulationShaping = MOD_SHAPING_G_BT_1;
  829. SX126x.ModulationParams.Params.Gfsk.Bandwidth = RadioGetFskBandwidthRegValue( bandwidth << 1 ); // SX126x badwidth is double sided
  830. SX126x.ModulationParams.Params.Gfsk.Fdev = fdev;
  831. SX126x.PacketParams.PacketType = PACKET_TYPE_GFSK;
  832. SX126x.PacketParams.Params.Gfsk.PreambleLength = ( preambleLen << 3 ); // convert byte into bit
  833. SX126x.PacketParams.Params.Gfsk.PreambleMinDetect = RADIO_PREAMBLE_DETECTOR_08_BITS;
  834. SX126x.PacketParams.Params.Gfsk.SyncWordLength = 3 << 3 ; // convert byte into bit
  835. SX126x.PacketParams.Params.Gfsk.AddrComp = RADIO_ADDRESSCOMP_FILT_OFF;
  836. SX126x.PacketParams.Params.Gfsk.HeaderType = ( fixLen == true ) ? RADIO_PACKET_FIXED_LENGTH : RADIO_PACKET_VARIABLE_LENGTH;
  837. if( crcOn == true )
  838. {
  839. SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_2_BYTES_CCIT;
  840. }
  841. else
  842. {
  843. SX126x.PacketParams.Params.Gfsk.CrcLength = RADIO_CRC_OFF;
  844. }
  845. SX126x.PacketParams.Params.Gfsk.DcFree = RADIO_DC_FREEWHITENING;
  846. RadioStandby( );
  847. RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  848. SX126xSetModulationParams( &SX126x.ModulationParams );
  849. SX126xSetPacketParams( &SX126x.PacketParams );
  850. SX126xSetSyncWord( syncWordBuf );
  851. SX126xSetWhiteningSeed( 0x01FF );
  852. break;
  853. case MODEM_LORA:
  854. SX126x.ModulationParams.PacketType = PACKET_TYPE_LORA;
  855. SX126x.ModulationParams.Params.LoRa.SpreadingFactor = ( RadioLoRaSpreadingFactors_t ) datarate;
  856. SX126x.ModulationParams.Params.LoRa.Bandwidth = Bandwidths[bandwidth];
  857. SX126x.ModulationParams.Params.LoRa.CodingRate= ( RadioLoRaCodingRates_t )coderate;
  858. if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
  859. ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
  860. {
  861. SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x01;
  862. }
  863. else
  864. {
  865. SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize = 0x00;
  866. }
  867. SX126x.PacketParams.PacketType = PACKET_TYPE_LORA;
  868. if( ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF5 ) ||
  869. ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor == LORA_SF6 ) )
  870. {
  871. if( preambleLen < 12 )
  872. {
  873. SX126x.PacketParams.Params.LoRa.PreambleLength = 12;
  874. }
  875. else
  876. {
  877. SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  878. }
  879. }
  880. else
  881. {
  882. SX126x.PacketParams.Params.LoRa.PreambleLength = preambleLen;
  883. }
  884. SX126x.PacketParams.Params.LoRa.HeaderType = ( RadioLoRaPacketLengthsMode_t )fixLen;
  885. SX126x.PacketParams.Params.LoRa.PayloadLength = MaxPayloadLength;
  886. SX126x.PacketParams.Params.LoRa.CrcMode = ( RadioLoRaCrcModes_t )crcOn;
  887. SX126x.PacketParams.Params.LoRa.InvertIQ = ( RadioLoRaIQModes_t )iqInverted;
  888. RadioStandby( );
  889. RadioSetModem( ( SX126x.ModulationParams.PacketType == PACKET_TYPE_GFSK ) ? MODEM_FSK : MODEM_LORA );
  890. SX126xSetModulationParams( &SX126x.ModulationParams );
  891. SX126xSetPacketParams( &SX126x.PacketParams );
  892. break;
  893. }
  894. // WORKAROUND - Modulation Quality with 500 kHz LoRa Bandwidth, see DS_SX1261-2_V1.2 datasheet chapter 15.1
  895. if( ( modem == MODEM_LORA ) && ( SX126x.ModulationParams.Params.LoRa.Bandwidth == LORA_BW_500 ) )
  896. {
  897. // RegTxModulation = @address 0x0889
  898. SX126xWriteRegister( 0x0889, SX126xReadRegister( 0x0889 ) & ~( 1 << 2 ) );
  899. }
  900. else
  901. {
  902. // RegTxModulation = @address 0x0889
  903. SX126xWriteRegister( 0x0889, SX126xReadRegister( 0x0889 ) | ( 1 << 2 ) );
  904. }
  905. // WORKAROUND END
  906. SX126xSetRfTxPower( power );
  907. TxTimeout = timeout;
  908. }
  909. bool RadioCheckRfFrequency( uint32_t frequency )
  910. {
  911. return true;
  912. }
  913. static uint32_t RadioGetLoRaBandwidthInHz( RadioLoRaBandwidths_t bw )
  914. {
  915. uint32_t bandwidthInHz = 0;
  916. switch( bw )
  917. {
  918. case LORA_BW_007:
  919. bandwidthInHz = 7812UL;
  920. break;
  921. case LORA_BW_010:
  922. bandwidthInHz = 10417UL;
  923. break;
  924. case LORA_BW_015:
  925. bandwidthInHz = 15625UL;
  926. break;
  927. case LORA_BW_020:
  928. bandwidthInHz = 20833UL;
  929. break;
  930. case LORA_BW_031:
  931. bandwidthInHz = 31250UL;
  932. break;
  933. case LORA_BW_041:
  934. bandwidthInHz = 41667UL;
  935. break;
  936. case LORA_BW_062:
  937. bandwidthInHz = 62500UL;
  938. break;
  939. case LORA_BW_125:
  940. bandwidthInHz = 125000UL;
  941. break;
  942. case LORA_BW_250:
  943. bandwidthInHz = 250000UL;
  944. break;
  945. case LORA_BW_500:
  946. bandwidthInHz = 500000UL;
  947. break;
  948. }
  949. return bandwidthInHz;
  950. }
  951. static uint32_t RadioGetGfskTimeOnAirNumerator( uint32_t datarate, uint8_t coderate,
  952. uint16_t preambleLen, bool fixLen, uint8_t payloadLen,
  953. bool crcOn )
  954. {
  955. const RadioAddressComp_t addrComp = RADIO_ADDRESSCOMP_FILT_OFF;
  956. const uint8_t syncWordLength = 3;
  957. return ( preambleLen << 3 ) +
  958. ( ( fixLen == false ) ? 8 : 0 ) +
  959. ( syncWordLength << 3 ) +
  960. ( ( payloadLen +
  961. ( addrComp == RADIO_ADDRESSCOMP_FILT_OFF ? 0 : 1 ) +
  962. ( ( crcOn == true ) ? 2 : 0 )
  963. ) << 3
  964. );
  965. }
  966. static uint32_t RadioGetLoRaTimeOnAirNumerator( uint32_t bandwidth,
  967. uint32_t datarate, uint8_t coderate,
  968. uint16_t preambleLen, bool fixLen, uint8_t payloadLen,
  969. bool crcOn )
  970. {
  971. int32_t crDenom = coderate + 4;
  972. bool lowDatareOptimize = false;
  973. int32_t ceilDenominator;
  974. int32_t ceilNumerator = 0;
  975. int32_t intermediate = 0;
  976. // Ensure that the preamble length is at least 12 symbols when using SF5 or
  977. // SF6
  978. if( ( datarate == 5 ) || ( datarate == 6 ) )
  979. {
  980. if( preambleLen < 12 )
  981. {
  982. preambleLen = 12;
  983. }
  984. }
  985. if( ( ( bandwidth == 0 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
  986. ( ( bandwidth == 1 ) && ( datarate == 12 ) ) )
  987. {
  988. lowDatareOptimize = true;
  989. }
  990. ceilNumerator = ( payloadLen << 3 ) +
  991. ( crcOn ? 16 : 0 ) -
  992. ( 4 * datarate ) +
  993. ( fixLen ? 0 : 20 );
  994. if( datarate <= 6 )
  995. {
  996. ceilDenominator = 4 * datarate;
  997. }
  998. else
  999. {
  1000. ceilNumerator += 8;
  1001. if( lowDatareOptimize == true )
  1002. {
  1003. ceilDenominator = 4 * ( datarate - 2 );
  1004. }
  1005. else
  1006. {
  1007. ceilDenominator = 4 * datarate;
  1008. }
  1009. }
  1010. if( ceilNumerator < 0 )
  1011. {
  1012. ceilNumerator = 0;
  1013. }
  1014. // Perform integral ceil()
  1015. intermediate =
  1016. ( ( ceilNumerator + ceilDenominator - 1 ) / ceilDenominator ) * crDenom + preambleLen + 12;
  1017. if( datarate <= 6 )
  1018. {
  1019. intermediate += 2;
  1020. }
  1021. return ( uint32_t )( ( 4 * intermediate + 1 ) * ( 1 << ( datarate - 2 ) ) );
  1022. }
  1023. uint32_t RadioTimeOnAir( RadioModems_t modem, uint32_t bandwidth,
  1024. uint32_t datarate, uint8_t coderate,
  1025. uint16_t preambleLen, bool fixLen, uint8_t payloadLen,
  1026. bool crcOn )
  1027. {
  1028. uint32_t numerator = 0;
  1029. uint32_t denominator = 1;
  1030. switch( modem )
  1031. {
  1032. case MODEM_FSK:
  1033. {
  1034. numerator = 1000U * RadioGetGfskTimeOnAirNumerator( datarate, coderate,
  1035. preambleLen, fixLen,
  1036. payloadLen, crcOn );
  1037. denominator = datarate;
  1038. }
  1039. break;
  1040. case MODEM_LORA:
  1041. {
  1042. numerator = 1000U * RadioGetLoRaTimeOnAirNumerator( bandwidth, datarate,
  1043. coderate, preambleLen,
  1044. fixLen, payloadLen, crcOn );
  1045. denominator = RadioGetLoRaBandwidthInHz( Bandwidths[bandwidth] );
  1046. }
  1047. break;
  1048. }
  1049. // Perform integral ceil()
  1050. return ( numerator + denominator - 1 ) / denominator;
  1051. }
  1052. //scx
  1053. uint32_t RadioTimeOnAir2( RadioModems_t modem, uint8_t pktLen )
  1054. {
  1055. uint32_t airTime = 0;
  1056. switch( modem )
  1057. {
  1058. case MODEM_FSK:
  1059. {
  1060. airTime = rint( ( 8 * ( SX126x.PacketParams.Params.Gfsk.PreambleLength +
  1061. ( SX126x.PacketParams.Params.Gfsk.SyncWordLength >> 3 ) +
  1062. ( ( SX126x.PacketParams.Params.Gfsk.HeaderType == RADIO_PACKET_FIXED_LENGTH ) ? 0.0 : 1.0 ) +
  1063. pktLen +
  1064. ( ( SX126x.PacketParams.Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES ) ? 2.0 : 0 ) ) /
  1065. SX126x.ModulationParams.Params.Gfsk.BitRate ) * 1e3 );
  1066. }
  1067. break;
  1068. case MODEM_LORA:
  1069. {
  1070. double ts = RadioLoRaSymbTime[SX126x.ModulationParams.Params.LoRa.Bandwidth - 4][12 - SX126x.ModulationParams.Params.LoRa.SpreadingFactor];
  1071. // time of preamble
  1072. double tPreamble = ( SX126x.PacketParams.Params.LoRa.PreambleLength + 4.25 ) * ts;
  1073. // Symbol length of payload and time
  1074. double tmp = ceil( ( 8 * pktLen - 4 * SX126x.ModulationParams.Params.LoRa.SpreadingFactor +
  1075. 28 + 16 * SX126x.PacketParams.Params.LoRa.CrcMode -
  1076. ( ( SX126x.PacketParams.Params.LoRa.HeaderType == LORA_PACKET_FIXED_LENGTH ) ? 20 : 0 ) ) /
  1077. ( double )( 4 * ( SX126x.ModulationParams.Params.LoRa.SpreadingFactor -
  1078. ( ( SX126x.ModulationParams.Params.LoRa.LowDatarateOptimize > 0 ) ? 2 : 0 ) ) ) ) *
  1079. ( ( SX126x.ModulationParams.Params.LoRa.CodingRate % 4 ) + 4 );
  1080. double nPayload = 8 + ( ( tmp > 0 ) ? tmp : 0 );
  1081. double tPayload = nPayload * ts;
  1082. // Time on air
  1083. double tOnAir = tPreamble + tPayload;
  1084. // return milli seconds
  1085. airTime = floor( tOnAir + 0.999 );
  1086. }
  1087. break;
  1088. }
  1089. return airTime;
  1090. }
  1091. void RadioSend( uint8_t *buffer, uint8_t size )
  1092. {
  1093. SX126xSetDioIrqParams( IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
  1094. IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT,
  1095. IRQ_RADIO_NONE,
  1096. IRQ_RADIO_NONE );
  1097. if( SX126xGetPacketType( ) == PACKET_TYPE_LORA )
  1098. {
  1099. SX126x.PacketParams.Params.LoRa.PayloadLength = size;
  1100. }
  1101. else
  1102. {
  1103. SX126x.PacketParams.Params.Gfsk.PayloadLength = size;
  1104. }
  1105. SX126xSetPacketParams( &SX126x.PacketParams );
  1106. SX126xSendPayload( buffer, size, 0 );
  1107. SX126xSetTxTimerValue(TxTimeout);
  1108. SX126xTxTimerStart();
  1109. }
  1110. void RadioSleep( void )
  1111. {
  1112. SleepParams_t params = { 0 };
  1113. params.Fields.WarmStart = 1;
  1114. SX126xSetSleep( params );
  1115. SX126xDelayMs( 2 );
  1116. }
  1117. void RadioStandby( void )
  1118. {
  1119. SX126xSetStandby( STDBY_RC );
  1120. }
  1121. void RadioRx( uint32_t timeout )
  1122. {
  1123. SX126xSetDioIrqParams( IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  1124. IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  1125. IRQ_RADIO_NONE,
  1126. IRQ_RADIO_NONE );
  1127. if( timeout != 0 )
  1128. {
  1129. SX126xSetRxTimerValue(timeout);
  1130. SX126xRxTimerStart();
  1131. }
  1132. if( RxContinuous == true )
  1133. {
  1134. SX126xSetRx( 0xFFFFFF ); // Rx Continuous
  1135. }
  1136. else
  1137. {
  1138. SX126xSetRx( RxTimeout << 6 );
  1139. }
  1140. }
  1141. void RadioRxBoosted( uint32_t timeout )
  1142. {
  1143. SX126xSetDioIrqParams( IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  1144. IRQ_RADIO_ALL, //IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT,
  1145. IRQ_RADIO_NONE,
  1146. IRQ_RADIO_NONE );
  1147. if( timeout != 0 )
  1148. {
  1149. SX126xSetRxTimerValue(timeout);
  1150. SX126xRxTimerStart();
  1151. }
  1152. if( RxContinuous == true )
  1153. {
  1154. SX126xSetRxBoosted( 0xFFFFFF ); // Rx Continuous
  1155. }
  1156. else
  1157. {
  1158. SX126xSetRxBoosted( RxTimeout << 6 );
  1159. }
  1160. }
  1161. void RadioSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime )
  1162. {
  1163. SX126xSetRxDutyCycle( rxTime, sleepTime );
  1164. }
  1165. void RadioStartCad( void )
  1166. {
  1167. SX126xSetDioIrqParams( IRQ_CAD_DONE | IRQ_CAD_ACTIVITY_DETECTED, IRQ_CAD_DONE | IRQ_CAD_ACTIVITY_DETECTED, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
  1168. SX126xSetCad( );
  1169. }
  1170. void RadioSetTxContinuousWave( uint32_t freq, int8_t power, uint16_t time )
  1171. {
  1172. uint32_t timeout = ( uint32_t )time * 1000;
  1173. SX126xSetRfFrequency( freq );
  1174. SX126xSetRfTxPower( power );
  1175. SX126xSetTxContinuousWave( );
  1176. SX126xSetTxTimerValue(timeout);
  1177. SX126xTxTimerStart();
  1178. }
  1179. int16_t RadioRssi( RadioModems_t modem )
  1180. {
  1181. return SX126xGetRssiInst( );
  1182. }
  1183. void RadioWrite( uint32_t addr, uint8_t data )
  1184. {
  1185. SX126xWriteRegister( addr, data );
  1186. }
  1187. uint8_t RadioRead( uint32_t addr )
  1188. {
  1189. return SX126xReadRegister( addr );
  1190. }
  1191. void RadioWriteBuffer( uint32_t addr, uint8_t *buffer, uint8_t size )
  1192. {
  1193. SX126xWriteRegisters( addr, buffer, size );
  1194. }
  1195. void RadioReadBuffer( uint32_t addr, uint8_t *buffer, uint8_t size )
  1196. {
  1197. SX126xReadRegisters( addr, buffer, size );
  1198. }
  1199. void RadioSetMaxPayloadLength( RadioModems_t modem, uint8_t max )
  1200. {
  1201. if( modem == MODEM_LORA )
  1202. {
  1203. SX126x.PacketParams.Params.LoRa.PayloadLength = MaxPayloadLength = max;
  1204. SX126xSetPacketParams( &SX126x.PacketParams );
  1205. }
  1206. else
  1207. {
  1208. if( SX126x.PacketParams.Params.Gfsk.HeaderType == RADIO_PACKET_VARIABLE_LENGTH )
  1209. {
  1210. SX126x.PacketParams.Params.Gfsk.PayloadLength = MaxPayloadLength = max;
  1211. SX126xSetPacketParams( &SX126x.PacketParams );
  1212. }
  1213. }
  1214. }
  1215. void RadioSetPublicNetwork( bool enable )
  1216. {
  1217. RadioPublicNetwork.Current = RadioPublicNetwork.Previous = enable;
  1218. RadioSetModem( MODEM_LORA );
  1219. if( enable == true )
  1220. {
  1221. // Change LoRa modem SyncWord
  1222. SX126xWriteRegister( REG_LR_SYNCWORD, ( LORA_MAC_PUBLIC_SYNCWORD >> 8 ) & 0xFF );
  1223. SX126xWriteRegister( REG_LR_SYNCWORD + 1, LORA_MAC_PUBLIC_SYNCWORD & 0xFF );
  1224. }
  1225. else
  1226. {
  1227. // Change LoRa modem SyncWord
  1228. SX126xWriteRegister( REG_LR_SYNCWORD, ( LORA_MAC_PRIVATE_SYNCWORD >> 8 ) & 0xFF );
  1229. SX126xWriteRegister( REG_LR_SYNCWORD + 1, LORA_MAC_PRIVATE_SYNCWORD & 0xFF );
  1230. }
  1231. }
  1232. uint32_t RadioGetWakeupTime( void )
  1233. {
  1234. return SX126xGetBoardTcxoWakeupTime( ) + RADIO_WAKEUP_TIME;
  1235. }
  1236. void RadioOnTxTimeoutIrq( void* context )
  1237. {
  1238. if( ( RadioEvents != NULL ) && ( RadioEvents->TxTimeout != NULL ) )
  1239. {
  1240. RadioEvents->TxTimeout( );
  1241. }
  1242. }
  1243. void RadioOnRxTimeoutIrq( void* context )
  1244. {
  1245. if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
  1246. {
  1247. RadioEvents->RxTimeout( );
  1248. }
  1249. }
  1250. void RadioOnDioIrq( void* context )
  1251. {
  1252. IrqFired = true;
  1253. }
  1254. extern u32 lora_begin_time,lora_end_time,lora_send_time;
  1255. extern volatile uint32_t TickCounter;
  1256. void RadioIrqProcess( void )
  1257. {
  1258. uint16_t irqRegs = 0;
  1259. if( IrqFired == true || timeout_isOut(&tt_lora_irq))
  1260. {
  1261. CRITICAL_SECTION_BEGIN( );
  1262. // Clear IRQ flag
  1263. if(IrqFired == true)
  1264. IrqFired = false;
  1265. CRITICAL_SECTION_END( );
  1266. // timeout_start(&tt_lora_irq,0xff);
  1267. irqRegs = SX126xGetIrqStatus( );
  1268. SX126xClearIrqStatus( irqRegs );
  1269. // printf("irqregs:%X, chipmode:%d, cmdstatus:%d\n",irqRegs,status.Fields.ChipMode,status.Fields.CmdStatus);
  1270. if( ( irqRegs & IRQ_TX_DONE ) == IRQ_TX_DONE )
  1271. {
  1272. SX126xTxTimerStop();
  1273. //!< Update operating mode state to a value lower than \ref MODE_STDBY_XOSC
  1274. SX126xSetOperatingMode( MODE_STDBY_RC );
  1275. if( ( RadioEvents != NULL ) && ( RadioEvents->TxDone != NULL ) )
  1276. {
  1277. RadioEvents->TxDone( );
  1278. }
  1279. }
  1280. if( ( irqRegs & IRQ_RX_DONE ) == IRQ_RX_DONE )
  1281. {
  1282. if( ( irqRegs & IRQ_CRC_ERROR ) == IRQ_CRC_ERROR )
  1283. {
  1284. // uint8_t size;
  1285. if( RxContinuous == false )
  1286. {
  1287. //!< Update operating mode state to a value lower than \ref MODE_STDBY_XOSC
  1288. SX126xSetOperatingMode( MODE_STDBY_RC );
  1289. }
  1290. if( ( RadioEvents != NULL ) && ( RadioEvents->RxError ) )
  1291. {
  1292. RadioEvents->RxError( );
  1293. }
  1294. }
  1295. else
  1296. {
  1297. uint8_t size;
  1298. SX126xRxTimerStop();
  1299. if( RxContinuous == false )
  1300. {
  1301. //!< Update operating mode state to a value lower than \ref MODE_STDBY_XOSC
  1302. SX126xSetOperatingMode( MODE_STDBY_RC );
  1303. // WORKAROUND - Implicit Header Mode Timeout Behavior, see DS_SX1261-2_V1.2 datasheet chapter 15.3
  1304. // RegRtcControl = @address 0x0902
  1305. SX126xWriteRegister( 0x0902, 0x00 );
  1306. // RegEventMask = @address 0x0944
  1307. SX126xWriteRegister( 0x0944, SX126xReadRegister( 0x0944 ) | ( 1 << 1 ) );
  1308. // WORKAROUND END
  1309. }
  1310. SX126xGetPayload( RadioRxPayload, &size , 255 );
  1311. SX126xGetPacketStatus( &RadioPktStatus );
  1312. if( ( RadioEvents != NULL ) && ( RadioEvents->RxDone != NULL ) )
  1313. {
  1314. RadioEvents->RxDone( RadioRxPayload, size, RadioPktStatus.Params.LoRa.RssiPkt, RadioPktStatus.Params.LoRa.SnrPkt );
  1315. }
  1316. // data_dump("lora rx2:", RadioRxPayload, size);
  1317. }
  1318. }
  1319. if( ( irqRegs & IRQ_CAD_DONE ) == IRQ_CAD_DONE )
  1320. {
  1321. //!< Update operating mode state to a value lower than \ref MODE_STDBY_XOSC
  1322. SX126xSetOperatingMode( MODE_STDBY_RC );
  1323. if( ( RadioEvents != NULL ) && ( RadioEvents->CadDone != NULL ) )
  1324. {
  1325. RadioEvents->CadDone( ( ( irqRegs & IRQ_CAD_ACTIVITY_DETECTED ) == IRQ_CAD_ACTIVITY_DETECTED ) );
  1326. }
  1327. }
  1328. if( ( irqRegs & IRQ_RX_TX_TIMEOUT ) == IRQ_RX_TX_TIMEOUT )
  1329. {
  1330. if( SX126xGetOperatingMode( ) == MODE_TX )
  1331. {
  1332. SX126xTxTimerStop();
  1333. //!< Update operating mode state to a value lower than \ref MODE_STDBY_XOSC
  1334. SX126xSetOperatingMode( MODE_STDBY_RC );
  1335. if( ( RadioEvents != NULL ) && ( RadioEvents->TxTimeout != NULL ) )
  1336. {
  1337. RadioEvents->TxTimeout( );
  1338. }
  1339. }
  1340. else if( SX126xGetOperatingMode( ) == MODE_RX )
  1341. {
  1342. SX126xRxTimerStop();
  1343. //!< Update operating mode state to a value lower than \ref MODE_STDBY_XOSC
  1344. SX126xSetOperatingMode( MODE_STDBY_RC );
  1345. if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
  1346. {
  1347. RadioEvents->RxTimeout( );
  1348. }
  1349. }
  1350. }
  1351. if( ( irqRegs & IRQ_PREAMBLE_DETECTED ) == IRQ_PREAMBLE_DETECTED )
  1352. {
  1353. //检测到前导码
  1354. //__NOP( );
  1355. RadioEvents->lora_preamble_detected = true;
  1356. }
  1357. if( ( irqRegs & IRQ_SYNCWORD_VALID ) == IRQ_SYNCWORD_VALID )
  1358. {
  1359. //检测到有效的同步字
  1360. //__NOP( );
  1361. }
  1362. if( ( irqRegs & IRQ_HEADER_VALID ) == IRQ_HEADER_VALID )
  1363. {
  1364. //检测到有效包头
  1365. //__NOP( );
  1366. RadioEvents->lora_header_valid = true;
  1367. }
  1368. if( ( irqRegs & IRQ_HEADER_ERROR ) == IRQ_HEADER_ERROR )
  1369. {
  1370. //检测到无效包头
  1371. SX126xRxTimerStop();
  1372. if( RxContinuous == false )
  1373. {
  1374. //!< Update operating mode state to a value lower than \ref MODE_STDBY_XOSC
  1375. SX126xSetOperatingMode( MODE_STDBY_RC );
  1376. }
  1377. if( ( RadioEvents != NULL ) && ( RadioEvents->RxTimeout != NULL ) )
  1378. {
  1379. RadioEvents->RxTimeout( );
  1380. }
  1381. }
  1382. }
  1383. }