sx1276-FskMisc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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-FskMisc.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 <math.h>
  30. #include "platform.h"
  31. #if defined( USE_SX1276_RADIO )
  32. #include "sx1276-Hal.h"
  33. #include "sx1276.h"
  34. #include "sx1276-Fsk.h"
  35. #include "sx1276-FskMisc.h"
  36. extern tFskSettings FskSettings;
  37. void SX1276FskSetRFFrequency( uint32_t freq )
  38. {
  39. FskSettings.RFFrequency = freq;
  40. freq = ( uint32_t )( ( double )freq / ( double )FREQ_STEP );
  41. SX1276->RegFrfMsb = ( uint8_t )( ( freq >> 16 ) & 0xFF );
  42. SX1276->RegFrfMid = ( uint8_t )( ( freq >> 8 ) & 0xFF );
  43. SX1276->RegFrfLsb = ( uint8_t )( freq & 0xFF );
  44. SX1276WriteBuffer( REG_FRFMSB, &SX1276->RegFrfMsb, 3 );
  45. }
  46. uint32_t SX1276FskGetRFFrequency( void )
  47. {
  48. SX1276ReadBuffer( REG_FRFMSB, &SX1276->RegFrfMsb, 3 );
  49. FskSettings.RFFrequency = ( ( uint32_t )SX1276->RegFrfMsb << 16 ) | ( ( uint32_t )SX1276->RegFrfMid << 8 ) | ( ( uint32_t )SX1276->RegFrfLsb );
  50. FskSettings.RFFrequency = ( uint32_t )( ( double )FskSettings.RFFrequency * ( double )FREQ_STEP );
  51. return FskSettings.RFFrequency;
  52. }
  53. void SX1276FskRxCalibrate( void )
  54. {
  55. // the function RadioRxCalibrate is called just after the reset so all register are at their default values
  56. uint8_t regPaConfigInitVal;
  57. uint32_t initialFreq;
  58. // save register values;
  59. SX1276Read( REG_PACONFIG, &regPaConfigInitVal );
  60. initialFreq = SX1276FskGetRFFrequency( );
  61. // Cut the PA just in case
  62. SX1276->RegPaConfig = 0x00; // RFO output, power = -1 dBm
  63. SX1276Write( REG_PACONFIG, SX1276->RegPaConfig );
  64. // Set Frequency in HF band
  65. SX1276FskSetRFFrequency( 860000000 );
  66. // Rx chain re-calibration workaround
  67. SX1276Read( REG_IMAGECAL, &SX1276->RegImageCal );
  68. SX1276->RegImageCal = ( SX1276->RegImageCal & RF_IMAGECAL_IMAGECAL_MASK ) | RF_IMAGECAL_IMAGECAL_START;
  69. SX1276Write( REG_IMAGECAL, SX1276->RegImageCal );
  70. SX1276Read( REG_IMAGECAL, &SX1276->RegImageCal );
  71. // rx_cal_run goes low when calibration in finished
  72. while( ( SX1276->RegImageCal & RF_IMAGECAL_IMAGECAL_RUNNING ) == RF_IMAGECAL_IMAGECAL_RUNNING )
  73. {
  74. SX1276Read( REG_IMAGECAL, &SX1276->RegImageCal );
  75. }
  76. // reload saved values into the registers
  77. SX1276->RegPaConfig = regPaConfigInitVal;
  78. SX1276Write( REG_PACONFIG, SX1276->RegPaConfig );
  79. SX1276FskSetRFFrequency( initialFreq );
  80. }
  81. void SX1276FskSetBitrate( uint32_t bitrate )
  82. {
  83. FskSettings.Bitrate = bitrate;
  84. bitrate = ( uint16_t )( ( double )XTAL_FREQ / ( double )bitrate );
  85. SX1276->RegBitrateMsb = ( uint8_t )( bitrate >> 8 );
  86. SX1276->RegBitrateLsb = ( uint8_t )( bitrate & 0xFF );
  87. SX1276WriteBuffer( REG_BITRATEMSB, &SX1276->RegBitrateMsb, 2 );
  88. }
  89. uint32_t SX1276FskGetBitrate( void )
  90. {
  91. SX1276ReadBuffer( REG_BITRATEMSB, &SX1276->RegBitrateMsb, 2 );
  92. FskSettings.Bitrate = ( ( ( uint32_t )SX1276->RegBitrateMsb << 8 ) | ( ( uint32_t )SX1276->RegBitrateLsb ) );
  93. FskSettings.Bitrate = ( uint16_t )( ( double )XTAL_FREQ / ( double )FskSettings.Bitrate );
  94. return FskSettings.Bitrate;
  95. }
  96. void SX1276FskSetFdev( uint32_t fdev )
  97. {
  98. FskSettings.Fdev = fdev;
  99. SX1276Read( REG_FDEVMSB, &SX1276->RegFdevMsb );
  100. fdev = ( uint16_t )( ( double )fdev / ( double )FREQ_STEP );
  101. SX1276->RegFdevMsb = ( ( SX1276->RegFdevMsb & RF_FDEVMSB_FDEV_MASK ) | ( ( ( uint8_t )( fdev >> 8 ) ) & ~RF_FDEVMSB_FDEV_MASK ) );
  102. SX1276->RegFdevLsb = ( uint8_t )( fdev & 0xFF );
  103. SX1276WriteBuffer( REG_FDEVMSB, &SX1276->RegFdevMsb, 2 );
  104. }
  105. uint32_t SX1276FskGetFdev( void )
  106. {
  107. SX1276ReadBuffer( REG_FDEVMSB, &SX1276->RegFdevMsb, 2 );
  108. FskSettings.Fdev = ( ( ( uint32_t )( ( SX1276->RegFdevMsb << 8 ) & ~RF_FDEVMSB_FDEV_MASK ) ) | ( ( uint32_t )SX1276->RegFdevLsb ) );
  109. FskSettings.Fdev = ( uint16_t )( ( double )FskSettings.Fdev * ( double )FREQ_STEP );
  110. return FskSettings.Fdev;
  111. }
  112. void SX1276FskSetRFPower( int8_t power )
  113. {
  114. SX1276Read( REG_PACONFIG, &SX1276->RegPaConfig );
  115. SX1276Read( REG_PADAC, &SX1276->RegPaDac );
  116. if( ( SX1276->RegPaConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
  117. {
  118. if( ( SX1276->RegPaDac & 0x87 ) == 0x87 )
  119. {
  120. if( power < 5 )
  121. {
  122. power = 5;
  123. }
  124. if( power > 20 )
  125. {
  126. power = 20;
  127. }
  128. SX1276->RegPaConfig = ( SX1276->RegPaConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
  129. SX1276->RegPaConfig = ( SX1276->RegPaConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 5 ) & 0x0F );
  130. }
  131. else
  132. {
  133. if( power < 2 )
  134. {
  135. power = 2;
  136. }
  137. if( power > 17 )
  138. {
  139. power = 17;
  140. }
  141. SX1276->RegPaConfig = ( SX1276->RegPaConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
  142. SX1276->RegPaConfig = ( SX1276->RegPaConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 2 ) & 0x0F );
  143. }
  144. }
  145. else
  146. {
  147. if( power < -1 )
  148. {
  149. power = -1;
  150. }
  151. if( power > 14 )
  152. {
  153. power = 14;
  154. }
  155. SX1276->RegPaConfig = ( SX1276->RegPaConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70;
  156. SX1276->RegPaConfig = ( SX1276->RegPaConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power + 1 ) & 0x0F );
  157. }
  158. SX1276Write( REG_PACONFIG, SX1276->RegPaConfig );
  159. FskSettings.Power = power;
  160. }
  161. int8_t SX1276FskGetRFPower( void )
  162. {
  163. SX1276Read( REG_PACONFIG, &SX1276->RegPaConfig );
  164. SX1276Read( REG_PADAC, &SX1276->RegPaDac );
  165. if( ( SX1276->RegPaConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
  166. {
  167. if( ( SX1276->RegPaDac & 0x07 ) == 0x07 )
  168. {
  169. FskSettings.Power = 5 + ( SX1276->RegPaConfig & ~RF_PACONFIG_OUTPUTPOWER_MASK );
  170. }
  171. else
  172. {
  173. FskSettings.Power = 2 + ( SX1276->RegPaConfig & ~RF_PACONFIG_OUTPUTPOWER_MASK );
  174. }
  175. }
  176. else
  177. {
  178. FskSettings.Power = -1 + ( SX1276->RegPaConfig & ~RF_PACONFIG_OUTPUTPOWER_MASK );
  179. }
  180. return FskSettings.Power;
  181. }
  182. /*!
  183. * \brief Computes the Rx bandwidth with the mantisse and exponent
  184. *
  185. * \param [IN] mantisse Mantisse of the bandwidth value
  186. * \param [IN] exponent Exponent of the bandwidth value
  187. * \retval bandwidth Computed bandwidth
  188. */
  189. static uint32_t SX1276FskComputeRxBw( uint8_t mantisse, uint8_t exponent )
  190. {
  191. // rxBw
  192. if( ( SX1276->RegOpMode & RF_OPMODE_MODULATIONTYPE_FSK ) == RF_OPMODE_MODULATIONTYPE_FSK )
  193. {
  194. return ( uint32_t )( ( double )XTAL_FREQ / ( mantisse * ( double )pow( 2, exponent + 2 ) ) );
  195. }
  196. else
  197. {
  198. return ( uint32_t )( ( double )XTAL_FREQ / ( mantisse * ( double )pow( 2, exponent + 3 ) ) );
  199. }
  200. }
  201. /*!
  202. * \brief Computes the mantisse and exponent from the bandwitdh value
  203. *
  204. * \param [IN] rxBwValue Bandwidth value
  205. * \param [OUT] mantisse Mantisse of the bandwidth value
  206. * \param [OUT] exponent Exponent of the bandwidth value
  207. */
  208. static void SX1276FskComputeRxBwMantExp( uint32_t rxBwValue, uint8_t* mantisse, uint8_t* exponent )
  209. {
  210. uint8_t tmpExp = 0;
  211. uint8_t tmpMant = 0;
  212. double tmpRxBw = 0;
  213. double rxBwMin = 10e6;
  214. for( tmpExp = 0; tmpExp < 8; tmpExp++ )
  215. {
  216. for( tmpMant = 16; tmpMant <= 24; tmpMant += 4 )
  217. {
  218. if( ( SX1276->RegOpMode & RF_OPMODE_MODULATIONTYPE_FSK ) == RF_OPMODE_MODULATIONTYPE_FSK )
  219. {
  220. tmpRxBw = ( double )XTAL_FREQ / ( tmpMant * ( double )pow( 2, tmpExp + 2 ) );
  221. }
  222. else
  223. {
  224. tmpRxBw = ( double )XTAL_FREQ / ( tmpMant * ( double )pow( 2, tmpExp + 3 ) );
  225. }
  226. if( fabs( tmpRxBw - rxBwValue ) < rxBwMin )
  227. {
  228. rxBwMin = fabs( tmpRxBw - rxBwValue );
  229. *mantisse = tmpMant;
  230. *exponent = tmpExp;
  231. }
  232. }
  233. }
  234. }
  235. void SX1276FskSetDccBw( uint8_t* reg, uint32_t dccValue, uint32_t rxBwValue )
  236. {
  237. uint8_t mantisse = 0;
  238. uint8_t exponent = 0;
  239. if( reg == &SX1276->RegRxBw )
  240. {
  241. *reg = ( uint8_t )dccValue & 0x60;
  242. }
  243. else
  244. {
  245. *reg = 0;
  246. }
  247. SX1276FskComputeRxBwMantExp( rxBwValue, &mantisse, &exponent );
  248. switch( mantisse )
  249. {
  250. case 16:
  251. *reg |= ( uint8_t )( 0x00 | ( exponent & 0x07 ) );
  252. break;
  253. case 20:
  254. *reg |= ( uint8_t )( 0x08 | ( exponent & 0x07 ) );
  255. break;
  256. case 24:
  257. *reg |= ( uint8_t )( 0x10 | ( exponent & 0x07 ) );
  258. break;
  259. default:
  260. // Something went terribely wrong
  261. break;
  262. }
  263. if( reg == &SX1276->RegRxBw )
  264. {
  265. SX1276Write( REG_RXBW, *reg );
  266. FskSettings.RxBw = rxBwValue;
  267. }
  268. else
  269. {
  270. SX1276Write( REG_AFCBW, *reg );
  271. FskSettings.RxBwAfc = rxBwValue;
  272. }
  273. }
  274. uint32_t SX1276FskGetBw( uint8_t* reg )
  275. {
  276. uint32_t rxBwValue = 0;
  277. uint8_t mantisse = 0;
  278. switch( ( *reg & 0x18 ) >> 3 )
  279. {
  280. case 0:
  281. mantisse = 16;
  282. break;
  283. case 1:
  284. mantisse = 20;
  285. break;
  286. case 2:
  287. mantisse = 24;
  288. break;
  289. default:
  290. break;
  291. }
  292. rxBwValue = SX1276FskComputeRxBw( mantisse, ( uint8_t )*reg & 0x07 );
  293. if( reg == &SX1276->RegRxBw )
  294. {
  295. return FskSettings.RxBw = rxBwValue;
  296. }
  297. else
  298. {
  299. return FskSettings.RxBwAfc = rxBwValue;
  300. }
  301. }
  302. void SX1276FskSetPacketCrcOn( bool enable )
  303. {
  304. SX1276Read( REG_PACKETCONFIG1, &SX1276->RegPacketConfig1 );
  305. SX1276->RegPacketConfig1 = ( SX1276->RegPacketConfig1 & RF_PACKETCONFIG1_CRC_MASK ) | ( enable << 4 );
  306. SX1276Write( REG_PACKETCONFIG1, SX1276->RegPacketConfig1 );
  307. FskSettings.CrcOn = enable;
  308. }
  309. bool SX1276FskGetPacketCrcOn( void )
  310. {
  311. SX1276Read( REG_PACKETCONFIG1, &SX1276->RegPacketConfig1 );
  312. FskSettings.CrcOn = ( SX1276->RegPacketConfig1 & RF_PACKETCONFIG1_CRC_ON ) >> 4;
  313. return FskSettings.CrcOn;
  314. }
  315. void SX1276FskSetAfcOn( bool enable )
  316. {
  317. SX1276Read( REG_RXCONFIG, &SX1276->RegRxConfig );
  318. SX1276->RegRxConfig = ( SX1276->RegRxConfig & RF_RXCONFIG_AFCAUTO_MASK ) | ( enable << 4 );
  319. SX1276Write( REG_RXCONFIG, SX1276->RegRxConfig );
  320. FskSettings.AfcOn = enable;
  321. }
  322. bool SX1276FskGetAfcOn( void )
  323. {
  324. SX1276Read( REG_RXCONFIG, &SX1276->RegRxConfig );
  325. FskSettings.AfcOn = ( SX1276->RegRxConfig & RF_RXCONFIG_AFCAUTO_ON ) >> 4;
  326. return FskSettings.AfcOn;
  327. }
  328. void SX1276FskSetPayloadLength( uint8_t value )
  329. {
  330. SX1276->RegPayloadLength = value;
  331. SX1276Write( REG_PAYLOADLENGTH, SX1276->RegPayloadLength );
  332. FskSettings.PayloadLength = value;
  333. }
  334. uint8_t SX1276FskGetPayloadLength( void )
  335. {
  336. SX1276Read( REG_PAYLOADLENGTH, &SX1276->RegPayloadLength );
  337. FskSettings.PayloadLength = SX1276->RegPayloadLength;
  338. return FskSettings.PayloadLength;
  339. }
  340. void SX1276FskSetPa20dBm( bool enale )
  341. {
  342. SX1276Read( REG_PADAC, &SX1276->RegPaDac );
  343. SX1276Read( REG_PACONFIG, &SX1276->RegPaConfig );
  344. if( ( SX1276->RegPaConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
  345. {
  346. if( enale == true )
  347. {
  348. SX1276->RegPaDac = 0x87;
  349. }
  350. }
  351. else
  352. {
  353. SX1276->RegPaDac = 0x84;
  354. }
  355. SX1276Write( REG_PADAC, SX1276->RegPaDac );
  356. }
  357. bool SX1276FskGetPa20dBm( void )
  358. {
  359. SX1276Read( REG_PADAC, &SX1276->RegPaDac );
  360. return ( ( SX1276->RegPaDac & 0x07 ) == 0x07 ) ? true : false;
  361. }
  362. void SX1276FskSetPAOutput( uint8_t outputPin )
  363. {
  364. SX1276Read( REG_PACONFIG, &SX1276->RegPaConfig );
  365. SX1276->RegPaConfig = (SX1276->RegPaConfig & RF_PACONFIG_PASELECT_MASK ) | outputPin;
  366. SX1276Write( REG_PACONFIG, SX1276->RegPaConfig );
  367. }
  368. uint8_t SX1276FskGetPAOutput( void )
  369. {
  370. SX1276Read( REG_PACONFIG, &SX1276->RegPaConfig );
  371. return SX1276->RegPaConfig & ~RF_PACONFIG_PASELECT_MASK;
  372. }
  373. void SX1276FskSetPaRamp( uint8_t value )
  374. {
  375. SX1276Read( REG_PARAMP, &SX1276->RegPaRamp );
  376. SX1276->RegPaRamp = ( SX1276->RegPaRamp & RF_PARAMP_MASK ) | ( value & ~RF_PARAMP_MASK );
  377. SX1276Write( REG_PARAMP, SX1276->RegPaRamp );
  378. }
  379. uint8_t SX1276FskGetPaRamp( void )
  380. {
  381. SX1276Read( REG_PARAMP, &SX1276->RegPaRamp );
  382. return SX1276->RegPaRamp & ~RF_PARAMP_MASK;
  383. }
  384. void SX1276FskSetRssiOffset( int8_t offset )
  385. {
  386. SX1276Read( REG_RSSICONFIG, &SX1276->RegRssiConfig );
  387. if( offset < 0 )
  388. {
  389. offset = ( ~offset & 0x1F );
  390. offset += 1;
  391. offset = -offset;
  392. }
  393. SX1276->RegRssiConfig |= ( uint8_t )( ( offset & 0x1F ) << 3 );
  394. SX1276Write( REG_RSSICONFIG, SX1276->RegRssiConfig );
  395. }
  396. int8_t SX1276FskGetRssiOffset( void )
  397. {
  398. SX1276Read( REG_RSSICONFIG, &SX1276->RegRssiConfig );
  399. int8_t offset = SX1276->RegRssiConfig >> 3;
  400. if( ( offset & 0x10 ) == 0x10 )
  401. {
  402. offset = ( ~offset & 0x1F );
  403. offset += 1;
  404. offset = -offset;
  405. }
  406. return offset;
  407. }
  408. int8_t SX1276FskGetRawTemp( void )
  409. {
  410. int8_t temp = 0;
  411. uint8_t previousOpMode;
  412. uint32_t startTick;
  413. // Enable Temperature reading
  414. SX1276Read( REG_IMAGECAL, &SX1276->RegImageCal );
  415. SX1276->RegImageCal = ( SX1276->RegImageCal & RF_IMAGECAL_TEMPMONITOR_MASK ) | RF_IMAGECAL_TEMPMONITOR_ON;
  416. SX1276Write( REG_IMAGECAL, SX1276->RegImageCal );
  417. // save current Op Mode
  418. SX1276Read( REG_OPMODE, &SX1276->RegOpMode );
  419. previousOpMode = SX1276->RegOpMode;
  420. // put device in FSK RxSynth
  421. SX1276->RegOpMode = RF_OPMODE_SYNTHESIZER_RX;
  422. SX1276Write( REG_OPMODE, SX1276->RegOpMode );
  423. // Wait 1ms
  424. startTick = GET_TICK_COUNT( );
  425. while( ( GET_TICK_COUNT( ) - startTick ) < TICK_RATE_MS( 1 ) );
  426. // Disable Temperature reading
  427. SX1276Read( REG_IMAGECAL, &SX1276->RegImageCal );
  428. SX1276->RegImageCal = ( SX1276->RegImageCal & RF_IMAGECAL_TEMPMONITOR_MASK ) | RF_IMAGECAL_TEMPMONITOR_OFF;
  429. SX1276Write( REG_IMAGECAL, SX1276->RegImageCal );
  430. // Read temperature
  431. SX1276Read( REG_TEMP, &SX1276->RegTemp );
  432. temp = SX1276->RegTemp & 0x7F;
  433. if( ( SX1276->RegTemp & 0x80 ) == 0x80 )
  434. {
  435. temp *= -1;
  436. }
  437. // Reload previous Op Mode
  438. SX1276Write( REG_OPMODE, previousOpMode );
  439. return temp;
  440. }
  441. int8_t SX1276FskCalibreateTemp( int8_t actualTemp )
  442. {
  443. return actualTemp - SX1276FskGetRawTemp( );
  444. }
  445. int8_t SX1276FskGetTemp( int8_t compensationFactor )
  446. {
  447. return SX1276FskGetRawTemp( ) + compensationFactor;
  448. }
  449. #endif // USE_SX1276_RADIO