1276

Dependents:   SX1276PingPong_RHF76-052_s

Fork of SX1276Lib by Semtech

Files at this revision

API Documentation at this revision

Comitter:
mluis
Date:
Fri May 13 15:09:10 2016 +0000
Parent:
26:79c5b50b2b9c
Commit message:
Synchronized with https://github.com/Lora-net/LoRaMac-node radio drivers git revision 55d16ca8949c09ee241c87b7600e2a8bc90d3743

Changed in this revision

sx1276/sx1276-hal.cpp Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276-hal.h Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276.cpp Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276.h Show annotated file Show diff for this revision Revisions of this file
diff -r 79c5b50b2b9c -r 3778e6204cc1 sx1276/sx1276-hal.cpp
--- a/sx1276/sx1276-hal.cpp	Tue Mar 15 13:12:44 2016 +0000
+++ b/sx1276/sx1276-hal.cpp	Fri May 13 15:09:10 2016 +0000
@@ -23,7 +23,7 @@
                             : SX1276( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 ),
                             antSwitch( antSwitch ),
                         #if( defined ( TARGET_NUCLEO_L152RE ) )
-                            fake( D8 ) 
+                            fake( D8 )
                         #else
                             fake( A3 )
                         #endif
@@ -31,15 +31,15 @@
     this->RadioEvents = events;
 
     Reset( );
-    
+
     RxChainCalibration( );
-    
+
     IoInit( );
-    
+
     SetOpMode( RF_OPMODE_SLEEP );
-    
+
     IoIrqInit( dioIrq );
-    
+
     RadioRegistersInit( );
 
     SetModem( MODEM_FSK );
@@ -47,7 +47,7 @@
     this->settings.State = RF_IDLE ;
 }
 
-SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events ) 
+SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events )
                         #if defined ( TARGET_NUCLEO_L152RE )
                         :   SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3
                             antSwitch( A4 ),
@@ -65,18 +65,18 @@
     this->RadioEvents = events;
 
     Reset( );
-    
+
     boardConnected = UNKNOWN;
-    
+
     DetectBoardType( );
-    
+
     RxChainCalibration( );
-    
+
     IoInit( );
-    
+
     SetOpMode( RF_OPMODE_SLEEP );
     IoIrqInit( dioIrq );
-    
+
     RadioRegistersInit( );
 
     SetModem( MODEM_FSK );
@@ -140,12 +140,12 @@
 
 void SX1276MB1xAS::IoIrqInit( DioIrqHandler *irqHandlers )
 {
-#if( defined ( TARGET_NUCLEO_L152RE ) ||  defined ( TARGET_LPC11U6X ) )
-    dio0.mode(PullDown);
-    dio1.mode(PullDown);   
-    dio2.mode(PullDown);
-    dio3.mode(PullDown); 
-    dio4.mode(PullDown); 
+#if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) )
+    dio0.mode( PullDown );
+    dio1.mode( PullDown );
+    dio2.mode( PullDown );
+    dio3.mode( PullDown );
+    dio4.mode( PullDown );
 #endif
     dio0.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[0] ) );
     dio1.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[1] ) );
@@ -207,14 +207,10 @@
 
 void SX1276MB1xAS::SetAntSw( uint8_t rxTx )
 {
-    if( this->rxTx == rxTx )
-    {
-        //no need to go further
-        return;
-    }
 
     this->rxTx = rxTx;
 
+    // 1: Tx, 0: Rx
     if( rxTx != 0 )
     {
         antSwitch = 1;
diff -r 79c5b50b2b9c -r 3778e6204cc1 sx1276/sx1276-hal.h
--- a/sx1276/sx1276-hal.h	Tue Mar 15 13:12:44 2016 +0000
+++ b/sx1276/sx1276-hal.h	Fri May 13 15:09:10 2016 +0000
@@ -26,6 +26,7 @@
     { MODEM_FSK , REG_LNA                , 0x23 },\
     { MODEM_FSK , REG_RXCONFIG           , 0x1E },\
     { MODEM_FSK , REG_RSSICONFIG         , 0xD2 },\
+    { MODEM_FSK , REG_AFCFEI             , 0x01 },\
     { MODEM_FSK , REG_PREAMBLEDETECT     , 0xAA },\
     { MODEM_FSK , REG_OSC                , 0x07 },\
     { MODEM_FSK , REG_SYNCCONFIG         , 0x12 },\
@@ -50,28 +51,28 @@
      * Antenna switch GPIO pins objects
      */
     DigitalInOut antSwitch;
-    
+
     DigitalIn fake;
-    
+
 private:
     static const RadioRegisters_t RadioRegsInit[];
-    
+
 public:
     SX1276MB1xAS( RadioEvents_t *events,
             PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
             PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5,
             PinName antSwitch ); 
-    
+
     SX1276MB1xAS( RadioEvents_t *events );
-    
+
     virtual ~SX1276MB1xAS( ) { };
-    
-    protected:
+
+protected:
     /*!
      * @brief Initializes the radio I/Os pins interface
      */
     virtual void IoInit( void );
-    
+
     /*!
      *  @brief Initializes the radio registers
      */
@@ -81,7 +82,7 @@
      * @brief Initializes the radio SPI
      */
     virtual void SpiInit( void );
-    
+
     /*!
      * @brief Initializes DIO IRQ handlers
      *
@@ -131,13 +132,13 @@
      * @param [IN] rxTx [1: Tx, 0: Rx]
      */
     virtual void SetAntSw( uint8_t rxTx );
-    
-    public:
+
+public:
     /*!
      * @brief Detect the board connected by reading the value of the antenna switch pin
      */
     virtual uint8_t DetectBoardType( void );    
-    
+
     /*!
      * @brief Checks if the given RF frequency is supported by the hardware
      *
@@ -145,15 +146,15 @@
      * @retval isSupported [true: supported, false: unsupported]
      */
     virtual bool CheckRfFrequency( uint32_t frequency );
-    
-        /*!
+
+    /*!
      * @brief Writes the radio register at the specified address
      *
      * @param [IN]: addr Register address
      * @param [IN]: data New register value
      */
     virtual void Write ( uint8_t addr, uint8_t data ) ;
-    
+
     /*!
      * @brief Reads the radio register at the specified address
      *
@@ -161,7 +162,7 @@
      * @retval data Register value
      */
     virtual uint8_t Read ( uint8_t addr ) ;
-    
+
     /*!
      * @brief Writes multiple radio registers starting at address
      *
@@ -170,7 +171,7 @@
      * @param [IN] size   Number of registers to be written
      */
     virtual void Write( uint8_t addr, uint8_t *buffer, uint8_t size ) ;
-    
+
     /*!
      * @brief Reads multiple radio registers starting at address
      *
@@ -179,7 +180,7 @@
      * @param [IN] size Number of registers to be read
      */
     virtual void Read ( uint8_t addr, uint8_t *buffer, uint8_t size ) ;
-    
+
     /*!
      * @brief Writes the buffer contents to the SX1276 FIFO
      *
@@ -195,7 +196,7 @@
      * @param [IN] size Number of bytes to be read from the FIFO
      */
     virtual void ReadFifo( uint8_t *buffer, uint8_t size ) ;
-    
+
     /*!
      * @brief Reset the SX1276
      */
diff -r 79c5b50b2b9c -r 3778e6204cc1 sx1276/sx1276.cpp
--- a/sx1276/sx1276.cpp	Tue Mar 15 13:12:44 2016 +0000
+++ b/sx1276/sx1276.cpp	Fri May 13 15:09:10 2016 +0000
@@ -54,7 +54,7 @@
     wait_ms( 10 );
     this->rxTx = 0;
     this->rxtxBuffer = new uint8_t[RX_BUFFER_SIZE];
-    previousOpMode = RF_OPMODE_STANDBY;
+    currentOpMode = RF_OPMODE_STANDBY;
     
     this->RadioEvents = events;
     
@@ -98,19 +98,19 @@
 bool SX1276::IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh )
 {
     int16_t rssi = 0;
-    
+
     SetModem( modem );
 
     SetChannel( freq );
-    
+
     SetOpMode( RF_OPMODE_RECEIVER );
 
     wait_ms( 1 );
-    
+
     rssi = GetRssi( modem );
-    
+
     Sleep( );
-    
+
     if( rssi > rssiThresh )
     {
         return false;
@@ -124,7 +124,7 @@
     uint32_t rnd = 0;
 
     /*
-     * Radio setup for random number generation 
+     * Radio setup for random number generation
      */
     // Set LoRa modem ON
     SetModem( MODEM_LORA );
@@ -182,7 +182,7 @@
     // Sets a Frequency in HF band
     SetChannel( 868000000 );
 
-    // Launch Rx chain calibration for HF band 
+    // Launch Rx chain calibration for HF band
     Write ( REG_IMAGECAL, ( Read( REG_IMAGECAL ) & RF_IMAGECAL_IMAGECAL_MASK ) | RF_IMAGECAL_IMAGECAL_START );
     while( ( Read( REG_IMAGECAL ) & RF_IMAGECAL_IMAGECAL_RUNNING ) == RF_IMAGECAL_IMAGECAL_RUNNING )
     {
@@ -237,7 +237,7 @@
             this->settings.Fsk.IqInverted = iqInverted;
             this->settings.Fsk.RxContinuous = rxContinuous;
             this->settings.Fsk.PreambleLen = preambleLen;
-            
+
             datarate = ( uint16_t )( ( double )XTAL_FREQ / ( double )datarate );
             Write( REG_BITRATEMSB, ( uint8_t )( datarate >> 8 ) );
             Write( REG_BITRATELSB, ( uint8_t )( datarate & 0xFF ) );
@@ -247,18 +247,18 @@
 
             Write( REG_PREAMBLEMSB, ( uint8_t )( ( preambleLen >> 8 ) & 0xFF ) );
             Write( REG_PREAMBLELSB, ( uint8_t )( preambleLen & 0xFF ) );
-            
+
             if( fixLen == 1 )
             {
                 Write( REG_PAYLOADLENGTH, payloadLen );
             }
             else
             {
-                Write( REG_PAYLOADLENGTH, 0xFF ); // Set payload length to the maximum 
+                Write( REG_PAYLOADLENGTH, 0xFF ); // Set payload length to the maximum
             }
             
             Write( REG_PACKETCONFIG1,
-                         ( Read( REG_PACKETCONFIG1 ) & 
+                         ( Read( REG_PACKETCONFIG1 ) &
                            RF_PACKETCONFIG1_CRC_MASK &
                            RF_PACKETCONFIG1_PACKETFORMAT_MASK ) |
                            ( ( fixLen == 1 ) ? RF_PACKETCONFIG1_PACKETFORMAT_FIXED : RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE ) |
@@ -293,7 +293,7 @@
             {
                 datarate = 6;
             }
-        
+
             if( ( ( bandwidth == 7 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
                 ( ( bandwidth == 8 ) && ( datarate == 12 ) ) )
             {
@@ -304,14 +304,14 @@
                 this->settings.LoRa.LowDatarateOptimize = 0x00;
             }
 
-            Write( REG_LR_MODEMCONFIG1, 
+            Write( REG_LR_MODEMCONFIG1,
                          ( Read( REG_LR_MODEMCONFIG1 ) &
                            RFLR_MODEMCONFIG1_BW_MASK &
                            RFLR_MODEMCONFIG1_CODINGRATE_MASK &
                            RFLR_MODEMCONFIG1_IMPLICITHEADER_MASK ) |
-                           ( bandwidth << 4 ) | ( coderate << 1 ) | 
+                           ( bandwidth << 4 ) | ( coderate << 1 ) |
                            fixLen );
-                        
+
             Write( REG_LR_MODEMCONFIG2,
                          ( Read( REG_LR_MODEMCONFIG2 ) &
                            RFLR_MODEMCONFIG2_SF_MASK &
@@ -320,13 +320,13 @@
                            ( datarate << 4 ) | ( crcOn << 2 ) |
                            ( ( symbTimeout >> 8 ) & ~RFLR_MODEMCONFIG2_SYMBTIMEOUTMSB_MASK ) );
 
-            Write( REG_LR_MODEMCONFIG3, 
+            Write( REG_LR_MODEMCONFIG3,
                          ( Read( REG_LR_MODEMCONFIG3 ) &
                            RFLR_MODEMCONFIG3_LOWDATARATEOPTIMIZE_MASK ) |
                            ( this->settings.LoRa.LowDatarateOptimize << 3 ) );
 
             Write( REG_LR_SYMBTIMEOUTLSB, ( uint8_t )( symbTimeout & 0xFF ) );
-            
+
             Write( REG_LR_PREAMBLEMSB, ( uint8_t )( ( preambleLen >> 8 ) & 0xFF ) );
             Write( REG_LR_PREAMBLELSB, ( uint8_t )( preambleLen & 0xFF ) );
 
@@ -343,7 +343,7 @@
 
             if( ( bandwidth == 9 ) && ( this->settings.Channel > RF_MID_BAND_THRESH ) )
             {
-                // ERRATA 2.1 - Sensitivity Optimization with a 500 kHz Bandwidth 
+                // ERRATA 2.1 - Sensitivity Optimization with a 500 kHz Bandwidth
                 Write( REG_LR_TEST36, 0x02 );
                 Write( REG_LR_TEST3A, 0x64 );
             }
@@ -358,14 +358,14 @@
                 // ERRATA 2.1 - Sensitivity Optimization with a 500 kHz Bandwidth
                 Write( REG_LR_TEST36, 0x03 );
             }
-            
+
             if( datarate == 6 )
             {
-                Write( REG_LR_DETECTOPTIMIZE, 
+                Write( REG_LR_DETECTOPTIMIZE,
                              ( Read( REG_LR_DETECTOPTIMIZE ) &
                                RFLR_DETECTIONOPTIMIZE_MASK ) |
                                RFLR_DETECTIONOPTIMIZE_SF6 );
-                Write( REG_LR_DETECTIONTHRESHOLD, 
+                Write( REG_LR_DETECTIONTHRESHOLD,
                              RFLR_DETECTIONTHRESH_SF6 );
             }
             else
@@ -374,7 +374,7 @@
                              ( Read( REG_LR_DETECTOPTIMIZE ) &
                              RFLR_DETECTIONOPTIMIZE_MASK ) |
                              RFLR_DETECTIONOPTIMIZE_SF7_TO_SF12 );
-                Write( REG_LR_DETECTIONTHRESHOLD, 
+                Write( REG_LR_DETECTIONTHRESHOLD,
                              RFLR_DETECTIONTHRESH_SF7_TO_SF12 );
             }
         }
@@ -382,17 +382,17 @@
     }
 }
 
-void SX1276::SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev, 
+void SX1276::SetTxConfig( RadioModems_t modem, int8_t power, uint32_t fdev,
                         uint32_t bandwidth, uint32_t datarate,
                         uint8_t coderate, uint16_t preambleLen,
-                        bool fixLen, bool crcOn, bool freqHopOn, 
+                        bool fixLen, bool crcOn, bool freqHopOn,
                         uint8_t hopPeriod, bool iqInverted, uint32_t timeout )
 {
     uint8_t paConfig = 0;
     uint8_t paDac = 0;
 
     SetModem( modem );
-    
+
     paConfig = Read( REG_PACONFIG );
     paDac = Read( REG_PADAC );
 
@@ -462,7 +462,7 @@
             this->settings.Fsk.CrcOn = crcOn;
             this->settings.Fsk.IqInverted = iqInverted;
             this->settings.Fsk.TxTimeout = timeout;
-            
+
             fdev = ( uint16_t )( ( double )fdev / ( double )FREQ_STEP );
             Write( REG_FDEVMSB, ( uint8_t )( fdev >> 8 ) );
             Write( REG_FDEVLSB, ( uint8_t )( fdev & 0xFF ) );
@@ -475,12 +475,12 @@
             Write( REG_PREAMBLELSB, preambleLen & 0xFF );
 
             Write( REG_PACKETCONFIG1,
-                         ( Read( REG_PACKETCONFIG1 ) & 
+                         ( Read( REG_PACKETCONFIG1 ) &
                            RF_PACKETCONFIG1_CRC_MASK &
                            RF_PACKETCONFIG1_PACKETFORMAT_MASK ) |
                            ( ( fixLen == 1 ) ? RF_PACKETCONFIG1_PACKETFORMAT_FIXED : RF_PACKETCONFIG1_PACKETFORMAT_VARIABLE ) |
                            ( crcOn << 4 ) );
-        
+
         }
         break;
     case MODEM_LORA:
@@ -527,12 +527,12 @@
                 Write( REG_LR_HOPPERIOD, this->settings.LoRa.HopPeriod );
             }
 
-            Write( REG_LR_MODEMCONFIG1, 
+            Write( REG_LR_MODEMCONFIG1,
                          ( Read( REG_LR_MODEMCONFIG1 ) &
                            RFLR_MODEMCONFIG1_BW_MASK &
                            RFLR_MODEMCONFIG1_CODINGRATE_MASK &
                            RFLR_MODEMCONFIG1_IMPLICITHEADER_MASK ) |
-                           ( bandwidth << 4 ) | ( coderate << 1 ) | 
+                           ( bandwidth << 4 ) | ( coderate << 1 ) |
                            fixLen );
 
             Write( REG_LR_MODEMCONFIG2,
@@ -541,21 +541,21 @@
                            RFLR_MODEMCONFIG2_RXPAYLOADCRC_MASK ) |
                            ( datarate << 4 ) | ( crcOn << 2 ) );
 
-            Write( REG_LR_MODEMCONFIG3, 
+            Write( REG_LR_MODEMCONFIG3,
                          ( Read( REG_LR_MODEMCONFIG3 ) &
                            RFLR_MODEMCONFIG3_LOWDATARATEOPTIMIZE_MASK ) |
                            ( this->settings.LoRa.LowDatarateOptimize << 3 ) );
-        
+
             Write( REG_LR_PREAMBLEMSB, ( preambleLen >> 8 ) & 0x00FF );
             Write( REG_LR_PREAMBLELSB, preambleLen & 0xFF );
-            
+
             if( datarate == 6 )
             {
-                Write( REG_LR_DETECTOPTIMIZE, 
+                Write( REG_LR_DETECTOPTIMIZE,
                              ( Read( REG_LR_DETECTOPTIMIZE ) &
                                RFLR_DETECTIONOPTIMIZE_MASK ) |
                                RFLR_DETECTIONOPTIMIZE_SF6 );
-                Write( REG_LR_DETECTIONTHRESHOLD, 
+                Write( REG_LR_DETECTIONTHRESHOLD,
                              RFLR_DETECTIONTHRESH_SF6 );
             }
             else
@@ -564,7 +564,7 @@
                              ( Read( REG_LR_DETECTOPTIMIZE ) &
                              RFLR_DETECTIONOPTIMIZE_MASK ) |
                              RFLR_DETECTIONOPTIMIZE_SF7_TO_SF12 );
-                Write( REG_LR_DETECTIONTHRESHOLD, 
+                Write( REG_LR_DETECTIONTHRESHOLD,
                              RFLR_DETECTIONTHRESH_SF7_TO_SF12 );
             }
         }
@@ -641,7 +641,7 @@
                                  ( this->settings.LoRa.Coderate + 4 );
             double nPayload = 8 + ( ( tmp > 0 ) ? tmp : 0 );
             double tPayload = nPayload * ts;
-            // Time on air 
+            // Time on air
             double tOnAir = tPreamble + tPayload;
             // return us secs
             airTime = floor( tOnAir * 1e6 + 0.999 );
@@ -669,8 +669,8 @@
             else
             {
                 Write( REG_PAYLOADLENGTH, size );
-            }            
-            
+            }
+
             if( ( size > 0 ) && ( size <= 64 ) )
             {
                 this->settings.FskPacketHandler.ChunkSize = size;
@@ -698,14 +698,14 @@
             {
                 Write( REG_LR_INVERTIQ, ( ( Read( REG_LR_INVERTIQ ) & RFLR_INVERTIQ_TX_MASK & RFLR_INVERTIQ_RX_MASK ) | RFLR_INVERTIQ_RX_OFF | RFLR_INVERTIQ_TX_OFF ) );
                 Write( REG_LR_INVERTIQ2, RFLR_INVERTIQ2_OFF );
-            }      
-        
+            }
+
             this->settings.LoRaPacketHandler.Size = size;
 
             // Initializes the payload size
             Write( REG_LR_PAYLOADLENGTH, size );
 
-            // Full buffer used for Tx            
+            // Full buffer used for Tx
             Write( REG_LR_FIFOTXBASEADDR, 0 );
             Write( REG_LR_FIFOADDRPTR, 0 );
 
@@ -752,7 +752,7 @@
     case MODEM_FSK:
         {
             rxContinuous = this->settings.Fsk.RxContinuous;
-            
+
             // DIO0=PayloadReady
             // DIO1=FifoLevel
             // DIO2=SyncAddr
@@ -763,15 +763,18 @@
                                                                             RF_DIOMAPPING1_DIO1_MASK &
                                                                             RF_DIOMAPPING1_DIO2_MASK ) |
                                                                             RF_DIOMAPPING1_DIO0_00 |
+                                                                            RF_DIOMAPPING1_DIO1_00 |
                                                                             RF_DIOMAPPING1_DIO2_11 );
-            
+
             Write( REG_DIOMAPPING2, ( Read( REG_DIOMAPPING2 ) & RF_DIOMAPPING2_DIO4_MASK &
-                                                                            RF_DIOMAPPING2_MAP_MASK ) | 
+                                                                            RF_DIOMAPPING2_MAP_MASK ) |
                                                                             RF_DIOMAPPING2_DIO4_11 |
                                                                             RF_DIOMAPPING2_MAP_PREAMBLEDETECT );
-            
+
             this->settings.FskPacketHandler.FifoThresh = Read( REG_FIFOTHRESH ) & 0x3F;
-            
+
+            Write( REG_RXCONFIG, RF_RXCONFIG_AFCAUTO_ON | RF_RXCONFIG_AGCAUTO_ON | RF_RXCONFIG_RXTRIGER_PREAMBLEDETECT );
+
             this->settings.FskPacketHandler.PreambleDetected = false;
             this->settings.FskPacketHandler.SyncWordDetected = false;
             this->settings.FskPacketHandler.NbBytes = 0;
@@ -789,8 +792,7 @@
             {
                 Write( REG_LR_INVERTIQ, ( ( Read( REG_LR_INVERTIQ ) & RFLR_INVERTIQ_TX_MASK & RFLR_INVERTIQ_RX_MASK ) | RFLR_INVERTIQ_RX_OFF | RFLR_INVERTIQ_TX_OFF ) );
                 Write( REG_LR_INVERTIQ2, RFLR_INVERTIQ2_OFF );
-            }         
-        
+            }
 
             // ERRATA 2.3 - Receiver Spurious Reception of a LoRa Signal
             if( this->settings.LoRa.Bandwidth < 9 )
@@ -840,7 +842,7 @@
             }
 
             rxContinuous = this->settings.LoRa.RxContinuous;
-            
+
             if( this->settings.LoRa.FreqHopOn == true )
             {
                 Write( REG_LR_IRQFLAGSMASK, //RFLR_IRQFLAGS_RXTIMEOUT |
@@ -851,7 +853,7 @@
                                                   RFLR_IRQFLAGS_CADDONE |
                                                   //RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
                                                   RFLR_IRQFLAGS_CADDETECTED );
-                                              
+
                 // DIO0=RxDone, DIO2=FhssChangeChannel
                 Write( REG_DIOMAPPING1, ( Read( REG_DIOMAPPING1 ) & RFLR_DIOMAPPING1_DIO0_MASK & RFLR_DIOMAPPING1_DIO2_MASK  ) | RFLR_DIOMAPPING1_DIO0_00 | RFLR_DIOMAPPING1_DIO2_00 );
             }
@@ -865,7 +867,7 @@
                                                   RFLR_IRQFLAGS_CADDONE |
                                                   RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
                                                   RFLR_IRQFLAGS_CADDETECTED );
-                                              
+
                 // DIO0=RxDone
                 Write( REG_DIOMAPPING1, ( Read( REG_DIOMAPPING1 ) & RFLR_DIOMAPPING1_DIO0_MASK ) | RFLR_DIOMAPPING1_DIO0_00 );
             }
@@ -886,14 +888,14 @@
     if( this->settings.Modem == MODEM_FSK )
     {
         SetOpMode( RF_OPMODE_RECEIVER );
-        
+
         if( rxContinuous == false )
         {
-            rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ( 8.0 * ( this->settings.Fsk.PreambleLen +
-                                                         ( ( Read( REG_SYNCCONFIG ) &
-                                                            ~RF_SYNCCONFIG_SYNCSIZE_MASK ) +
-                                                         1.0 ) + 10.0 ) /
-                                                        ( double )this->settings.Fsk.Datarate ) * 1e6 );
+            rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ceil( ( 8.0 * ( this->settings.Fsk.PreambleLen +
+                                                             ( ( Read( REG_SYNCCONFIG ) &
+                                                                ~RF_SYNCCONFIG_SYNCSIZE_MASK ) +
+                                                                1.0 ) + 10.0 ) /
+                                                             ( double )this->settings.Fsk.Datarate ) * 1e6 ) + 4000 );
         }
     }
     else
@@ -923,8 +925,9 @@
             // DIO4=LowBat
             // DIO5=ModeReady
             Write( REG_DIOMAPPING1, ( Read( REG_DIOMAPPING1 ) & RF_DIOMAPPING1_DIO0_MASK &
-                                                                RF_DIOMAPPING1_DIO2_MASK ) |
-                                                                RF_DIOMAPPING1_DIO1_01 );
+                                                                            RF_DIOMAPPING1_DIO1_MASK &
+                                                                            RF_DIOMAPPING1_DIO2_MASK ) |
+                                                                            RF_DIOMAPPING1_DIO1_01 );
 
             Write( REG_DIOMAPPING2, ( Read( REG_DIOMAPPING2 ) & RF_DIOMAPPING2_DIO4_MASK &
                                                                             RF_DIOMAPPING2_MAP_MASK ) );
@@ -943,7 +946,7 @@
                                                   RFLR_IRQFLAGS_CADDONE |
                                                   //RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL |
                                                   RFLR_IRQFLAGS_CADDETECTED );
-                                              
+
                 // DIO0=TxDone, DIO2=FhssChangeChannel
                 Write( REG_DIOMAPPING1, ( Read( REG_DIOMAPPING1 ) & RFLR_DIOMAPPING1_DIO0_MASK & RFLR_DIOMAPPING1_DIO2_MASK ) | RFLR_DIOMAPPING1_DIO0_01 | RFLR_DIOMAPPING1_DIO2_00 );
             }
@@ -976,7 +979,7 @@
     {
     case MODEM_FSK:
         {
-           
+
         }
         break;
     case MODEM_LORA:
@@ -988,12 +991,12 @@
                                         RFLR_IRQFLAGS_TXDONE |
                                         //RFLR_IRQFLAGS_CADDONE |
                                         RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL // |
-                                        //RFLR_IRQFLAGS_CADDETECTED 
+                                        //RFLR_IRQFLAGS_CADDETECTED
                                         );
-                                          
+
             // DIO3=CADDone
             Write( REG_DIOMAPPING1, ( Read( REG_DIOMAPPING1 ) & RFLR_DIOMAPPING1_DIO0_MASK ) | RFLR_DIOMAPPING1_DIO0_00 );
-            
+
             this->settings.State = RF_CAD;
             SetOpMode( RFLR_OPMODE_CAD );
         }
@@ -1031,27 +1034,23 @@
 
 void SX1276::SetOpMode( uint8_t opMode )
 {
-    if( opMode != previousOpMode )
+    if( opMode == RF_OPMODE_SLEEP )
     {
-        previousOpMode = opMode;
-        if( opMode == RF_OPMODE_SLEEP )
+        SetAntSwLowPower( true );
+    }
+    else
+    {
+        SetAntSwLowPower( false );
+        if( opMode == RF_OPMODE_TRANSMITTER )
         {
-            SetAntSwLowPower( true );
+            SetAntSw( 1 );
         }
         else
         {
-            SetAntSwLowPower( false );
-            if( opMode == RF_OPMODE_TRANSMITTER )
-            {
-                 SetAntSw( 1 );
-            }
-            else
-            {
-                 SetAntSw( 0 );
-            }
+            SetAntSw( 0 );
         }
-        Write( REG_OPMODE, ( Read( REG_OPMODE ) & RF_OPMODE_MASK ) | opMode );
     }
+    Write( REG_OPMODE, ( Read( REG_OPMODE ) & RF_OPMODE_MASK ) | opMode );
 }
 
 void SX1276::SetModem( RadioModems_t modem )
@@ -1113,7 +1112,7 @@
             this->settings.FskPacketHandler.Size = 0;
 
             // Clear Irqs
-            Write( REG_IRQFLAGS1, RF_IRQFLAGS1_RSSI | 
+            Write( REG_IRQFLAGS1, RF_IRQFLAGS1_RSSI |
                                         RF_IRQFLAGS1_PREAMBLEDETECT |
                                         RF_IRQFLAGS1_SYNCADDRESSMATCH );
             Write( REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN );
@@ -1122,6 +1121,11 @@
             {
                 // Continuous mode restart Rx chain
                 Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
+                rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ceil( ( 8.0 * ( this->settings.Fsk.PreambleLen +
+                                                             ( ( Read( REG_SYNCCONFIG ) &
+                                                                ~RF_SYNCCONFIG_SYNCSIZE_MASK ) +
+                                                             1.0 ) + 10.0 ) /
+                                                            ( double )this->settings.Fsk.Datarate ) * 1e6 ) + 4000 );
             }
             else
             {
@@ -1151,7 +1155,7 @@
     volatile uint8_t irqFlags = 0;
 
     switch( this->settings.State )
-    {                
+    {
         case RF_RX_RUNNING:
             //TimerStop( &RxTimeoutTimer );
             // RxDone interrupt
@@ -1164,27 +1168,30 @@
                     if( ( irqFlags & RF_IRQFLAGS2_CRCOK ) != RF_IRQFLAGS2_CRCOK )
                     {
                         // Clear Irqs
-                        Write( REG_IRQFLAGS1, RF_IRQFLAGS1_RSSI | 
+                        Write( REG_IRQFLAGS1, RF_IRQFLAGS1_RSSI |
                                                     RF_IRQFLAGS1_PREAMBLEDETECT |
                                                     RF_IRQFLAGS1_SYNCADDRESSMATCH );
                         Write( REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN );
-    
+
+                        rxTimeoutTimer.detach( );
+
                         if( this->settings.Fsk.RxContinuous == false )
                         {
+                            rxTimeoutSyncWord.detach( );
                             this->settings.State = RF_IDLE;
-                            rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, (  8.0 * ( this->settings.Fsk.PreambleLen +
-                                                             ( ( Read( REG_SYNCCONFIG ) &
-                                                                ~RF_SYNCCONFIG_SYNCSIZE_MASK ) +
-                                                             1.0 ) + 10.0 ) /
-                                                            ( double )this->settings.Fsk.Datarate ) * 1e6 ) ;
                         }
                         else
                         {
                             // Continuous mode restart Rx chain
                             Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
+                            rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ceil( ( 8.0 * ( this->settings.Fsk.PreambleLen +
+                                                                         ( ( Read( REG_SYNCCONFIG ) &
+                                                                            ~RF_SYNCCONFIG_SYNCSIZE_MASK ) +
+                                                                         1.0 ) + 10.0 ) /
+                                                                        ( double )this->settings.Fsk.Datarate ) * 1e6 ) + 4000 );
                         }
-                        rxTimeoutTimer.detach( );
-    
+
+
                         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxError != NULL ) )
                         {
                             this->RadioEvents->RxError( );
@@ -1196,7 +1203,7 @@
                         break;
                     }
                 }
-                
+
                 // Read received packet size
                 if( ( this->settings.FskPacketHandler.Size == 0 ) && ( this->settings.FskPacketHandler.NbBytes == 0 ) )
                 {
@@ -1217,26 +1224,28 @@
                     this->settings.FskPacketHandler.NbBytes += ( this->settings.FskPacketHandler.Size - this->settings.FskPacketHandler.NbBytes );
                 }
 
+                rxTimeoutTimer.detach( );
+
                 if( this->settings.Fsk.RxContinuous == false )
                 {
                     this->settings.State = RF_IDLE;
-                    rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ( 8.0 * ( this->settings.Fsk.PreambleLen +
-                                                         ( ( Read( REG_SYNCCONFIG ) &
-                                                            ~RF_SYNCCONFIG_SYNCSIZE_MASK ) +
-                                                         1.0 ) + 10.0 ) /
-                                                        ( double )this->settings.Fsk.Datarate ) * 1e6 ) ;
+                    rxTimeoutSyncWord.detach( );
                 }
                 else
                 {
                     // Continuous mode restart Rx chain
                     Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
+                    rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ceil( ( 8.0 * ( this->settings.Fsk.PreambleLen +
+                                                                 ( ( Read( REG_SYNCCONFIG ) &
+                                                                    ~RF_SYNCCONFIG_SYNCSIZE_MASK ) +
+                                                                 1.0 ) + 10.0 ) /
+                                                                ( double )this->settings.Fsk.Datarate ) * 1e6 ) + 4000 );
                 }
-                rxTimeoutTimer.detach( );
 
                 if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxDone != NULL ) )
                 {
-                    this->RadioEvents->RxDone( rxtxBuffer, this->settings.FskPacketHandler.Size, this->settings.FskPacketHandler.RssiValue, 0 ); 
-                } 
+                    this->RadioEvents->RxDone( rxtxBuffer, this->settings.FskPacketHandler.Size, this->settings.FskPacketHandler.RssiValue, 0 );
+                }
                 this->settings.FskPacketHandler.PreambleDetected = false;
                 this->settings.FskPacketHandler.SyncWordDetected = false;
                 this->settings.FskPacketHandler.NbBytes = 0;
@@ -1296,7 +1305,7 @@
                         }
                     }
                     else
-                    {    
+                    {
                         if( this->settings.Channel > RF_MID_BAND_THRESH )
                         {
                             this->settings.LoRaPacketHandler.RssiValue = RSSI_OFFSET_HF + rssi + ( rssi >> 4 );
@@ -1309,7 +1318,7 @@
 
                     this->settings.LoRaPacketHandler.Size = Read( REG_LR_RXNBBYTES );
                     ReadFifo( rxtxBuffer, this->settings.LoRaPacketHandler.Size );
-                
+
                     if( this->settings.LoRa.RxContinuous == false )
                     {
                         this->settings.State = RF_IDLE;
@@ -1327,7 +1336,7 @@
             }
             break;
         case RF_TX_RUNNING:
-            txTimeoutTimer.detach(  );
+            txTimeoutTimer.detach( );
             // TxDone interrupt
             switch( this->settings.Modem )
             {
@@ -1341,7 +1350,7 @@
                 if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->TxDone != NULL ) )
                 {
                     this->RadioEvents->TxDone( );
-                } 
+                }
                 break;
             }
             break;
@@ -1353,7 +1362,7 @@
 void SX1276::OnDio1Irq( void )
 {
     switch( this->settings.State )
-    {                
+    {
         case RF_RX_RUNNING:
             switch( this->settings.Modem )
             {
@@ -1400,13 +1409,13 @@
             switch( this->settings.Modem )
             {
             case MODEM_FSK:
-                // FifoLevel interrupt
+                // FifoEmpty interrupt
                 if( ( this->settings.FskPacketHandler.Size - this->settings.FskPacketHandler.NbBytes ) > this->settings.FskPacketHandler.ChunkSize )
                 {
                     WriteFifo( ( rxtxBuffer + this->settings.FskPacketHandler.NbBytes ), this->settings.FskPacketHandler.ChunkSize );
                     this->settings.FskPacketHandler.NbBytes += this->settings.FskPacketHandler.ChunkSize;
                 }
-                else 
+                else
                 {
                     // Write the last chunk of data
                     WriteFifo( rxtxBuffer + this->settings.FskPacketHandler.NbBytes, this->settings.FskPacketHandler.Size - this->settings.FskPacketHandler.NbBytes );
@@ -1427,7 +1436,7 @@
 void SX1276::OnDio2Irq( void )
 {
     switch( this->settings.State )
-    {                
+    {
         case RF_RX_RUNNING:
             switch( this->settings.Modem )
             {
@@ -1435,9 +1444,9 @@
                 if( ( this->settings.FskPacketHandler.PreambleDetected == true ) && ( this->settings.FskPacketHandler.SyncWordDetected == false ) )
                 {
                     rxTimeoutSyncWord.detach( );
-                    
+
                     this->settings.FskPacketHandler.SyncWordDetected = true;
-                
+
                     this->settings.FskPacketHandler.RssiValue = -( Read( REG_RSSIVALUE ) >> 1 );
 
                     this->settings.FskPacketHandler.AfcValue = ( int32_t )( double )( ( ( uint16_t )Read( REG_AFCMSB ) << 8 ) |
@@ -1451,7 +1460,7 @@
                 {
                     // Clear Irq
                     Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL );
-                    
+
                     if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->FhssChangeChannel != NULL ) )
                     {
                         this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) );
@@ -1472,7 +1481,7 @@
                 {
                     // Clear Irq
                     Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL );
-                    
+
                     if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->FhssChangeChannel != NULL ) )
                     {
                         this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) );
@@ -1505,7 +1514,7 @@
             }
         }
         else
-        {        
+        {
             // Clear Irq
             Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_CADDONE );
             if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->CadDone != NULL ) )
@@ -1528,7 +1537,7 @@
             if( this->settings.FskPacketHandler.PreambleDetected == false )
             {
                 this->settings.FskPacketHandler.PreambleDetected = true;
-            }    
+            }
         }
         break;
     case MODEM_LORA:
diff -r 79c5b50b2b9c -r 3778e6204cc1 sx1276/sx1276.h
--- a/sx1276/sx1276.h	Tue Mar 15 13:12:44 2016 +0000
+++ b/sx1276/sx1276.h	Fri May 13 15:09:10 2016 +0000
@@ -23,7 +23,18 @@
 /*!
  * Radio wakeup time from SLEEP mode
  */
-#define RADIO_WAKEUP_TIME                           1000 // [us]
+#define RADIO_OSC_STARTUP                           1000 // [us]
+
+/*!
+ * Radio PLL lock and Mode Ready delay which can vary with the temperature
+ */
+#define RADIO_SLEEP_TO_RX                           2000 // [us]
+
+/*!
+ * Radio complete Wake-up Time with margin for temperature compensation
+ */
+#define RADIO_WAKEUP_TIME                           ( RADIO_OSC_STARTUP + RADIO_SLEEP_TO_RX )
+
 
 /*!
  * SX1276 definitions
@@ -67,34 +78,34 @@
     InterruptIn dio3;
     InterruptIn dio4;
     DigitalIn dio5;
-    
+
     bool isRadioActive;
-    
+
     uint8_t boardConnected; //1 = SX1276MB1LAS; 0 = SX1276MB1MAS
-    
+
     uint8_t *rxtxBuffer;
     
-    uint8_t previousOpMode;
-    
+    uint8_t currentOpMode;
+
     /*!
      * Hardware DIO IRQ functions
      */
     DioIrqHandler *dioIrq;
-    
+
     /*!
      * Tx and Rx timers
      */
     Timeout txTimeoutTimer;
     Timeout rxTimeoutTimer;
     Timeout rxTimeoutSyncWord;
-    
+
     /*!
      *  rxTx: [1: Tx, 0: Rx]
      */
     uint8_t rxTx;
-    
+
     RadioSettings_t settings;
-    
+
     static const FskBandwidth_t FskBandwidths[] ;
 protected:
 
@@ -127,7 +138,7 @@
      * @param status Radio status. [RF_IDLE, RX_RUNNING, TX_RUNNING]
      */
     virtual RadioState GetStatus( void ); 
-    
+
     /*!
      * @brief Configures the SX1276 with the given modem
      *
@@ -141,7 +152,7 @@
      * @param [IN] freq         Channel RF frequency
      */
     virtual void SetChannel( uint32_t freq );
-    
+
     /*!
      * @brief Sets the channels configuration
      *
@@ -152,7 +163,7 @@
      * @retval isFree         [true: Channel is free, false: Channel is not free]
      */
     virtual bool IsChannelFree( RadioModems_t modem, uint32_t freq, int16_t rssiThresh );
-    
+
     /*!
      * @brief Generates a 32 bits random value based on the RSSI readings
      *
@@ -164,7 +175,7 @@
      * @retval randomValue    32 bits random value
      */
     virtual uint32_t Random( void );
-    
+
     /*!
      * @brief Sets the reception parameters
      *
@@ -207,7 +218,7 @@
                                uint8_t payloadLen,
                                bool crcOn, bool freqHopOn, uint8_t hopPeriod,
                                bool iqInverted, bool rxContinuous );
-    
+
     /*!
      * @brief Sets the transmission parameters
      *
@@ -242,7 +253,7 @@
                               uint8_t coderate, uint16_t preambleLen,
                               bool fixLen, bool crcOn, bool freqHopOn,
                               uint8_t hopPeriod, bool iqInverted, uint32_t timeout );
-    
+
     /*!
      * @brief Computes the packet time on air for the given payload
      *
@@ -254,7 +265,7 @@
      * @retval airTime        Computed airTime for the given packet payload length
      */
     virtual double TimeOnAir ( RadioModems_t modem, uint8_t pktLen );
-    
+
     /*!
      * @brief Sends the buffer of size. Prepares the packet to be sent and sets
      *        the radio in transmission
@@ -263,7 +274,7 @@
      * @param [IN]: size       Buffer size
      */
     virtual void Send( uint8_t *buffer, uint8_t size );
-    
+
     /*!
      * @brief Sets the radio in sleep mode
      */
@@ -273,33 +284,33 @@
      * @brief Sets the radio in standby mode
      */
     virtual void Standby( void );
-    
+
     /*!
      * @brief Sets the radio in reception mode for the given time
      * @param [IN] timeout Reception timeout [us]
      *                     [0: continuous, others timeout]
      */
     virtual void Rx( uint32_t timeout );
-    
+
     /*!
      * @brief Sets the radio in transmission mode for the given time
      * @param [IN] timeout Transmission timeout [us]
      *                     [0: continuous, others timeout]
      */
     virtual void Tx( uint32_t timeout );
-    
+
     /*!
      * @brief Start a Channel Activity Detection
      */
     virtual void StartCad( void );    
-    
+
     /*!
      * @brief Reads the current RSSI value
      *
      * @retval rssiValue Current RSSI value in [dBm]
      */
     virtual int16_t GetRssi ( RadioModems_t modem );
-    
+
     /*!
      * @brief Writes the radio register at the specified address
      *
@@ -307,7 +318,7 @@
      * @param [IN]: data New register value
      */
     virtual void Write ( uint8_t addr, uint8_t data ) = 0;
-    
+
     /*!
      * @brief Reads the radio register at the specified address
      *
@@ -315,7 +326,7 @@
      * @retval data Register value
      */
     virtual uint8_t Read ( uint8_t addr ) = 0;
-    
+
     /*!
      * @brief Writes multiple radio registers starting at address
      *
@@ -324,7 +335,7 @@
      * @param [IN] size   Number of registers to be written
      */
     virtual void Write( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
-    
+
     /*!
      * @brief Reads multiple radio registers starting at address
      *
@@ -333,7 +344,7 @@
      * @param [IN] size Number of registers to be read
      */
     virtual void Read ( uint8_t addr, uint8_t *buffer, uint8_t size ) = 0;
-    
+
     /*!
      * @brief Writes the buffer contents to the SX1276 FIFO
      *
@@ -353,7 +364,7 @@
      * @brief Resets the SX1276
      */
     virtual void Reset( void ) = 0;
-    
+
     /*!
      * @brief Sets the maximum payload length.
      *
@@ -361,11 +372,11 @@
      * @param [IN] max        Maximum payload length in bytes
      */
     virtual void SetMaxPayloadLength( RadioModems_t modem, uint8_t max );
-    
+
     //-------------------------------------------------------------------------
     //                        Board relative functions
     //-------------------------------------------------------------------------
-    
+
 protected:
     /*!
      * @brief Initializes the radio I/Os pins interface
@@ -381,7 +392,7 @@
      * @brief Initializes the radio SPI
      */
     virtual void SpiInit( void ) = 0;
-    
+
     /*!
      * @brief Initializes DIO IRQ handlers
      *
@@ -431,7 +442,7 @@
      * @param [IN] rxTx [1: Tx, 0: Rx]
      */
     virtual void SetAntSw( uint8_t rxTx ) = 0;
-    
+
     /*!
      * @brief Checks if the given RF frequency is supported by the hardware
      *
@@ -486,7 +497,7 @@
      * @brief Tx & Rx timeout timer callback
      */
     virtual void OnTimeoutIrq( void );
-    
+
     /*!
      * Returns the known FSK bandwidth registers value
      *