Ivano Calabrese / ST-DEVKIT-LRWAN

Dependents:   DISCO-L072CZ-LRWAN1-base

Fork of SX1276GenericLib by Helmut Tschemernjak

Files at this revision

API Documentation at this revision

Comitter:
Helmut Tschemernjak
Date:
Mon May 01 18:56:35 2017 +0200
Parent:
32:f6f0bff38543
Child:
34:07e89f23c734
Commit message:
Added more typedefs for bandwidth, coding rate, data rate, etc.
This makes the maintenance and debugging much easier.

Changed in this revision

enums/enums.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/enums/enums.h	Mon May 01 18:00:12 2017 +0200
+++ b/enums/enums.h	Mon May 01 18:56:35 2017 +0200
@@ -42,6 +42,8 @@
 {
     SX1276MB1MAS = 0,
     SX1276MB1LAS,
+    RFM95_SX1276,
+    MURATA_SX1276,
     UNKNOWN
 }BoardType_t;
 
--- a/sx1276/sx1276.cpp	Mon May 01 18:00:12 2017 +0200
+++ b/sx1276/sx1276.cpp	Mon May 01 18:56:35 2017 +0200
@@ -268,12 +268,11 @@
         break;
     case MODEM_LORA:
         {
-            if( bandwidth > 2 )
+            if( bandwidth > LORA_BANKWIDTH_500kHz )
             {
                 // Fatal error: When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
                 while( 1 );
             }
-            bandwidth += 7;
             this->settings.LoRa.Bandwidth = bandwidth;
             this->settings.LoRa.Datarate = datarate;
             this->settings.LoRa.Coderate = coderate;
@@ -286,17 +285,17 @@
             this->settings.LoRa.IqInverted = iqInverted;
             this->settings.LoRa.RxContinuous = rxContinuous;
 
-            if( datarate > 12 )
+            if( datarate > LORA_SF12 )
             {
-                datarate = 12;
+                datarate = LORA_SF12;
             }
-            else if( datarate < 6 )
+            else if( datarate < LORA_SF6 )
             {
-                datarate = 6;
+                datarate = LORA_SF6;
             }
 
-            if( ( ( bandwidth == 7 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
-                ( ( bandwidth == 8 ) && ( datarate == 12 ) ) )
+            if( ( ( bandwidth == LORA_BANKWIDTH_125kHz ) && ( ( datarate == LORA_SF11 ) || ( datarate == LORA_SF12 ) ) ) ||
+                ( ( bandwidth == LORA_BANKWIDTH_250kHz ) && ( datarate == LORA_SF12 ) ) )
             {
                 this->settings.LoRa.LowDatarateOptimize = 0x01;
             }
@@ -342,13 +341,13 @@
                 Write( REG_LR_HOPPERIOD, this->settings.LoRa.HopPeriod );
             }
 
-			if( ( bandwidth == 9 ) && ( this->settings.Channel > RF_MID_BAND_THRESH ) )
+			if( ( bandwidth == LORA_BANKWIDTH_500kHz ) && ( this->settings.Channel > RF_MID_BAND_THRESH ) )
             {
                 // ERRATA 2.1 - Sensitivity Optimization with a 500 kHz Bandwidth
                 Write( REG_LR_TEST36, 0x02 );
                 Write( REG_LR_TEST3A, 0x64 );
             }
-            else if( bandwidth == 9 )
+            else if( bandwidth == LORA_BANKWIDTH_500kHz )
             {
                 // ERRATA 2.1 - Sensitivity Optimization with a 500 kHz Bandwidth
                 Write( REG_LR_TEST36, 0x02 );
@@ -360,7 +359,7 @@
                 Write( REG_LR_TEST36, 0x03 );
             }
 
-            if( datarate == 6 )
+            if( datarate == LORA_SF6 )
             {
 				Write( REG_LR_DETECTOPTIMIZE,
                              ( Read( REG_LR_DETECTOPTIMIZE ) &
@@ -429,12 +428,11 @@
     case MODEM_LORA:
         {
             this->settings.LoRa.Power = power;
-            if( bandwidth > 2 )
+            if( bandwidth > LORA_BANKWIDTH_500kHz )
             {
                 // Fatal error: When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
                 while( 1 );
             }
-            bandwidth += 7;
             this->settings.LoRa.Bandwidth = bandwidth;
             this->settings.LoRa.Datarate = datarate;
             this->settings.LoRa.Coderate = coderate;
@@ -446,16 +444,16 @@
             this->settings.LoRa.IqInverted = iqInverted;
             this->settings.LoRa.TxTimeout = timeout;
 
-            if( datarate > 12 )
+            if( datarate > LORA_SF12 )
             {
-                datarate = 12;
+                datarate = LORA_SF12;
             }
-            else if( datarate < 6 )
+            else if( datarate < LORA_SF6 )
             {
-                datarate = 6;
+                datarate = LORA_SF6;
             }
-            if( ( ( bandwidth == 7 ) && ( ( datarate == 11 ) || ( datarate == 12 ) ) ) ||
-                ( ( bandwidth == 8 ) && ( datarate == 12 ) ) )
+            if( ( ( bandwidth == LORA_BANKWIDTH_125kHz ) && ( ( datarate == LORA_SF11 ) || ( datarate == LORA_SF12 ) ) ) ||
+                ( ( bandwidth == LORA_BANKWIDTH_250kHz ) && ( datarate == LORA_SF12 ) ) )
             {
                 this->settings.LoRa.LowDatarateOptimize = 0x01;
             }
@@ -492,7 +490,7 @@
             Write( REG_LR_PREAMBLEMSB, ( preambleLen >> 8 ) & 0x00FF );
             Write( REG_LR_PREAMBLELSB, preambleLen & 0xFF );
 
-            if( datarate == 6 )
+            if( datarate == LORA_SF6 )
             {
                 Write( REG_LR_DETECTOPTIMIZE,
                              ( Read( REG_LR_DETECTOPTIMIZE ) &
@@ -538,34 +536,34 @@
             // REMARK: When using LoRa modem only bandwidths 125, 250 and 500 kHz are supported
             switch( this->settings.LoRa.Bandwidth )
             {
-            //case 0: // 7.8 kHz
-            //    bw = 78e2;
-            //    break;
-            //case 1: // 10.4 kHz
-            //    bw = 104e2;
-            //    break;
-            //case 2: // 15.6 kHz
-            //    bw = 156e2;
-            //    break;
-            //case 3: // 20.8 kHz
-            //    bw = 208e2;
-            //    break;
-            //case 4: // 31.2 kHz
-            //    bw = 312e2;
-            //    break;
-            //case 5: // 41.4 kHz
-            //    bw = 414e2;
-            //    break;
-            //case 6: // 62.5 kHz
-            //    bw = 625e2;
-            //    break;
-            case 7: // 125 kHz
+            case LORA_BANKWIDTH_7kHz: // 7.8 kHz
+                bw = 78e2;
+                break;
+            case LORA_BANKWIDTH_10kHz: // 10.4 kHz
+                bw = 104e2;
+                break;
+            case LORA_BANKWIDTH_15kHz: // 15.6 kHz
+                bw = 156e2;
+                break;
+            case LORA_BANKWIDTH_20kHz: // 20.8 kHz
+                bw = 208e2;
+                break;
+            case LORA_BANKWIDTH_31kHz: // 31.2 kHz
+        	    bw = 312e2;
+                break;
+            case LORA_BANKWIDTH_41kHz: // 41.4 kHz
+                bw = 414e2;
+                break;
+            case LORA_BANKWIDTH_62kHz: // 62.5 kHz
+                bw = 625e2;
+                break;
+            case LORA_BANKWIDTH_125kHz: // 125 kHz
                 bw = 125e3;
                 break;
-            case 8: // 250 kHz
+            case LORA_BANKWIDTH_250kHz: // 250 kHz
                 bw = 250e3;
                 break;
-            case 9: // 500 kHz
+            case LORA_BANKWIDTH_500kHz: // 500 kHz
                 bw = 500e3;
                 break;
             }
@@ -738,43 +736,43 @@
 			}
 
             // ERRATA 2.3 - Receiver Spurious Reception of a LoRa Signal
-            if( this->settings.LoRa.Bandwidth < 9 )
+            if( this->settings.LoRa.Bandwidth < LORA_BANKWIDTH_500kHz )
             {
                 Write( REG_LR_DETECTOPTIMIZE, Read( REG_LR_DETECTOPTIMIZE ) & 0x7F );
                 Write( REG_LR_TEST30, 0x00 );
                 switch( this->settings.LoRa.Bandwidth )
                 {
-                case 0: // 7.8 kHz
+                case LORA_BANKWIDTH_7kHz: // 7.8 kHz
                     Write( REG_LR_TEST2F, 0x48 );
                     SetChannel(this->settings.Channel + 7.81e3 );
                     break;
-                case 1: // 10.4 kHz
+                case LORA_BANKWIDTH_10kHz: // 10.4 kHz
                     Write( REG_LR_TEST2F, 0x44 );
                     SetChannel(this->settings.Channel + 10.42e3 );
                     break;
-                case 2: // 15.6 kHz
+                case LORA_BANKWIDTH_15kHz: // 15.6 kHz
                     Write( REG_LR_TEST2F, 0x44 );
                     SetChannel(this->settings.Channel + 15.62e3 );
                     break;
-                case 3: // 20.8 kHz
+                case LORA_BANKWIDTH_20kHz: // 20.8 kHz
                     Write( REG_LR_TEST2F, 0x44 );
                     SetChannel(this->settings.Channel + 20.83e3 );
                     break;
-                case 4: // 31.2 kHz
+                case LORA_BANKWIDTH_31kHz: // 31.2 kHz
                     Write( REG_LR_TEST2F, 0x44 );
                     SetChannel(this->settings.Channel + 31.25e3 );
                     break;
-                case 5: // 41.4 kHz
+                case LORA_BANKWIDTH_41kHz: // 41.4 kHz
                     Write( REG_LR_TEST2F, 0x44 );
                     SetChannel(this->settings.Channel + 41.67e3 );
                     break;
-                case 6: // 62.5 kHz
+                case LORA_BANKWIDTH_62kHz: // 62.5 kHz
                     Write( REG_LR_TEST2F, 0x40 );
                     break;
-                case 7: // 125 kHz
+                case LORA_BANKWIDTH_125kHz: // 125 kHz
                     Write( REG_LR_TEST2F, 0x40 );
                     break;
-                case 8: // 250 kHz
+                case LORA_BANKWIDTH_250kHz: // 250 kHz
                     Write( REG_LR_TEST2F, 0x40 );
                     break;
                 }
--- a/sx1276/sx1276.h	Mon May 01 18:00:12 2017 +0200
+++ b/sx1276/sx1276.h	Mon May 01 18:56:35 2017 +0200
@@ -58,7 +58,52 @@
  #define RFM95_MODULE		1	// RFM95 modules are SX1276MB1LAS compatible
 #endif
 
-/*! 
+typedef enum {
+    LORA_BANKWIDTH_7kHz  = 0, //  7.8 kHz requires TCXO
+    LORA_BANKWIDTH_10kHz = 1, // 10.4 kHz requires TCXO
+    LORA_BANKWIDTH_15kHz = 2, // 15.6 kHz requires TCXO
+    LORA_BANKWIDTH_20kHz = 3, // 20.8 kHz requires TCXO
+    LORA_BANKWIDTH_31kHz = 4, // 31.2 kHz requires TCXO
+	LORA_BANKWIDTH_41kHz = 5, // 41.4 kHz requires TCXO
+	LORA_BANKWIDTH_62kHz = 6, // 62.5 kHz requires TCXO
+    
+    LORA_BANKWIDTH_125kHz = 7,
+    LORA_BANKWIDTH_250kHz = 8,
+    LORA_BANKWIDTH_500kHz = 9,
+    LORA_BANKWIDTH_RESERVED = 10,
+} Lora_bandwidth_t;
+
+
+
+typedef enum {
+    LORA_SF6 =  6,  //   64 chips/symbol, SF6 requires an TCXO!
+    LORA_SF7 =  7,  //  128 chips/symbol
+    LORA_SF8 =  8,  //  256 chips/symbol
+    LORA_SF9 =  9,  //  512 chips/symbol
+    LORA_SF10 = 10, // 1024 chips/symbol
+    LORA_SF11 = 11, // 2048 chips/symbol
+    LORA_SF12 = 12, // 4096 chips/symbol
+} lora_spreading_factor_t;
+
+
+typedef enum {  // cyclic error coding to perform forward error detection and correction
+    LORA_ERROR_CODING_RATE_4_5 = 1,   // 1.25x overhead
+    LORA_ERROR_CODING_RATE_4_6 = 2,   // 1.50x overhead
+    LORA_ERROR_CODING_RATE_4_7 = 3,   // 1.75x overhead
+    LORA_ERROR_CODING_RATE_4_8 = 4,   // 2.00x overhead
+} lora_coding_rate_t;
+
+
+typedef enum {
+    RF_FREQUENCY_868_0 = 868000000, // Hz
+    RF_FREQUENCY_868_1 = 868100000, // Hz
+    RF_FREQUENCY_868_3 = 868300000, // Hz
+    RF_FREQUENCY_868_5 = 868500000, // Hz
+} rf_frequency_t;
+
+
+
+/*!
  * Actual implementation of a SX1276 radio, inherits Radio
  */
 class SX1276 : public Radio