r g / SX1276GenericLib-us915

Dependents:   DISCO-L072CZ-LRWAN1_LoRa_PingPong

Fork of SX1276GenericLib by Helmut Tschemernjak

Files at this revision

API Documentation at this revision

Comitter:
Helmut Tschemernjak
Date:
Mon May 08 22:52:31 2017 +0200
Parent:
41:2dbc4afedf61
Child:
43:90de42f3c1fd
Child:
44:544add59b26d
Commit message:
Moved the timeout timer into the mbed-hal part to keep sx1276.cpp
independent of the OS.

Changed in this revision

LoRa_TODO.txt Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276-mbed-hal.cpp Show annotated file Show diff for this revision Revisions of this file
sx1276/sx1276-mbed-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
--- a/LoRa_TODO.txt	Mon May 08 15:39:42 2017 +0200
+++ b/LoRa_TODO.txt	Mon May 08 22:52:31 2017 +0200
@@ -12,7 +12,10 @@
   Rx/Tx parameters.
 - Add support for larger Lora packets (can be up to 2048 bytes)
   this feature is not so important, however the current implementation
-  is very basic.
+  is very basic.
+- It is a little bit strange that RX/TX/Cad Timeout Timer calling the
+  some handler OnTimeoutIrq. Maybe we just need a single timer, or 
+  it is a good idea to split the OnTimeoutIrq function
 
 
 
--- a/sx1276/sx1276-mbed-hal.cpp	Mon May 08 15:39:42 2017 +0200
+++ b/sx1276/sx1276-mbed-hal.cpp	Mon May 08 22:52:31 2017 +0200
@@ -347,9 +347,30 @@
     }
 }
 
-void SX1276Generic::SetTimeout(Timeout_t timer, int timeout_ms)
+void SX1276Generic::SetTimeout(TimeoutTimer_t timer, int timeout_ms)
 {
-    
+    SX1276 *sx = this;
+    switch(timer) {
+	    case RXTimeoutTimer:
+            if (timeout_ms)
+                rxTimeoutTimer.attach_us(callback(sx, &SX1276::OnTimeoutIrq), timeout_ms);
+            else
+                rxTimeoutTimer.detach();
+            break;
+        case TXTimeoutTimer:
+            if (timeout_ms)
+                txTimeoutTimer.attach_us(callback(sx, &SX1276::OnTimeoutIrq), timeout_ms);
+            else
+                txTimeoutTimer.detach();
+            break;
+        case RXTimeoutSyncWorldTimer:
+            if (timeout_ms)
+                rxTimeoutSyncWord.attach_us(callback(sx, &SX1276::OnTimeoutIrq), timeout_ms);
+            else
+                rxTimeoutSyncWord.detach();
+            break;
+        break;
+    }
 }
 
 bool SX1276Generic::CheckRfFrequency( uint32_t frequency )
--- a/sx1276/sx1276-mbed-hal.h	Mon May 08 15:39:42 2017 +0200
+++ b/sx1276/sx1276-mbed-hal.h	Mon May 08 22:52:31 2017 +0200
@@ -64,7 +64,12 @@
     InterruptIn *_dio4;
     DigitalIn *_dio5;
     
+    /*!
+     * Tx and Rx timers
+     */
     Timeout txTimeoutTimer;
+    Timeout rxTimeoutTimer;
+    Timeout rxTimeoutSyncWord;
     
     
 private:
@@ -156,7 +161,7 @@
     /*
      * The the Timeout for a given Timer.
      */
-    virtual void SetTimeout(Timeout_t timer, int timeout_ms);
+	virtual void SetTimeout(TimeoutTimer_t timer, int timeout_ms);
 
 public:
     
--- a/sx1276/sx1276.cpp	Mon May 08 15:39:42 2017 +0200
+++ b/sx1276/sx1276.cpp	Mon May 08 22:52:31 2017 +0200
@@ -709,8 +709,8 @@
 
 void SX1276::Sleep( void )
 {
-    txTimeoutTimer.detach( );
-    rxTimeoutTimer.detach( );
+    SetTimeout(TXTimeoutTimer, 0);
+    SetTimeout(RXTimeoutTimer, 0);
 
     SetOpMode( RF_OPMODE_SLEEP );
     this->settings.State = RF_IDLE;
@@ -718,8 +718,8 @@
 
 void SX1276::Standby( void )
 {
-    txTimeoutTimer.detach( );
-    rxTimeoutTimer.detach( );
+    SetTimeout(TXTimeoutTimer, 0);
+    SetTimeout(RXTimeoutTimer, 0);
 
     SetOpMode( RF_OPMODE_STANDBY );
     this->settings.State = RF_IDLE;
@@ -864,7 +864,7 @@
     this->settings.State = RF_RX_RUNNING;
     if( timeout != 0 )
     {
-        rxTimeoutTimer.attach_us(callback( this, &SX1276::OnTimeoutIrq ), timeout * 1e3 );
+        SetTimeout(RXTimeoutTimer, timeout * 1e3 );
     }
 
     if( this->settings.Modem == MODEM_FSK )
@@ -873,8 +873,7 @@
 
         if( rxContinuous == false )
         {
-            rxTimeoutSyncWord.attach_us(callback( this, &SX1276::OnTimeoutIrq ),
-                                        this->settings.Fsk.RxSingleTimeout * 1e3 );
+            SetTimeout(RXTimeoutSyncWorldTimer, this->settings.Fsk.RxSingleTimeout * 1e3);
         }
     }
     else
@@ -948,7 +947,7 @@
     }
 
     this->settings.State = RF_TX_RUNNING;
-    txTimeoutTimer.attach_us(callback( this, &SX1276::OnTimeoutIrq ), timeout * 1e3 );
+    SetTimeout(TXTimeoutTimer,  timeout * 1e3);
     SetOpMode( RF_OPMODE_TRANSMITTER );
 }
 
@@ -999,7 +998,7 @@
     Write( REG_DIOMAPPING2, RF_DIOMAPPING2_DIO4_10 | RF_DIOMAPPING2_DIO5_10 );
     
     this->settings.State = RF_TX_RUNNING;
-    txTimeoutTimer.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ), timeout );
+    SetTimeout(TXTimeoutTimer, timeout);
     SetOpMode( RF_OPMODE_TRANSMITTER );
 }
 
@@ -1137,13 +1136,12 @@
             {
                 // Continuous mode restart Rx chain
                 Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
-                rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ),
-                                            this->settings.Fsk.RxSingleTimeout * 1e3 );
+                SetTimeout(RXTimeoutSyncWorldTimer, this->settings.Fsk.RxSingleTimeout * 1e3);
             }
             else
             {
                 this->settings.State = RF_IDLE;
-                rxTimeoutSyncWord.detach( );
+                SetTimeout(RXTimeoutSyncWorldTimer, 0);
             }
         }
         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxTimeout != NULL ) )
@@ -1210,19 +1208,18 @@
                                                     RF_IRQFLAGS1_SYNCADDRESSMATCH );
                         Write( REG_IRQFLAGS2, RF_IRQFLAGS2_FIFOOVERRUN );
 
-                        rxTimeoutTimer.detach( );
+                        SetTimeout(RXTimeoutTimer, 0);
 
                         if( this->settings.Fsk.RxContinuous == false )
                         {
-                            rxTimeoutSyncWord.detach( );
+                            SetTimeout(RXTimeoutSyncWorldTimer, 0);
                             this->settings.State = RF_IDLE;
                         }
                         else
                         {
                             // Continuous mode restart Rx chain
                             Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
-                            rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ),
-                                                        this->settings.Fsk.RxSingleTimeout * 1e3 );
+                            SetTimeout(RXTimeoutSyncWorldTimer, this->settings.Fsk.RxSingleTimeout * 1e3);
                         }
 
                         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxError != NULL ) )
@@ -1257,19 +1254,18 @@
                     this->settings.FskPacketHandler.NbBytes += ( this->settings.FskPacketHandler.Size - this->settings.FskPacketHandler.NbBytes );
                 }
 
-                rxTimeoutTimer.detach( );
-
+                SetTimeout(RXTimeoutTimer, 0);
+                    
                 if( this->settings.Fsk.RxContinuous == false )
                 {
                     this->settings.State = RF_IDLE;
-                    rxTimeoutSyncWord.detach( );
+                    SetTimeout(RXTimeoutSyncWorldTimer, 0);
                 }
                 else
                 {
                     // Continuous mode restart Rx chain
                     Write( REG_RXCONFIG, Read( REG_RXCONFIG ) | RF_RXCONFIG_RESTARTRXWITHOUTPLLLOCK );
-                    rxTimeoutSyncWord.attach_us( mbed::callback( this, &SX1276::OnTimeoutIrq ),
-                                                this->settings.Fsk.RxSingleTimeout * 1e3 );
+                    SetTimeout(RXTimeoutSyncWorldTimer, this->settings.Fsk.RxSingleTimeout * 1e3);
 				}
 
                 if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxDone != NULL ) )
@@ -1298,8 +1294,8 @@
                         {
                             this->settings.State = RF_IDLE;
                         }
-                        rxTimeoutTimer.detach( );
-
+                        SetTimeout(RXTimeoutTimer, 0);
+                        
                         if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxError != NULL ) )
                         {
                             this->RadioEvents->RxError( );
@@ -1353,8 +1349,8 @@
                     {
                         this->settings.State = RF_IDLE;
                     }
-                    rxTimeoutTimer.detach( );
-
+                    SetTimeout(RXTimeoutTimer, 0);
+                    
                     if( ( this->RadioEvents != NULL ) && ( this->RadioEvents->RxDone != NULL ) )
                     {
                         this->RadioEvents->RxDone( rxtxBuffer, this->settings.LoRaPacketHandler.Size, this->settings.LoRaPacketHandler.RssiValue, this->settings.LoRaPacketHandler.SnrValue );
@@ -1366,7 +1362,7 @@
             }
             break;
         case RF_TX_RUNNING:
-			txTimeoutTimer.detach( );
+            SetTimeout(TXTimeoutTimer, 0);
             // TxDone interrupt
             switch( this->settings.Modem )
             {
@@ -1424,8 +1420,8 @@
                 break;
             case MODEM_LORA:
                 // Sync time out
-                rxTimeoutTimer.detach( );
-				// Clear Irq
+                SetTimeout(RXTimeoutTimer, 0);
+                // Clear Irq
 				Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_RXTIMEOUT );
 
                 this->settings.State = RF_IDLE;
@@ -1482,8 +1478,8 @@
 
                 if( ( this->settings.FskPacketHandler.PreambleDetected == true ) && ( this->settings.FskPacketHandler.SyncWordDetected == false ) )
                 {
-                    rxTimeoutSyncWord.detach( );
-
+                    SetTimeout(RXTimeoutSyncWorldTimer, 0);
+                    
                     this->settings.FskPacketHandler.SyncWordDetected = true;
 
                     this->settings.FskPacketHandler.RssiValue = -( Read( REG_RSSIVALUE ) >> 1 );
--- a/sx1276/sx1276.h	Mon May 08 15:39:42 2017 +0200
+++ b/sx1276/sx1276.h	Mon May 08 22:52:31 2017 +0200
@@ -83,12 +83,6 @@
 }RadioRegisters_t;
 
 
-typedef enum {
-    RXTimeout,
-    TXTimeout,
-    RXTimeoutSyncWorld
-} Timeout_t;
-
 /*!
  * Type of the supported board. [SX1276MB1MAS / SX1276MB1LAS]
  */
@@ -486,8 +480,14 @@
     //-------------------------------------------------------------------------
 
     static const RadioRegisters_t RadioRegsInit[];
-private:
-    
+
+    typedef enum {
+        RXTimeoutTimer,
+        TXTimeoutTimer,
+        RXTimeoutSyncWorldTimer
+    } TimeoutTimer_t;
+
+
 protected:
     /*!
      * @brief Initializes the radio I/Os pins interface
@@ -559,9 +559,8 @@
     /*
      * The the Timeout for a given Timer.
      */
-    virtual void SetTimeout(Timeout_t timer, int timeout_ms) = 0;
-    
-    
+    virtual void SetTimeout(TimeoutTimer_t timer, int timeout_ms) = 0;
+
 protected:
 
     /*!
@@ -606,17 +605,18 @@
     virtual void OnDio5Irq( void );
 
     /*!
-     * @brief Tx & Rx timeout timer callback
-     */
-    virtual void OnTimeoutIrq( void );
-
-    /*!
      * Returns the known FSK bandwidth registers value
      *
      * \param [IN] bandwidth Bandwidth value in Hz
      * \retval regValue Bandwidth register value.
      */
     static uint8_t GetFskBandwidthRegValue( uint32_t bandwidth );
+    
+public:
+    /*!
+     * @brief Tx & Rx timeout timer callback
+     */
+    virtual void OnTimeoutIrq( void );
 };
 
 #endif // __SX1276_H__