SX1276 Ping Pong FHSS Demo Application

Dependencies:   SX1276Lib mbed

Ping-Pong demo application using FHSS (Frequency Hoping Spread Spectrum) between two SX1276MB1xAs demo board.

Application demonstrating simple Tx/Rx between two boards. By default, each board starts as a "master" and will transmit a "Ping" message, and then wait for an answer. The first board receiving a "Ping" message will become a slave and answer the "master" with a "Pong". The Ping-Pong is then started...

Revision:
6:235854d4b0c4
Parent:
4:cd44376a1408
diff -r b07ae0f2ae05 -r 235854d4b0c4 main.cpp
--- a/main.cpp	Thu Nov 26 17:01:16 2015 +0000
+++ b/main.cpp	Mon Apr 24 13:23:42 2017 +0000
@@ -31,7 +31,7 @@
     #define LORA_NB_SYMB_HOP                            4     
     #define LORA_IQ_INVERSION_ON                        false
     #define LORA_CRC_ENABLED                            true
-    
+
 #elif USE_MODEM_FSK == 1
 
     #define FSK_FDEV                                    25000     // Hz
@@ -41,18 +41,18 @@
     #define FSK_PREAMBLE_LENGTH                         5         // Same for Tx and Rx
     #define FSK_FIX_LENGTH_PAYLOAD_ON                   false
     #define FSK_CRC_ENABLED                             true
-    
+
 #else
     #error "Please define a modem in the compiler options."
 #endif
 
-#define RX_TIMEOUT_VALUE                                3500000   // in us
-#define BUFFER_SIZE                                     32        // Define the payload size here [min:1 max:255]
+#define RX_TIMEOUT_VALUE                                3500      // in ms
+#define BUFFER_SIZE                                     32        // Define the payload size here
 
 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
-DigitalOut led(LED2);
+DigitalOut led( LED2 );
 #else
-DigitalOut led(LED1);
+DigitalOut led( LED1 );
 #endif
 
 /*
@@ -62,14 +62,14 @@
 {
     LOWPOWER = 0,
     IDLE,
-    
+
     RX,
     RX_TIMEOUT,
     RX_ERROR,
-    
+
     TX,
     TX_TIMEOUT,
-    
+
     CAD,
     CAD_DONE
 }AppStates_t;
@@ -95,11 +95,11 @@
 int16_t RssiValue = 0.0;
 int8_t SnrValue = 0.0;
 
-int main() 
+int main( void ) 
 {
     uint8_t i;
     bool isMaster = true;
-    
+
     debug( "\n\n\r     SX1276 Ping Pong Demo Application \n\n\r" );
 
     // Initialize Radio driver
@@ -110,61 +110,61 @@
     RadioEvents.RxTimeout = OnRxTimeout;
     RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
     Radio.Init( &RadioEvents );
-    
+
     // verify the connection with the board
     while( Radio.Read( REG_VERSION ) == 0x00  )
     {
         debug( "Radio could not be detected!\n\r", NULL );
         wait( 1 );
     }
-            
-    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" );
-    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" );
-    
+
+    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r > Board Type: SX1276MB1LAS < \n\r" );
+    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r > Board Type: SX1276MB1MAS < \n\r" );
+
     Radio.SetChannel( HoppingFrequencies[0] ); 
 
 #if USE_MODEM_LORA == 1
-    
-    debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r");
-    debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r");
+
+    debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r" );
+    debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r" );
 
     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                          LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
-                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
-                         LORA_IQ_INVERSION_ON, 4000000 );
-    
+                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
+                         LORA_IQ_INVERSION_ON, 4000 );
+
     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
                          LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
                          LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
-                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, 
+                         LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
                          LORA_IQ_INVERSION_ON, true );
-                         
+
 #elif USE_MODEM_FSK == 1
 
-    debug("\n\n\r              > FSK Mode < \n\n\r");
+    debug("\n\n\r              > FSK Mode < \n\n\r" );
     Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
                          FSK_DATARATE, 0,
                          FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
-                         FSK_CRC_ENABLED, 0, 0, 0, 3000000 );
-    
+                         FSK_CRC_ENABLED, 0, 0, 0, 3000 );
+
     Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
                          0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
                          0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
                          0, 0, false, true );
-                         
+
 #else
 
 #error "Please define a modem in the compiler options."
 
 #endif
-     
-    debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); 
-        
+
+    debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
+
     led = 0;
-        
+
     Radio.Rx( RX_TIMEOUT_VALUE );
-    
+
     while( 1 )
     {
         switch( State )
@@ -178,14 +178,14 @@
                     {
                         led = !led;
                         debug( "...Pong\r\n" );
-                        // Send the next PING frame            
+                        // Send the next PING frame
                         strcpy( ( char* )Buffer, ( char* )PingMsg );
-                        // We fill the buffer with numbers for the payload 
+                        // We fill the buffer with numbers for the payload
                         for( i = 4; i < BufferSize; i++ )
                         {
                             Buffer[i] = i - 4;
                         }
-                        wait_ms( 10 ); 
+                        wait_ms( 10 );
                         Radio.Send( Buffer, BufferSize );
                     }
                     else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
@@ -193,21 +193,21 @@
                         debug( "...Ping\r\n" );
                         led = !led;
                         isMaster = false;
-                        // Send the next PONG frame            
+                        // Send the next PONG frame
                         strcpy( ( char* )Buffer, ( char* )PongMsg );
-                        // We fill the buffer with numbers for the payload 
+                        // We fill the buffer with numbers for the payload
                         for( i = 4; i < BufferSize; i++ )
                         {
                             Buffer[i] = i - 4;
                         }
-                        wait_ms( 10 ); 
+                        wait_ms( 10 );
                         Radio.Send( Buffer, BufferSize );
                     }
                     else // valid reception but neither a PING or a PONG message
                     {    // Set device as master ans start again
                         isMaster = true;
                         Radio.Rx( RX_TIMEOUT_VALUE );
-                    }    
+                    }
                 }
             }
             else
@@ -220,25 +220,25 @@
                         debug( "...Ping\r\n" );
                         // Send the reply to the PING string
                         strcpy( ( char* )Buffer, ( char* )PongMsg );
-                        // We fill the buffer with numbers for the payload 
+                        // We fill the buffer with numbers for the payload
                         for( i = 4; i < BufferSize; i++ )
                         {
                             Buffer[i] = i - 4;
                         }
-                        wait_ms( 10 );  
+                        wait_ms( 10 );
                         Radio.Send( Buffer, BufferSize );
                     }
                     else // valid reception but not a PING as expected
                     {    // Set device as master and start again
                         isMaster = true;
                         Radio.Rx( RX_TIMEOUT_VALUE );
-                    }    
+                    }
                 }
             }
             State = LOWPOWER;
             break;
-        case TX:    
-            led = !led; 
+        case TX:
+            led = !led;
             if( isMaster == true )  
             {
                 debug( "Ping...\r\n" );
@@ -259,13 +259,13 @@
                 {
                     Buffer[i] = i - 4;
                 }
-                wait_ms( 10 ); 
+                wait_ms( 10 );
                 Radio.Send( Buffer, BufferSize );
             }
             else
             {
-                Radio.Rx( RX_TIMEOUT_VALUE );  
-            }             
+                Radio.Rx( RX_TIMEOUT_VALUE );
+            }
             State = LOWPOWER;
             break;
         case RX_ERROR:
@@ -278,7 +278,7 @@
                 {
                     Buffer[i] = i - 4;
                 }
-                wait_ms( 10 );  
+                wait_ms( 10 );
                 Radio.Send( Buffer, BufferSize );
             }
             else
@@ -289,7 +289,7 @@
                 {
                     Buffer[i] = i - 4;
                 }
-                wait_ms( 10 );  
+                wait_ms( 10 );
                 Radio.Send( Buffer, BufferSize );
             }
             State = LOWPOWER;
@@ -303,7 +303,7 @@
         default:
             State = LOWPOWER;
             break;
-        }    
+        }
     }
 }
 
@@ -315,7 +315,7 @@
     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
 }
 
-void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
+void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
 {
     Radio.SetChannel( HoppingFrequencies[0] );
     Radio.Sleep( );
@@ -339,7 +339,7 @@
 {
     Radio.SetChannel( HoppingFrequencies[0] );
     Radio.Sleep( );
-    Buffer[ BufferSize ] = 0;
+    Buffer[BufferSize] = 0;
     State = RX_TIMEOUT;
     debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
 }
@@ -355,5 +355,5 @@
 void OnFhssChangeChannel( uint8_t channelIndex )
 {
     Radio.SetChannel( HoppingFrequencies[channelIndex] );
-    debug_if( DEBUG_MESSAGE, "F%d-", channelIndex);
+    debug_if( DEBUG_MESSAGE, "F%d-", channelIndex );
 }