sx1276-LoRaMisc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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-LoRaMisc.c
  13. * \brief SX1276 RF chip high level functions driver
  14. *
  15. * \remark Optional support functions.
  16. * These functions are defined only to easy the change of the
  17. * parameters.
  18. * For a final firmware the radio parameters will be known so
  19. * there is no need to support all possible parameters.
  20. * Removing these functions will greatly reduce the final firmware
  21. * size.
  22. *
  23. * \version 2.0.0
  24. * \date May 6 2013
  25. * \author Gregory Cristian
  26. *
  27. * Last modified by Miguel Luis on Jun 19 2013
  28. */
  29. #include "platform.h"
  30. #if defined( USE_SX1276_RADIO )
  31. #include "sx1276-Hal.h"
  32. #include "sx1276.h"
  33. #include "sx1276-LoRa.h"
  34. #include "sx1276-LoRaMisc.h"
  35. /*!
  36. * SX1276 definitions
  37. */
  38. #define XTAL_FREQ 32000000
  39. #define FREQ_STEP 61.03515625
  40. extern tLoRaSettings LoRaSettings;
  41. void SX1276LoRaSetRFFrequency( uint32_t freq )
  42. {
  43. LoRaSettings.RFFrequency = freq;
  44. freq = ( uint32_t )( ( double )freq / ( double )FREQ_STEP );
  45. SX1276LR->RegFrfMsb = ( uint8_t )( ( freq >> 16 ) & 0xFF );
  46. SX1276LR->RegFrfMid = ( uint8_t )( ( freq >> 8 ) & 0xFF );
  47. SX1276LR->RegFrfLsb = ( uint8_t )( freq & 0xFF );
  48. SX1276WriteBuffer( REG_LR_FRFMSB, &SX1276LR->RegFrfMsb, 3 );
  49. }
  50. uint32_t SX1276LoRaGetRFFrequency( void )
  51. {
  52. SX1276ReadBuffer( REG_LR_FRFMSB, &SX1276LR->RegFrfMsb, 3 );
  53. LoRaSettings.RFFrequency = ( ( uint32_t )SX1276LR->RegFrfMsb << 16 ) | ( ( uint32_t )SX1276LR->RegFrfMid << 8 ) | ( ( uint32_t )SX1276LR->RegFrfLsb );
  54. LoRaSettings.RFFrequency = ( uint32_t )( ( double )LoRaSettings.RFFrequency * ( double )FREQ_STEP );
  55. return LoRaSettings.RFFrequency;
  56. }
  57. void SX1276LoRaSetRFPower( int8_t power )
  58. {
  59. SX1276Read( REG_LR_PACONFIG, &SX1276LR->RegPaConfig );
  60. SX1276Read( REG_LR_PADAC, &SX1276LR->RegPaDac );
  61. if( ( SX1276LR->RegPaConfig & RFLR_PACONFIG_PASELECT_PABOOST ) == RFLR_PACONFIG_PASELECT_PABOOST )
  62. {
  63. if( ( SX1276LR->RegPaDac & 0x87 ) == 0x87 )
  64. {
  65. if( power < 5 )
  66. {
  67. power = 5;
  68. }
  69. if( power > 20 )
  70. {
  71. power = 20;
  72. }
  73. SX1276LR->RegPaConfig = ( SX1276LR->RegPaConfig & RFLR_PACONFIG_MAX_POWER_MASK ) | 0x70;
  74. SX1276LR->RegPaConfig = ( SX1276LR->RegPaConfig & RFLR_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 5 ) & 0x0F );
  75. }
  76. else
  77. {
  78. if( power < 2 )
  79. {
  80. power = 2;
  81. }
  82. if( power > 17 )
  83. {
  84. power = 17;
  85. }
  86. SX1276LR->RegPaConfig = ( SX1276LR->RegPaConfig & RFLR_PACONFIG_MAX_POWER_MASK ) | 0x70;
  87. SX1276LR->RegPaConfig = ( SX1276LR->RegPaConfig & RFLR_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 2 ) & 0x0F );
  88. }
  89. }
  90. else
  91. {
  92. if( power < -1 )
  93. {
  94. power = -1;
  95. }
  96. if( power > 14 )
  97. {
  98. power = 14;
  99. }
  100. SX1276LR->RegPaConfig = ( SX1276LR->RegPaConfig & RFLR_PACONFIG_MAX_POWER_MASK ) | 0x70;
  101. SX1276LR->RegPaConfig = ( SX1276LR->RegPaConfig & RFLR_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power + 1 ) & 0x0F );
  102. }
  103. SX1276Write( REG_LR_PACONFIG, SX1276LR->RegPaConfig );
  104. LoRaSettings.Power = power;
  105. }
  106. int8_t SX1276LoRaGetRFPower( void )
  107. {
  108. SX1276Read( REG_LR_PACONFIG, &SX1276LR->RegPaConfig );
  109. SX1276Read( REG_LR_PADAC, &SX1276LR->RegPaDac );
  110. if( ( SX1276LR->RegPaConfig & RFLR_PACONFIG_PASELECT_PABOOST ) == RFLR_PACONFIG_PASELECT_PABOOST )
  111. {
  112. if( ( SX1276LR->RegPaDac & 0x07 ) == 0x07 )
  113. {
  114. LoRaSettings.Power = 5 + ( SX1276LR->RegPaConfig & ~RFLR_PACONFIG_OUTPUTPOWER_MASK );
  115. }
  116. else
  117. {
  118. LoRaSettings.Power = 2 + ( SX1276LR->RegPaConfig & ~RFLR_PACONFIG_OUTPUTPOWER_MASK );
  119. }
  120. }
  121. else
  122. {
  123. LoRaSettings.Power = -1 + ( SX1276LR->RegPaConfig & ~RFLR_PACONFIG_OUTPUTPOWER_MASK );
  124. }
  125. return LoRaSettings.Power;
  126. }
  127. void SX1276LoRaSetSignalBandwidth( uint8_t bw )
  128. {
  129. SX1276Read( REG_LR_MODEMCONFIG1, &SX1276LR->RegModemConfig1 );
  130. SX1276LR->RegModemConfig1 = ( SX1276LR->RegModemConfig1 & RFLR_MODEMCONFIG1_BW_MASK ) | ( bw << 4 );
  131. SX1276Write( REG_LR_MODEMCONFIG1, SX1276LR->RegModemConfig1 );
  132. LoRaSettings.SignalBw = bw;
  133. }
  134. uint8_t SX1276LoRaGetSignalBandwidth( void )
  135. {
  136. SX1276Read( REG_LR_MODEMCONFIG1, &SX1276LR->RegModemConfig1 );
  137. LoRaSettings.SignalBw = ( SX1276LR->RegModemConfig1 & ~RFLR_MODEMCONFIG1_BW_MASK ) >> 4;
  138. return LoRaSettings.SignalBw;
  139. }
  140. void SX1276LoRaSetSpreadingFactor( uint8_t factor )
  141. {
  142. if( factor > 12 )
  143. {
  144. factor = 12;
  145. }
  146. else if( factor < 6 )
  147. {
  148. factor = 6;
  149. }
  150. if( factor == 6 )
  151. {
  152. SX1276LoRaSetNbTrigPeaks( 5 );
  153. }
  154. else
  155. {
  156. SX1276LoRaSetNbTrigPeaks( 3 );
  157. }
  158. SX1276Read( REG_LR_MODEMCONFIG2, &SX1276LR->RegModemConfig2 );
  159. SX1276LR->RegModemConfig2 = ( SX1276LR->RegModemConfig2 & RFLR_MODEMCONFIG2_SF_MASK ) | ( factor << 4 );
  160. SX1276Write( REG_LR_MODEMCONFIG2, SX1276LR->RegModemConfig2 );
  161. LoRaSettings.SpreadingFactor = factor;
  162. }
  163. uint8_t SX1276LoRaGetSpreadingFactor( void )
  164. {
  165. SX1276Read( REG_LR_MODEMCONFIG2, &SX1276LR->RegModemConfig2 );
  166. LoRaSettings.SpreadingFactor = ( SX1276LR->RegModemConfig2 & ~RFLR_MODEMCONFIG2_SF_MASK ) >> 4;
  167. return LoRaSettings.SpreadingFactor;
  168. }
  169. void SX1276LoRaSetErrorCoding( uint8_t value )
  170. {
  171. SX1276Read( REG_LR_MODEMCONFIG1, &SX1276LR->RegModemConfig1 );
  172. SX1276LR->RegModemConfig1 = ( SX1276LR->RegModemConfig1 & RFLR_MODEMCONFIG1_CODINGRATE_MASK ) | ( value << 1 );
  173. SX1276Write( REG_LR_MODEMCONFIG1, SX1276LR->RegModemConfig1 );
  174. LoRaSettings.ErrorCoding = value;
  175. }
  176. uint8_t SX1276LoRaGetErrorCoding( void )
  177. {
  178. SX1276Read( REG_LR_MODEMCONFIG1, &SX1276LR->RegModemConfig1 );
  179. LoRaSettings.ErrorCoding = ( SX1276LR->RegModemConfig1 & ~RFLR_MODEMCONFIG1_CODINGRATE_MASK ) >> 1;
  180. return LoRaSettings.ErrorCoding;
  181. }
  182. void SX1276LoRaSetPacketCrcOn( bool enable )
  183. {
  184. SX1276Read( REG_LR_MODEMCONFIG2, &SX1276LR->RegModemConfig2 );
  185. SX1276LR->RegModemConfig2 = ( SX1276LR->RegModemConfig2 & RFLR_MODEMCONFIG2_RXPAYLOADCRC_MASK ) | ( enable << 2 );
  186. SX1276Write( REG_LR_MODEMCONFIG2, SX1276LR->RegModemConfig2 );
  187. LoRaSettings.CrcOn = enable;
  188. }
  189. void SX1276LoRaSetPreambleLength( uint16_t value )
  190. {
  191. SX1276ReadBuffer( REG_LR_PREAMBLEMSB, &SX1276LR->RegPreambleMsb, 2 );
  192. SX1276LR->RegPreambleMsb = ( value >> 8 ) & 0x00FF;
  193. SX1276LR->RegPreambleLsb = value & 0xFF;
  194. SX1276WriteBuffer( REG_LR_PREAMBLEMSB, &SX1276LR->RegPreambleMsb, 2 );
  195. }
  196. uint16_t SX1276LoRaGetPreambleLength( void )
  197. {
  198. SX1276ReadBuffer( REG_LR_PREAMBLEMSB, &SX1276LR->RegPreambleMsb, 2 );
  199. return ( ( SX1276LR->RegPreambleMsb & 0x00FF ) << 8 ) | SX1276LR->RegPreambleLsb;
  200. }
  201. bool SX1276LoRaGetPacketCrcOn( void )
  202. {
  203. SX1276Read( REG_LR_MODEMCONFIG2, &SX1276LR->RegModemConfig2 );
  204. LoRaSettings.CrcOn = ( SX1276LR->RegModemConfig2 & RFLR_MODEMCONFIG2_RXPAYLOADCRC_ON ) >> 1;
  205. return LoRaSettings.CrcOn;
  206. }
  207. void SX1276LoRaSetImplicitHeaderOn( bool enable )
  208. {
  209. SX1276Read( REG_LR_MODEMCONFIG1, &SX1276LR->RegModemConfig1 );
  210. SX1276LR->RegModemConfig1 = ( SX1276LR->RegModemConfig1 & RFLR_MODEMCONFIG1_IMPLICITHEADER_MASK ) | ( enable );
  211. SX1276Write( REG_LR_MODEMCONFIG1, SX1276LR->RegModemConfig1 );
  212. LoRaSettings.ImplicitHeaderOn = enable;
  213. }
  214. bool SX1276LoRaGetImplicitHeaderOn( void )
  215. {
  216. SX1276Read( REG_LR_MODEMCONFIG1, &SX1276LR->RegModemConfig1 );
  217. LoRaSettings.ImplicitHeaderOn = ( SX1276LR->RegModemConfig1 & RFLR_MODEMCONFIG1_IMPLICITHEADER_ON );
  218. return LoRaSettings.ImplicitHeaderOn;
  219. }
  220. void SX1276LoRaSetRxSingleOn( bool enable )
  221. {
  222. LoRaSettings.RxSingleOn = enable;
  223. }
  224. bool SX1276LoRaGetRxSingleOn( void )
  225. {
  226. return LoRaSettings.RxSingleOn;
  227. }
  228. void SX1276LoRaSetFreqHopOn( bool enable )
  229. {
  230. LoRaSettings.FreqHopOn = enable;
  231. }
  232. bool SX1276LoRaGetFreqHopOn( void )
  233. {
  234. return LoRaSettings.FreqHopOn;
  235. }
  236. void SX1276LoRaSetHopPeriod( uint8_t value )
  237. {
  238. SX1276LR->RegHopPeriod = value;
  239. SX1276Write( REG_LR_HOPPERIOD, SX1276LR->RegHopPeriod );
  240. LoRaSettings.HopPeriod = value;
  241. }
  242. uint8_t SX1276LoRaGetHopPeriod( void )
  243. {
  244. SX1276Read( REG_LR_HOPPERIOD, &SX1276LR->RegHopPeriod );
  245. LoRaSettings.HopPeriod = SX1276LR->RegHopPeriod;
  246. return LoRaSettings.HopPeriod;
  247. }
  248. void SX1276LoRaSetTxPacketTimeout( uint32_t value )
  249. {
  250. LoRaSettings.TxPacketTimeout = value;
  251. }
  252. uint32_t SX1276LoRaGetTxPacketTimeout( void )
  253. {
  254. return LoRaSettings.TxPacketTimeout;
  255. }
  256. void SX1276LoRaSetRxPacketTimeout( uint32_t value )
  257. {
  258. LoRaSettings.RxPacketTimeout = value;
  259. }
  260. uint32_t SX1276LoRaGetRxPacketTimeout( void )
  261. {
  262. return LoRaSettings.RxPacketTimeout;
  263. }
  264. void SX1276LoRaSetPayloadLength( uint8_t value )
  265. {
  266. SX1276LR->RegPayloadLength = value;
  267. SX1276Write( REG_LR_PAYLOADLENGTH, SX1276LR->RegPayloadLength );
  268. LoRaSettings.PayloadLength = value;
  269. }
  270. uint8_t SX1276LoRaGetPayloadLength( void )
  271. {
  272. SX1276Read( REG_LR_PAYLOADLENGTH, &SX1276LR->RegPayloadLength );
  273. LoRaSettings.PayloadLength = SX1276LR->RegPayloadLength;
  274. return LoRaSettings.PayloadLength;
  275. }
  276. void SX1276LoRaSetPa20dBm( bool enale )
  277. {
  278. SX1276Read( REG_LR_PADAC, &SX1276LR->RegPaDac );
  279. SX1276Read( REG_LR_PACONFIG, &SX1276LR->RegPaConfig );
  280. if( ( SX1276LR->RegPaConfig & RFLR_PACONFIG_PASELECT_PABOOST ) == RFLR_PACONFIG_PASELECT_PABOOST )
  281. {
  282. if( enale == true )
  283. {
  284. SX1276LR->RegPaDac = 0x87;
  285. }
  286. }
  287. else
  288. {
  289. SX1276LR->RegPaDac = 0x84;
  290. }
  291. SX1276Write( REG_LR_PADAC, SX1276LR->RegPaDac );
  292. }
  293. bool SX1276LoRaGetPa20dBm( void )
  294. {
  295. SX1276Read( REG_LR_PADAC, &SX1276LR->RegPaDac );
  296. return ( ( SX1276LR->RegPaDac & 0x07 ) == 0x07 ) ? true : false;
  297. }
  298. void SX1276LoRaSetPAOutput( uint8_t outputPin )
  299. {
  300. SX1276Read( REG_LR_PACONFIG, &SX1276LR->RegPaConfig );
  301. SX1276LR->RegPaConfig = (SX1276LR->RegPaConfig & RFLR_PACONFIG_PASELECT_MASK ) | outputPin;
  302. SX1276Write( REG_LR_PACONFIG, SX1276LR->RegPaConfig );
  303. }
  304. uint8_t SX1276LoRaGetPAOutput( void )
  305. {
  306. SX1276Read( REG_LR_PACONFIG, &SX1276LR->RegPaConfig );
  307. return SX1276LR->RegPaConfig & ~RFLR_PACONFIG_PASELECT_MASK;
  308. }
  309. void SX1276LoRaSetPaRamp( uint8_t value )
  310. {
  311. SX1276Read( REG_LR_PARAMP, &SX1276LR->RegPaRamp );
  312. SX1276LR->RegPaRamp = ( SX1276LR->RegPaRamp & RFLR_PARAMP_MASK ) | ( value & ~RFLR_PARAMP_MASK );
  313. SX1276Write( REG_LR_PARAMP, SX1276LR->RegPaRamp );
  314. }
  315. uint8_t SX1276LoRaGetPaRamp( void )
  316. {
  317. SX1276Read( REG_LR_PARAMP, &SX1276LR->RegPaRamp );
  318. return SX1276LR->RegPaRamp & ~RFLR_PARAMP_MASK;
  319. }
  320. void SX1276LoRaSetSymbTimeout( uint16_t value )
  321. {
  322. SX1276ReadBuffer( REG_LR_MODEMCONFIG2, &SX1276LR->RegModemConfig2, 2 );
  323. SX1276LR->RegModemConfig2 = ( SX1276LR->RegModemConfig2 & RFLR_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK ) | ( ( value >> 8 ) & ~RFLR_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK );
  324. SX1276LR->RegSymbTimeoutLsb = value & 0xFF;
  325. SX1276WriteBuffer( REG_LR_MODEMCONFIG2, &SX1276LR->RegModemConfig2, 2 );
  326. }
  327. uint16_t SX1276LoRaGetSymbTimeout( void )
  328. {
  329. SX1276ReadBuffer( REG_LR_MODEMCONFIG2, &SX1276LR->RegModemConfig2, 2 );
  330. return ( ( SX1276LR->RegModemConfig2 & ~RFLR_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK ) << 8 ) | SX1276LR->RegSymbTimeoutLsb;
  331. }
  332. void SX1276LoRaSetLowDatarateOptimize( bool enable )
  333. {
  334. SX1276Read( REG_LR_MODEMCONFIG3, &SX1276LR->RegModemConfig3 );
  335. SX1276LR->RegModemConfig3 = ( SX1276LR->RegModemConfig3 & RFLR_MODEMCONFIG3_LOWDATARATEOPTIMIZE_MASK ) | ( enable << 3 );
  336. SX1276Write( REG_LR_MODEMCONFIG3, SX1276LR->RegModemConfig3 );
  337. }
  338. bool SX1276LoRaGetLowDatarateOptimize( void )
  339. {
  340. SX1276Read( REG_LR_MODEMCONFIG3, &SX1276LR->RegModemConfig3 );
  341. return ( ( SX1276LR->RegModemConfig3 & RFLR_MODEMCONFIG3_LOWDATARATEOPTIMIZE_ON ) >> 3 );
  342. }
  343. void SX1276LoRaSetNbTrigPeaks( uint8_t value )
  344. {
  345. SX1276Read( 0x31, &SX1276LR->RegDetectOptimize );
  346. SX1276LR->RegDetectOptimize = ( SX1276LR->RegDetectOptimize & 0xF8 ) | value;
  347. SX1276Write( 0x31, SX1276LR->RegDetectOptimize );
  348. }
  349. uint8_t SX1276LoRaGetNbTrigPeaks( void )
  350. {
  351. SX1276Read( 0x31, &SX1276LR->RegDetectOptimize );
  352. return ( SX1276LR->RegDetectOptimize & 0x07 );
  353. }
  354. #endif // USE_SX1276_RADIO