sx1276-LoRa.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
  3. * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
  4. * CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
  5. * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
  6. * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
  7. * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  8. *
  9. * Copyright (C) SEMTECH S.A.
  10. */
  11. /*!
  12. * \file sx1276-LoRa.c
  13. * \brief SX1276 RF chip driver mode LoRa
  14. *
  15. * \version 2.0.0
  16. * \date May 6 2013
  17. * \author Gregory Cristian
  18. *
  19. * Last modified by Miguel Luis on Jun 19 2013
  20. */
  21. #include <string.h>
  22. #include "platform.h"
  23. #if defined( USE_SX1276_RADIO )
  24. #include "radio.h"
  25. #include "sx1276-Hal.h"
  26. #include "sx1276.h"
  27. #include "sx1276-LoRaMisc.h"
  28. #include "sx1276-LoRa.h"
  29. #include "delay.h"
  30. /*!
  31. * Constant values need to compute the RSSI value
  32. */
  33. #define RSSI_OFFSET_LF -164.0
  34. #define RSSI_OFFSET_HF -157.0
  35. /*!
  36. * Frequency hopping frequencies table
  37. */
  38. const int32_t HoppingFrequencies[] =
  39. {
  40. 916500000,
  41. 923500000,
  42. 906500000,
  43. 917500000,
  44. 917500000,
  45. 909000000,
  46. 903000000,
  47. 916000000,
  48. 912500000,
  49. 926000000,
  50. 925000000,
  51. 909500000,
  52. 913000000,
  53. 918500000,
  54. 918500000,
  55. 902500000,
  56. 911500000,
  57. 926500000,
  58. 902500000,
  59. 922000000,
  60. 924000000,
  61. 903500000,
  62. 913000000,
  63. 922000000,
  64. 926000000,
  65. 910000000,
  66. 920000000,
  67. 922500000,
  68. 911000000,
  69. 922000000,
  70. 909500000,
  71. 926000000,
  72. 922000000,
  73. 918000000,
  74. 925500000,
  75. 908000000,
  76. 917500000,
  77. 926500000,
  78. 908500000,
  79. 916000000,
  80. 905500000,
  81. 916000000,
  82. 903000000,
  83. 905000000,
  84. 915000000,
  85. 913000000,
  86. 907000000,
  87. 910000000,
  88. 926500000,
  89. 925500000,
  90. 911000000,
  91. };
  92. // Default settings
  93. tLoRaSettings LoRaSettings =
  94. {
  95. 470000000, // RFFrequency
  96. 20, // Power
  97. 9, // SignalBw [0: 7.8kHz, 1: 10.4 kHz, 2: 15.6 kHz, 3: 20.8 kHz, 4: 31.2 kHz,
  98. // 5: 41.6 kHz, 6: 62.5 kHz, 7: 125 kHz, 8: 250 kHz, 9: 500 kHz, other: Reserved]
  99. 12, // SpreadingFactor [6: 64, 7: 128, 8: 256, 9: 512, 10: 1024, 11: 2048, 12: 4096 chips]
  100. 2, // ErrorCoding [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  101. true, // CrcOn [0: OFF, 1: ON]
  102. false, // ImplicitHeaderOn [0: OFF, 1: ON]
  103. 0, // RxSingleOn [0: Continuous, 1 Single]
  104. 0, // FreqHopOn [0: OFF, 1: ON]
  105. 4, // HopPeriod Hops every frequency hopping period symbols
  106. 1000, // TxPacketTimeout
  107. 1000, // RxPacketTimeout
  108. 4, // PayloadLength (used for implicit header mode)
  109. };
  110. /*!
  111. * SX1276 LoRa registers variable
  112. */
  113. tSX1276LR* SX1276LR;
  114. /*!
  115. * Local RF buffer for communication support
  116. */
  117. static uint8_t RFBuffer[RF_BUFFER_SIZE];
  118. /*!
  119. * RF state machine variable
  120. */
  121. static uint8_t RFLRState = RFLR_STATE_IDLE;
  122. /*!
  123. * Rx management support variables
  124. */
  125. static uint16_t RxPacketSize = 0;
  126. static int8_t RxPacketSnrEstimate;
  127. static double RxPacketRssiValue;
  128. static uint8_t RxGain = 1;
  129. static uint32_t RxTimeoutTimer = 0;
  130. /*!
  131. * PacketTimeout Stores the Rx window time value for packet reception
  132. */
  133. static uint32_t PacketTimeout;
  134. /*!
  135. * Tx management support variables
  136. */
  137. static uint16_t TxPacketSize = 0;
  138. void SX1276LoRaInit( void )
  139. {
  140. RFLRState = RFLR_STATE_IDLE;
  141. SX1276LoRaSetDefaults( );
  142. SX1276ReadBuffer( REG_LR_OPMODE, SX1276Regs + 1, 0x70 - 1 );
  143. SX1276LR->RegLna = RFLR_LNA_GAIN_G1;
  144. SX1276WriteBuffer( REG_LR_OPMODE, SX1276Regs + 1, 0x70 - 1 );
  145. // set the RF settings
  146. SX1276LoRaSetRFFrequency( LoRaSettings.RFFrequency );
  147. SX1276LoRaSetSpreadingFactor( LoRaSettings.SpreadingFactor ); // SF6 only operates in implicit header mode.
  148. SX1276LoRaSetErrorCoding( LoRaSettings.ErrorCoding );
  149. SX1276LoRaSetPacketCrcOn( LoRaSettings.CrcOn );
  150. SX1276LoRaSetSignalBandwidth( LoRaSettings.SignalBw );
  151. SX1276LoRaSetImplicitHeaderOn( LoRaSettings.ImplicitHeaderOn );
  152. SX1276LoRaSetSymbTimeout( 0x3FF );
  153. SX1276LoRaSetPayloadLength( LoRaSettings.PayloadLength );
  154. SX1276LoRaSetLowDatarateOptimize( true );
  155. #if( ( MODULE_SX1276RF1IAS == 1 ) || ( MODULE_SX1276RF1KAS == 1 ) )
  156. if( LoRaSettings.RFFrequency > 860000000 )
  157. {
  158. SX1276LoRaSetPAOutput( RFLR_PACONFIG_PASELECT_RFO );
  159. SX1276LoRaSetPa20dBm( false );
  160. LoRaSettings.Power = 14;
  161. SX1276LoRaSetRFPower( LoRaSettings.Power );
  162. }
  163. else
  164. {
  165. SX1276LoRaSetPAOutput( RFLR_PACONFIG_PASELECT_PABOOST );
  166. SX1276LoRaSetPa20dBm( true );
  167. LoRaSettings.Power = 20;
  168. SX1276LoRaSetRFPower( LoRaSettings.Power );
  169. }
  170. #elif( MODULE_SX1276RF1JAS == 1 )
  171. if( LoRaSettings.RFFrequency > 860000000 )
  172. {
  173. SX1276LoRaSetPAOutput( RFLR_PACONFIG_PASELECT_PABOOST );
  174. SX1276LoRaSetPa20dBm( true );
  175. LoRaSettings.Power = 20;
  176. SX1276LoRaSetRFPower( LoRaSettings.Power );
  177. }
  178. else
  179. {
  180. SX1276LoRaSetPAOutput( RFLR_PACONFIG_PASELECT_RFO );
  181. SX1276LoRaSetPa20dBm( false );
  182. LoRaSettings.Power = 14;
  183. SX1276LoRaSetRFPower( LoRaSettings.Power );
  184. }
  185. #endif
  186. SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
  187. }
  188. //改写启动函数,使其可以根据默认配置完成启动模式
  189. void SX1276LoRaInit1( tLoRaSettings *LoRaSettings )
  190. {
  191. }
  192. void SX1276LoRaSetDefaults( void )
  193. {
  194. // REMARK: See SX1276 datasheet for modified default values.
  195. SX1276Read( REG_LR_VERSION, &SX1276LR->RegVersion );
  196. }
  197. void SX1276LoRaReset( void )
  198. {
  199. SX1276SetReset( RADIO_RESET_ON );
  200. // Wait 1ms
  201. delay_ms(1);
  202. SX1276SetReset( RADIO_RESET_OFF );
  203. // Wait 6ms
  204. delay_ms(6);
  205. }
  206. void SX1276LoRaSetOpMode( uint8_t opMode )
  207. {
  208. static uint8_t opModePrev = RFLR_OPMODE_STANDBY;
  209. static bool antennaSwitchTxOnPrev = true;
  210. bool antennaSwitchTxOn = false;
  211. opModePrev = SX1276LR->RegOpMode & ~RFLR_OPMODE_MASK;
  212. if( opMode != opModePrev )
  213. {
  214. if( opMode == RFLR_OPMODE_TRANSMITTER )
  215. {
  216. antennaSwitchTxOn = true;
  217. }
  218. else
  219. {
  220. antennaSwitchTxOn = false;
  221. }
  222. if( antennaSwitchTxOn != antennaSwitchTxOnPrev )
  223. {
  224. antennaSwitchTxOnPrev = antennaSwitchTxOn;
  225. RXTX( antennaSwitchTxOn ); // Antenna switch control
  226. }
  227. SX1276LR->RegOpMode = ( SX1276LR->RegOpMode & RFLR_OPMODE_MASK ) | opMode;
  228. SX1276Write( REG_LR_OPMODE, SX1276LR->RegOpMode );
  229. }
  230. }
  231. uint8_t SX1276LoRaGetOpMode( void )
  232. {
  233. SX1276Read( REG_LR_OPMODE, &SX1276LR->RegOpMode );
  234. return SX1276LR->RegOpMode & ~RFLR_OPMODE_MASK;
  235. }
  236. uint8_t SX1276LoRaReadRxGain( void )
  237. {
  238. SX1276Read( REG_LR_LNA, &SX1276LR->RegLna );
  239. return( SX1276LR->RegLna >> 5 ) & 0x07;
  240. }
  241. double SX1276LoRaReadRssi( void )
  242. {
  243. // Reads the RSSI value
  244. SX1276Read( REG_LR_RSSIVALUE, &SX1276LR->RegRssiValue );
  245. if( LoRaSettings.RFFrequency < 860000000 ) // LF
  246. {
  247. return RSSI_OFFSET_LF + ( double )SX1276LR->RegRssiValue;
  248. }
  249. else
  250. {
  251. return RSSI_OFFSET_HF + ( double )SX1276LR->RegRssiValue;
  252. }
  253. }
  254. uint8_t SX1276LoRaGetPacketRxGain( void )
  255. {
  256. return RxGain;
  257. }
  258. int8_t SX1276LoRaGetPacketSnr( void )
  259. {
  260. return RxPacketSnrEstimate;
  261. }
  262. double SX1276LoRaGetPacketRssi( void )
  263. {
  264. return RxPacketRssiValue;
  265. }
  266. void SX1276LoRaStartRx( void )
  267. {
  268. SX1276LoRaSetRFState( RFLR_STATE_RX_INIT );
  269. }
  270. void SX1276LoRaGetRxPacket( void *buffer, uint16_t *size )
  271. {
  272. *size = RxPacketSize;
  273. RxPacketSize = 0;
  274. memcpy( ( void * )buffer, ( void * )RFBuffer, ( size_t )*size );
  275. }
  276. void SX1276LoRaSetTxPacket( const void *buffer, uint16_t size )
  277. {
  278. TxPacketSize = size;
  279. memcpy( ( void * )RFBuffer, buffer, ( size_t )TxPacketSize );
  280. RFLRState = RFLR_STATE_TX_INIT;
  281. }
  282. uint8_t SX1276LoRaGetRFState( void )
  283. {
  284. return RFLRState;
  285. }
  286. void SX1276LoRaSetRFState( uint8_t state )
  287. {
  288. RFLRState = state;
  289. }
  290. /*!
  291. * \brief Process the LoRa modem Rx and Tx state machines depending on the
  292. * SX1276 operating mode.
  293. *
  294. * \retval rfState Current RF state [RF_IDLE, RF_BUSY,
  295. * RF_RX_DONE, RF_RX_TIMEOUT,
  296. * RF_TX_DONE, RF_TX_TIMEOUT]
  297. */
  298. uint32_t SX1276LoRaProcess( void )
  299. {
  300. uint32_t result = RF_BUSY;
  301. switch( RFLRState )
  302. {
  303. case RFLR_STATE_IDLE:
  304. break;
  305. case RFLR_STATE_RX_INIT:
  306. SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
  307. SX1276LR->RegIrqFlagsMask = RFLR_IRQFLAGS_RXTIMEOUT |
  308. //RFLR_IRQFLAGS_RXDONE |
  309. //RFLR_IRQFLAGS_PAYLOADCRCERROR |
  310. RFLR_IRQFLAGS_VALIDHEADER |
  311. RFLR_IRQFLAGS_TXDONE |
  312. RFLR_IRQFLAGS_CADDONE |
  313. //RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
  314. RFLR_IRQFLAGS_CADDETECTED;
  315. SX1276Write( REG_LR_IRQFLAGSMASK, SX1276LR->RegIrqFlagsMask );
  316. if( LoRaSettings.FreqHopOn == true )
  317. {
  318. SX1276LR->RegHopPeriod = LoRaSettings.HopPeriod;
  319. SX1276Read( REG_LR_HOPCHANNEL, &SX1276LR->RegHopChannel );
  320. SX1276LoRaSetRFFrequency( HoppingFrequencies[SX1276LR->RegHopChannel & RFLR_HOPCHANNEL_CHANNEL_MASK] );
  321. }
  322. else
  323. {
  324. SX1276LR->RegHopPeriod = 255;
  325. }
  326. SX1276Write( REG_LR_HOPPERIOD, SX1276LR->RegHopPeriod );
  327. // RxDone RxTimeout FhssChangeChannel CadDone
  328. SX1276LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_00 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_00;
  329. // CadDetected ModeReady
  330. SX1276LR->RegDioMapping2 = RFLR_DIOMAPPING2_DIO4_00 | RFLR_DIOMAPPING2_DIO5_00;
  331. SX1276WriteBuffer( REG_LR_DIOMAPPING1, &SX1276LR->RegDioMapping1, 2 );
  332. if( LoRaSettings.RxSingleOn == true ) // Rx single mode
  333. {
  334. SX1276LoRaSetOpMode( RFLR_OPMODE_RECEIVER_SINGLE );
  335. }
  336. else // Rx continuous mode
  337. {
  338. SX1276LR->RegFifoAddrPtr = SX1276LR->RegFifoRxBaseAddr;
  339. SX1276Write( REG_LR_FIFOADDRPTR, SX1276LR->RegFifoAddrPtr );
  340. SX1276LoRaSetOpMode( RFLR_OPMODE_RECEIVER );
  341. }
  342. memset( RFBuffer, 0, ( size_t )RF_BUFFER_SIZE );
  343. PacketTimeout = LoRaSettings.RxPacketTimeout;
  344. RxTimeoutTimer = GET_TICK_COUNT();
  345. RFLRState = RFLR_STATE_RX_RUNNING;
  346. break;
  347. case RFLR_STATE_RX_RUNNING:
  348. if( DIO0 == 1 ) // RxDone
  349. {
  350. RxTimeoutTimer = GET_TICK_COUNT( );
  351. if( LoRaSettings.FreqHopOn == true )
  352. {
  353. SX1276Read( REG_LR_HOPCHANNEL, &SX1276LR->RegHopChannel );
  354. SX1276LoRaSetRFFrequency( HoppingFrequencies[SX1276LR->RegHopChannel & RFLR_HOPCHANNEL_CHANNEL_MASK] );
  355. }
  356. // Clear Irq
  357. SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_RXDONE );
  358. RFLRState = RFLR_STATE_RX_DONE;
  359. }
  360. // if( DIO2 == 1 ) // FHSS Changed Channel
  361. // {
  362. // RxTimeoutTimer = GET_TICK_COUNT( );
  363. // if( LoRaSettings.FreqHopOn == true )
  364. // {
  365. // SX1276Read( REG_LR_HOPCHANNEL, &SX1276LR->RegHopChannel );
  366. // SX1276LoRaSetRFFrequency( HoppingFrequencies[SX1276LR->RegHopChannel & RFLR_HOPCHANNEL_CHANNEL_MASK] );
  367. // }
  368. // // Clear Irq
  369. // SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL );
  370. // // Debug
  371. // RxGain = SX1276LoRaReadRxGain( );
  372. // }
  373. if( LoRaSettings.RxSingleOn == true ) // Rx single mode
  374. {
  375. if( ( GET_TICK_COUNT( ) - RxTimeoutTimer ) > PacketTimeout )
  376. {
  377. RFLRState = RFLR_STATE_RX_TIMEOUT;
  378. }
  379. }
  380. break;
  381. case RFLR_STATE_RX_DONE:
  382. SX1276Read( REG_LR_IRQFLAGS, &SX1276LR->RegIrqFlags );
  383. if( ( SX1276LR->RegIrqFlags & RFLR_IRQFLAGS_PAYLOADCRCERROR ) == RFLR_IRQFLAGS_PAYLOADCRCERROR )
  384. {
  385. // Clear Irq
  386. SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_PAYLOADCRCERROR );
  387. if( LoRaSettings.RxSingleOn == true ) // Rx single mode
  388. {
  389. RFLRState = RFLR_STATE_RX_INIT;
  390. }
  391. else
  392. {
  393. RFLRState = RFLR_STATE_RX_RUNNING;
  394. }
  395. break;
  396. }
  397. {
  398. uint8_t rxSnrEstimate;
  399. SX1276Read( REG_LR_PKTSNRVALUE, &rxSnrEstimate );
  400. if( rxSnrEstimate & 0x80 ) // The SNR sign bit is 1
  401. {
  402. // Invert and divide by 4
  403. RxPacketSnrEstimate = ( ( ~rxSnrEstimate + 1 ) & 0xFF ) >> 2;
  404. RxPacketSnrEstimate = -RxPacketSnrEstimate;
  405. }
  406. else
  407. {
  408. // Divide by 4
  409. RxPacketSnrEstimate = ( rxSnrEstimate & 0xFF ) >> 2;
  410. }
  411. }
  412. SX1276Read( REG_LR_PKTRSSIVALUE, &SX1276LR->RegPktRssiValue );
  413. if( LoRaSettings.RFFrequency < 860000000 ) // LF
  414. {
  415. if( RxPacketSnrEstimate < 0 )
  416. {
  417. RxPacketRssiValue = RSSI_OFFSET_LF + ( ( double )SX1276LR->RegPktRssiValue ) + RxPacketSnrEstimate;
  418. }
  419. else
  420. {
  421. RxPacketRssiValue = RSSI_OFFSET_LF + ( 1.0666 * ( ( double )SX1276LR->RegPktRssiValue ) );
  422. }
  423. }
  424. else // HF
  425. {
  426. if( RxPacketSnrEstimate < 0 )
  427. {
  428. RxPacketRssiValue = RSSI_OFFSET_HF + ( ( double )SX1276LR->RegPktRssiValue ) + RxPacketSnrEstimate;
  429. }
  430. else
  431. {
  432. RxPacketRssiValue = RSSI_OFFSET_HF + ( 1.0666 * ( ( double )SX1276LR->RegPktRssiValue ) );
  433. }
  434. }
  435. if( LoRaSettings.RxSingleOn == true ) // Rx single mode
  436. {
  437. SX1276LR->RegFifoAddrPtr = SX1276LR->RegFifoRxBaseAddr;
  438. SX1276Write( REG_LR_FIFOADDRPTR, SX1276LR->RegFifoAddrPtr );
  439. if( LoRaSettings.ImplicitHeaderOn == true )
  440. {
  441. RxPacketSize = SX1276LR->RegPayloadLength;
  442. SX1276ReadFifo( RFBuffer, SX1276LR->RegPayloadLength );
  443. }
  444. else
  445. {
  446. SX1276Read( REG_LR_NBRXBYTES, &SX1276LR->RegNbRxBytes );
  447. RxPacketSize = SX1276LR->RegNbRxBytes;
  448. SX1276ReadFifo( RFBuffer, SX1276LR->RegNbRxBytes );
  449. }
  450. }
  451. else // Rx continuous mode
  452. {
  453. SX1276Read( REG_LR_FIFORXCURRENTADDR, &SX1276LR->RegFifoRxCurrentAddr );
  454. if( LoRaSettings.ImplicitHeaderOn == true )
  455. {
  456. RxPacketSize = SX1276LR->RegPayloadLength;
  457. SX1276LR->RegFifoAddrPtr = SX1276LR->RegFifoRxCurrentAddr;
  458. SX1276Write( REG_LR_FIFOADDRPTR, SX1276LR->RegFifoAddrPtr );
  459. SX1276ReadFifo( RFBuffer, SX1276LR->RegPayloadLength );
  460. }
  461. else
  462. {
  463. SX1276Read( REG_LR_NBRXBYTES, &SX1276LR->RegNbRxBytes );
  464. RxPacketSize = SX1276LR->RegNbRxBytes;
  465. SX1276LR->RegFifoAddrPtr = SX1276LR->RegFifoRxCurrentAddr;
  466. SX1276Write( REG_LR_FIFOADDRPTR, SX1276LR->RegFifoAddrPtr );
  467. SX1276ReadFifo( RFBuffer, SX1276LR->RegNbRxBytes );
  468. }
  469. }
  470. if( LoRaSettings.RxSingleOn == true ) // Rx single mode
  471. {
  472. RFLRState = RFLR_STATE_RX_INIT;
  473. }
  474. else // Rx continuous mode
  475. {
  476. RFLRState = RFLR_STATE_RX_RUNNING;
  477. }
  478. result = RF_RX_DONE;
  479. break;
  480. case RFLR_STATE_RX_TIMEOUT:
  481. RFLRState = RFLR_STATE_RX_INIT;
  482. result = RF_RX_TIMEOUT;
  483. break;
  484. case RFLR_STATE_TX_INIT:
  485. SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
  486. if( LoRaSettings.FreqHopOn == true )
  487. {
  488. SX1276LR->RegIrqFlagsMask = RFLR_IRQFLAGS_RXTIMEOUT |
  489. RFLR_IRQFLAGS_RXDONE |
  490. RFLR_IRQFLAGS_PAYLOADCRCERROR |
  491. RFLR_IRQFLAGS_VALIDHEADER |
  492. //RFLR_IRQFLAGS_TXDONE |
  493. RFLR_IRQFLAGS_CADDONE |
  494. //RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
  495. RFLR_IRQFLAGS_CADDETECTED;
  496. SX1276LR->RegHopPeriod = LoRaSettings.HopPeriod;
  497. SX1276Read( REG_LR_HOPCHANNEL, &SX1276LR->RegHopChannel );
  498. SX1276LoRaSetRFFrequency( HoppingFrequencies[SX1276LR->RegHopChannel & RFLR_HOPCHANNEL_CHANNEL_MASK] );
  499. }
  500. else
  501. {
  502. SX1276LR->RegIrqFlagsMask = RFLR_IRQFLAGS_RXTIMEOUT |
  503. RFLR_IRQFLAGS_RXDONE |
  504. RFLR_IRQFLAGS_PAYLOADCRCERROR |
  505. RFLR_IRQFLAGS_VALIDHEADER |
  506. //RFLR_IRQFLAGS_TXDONE |
  507. RFLR_IRQFLAGS_CADDONE |
  508. RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
  509. RFLR_IRQFLAGS_CADDETECTED;
  510. SX1276LR->RegHopPeriod = 0;
  511. }
  512. SX1276Write( REG_LR_HOPPERIOD, SX1276LR->RegHopPeriod );
  513. SX1276Write( REG_LR_IRQFLAGSMASK, SX1276LR->RegIrqFlagsMask );
  514. // Initializes the payload size
  515. SX1276LR->RegPayloadLength = TxPacketSize;
  516. SX1276Write( REG_LR_PAYLOADLENGTH, SX1276LR->RegPayloadLength );
  517. SX1276LR->RegFifoTxBaseAddr = 0x00; // Full buffer used for Tx
  518. SX1276Write( REG_LR_FIFOTXBASEADDR, SX1276LR->RegFifoTxBaseAddr );
  519. SX1276LR->RegFifoAddrPtr = SX1276LR->RegFifoTxBaseAddr;
  520. SX1276Write( REG_LR_FIFOADDRPTR, SX1276LR->RegFifoAddrPtr );
  521. // Write payload buffer to LORA modem
  522. SX1276WriteFifo( RFBuffer, SX1276LR->RegPayloadLength );
  523. // TxDone RxTimeout FhssChangeChannel ValidHeader
  524. SX1276LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_01 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_01;
  525. // PllLock Mode Ready
  526. SX1276LR->RegDioMapping2 = RFLR_DIOMAPPING2_DIO4_01 | RFLR_DIOMAPPING2_DIO5_00;
  527. SX1276WriteBuffer( REG_LR_DIOMAPPING1, &SX1276LR->RegDioMapping1, 2 );
  528. SX1276LoRaSetOpMode( RFLR_OPMODE_TRANSMITTER );
  529. RFLRState = RFLR_STATE_TX_RUNNING;
  530. break;
  531. case RFLR_STATE_TX_RUNNING:
  532. if( DIO0 == 1 ) // TxDone
  533. {
  534. // Clear Irq
  535. SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_TXDONE );
  536. RFLRState = RFLR_STATE_TX_DONE;
  537. }
  538. // if( DIO2 == 1 ) // FHSS Changed Channel
  539. // {
  540. // if( LoRaSettings.FreqHopOn == true )
  541. // {
  542. // SX1276Read( REG_LR_HOPCHANNEL, &SX1276LR->RegHopChannel );
  543. // SX1276LoRaSetRFFrequency( HoppingFrequencies[SX1276LR->RegHopChannel & RFLR_HOPCHANNEL_CHANNEL_MASK] );
  544. // }
  545. // // Clear Irq
  546. // SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL );
  547. // }
  548. break;
  549. case RFLR_STATE_TX_DONE:
  550. // optimize the power consumption by switching off the transmitter as soon as the packet has been sent
  551. SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
  552. RFLRState = RFLR_STATE_IDLE;
  553. result = RF_TX_DONE;
  554. break;
  555. case RFLR_STATE_CAD_INIT:
  556. SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
  557. SX1276LR->RegIrqFlagsMask = RFLR_IRQFLAGS_RXTIMEOUT |
  558. RFLR_IRQFLAGS_RXDONE |
  559. RFLR_IRQFLAGS_PAYLOADCRCERROR |
  560. RFLR_IRQFLAGS_VALIDHEADER |
  561. RFLR_IRQFLAGS_TXDONE |
  562. //RFLR_IRQFLAGS_CADDONE |
  563. RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL; // |
  564. //RFLR_IRQFLAGS_CADDETECTED;
  565. SX1276Write( REG_LR_IRQFLAGSMASK, SX1276LR->RegIrqFlagsMask );
  566. // RxDone RxTimeout FhssChangeChannel CadDone
  567. SX1276LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_00 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_00;
  568. // CAD Detected ModeReady
  569. SX1276LR->RegDioMapping2 = RFLR_DIOMAPPING2_DIO4_00 | RFLR_DIOMAPPING2_DIO5_00;
  570. SX1276WriteBuffer( REG_LR_DIOMAPPING1, &SX1276LR->RegDioMapping1, 2 );
  571. SX1276LoRaSetOpMode( RFLR_OPMODE_CAD );
  572. RFLRState = RFLR_STATE_CAD_RUNNING;
  573. break;
  574. case RFLR_STATE_CAD_RUNNING:
  575. if( DIO3 == 1 ) //CAD Done interrupt
  576. {
  577. // Clear Irq
  578. SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_CADDONE );
  579. if( DIO4 == 1 ) // CAD Detected interrupt
  580. {
  581. // Clear Irq
  582. SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_CADDETECTED );
  583. // CAD detected, we have a LoRa preamble
  584. RFLRState = RFLR_STATE_RX_INIT;
  585. result = RF_CHANNEL_ACTIVITY_DETECTED;
  586. }
  587. else
  588. {
  589. // The device goes in Standby Mode automatically
  590. RFLRState = RFLR_STATE_IDLE;
  591. result = RF_CHANNEL_EMPTY;
  592. }
  593. }
  594. break;
  595. default:
  596. break;
  597. }
  598. return result;
  599. }
  600. #endif // USE_SX1276_RADIO