sx1276.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.c
  13. * \brief SX1276 RF chip driver
  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 "platform.h"
  22. #include "radio.h"
  23. #if defined( USE_SX1276_RADIO )
  24. #include "sx1276.h"
  25. #include "sx1276-Hal.h"
  26. #include "sx1276-Fsk.h"
  27. #include "sx1276-LoRa.h"
  28. #include "delay.h"
  29. #include "stdio.h"
  30. /*!
  31. * SX1276 registers variable
  32. */
  33. uint8_t SX1276Regs[0x70];
  34. static bool LoRaOn = false;
  35. static bool LoRaOnState = false;
  36. void SX1276Init( void )
  37. {
  38. // Initialize FSK and LoRa registers structure
  39. SX1276 = ( tSX1276* )SX1276Regs;
  40. SX1276LR = ( tSX1276LR* )SX1276Regs;
  41. SX1276InitIo( );
  42. SX1276Reset( );
  43. uint8_t RegVersion = 0;
  44. SX1276Read( REG_LR_VERSION, &RegVersion );
  45. if(RegVersion != 0x12)
  46. {
  47. printf("LoRa read Error!\r\n");
  48. printf("LoRa RegVersion = %d!\r\n",RegVersion);
  49. }
  50. else
  51. {
  52. printf("LoRa read Ok!\r\n");
  53. printf("LoRa RegVersion = %d!\r\n",RegVersion);
  54. }
  55. // REMARK: After radio reset the default modem is FSK
  56. #if ( LORA == 0 )
  57. LoRaOn = false;
  58. SX1276SetLoRaOn( LoRaOn );
  59. // Initialize FSK modem
  60. SX1276FskInit( );
  61. #else
  62. LoRaOn = true;
  63. SX1276SetLoRaOn( LoRaOn );
  64. // Initialize LoRa modem
  65. SX1276LoRaInit( );
  66. #endif
  67. }
  68. void SX1276Reset( void )
  69. {
  70. SX1276SetReset( RADIO_RESET_OFF );
  71. // Wait 1ms
  72. delay_ms(1);
  73. SX1276SetReset( RADIO_RESET_ON );
  74. // Wait 6ms
  75. delay_ms(6);
  76. }
  77. void SX1276SetLoRaOn( bool enable )
  78. {
  79. if( LoRaOnState == enable )
  80. {
  81. return;
  82. }
  83. LoRaOnState = enable;
  84. LoRaOn = enable;
  85. if( LoRaOn == true )
  86. {
  87. SX1276LoRaSetOpMode( RFLR_OPMODE_SLEEP );
  88. SX1276LR->RegOpMode = ( SX1276LR->RegOpMode & RFLR_OPMODE_LONGRANGEMODE_MASK ) | RFLR_OPMODE_LONGRANGEMODE_ON;
  89. SX1276Write( REG_LR_OPMODE, SX1276LR->RegOpMode );
  90. SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
  91. // RxDone RxTimeout FhssChangeChannel CadDone
  92. SX1276LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_00 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_00;
  93. // CadDetected ModeReady
  94. SX1276LR->RegDioMapping2 = RFLR_DIOMAPPING2_DIO4_00 | RFLR_DIOMAPPING2_DIO5_00;
  95. SX1276WriteBuffer( REG_LR_DIOMAPPING1, &SX1276LR->RegDioMapping1, 2 );
  96. SX1276ReadBuffer( REG_LR_OPMODE, SX1276Regs + 1, 0x70 - 1 );
  97. }
  98. else
  99. {
  100. SX1276LoRaSetOpMode( RFLR_OPMODE_SLEEP );
  101. SX1276LR->RegOpMode = ( SX1276LR->RegOpMode & RFLR_OPMODE_LONGRANGEMODE_MASK ) | RFLR_OPMODE_LONGRANGEMODE_OFF;
  102. SX1276Write( REG_LR_OPMODE, SX1276LR->RegOpMode );
  103. SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
  104. SX1276ReadBuffer( REG_OPMODE, SX1276Regs + 1, 0x70 - 1 );
  105. }
  106. }
  107. bool SX1276GetLoRaOn( void )
  108. {
  109. return LoRaOn;
  110. }
  111. void SX1276SetOpMode( uint8_t opMode )
  112. {
  113. if( LoRaOn == false )
  114. {
  115. SX1276FskSetOpMode( opMode );
  116. }
  117. else
  118. {
  119. SX1276LoRaSetOpMode( opMode );
  120. }
  121. }
  122. uint8_t SX1276GetOpMode( void )
  123. {
  124. if( LoRaOn == false )
  125. {
  126. return SX1276FskGetOpMode( );
  127. }
  128. else
  129. {
  130. return SX1276LoRaGetOpMode( );
  131. }
  132. }
  133. double SX1276ReadRssi( void )
  134. {
  135. if( LoRaOn == false )
  136. {
  137. return SX1276FskReadRssi( );
  138. }
  139. else
  140. {
  141. return SX1276LoRaReadRssi( );
  142. }
  143. }
  144. uint8_t SX1276ReadRxGain( void )
  145. {
  146. if( LoRaOn == false )
  147. {
  148. return SX1276FskReadRxGain( );
  149. }
  150. else
  151. {
  152. return SX1276LoRaReadRxGain( );
  153. }
  154. }
  155. uint8_t SX1276GetPacketRxGain( void )
  156. {
  157. if( LoRaOn == false )
  158. {
  159. return SX1276FskGetPacketRxGain( );
  160. }
  161. else
  162. {
  163. return SX1276LoRaGetPacketRxGain( );
  164. }
  165. }
  166. int8_t SX1276GetPacketSnr( void )
  167. {
  168. if( LoRaOn == false )
  169. {
  170. while( 1 )
  171. {
  172. // Useless in FSK mode
  173. // Block program here
  174. }
  175. }
  176. else
  177. {
  178. return SX1276LoRaGetPacketSnr( );
  179. }
  180. }
  181. double SX1276GetPacketRssi( void )
  182. {
  183. if( LoRaOn == false )
  184. {
  185. return SX1276FskGetPacketRssi( );
  186. }
  187. else
  188. {
  189. return SX1276LoRaGetPacketRssi( );
  190. }
  191. }
  192. uint32_t SX1276GetPacketAfc( void )
  193. {
  194. if( LoRaOn == false )
  195. {
  196. return SX1276FskGetPacketAfc( );
  197. }
  198. else
  199. {
  200. while( 1 )
  201. {
  202. // Useless in LoRa mode
  203. // Block program here
  204. }
  205. }
  206. }
  207. void SX1276StartRx( void )
  208. {
  209. if( LoRaOn == false )
  210. {
  211. SX1276FskSetRFState( RF_STATE_RX_INIT );
  212. }
  213. else
  214. {
  215. SX1276LoRaSetRFState( RFLR_STATE_RX_INIT );
  216. }
  217. }
  218. void SX1276GetRxPacket( void *buffer, uint16_t *size )
  219. {
  220. if( LoRaOn == false )
  221. {
  222. SX1276FskGetRxPacket( buffer, size );
  223. }
  224. else
  225. {
  226. SX1276LoRaGetRxPacket( buffer, size );
  227. }
  228. }
  229. void SX1276SetTxPacket( const void *buffer, uint16_t size )
  230. {
  231. if( LoRaOn == false )
  232. {
  233. SX1276FskSetTxPacket( buffer, size );
  234. }
  235. else
  236. {
  237. SX1276LoRaSetTxPacket( buffer, size );
  238. }
  239. }
  240. uint8_t SX1276GetRFState( void )
  241. {
  242. if( LoRaOn == false )
  243. {
  244. return SX1276FskGetRFState( );
  245. }
  246. else
  247. {
  248. return SX1276LoRaGetRFState( );
  249. }
  250. }
  251. void SX1276SetRFState( uint8_t state )
  252. {
  253. if( LoRaOn == false )
  254. {
  255. SX1276FskSetRFState( state );
  256. }
  257. else
  258. {
  259. SX1276LoRaSetRFState( state );
  260. }
  261. }
  262. uint32_t SX1276Process( void )
  263. {
  264. if( LoRaOn == false )
  265. {
  266. return SX1276FskProcess( );
  267. }
  268. else
  269. {
  270. return SX1276LoRaProcess( );
  271. }
  272. }
  273. #endif // USE_SX1276_RADIO