sx1276-Fsk.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 <string.h>
  22. #include <math.h>
  23. #include "platform.h"
  24. #if defined( USE_SX1276_RADIO )
  25. #include "radio.h"
  26. #include "sx1276-Hal.h"
  27. #include "sx1276.h"
  28. #include "sx1276-FskMisc.h"
  29. #include "sx1276-Fsk.h"
  30. // Default settings
  31. tFskSettings FskSettings =
  32. {
  33. 870000000, // RFFrequency
  34. 9600, // Bitrate
  35. 50000, // Fdev
  36. 20, // Power
  37. 100000, // RxBw
  38. 150000, // RxBwAfc
  39. true, // CrcOn
  40. true, // AfcOn
  41. 255 // PayloadLength (set payload size to the maximum for variable mode, else set the exact payload length)
  42. };
  43. /*!
  44. * SX1276 FSK registers variable
  45. */
  46. tSX1276* SX1276;
  47. /*!
  48. * Local RF buffer for communication support
  49. */
  50. static uint8_t RFBuffer[RF_BUFFER_SIZE];
  51. /*!
  52. * Chunk size of data write in buffer
  53. */
  54. static uint8_t DataChunkSize = 32;
  55. /*!
  56. * RF state machine variable
  57. */
  58. static uint8_t RFState = RF_STATE_IDLE;
  59. /*!
  60. * Rx management support variables
  61. */
  62. /*!
  63. * PacketTimeout holds the RF packet timeout
  64. * SyncSize = [0..8]
  65. * VariableSize = [0;1]
  66. * AddressSize = [0;1]
  67. * PayloadSize = [0..RF_BUFFER_SIZE]
  68. * CrcSize = [0;2]
  69. * PacketTimeout = ( ( 8 * ( VariableSize + AddressSize + PayloadSize + CrcSize ) / BR ) * 1000.0 ) + 1
  70. * Computed timeout is in miliseconds
  71. */
  72. static uint32_t PacketTimeout;
  73. /*!
  74. * Preamble2SyncTimeout
  75. * Preamble2SyncTimeout = ( ( 8 * ( PremableSize + SyncSize ) / RFBitrate ) * 1000.0 ) + 1
  76. * Computed timeout is in miliseconds
  77. */
  78. static uint32_t Preamble2SyncTimeout;
  79. static bool PreambleDetected = false;
  80. static bool SyncWordDetected = false;
  81. static bool PacketDetected = false;
  82. static uint16_t RxPacketSize = 0;
  83. static uint8_t RxBytesRead = 0;
  84. static uint8_t TxBytesSent = 0;
  85. static double RxPacketRssiValue;
  86. static uint32_t RxPacketAfcValue;
  87. static uint8_t RxGain = 1;
  88. static uint32_t RxTimeoutTimer = 0;
  89. static uint32_t Preamble2SyncTimer = 0;
  90. /*!
  91. * Tx management support variables
  92. */
  93. static uint16_t TxPacketSize = 0;
  94. static uint32_t TxTimeoutTimer = 0;
  95. void SX1276FskInit( void )
  96. {
  97. RFState = RF_STATE_IDLE;
  98. SX1276FskSetDefaults( );
  99. SX1276ReadBuffer( REG_OPMODE, SX1276Regs + 1, 0x70 - 1 );
  100. // Set the device in FSK mode and Sleep Mode
  101. SX1276->RegOpMode = RF_OPMODE_MODULATIONTYPE_FSK | RF_OPMODE_SLEEP;
  102. SX1276Write( REG_OPMODE, SX1276->RegOpMode );
  103. SX1276->RegPaRamp = RF_PARAMP_MODULATIONSHAPING_01;
  104. SX1276Write( REG_PARAMP, SX1276->RegPaRamp );
  105. SX1276->RegLna = RF_LNA_GAIN_G1;
  106. SX1276Write( REG_LNA, SX1276->RegLna );
  107. if( FskSettings.AfcOn == true )
  108. {
  109. SX1276->RegRxConfig = RF_RXCONFIG_RESTARTRXONCOLLISION_OFF | RF_RXCONFIG_AFCAUTO_ON |
  110. RF_RXCONFIG_AGCAUTO_ON | RF_RXCONFIG_RXTRIGER_PREAMBLEDETECT;
  111. }
  112. else
  113. {
  114. SX1276->RegRxConfig = RF_RXCONFIG_RESTARTRXONCOLLISION_OFF | RF_RXCONFIG_AFCAUTO_OFF |
  115. RF_RXCONFIG_AGCAUTO_ON | RF_RXCONFIG_RXTRIGER_PREAMBLEDETECT;
  116. }
  117. SX1276->RegPreambleLsb = 8;
  118. SX1276->RegPreambleDetect = RF_PREAMBLEDETECT_DETECTOR_ON | RF_PREAMBLEDETECT_DETECTORSIZE_2 |
  119. RF_PREAMBLEDETECT_DETECTORTOL_10;
  120. SX1276->RegRssiThresh = 0xFF;
  121. SX1276->RegSyncConfig = RF_SYNCCONFIG_AUTORESTARTRXMODE_WAITPLL_ON | RF_SYNCCONFIG_PREAMBLEPOLARITY_AA |
  122. RF_SYNCCONFIG_SYNC_ON |
  123. RF_SYNCCONFIG_SYNCSIZE_4;
  124. SX1276->RegSyncValue1 = 0x69;
  125. SX1276->RegSyncValue2 = 0x81;
  126. SX1276->RegSyncValue3 = 0x7E;
  127. SX1276->RegSyncValue4 = 0x96;
  128. SX1276->RegPacketConfig1 = RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE | RF_PACKETCONFIG1_DCFREE_OFF |
  129. ( FskSettings.CrcOn << 4 ) | RF_PACKETCONFIG1_CRCAUTOCLEAR_ON |
  130. RF_PACKETCONFIG1_ADDRSFILTERING_OFF | RF_PACKETCONFIG1_CRCWHITENINGTYPE_CCITT;
  131. SX1276FskGetPacketCrcOn( ); // Update CrcOn on FskSettings
  132. SX1276->RegPayloadLength = FskSettings.PayloadLength;
  133. // we can now update the registers with our configuration
  134. SX1276WriteBuffer( REG_OPMODE, SX1276Regs + 1, 0x70 - 1 );
  135. // then we need to set the RF settings
  136. SX1276FskSetRFFrequency( FskSettings.RFFrequency );
  137. SX1276FskSetBitrate( FskSettings.Bitrate );
  138. SX1276FskSetFdev( FskSettings.Fdev );
  139. SX1276FskSetDccBw( &SX1276->RegRxBw, 0, FskSettings.RxBw );
  140. SX1276FskSetDccBw( &SX1276->RegAfcBw, 0, FskSettings.RxBwAfc );
  141. SX1276FskSetRssiOffset( 0 );
  142. #if( ( MODULE_SX1276RF1IAS == 1 ) || ( MODULE_SX1276RF1KAS == 1 ) )
  143. if( FskSettings.RFFrequency > 860000000 )
  144. {
  145. SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_RFO );
  146. SX1276FskSetPa20dBm( false );
  147. FskSettings.Power = 14;
  148. SX1276FskSetRFPower( FskSettings.Power );
  149. }
  150. else
  151. {
  152. SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_PABOOST );
  153. SX1276FskSetPa20dBm( true );
  154. FskSettings.Power = 20;
  155. SX1276FskSetRFPower( FskSettings.Power );
  156. }
  157. #elif( MODULE_SX1276RF1JAS == 1 )
  158. if( FskSettings.RFFrequency > 860000000 )
  159. {
  160. SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_PABOOST );
  161. SX1276FskSetPa20dBm( true );
  162. FskSettings.Power = 20;
  163. SX1276FskSetRFPower( FskSettings.Power );
  164. }
  165. else
  166. {
  167. SX1276FskSetPAOutput( RF_PACONFIG_PASELECT_RFO );
  168. SX1276FskSetPa20dBm( false );
  169. FskSettings.Power = 14;
  170. SX1276FskSetRFPower( FskSettings.Power );
  171. }
  172. #endif
  173. SX1276FskSetOpMode( RF_OPMODE_STANDBY );
  174. // Calibrate the HF
  175. SX1276FskRxCalibrate( );
  176. }
  177. void SX1276FskSetDefaults( void )
  178. {
  179. // REMARK: See SX1276 datasheet for modified default values.
  180. SX1276Read( REG_VERSION, &SX1276->RegVersion );
  181. }
  182. void SX1276FskSetOpMode( uint8_t opMode )
  183. {
  184. static uint8_t opModePrev = RF_OPMODE_STANDBY;
  185. static bool antennaSwitchTxOnPrev = true;
  186. bool antennaSwitchTxOn = false;
  187. opModePrev = SX1276->RegOpMode & ~RF_OPMODE_MASK;
  188. if( opMode != opModePrev )
  189. {
  190. if( opMode == RF_OPMODE_TRANSMITTER )
  191. {
  192. antennaSwitchTxOn = true;
  193. }
  194. else
  195. {
  196. antennaSwitchTxOn = false;
  197. }
  198. if( antennaSwitchTxOn != antennaSwitchTxOnPrev )
  199. {
  200. antennaSwitchTxOnPrev = antennaSwitchTxOn;
  201. RXTX( antennaSwitchTxOn ); // Antenna switch control
  202. }
  203. SX1276->RegOpMode = ( SX1276->RegOpMode & RF_OPMODE_MASK ) | opMode;
  204. SX1276Write( REG_OPMODE, SX1276->RegOpMode );
  205. }
  206. }
  207. uint8_t SX1276FskGetOpMode( void )
  208. {
  209. SX1276Read( REG_OPMODE, &SX1276->RegOpMode );
  210. return SX1276->RegOpMode & ~RF_OPMODE_MASK;
  211. }
  212. int32_t SX1276FskReadFei( void )
  213. {
  214. SX1276ReadBuffer( REG_FEIMSB, &SX1276->RegFeiMsb, 2 ); // Reads the FEI value
  215. return ( int32_t )( double )( ( ( uint16_t )SX1276->RegFeiMsb << 8 ) | ( uint16_t )SX1276->RegFeiLsb ) * ( double )FREQ_STEP;
  216. }
  217. int32_t SX1276FskReadAfc( void )
  218. {
  219. SX1276ReadBuffer( REG_AFCMSB, &SX1276->RegAfcMsb, 2 ); // Reads the AFC value
  220. return ( int32_t )( double )( ( ( uint16_t )SX1276->RegAfcMsb << 8 ) | ( uint16_t )SX1276->RegAfcLsb ) * ( double )FREQ_STEP;
  221. }
  222. uint8_t SX1276FskReadRxGain( void )
  223. {
  224. SX1276Read( REG_LNA, &SX1276->RegLna );
  225. return( SX1276->RegLna >> 5 ) & 0x07;
  226. }
  227. double SX1276FskReadRssi( void )
  228. {
  229. SX1276Read( REG_RSSIVALUE, &SX1276->RegRssiValue ); // Reads the RSSI value
  230. return -( double )( ( double )SX1276->RegRssiValue / 2.0 );
  231. }
  232. uint8_t SX1276FskGetPacketRxGain( void )
  233. {
  234. return RxGain;
  235. }
  236. double SX1276FskGetPacketRssi( void )
  237. {
  238. return RxPacketRssiValue;
  239. }
  240. uint32_t SX1276FskGetPacketAfc( void )
  241. {
  242. return RxPacketAfcValue;
  243. }
  244. void SX1276FskStartRx( void )
  245. {
  246. SX1276FskSetRFState( RF_STATE_RX_INIT );
  247. }
  248. void SX1276FskGetRxPacket( void *buffer, uint16_t *size )
  249. {
  250. *size = RxPacketSize;
  251. RxPacketSize = 0;
  252. memcpy( ( void * )buffer, ( void * )RFBuffer, ( size_t )*size );
  253. }
  254. void SX1276FskSetTxPacket( const void *buffer, uint16_t size )
  255. {
  256. TxPacketSize = size;
  257. memcpy( ( void * )RFBuffer, buffer, ( size_t )TxPacketSize );
  258. RFState = RF_STATE_TX_INIT;
  259. }
  260. // Remark: SX1276 must be fully initialized before calling this function
  261. uint16_t SX1276FskGetPacketPayloadSize( void )
  262. {
  263. uint16_t syncSize;
  264. uint16_t variableSize;
  265. uint16_t addressSize;
  266. uint16_t payloadSize;
  267. uint16_t crcSize;
  268. syncSize = ( SX1276->RegSyncConfig & 0x07 ) + 1;
  269. variableSize = ( ( SX1276->RegPacketConfig1 & 0x80 ) == 0x80 ) ? 1 : 0;
  270. addressSize = ( ( SX1276->RegPacketConfig1 & 0x06 ) != 0x00 ) ? 1 : 0;
  271. payloadSize = SX1276->RegPayloadLength;
  272. crcSize = ( ( SX1276->RegPacketConfig1 & 0x10 ) == 0x10 ) ? 2 : 0;
  273. return syncSize + variableSize + addressSize + payloadSize + crcSize;
  274. }
  275. // Remark: SX1276 must be fully initialized before calling this function
  276. uint16_t SX1276FskGetPacketHeaderSize( void )
  277. {
  278. uint16_t preambleSize;
  279. uint16_t syncSize;
  280. preambleSize = ( ( uint16_t )SX1276->RegPreambleMsb << 8 ) | ( uint16_t )SX1276->RegPreambleLsb;
  281. syncSize = ( SX1276->RegSyncConfig & 0x07 ) + 1;
  282. return preambleSize + syncSize;
  283. }
  284. uint8_t SX1276FskGetRFState( void )
  285. {
  286. return RFState;
  287. }
  288. void SX1276FskSetRFState( uint8_t state )
  289. {
  290. RFState = state;
  291. }
  292. uint32_t SX1276FskProcess( void )
  293. {
  294. uint32_t result = RF_BUSY;
  295. switch( RFState )
  296. {
  297. case RF_STATE_IDLE:
  298. break;
  299. // Rx management
  300. case RF_STATE_RX_INIT:
  301. // DIO mapping setup
  302. if( ( SX1276->RegPacketConfig1 & RF_PACKETCONFIG1_CRC_ON ) == RF_PACKETCONFIG1_CRC_ON )
  303. {
  304. // CrcOk, FifoLevel, SyncAddr, FifoEmpty
  305. SX1276->RegDioMapping1 = RF_DIOMAPPING1_DIO0_01 | RF_DIOMAPPING1_DIO1_00 | RF_DIOMAPPING1_DIO2_11 | RF_DIOMAPPING1_DIO3_00;
  306. }
  307. else
  308. {
  309. // PayloadReady, FifoLevel, SyncAddr, FifoEmpty
  310. SX1276->RegDioMapping1 = RF_DIOMAPPING1_DIO0_00 | RF_DIOMAPPING1_DIO1_00 | RF_DIOMAPPING1_DIO2_11 | RF_DIOMAPPING1_DIO3_00;
  311. }
  312. // Preamble, Data
  313. SX1276->RegDioMapping2 = RF_DIOMAPPING2_DIO4_11 | RF_DIOMAPPING2_DIO5_10 | RF_DIOMAPPING2_MAP_PREAMBLEDETECT;
  314. SX1276WriteBuffer( REG_DIOMAPPING1, &SX1276->RegDioMapping1, 2 );
  315. SX1276FskSetOpMode( RF_OPMODE_RECEIVER );
  316. memset( RFBuffer, 0, ( size_t )RF_BUFFER_SIZE );
  317. PacketTimeout = ( uint16_t )( round( ( 8.0 * ( ( double )SX1276FskGetPacketPayloadSize( ) ) / ( double )FskSettings.Bitrate ) * 1000.0 ) + 1.0 );
  318. PacketTimeout = PacketTimeout + ( PacketTimeout >> 1 ); // Set the Packet timeout as 1.5 times the full payload transmission time
  319. Preamble2SyncTimeout = PacketTimeout;
  320. Preamble2SyncTimer = RxTimeoutTimer = GET_TICK_COUNT( );
  321. SX1276->RegFifoThresh = RF_FIFOTHRESH_TXSTARTCONDITION_FIFONOTEMPTY | 0x20; // 32 bytes of data
  322. SX1276Write( REG_FIFOTHRESH, SX1276->RegFifoThresh );
  323. PreambleDetected = false;
  324. SyncWordDetected = false;
  325. PacketDetected = false;
  326. RxBytesRead = 0;
  327. RxPacketSize = 0;
  328. RFState = RF_STATE_RX_SYNC;
  329. break;
  330. case RF_STATE_RX_SYNC:
  331. if( ( DIO4 == 1 ) && ( PreambleDetected == false ) )// Preamble
  332. {
  333. PreambleDetected = true;
  334. Preamble2SyncTimer = GET_TICK_COUNT( );
  335. }
  336. if( ( DIO2 == 1 ) && ( PreambleDetected == true ) && ( SyncWordDetected == false ) ) // SyncAddr
  337. {
  338. SyncWordDetected = true;
  339. RxPacketRssiValue = SX1276FskReadRssi( );
  340. RxPacketAfcValue = SX1276FskReadAfc( );
  341. RxGain = SX1276FskReadRxGain( );
  342. Preamble2SyncTimer = RxTimeoutTimer = GET_TICK_COUNT( );
  343. RFState = RF_STATE_RX_RUNNING;
  344. }
  345. // Preamble 2 SyncAddr timeout
  346. if( ( SyncWordDetected == false ) && ( PreambleDetected == true ) && ( ( GET_TICK_COUNT( ) - Preamble2SyncTimer ) > Preamble2SyncTimeout ) )
  347. {
  348. RFState = RF_STATE_RX_INIT;
  349. SX1276Write( REG_RXCONFIG, SX1276->RegRxConfig | RF_RXCONFIG_RESTARTRXWITHPLLLOCK );
  350. }
  351. if( ( SyncWordDetected == false ) &&
  352. ( PreambleDetected == false ) &&
  353. ( PacketDetected == false ) &&
  354. ( ( GET_TICK_COUNT( ) - RxTimeoutTimer ) > PacketTimeout ) )
  355. {
  356. RFState = RF_STATE_RX_TIMEOUT;
  357. }
  358. break;
  359. case RF_STATE_RX_RUNNING:
  360. if( RxPacketSize > RF_BUFFER_SIZE_MAX )
  361. {
  362. RFState = RF_STATE_RX_LEN_ERROR;
  363. break;
  364. }
  365. #if 1
  366. if( DIO1 == 1 ) // FifoLevel
  367. {
  368. if( ( RxPacketSize == 0 ) && ( RxBytesRead == 0 ) ) // Read received packet size
  369. {
  370. if( ( SX1276->RegPacketConfig1 & RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE ) == RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE )
  371. {
  372. SX1276ReadFifo( ( uint8_t* )&RxPacketSize, 1 );
  373. }
  374. else
  375. {
  376. RxPacketSize = SX1276->RegPayloadLength;
  377. }
  378. }
  379. if( ( RxPacketSize - RxBytesRead ) > ( SX1276->RegFifoThresh & 0x3F ) )
  380. {
  381. SX1276ReadFifo( ( RFBuffer + RxBytesRead ), ( SX1276->RegFifoThresh & 0x3F ) );
  382. RxBytesRead += ( SX1276->RegFifoThresh & 0x3F );
  383. }
  384. else
  385. {
  386. SX1276ReadFifo( ( RFBuffer + RxBytesRead ), RxPacketSize - RxBytesRead );
  387. RxBytesRead += ( RxPacketSize - RxBytesRead );
  388. }
  389. }
  390. #endif
  391. if( DIO0 == 1 ) // PayloadReady/CrcOk
  392. {
  393. RxTimeoutTimer = GET_TICK_COUNT( );
  394. if( ( RxPacketSize == 0 ) && ( RxBytesRead == 0 ) ) // Read received packet size
  395. {
  396. if( ( SX1276->RegPacketConfig1 & RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE ) == RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE )
  397. {
  398. SX1276ReadFifo( ( uint8_t* )&RxPacketSize, 1 );
  399. }
  400. else
  401. {
  402. RxPacketSize = SX1276->RegPayloadLength;
  403. }
  404. SX1276ReadFifo( RFBuffer + RxBytesRead, RxPacketSize - RxBytesRead );
  405. RxBytesRead += ( RxPacketSize - RxBytesRead );
  406. PacketDetected = true;
  407. RFState = RF_STATE_RX_DONE;
  408. }
  409. else
  410. {
  411. SX1276ReadFifo( RFBuffer + RxBytesRead, RxPacketSize - RxBytesRead );
  412. RxBytesRead += ( RxPacketSize - RxBytesRead );
  413. PacketDetected = true;
  414. RFState = RF_STATE_RX_DONE;
  415. }
  416. }
  417. // Packet timeout
  418. if( ( PacketDetected == false ) && ( ( GET_TICK_COUNT( ) - RxTimeoutTimer ) > PacketTimeout ) )
  419. {
  420. RFState = RF_STATE_RX_TIMEOUT;
  421. }
  422. break;
  423. case RF_STATE_RX_DONE:
  424. RxBytesRead = 0;
  425. RFState = RF_STATE_RX_INIT;
  426. result = RF_RX_DONE;
  427. break;
  428. case RF_STATE_RX_TIMEOUT:
  429. RxBytesRead = 0;
  430. RxPacketSize = 0;
  431. SX1276Write( REG_RXCONFIG, SX1276->RegRxConfig | RF_RXCONFIG_RESTARTRXWITHPLLLOCK );
  432. RFState = RF_STATE_RX_INIT;
  433. result = RF_RX_TIMEOUT;
  434. break;
  435. case RF_STATE_RX_LEN_ERROR:
  436. RxBytesRead = 0;
  437. RxPacketSize = 0;
  438. SX1276Write( REG_RXCONFIG, SX1276->RegRxConfig | RF_RXCONFIG_RESTARTRXWITHPLLLOCK );
  439. RFState = RF_STATE_RX_INIT;
  440. result = RF_LEN_ERROR;
  441. break;
  442. // Tx management
  443. case RF_STATE_TX_INIT:
  444. // Packet DIO mapping setup
  445. // PacketSent, FifoLevel, FifoFull, TxReady
  446. SX1276->RegDioMapping1 = RF_DIOMAPPING1_DIO0_00 | RF_DIOMAPPING1_DIO1_00 | RF_DIOMAPPING1_DIO2_00 | RF_DIOMAPPING1_DIO3_01;
  447. // LowBat, Data
  448. SX1276->RegDioMapping2 = RF_DIOMAPPING2_DIO4_00 | RF_DIOMAPPING2_DIO5_10;
  449. SX1276WriteBuffer( REG_DIOMAPPING1, &SX1276->RegDioMapping1, 2 );
  450. SX1276->RegFifoThresh = RF_FIFOTHRESH_TXSTARTCONDITION_FIFONOTEMPTY | 0x18; // 24 bytes of data
  451. SX1276Write( REG_FIFOTHRESH, SX1276->RegFifoThresh );
  452. SX1276FskSetOpMode( RF_OPMODE_TRANSMITTER );
  453. RFState = RF_STATE_TX_READY_WAIT;
  454. TxBytesSent = 0;
  455. break;
  456. case RF_STATE_TX_READY_WAIT:
  457. if( DIO3 == 1 ) // TxReady
  458. {
  459. if( ( SX1276->RegPacketConfig1 & RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE ) == RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE )
  460. {
  461. SX1276WriteFifo( ( uint8_t* )&TxPacketSize, 1 );
  462. }
  463. if( ( TxPacketSize > 0 ) && ( TxPacketSize <= 64 ) )
  464. {
  465. DataChunkSize = TxPacketSize;
  466. }
  467. else
  468. {
  469. DataChunkSize = 32;
  470. }
  471. SX1276WriteFifo( RFBuffer, DataChunkSize );
  472. TxBytesSent += DataChunkSize;
  473. TxTimeoutTimer = GET_TICK_COUNT( );
  474. RFState = RF_STATE_TX_RUNNING;
  475. }
  476. break;
  477. case RF_STATE_TX_RUNNING:
  478. if( DIO1 == 0 ) // FifoLevel below thresold
  479. {
  480. if( ( TxPacketSize - TxBytesSent ) > DataChunkSize )
  481. {
  482. SX1276WriteFifo( ( RFBuffer + TxBytesSent ), DataChunkSize );
  483. TxBytesSent += DataChunkSize;
  484. }
  485. else
  486. {
  487. // we write the last chunk of data
  488. SX1276WriteFifo( RFBuffer + TxBytesSent, TxPacketSize - TxBytesSent );
  489. TxBytesSent += TxPacketSize - TxBytesSent;
  490. }
  491. }
  492. if( DIO0 == 1 ) // PacketSent
  493. {
  494. TxTimeoutTimer = GET_TICK_COUNT( );
  495. RFState = RF_STATE_TX_DONE;
  496. SX1276FskSetOpMode( RF_OPMODE_STANDBY );
  497. }
  498. // Packet timeout
  499. if( ( GET_TICK_COUNT( ) - TxTimeoutTimer ) > TICK_RATE_MS( 1000 ) )
  500. {
  501. RFState = RF_STATE_TX_TIMEOUT;
  502. }
  503. break;
  504. case RF_STATE_TX_DONE:
  505. RFState = RF_STATE_IDLE;
  506. result = RF_TX_DONE;
  507. break;
  508. case RF_STATE_TX_TIMEOUT:
  509. RFState = RF_STATE_IDLE;
  510. result = RF_TX_TIMEOUT;
  511. break;
  512. default:
  513. break;
  514. }
  515. return result;
  516. }
  517. #endif // USE_SX1276_RADIO