Aloha implementation of LoRa technology

Dependencies:   SX1276Lib mbed

Fork of SX1276PingPong by Semtech

Revision:
15:f790f35839db
Parent:
14:c7251480feb9
Child:
16:c3c6b13c3c42
diff -r c7251480feb9 -r f790f35839db main.cpp
--- a/main.cpp	Wed Feb 17 09:28:29 2016 +0000
+++ b/main.cpp	Tue May 31 09:52:21 2016 +0000
@@ -2,58 +2,42 @@
 #include "main.h"
 #include "sx1276-hal.h"
 #include "debug.h"
+#include "Aloha.h"
+#include "AlohaPacket.h"
 
 /* Set this flag to '1' to display debug messages on the console */
-#define DEBUG_MESSAGE   0
-
-/* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
-#define USE_MODEM_LORA  1
-#define USE_MODEM_FSK   !USE_MODEM_LORA
+#define DEBUG_MESSAGE   1
 
 #define RF_FREQUENCY                                    868000000 // Hz
 #define TX_OUTPUT_POWER                                 14        // 14 dBm
-
-#if USE_MODEM_LORA == 1
+#define LORA_BANDWIDTH                              2         // [0: 125 kHz,
+                                                              //  1: 250 kHz,
+                                                              //  2: 500 kHz,
+                                                              //  3: Reserved]
+#define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
+#define LORA_CODINGRATE                             1         // [1: 4/5,
+                                                              //  2: 4/6,
+                                                              //  3: 4/7,
+                                                              //  4: 4/8]
+#define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
+#define LORA_SYMBOL_TIMEOUT                         5         // Symbols
+#define LORA_FIX_LENGTH_PAYLOAD_ON                  false
+#define LORA_FHSS_ENABLED                           false  
+#define LORA_NB_SYMB_HOP                            4     
+#define LORA_IQ_INVERSION_ON                        false
+#define LORA_CRC_ENABLED                            true
 
-    #define LORA_BANDWIDTH                              2         // [0: 125 kHz,
-                                                                  //  1: 250 kHz,
-                                                                  //  2: 500 kHz,
-                                                                  //  3: Reserved]
-    #define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
-    #define LORA_CODINGRATE                             1         // [1: 4/5,
-                                                                  //  2: 4/6,
-                                                                  //  3: 4/7,
-                                                                  //  4: 4/8]
-    #define LORA_PREAMBLE_LENGTH                        8         // Same for Tx and Rx
-    #define LORA_SYMBOL_TIMEOUT                         5         // Symbols
-    #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
-    #define LORA_FHSS_ENABLED                           false  
-    #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
-    #define FSK_DATARATE                                19200     // bps
-    #define FSK_BANDWIDTH                               50000     // Hz
-    #define FSK_AFC_BANDWIDTH                           83333     // Hz
-    #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
+#define BUFFER_SIZE                                     4 // Define the payload size here
 
-#if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
 DigitalOut led(LED2);
-#else
-DigitalOut led(LED1);
-#endif
+
+uint16_t BufferSize = BUFFER_SIZE;
+uint8_t Buffer[BUFFER_SIZE];
+
+int16_t RssiValue = 0.0;
+int8_t SnrValue = 0.0;
 
 /*
  *  Global variables declarations
@@ -76,6 +60,7 @@
 
 volatile AppStates_t State = LOWPOWER;
 
+
 /*!
  * Radio events function pointer
  */
@@ -86,22 +71,24 @@
  */
 SX1276MB1xAS Radio( NULL );
 
-const uint8_t PingMsg[] = "PING";
-const uint8_t PongMsg[] = "PONG";
-
-uint16_t BufferSize = BUFFER_SIZE;
-uint8_t Buffer[BUFFER_SIZE];
+// aloha protocol
+Aloha aloha;
 
-int16_t RssiValue = 0.0;
-int8_t SnrValue = 0.0;
-
-int main() 
+void setExpire()
 {
-    uint8_t i;
-    bool isMaster = true;
-    
-    debug( "\n\n\r     SX1276 Ping Pong Demo Application \n\n\r" );
+    if (aloha.attempts >= ALOHA_MAX_ATTEMPT)
+    {
+        aloha.state = Aloha::EXPIRED;
+    }
+    else
+    {
+        aloha.state = Aloha::RETRANSMIT;
+        aloha.delay = aloha.delay * aloha.delay;
+    }
+}
 
+void RadioInit()
+{
     // Initialize Radio driver
     RadioEvents.TxDone = OnTxDone;
     RadioEvents.RxDone = OnRxDone;
@@ -110,23 +97,18 @@
     RadioEvents.RxTimeout = OnRxTimeout;
     Radio.Init( &RadioEvents );
     
-    // verify the connection with the board
+    // detect radio hardware
     while( Radio.Read( REG_VERSION ) == 0x00  )
     {
-        debug( "Radio could not be detected!\n\r", NULL );
+        printf( "Radio could not be detected!\n\r" );
         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" );
+    printf("RadioRegVersion: %d\r\n", Radio.Read( REG_VERSION ));
     
+    // set radio frequency channel
     Radio.SetChannel( RF_FREQUENCY ); 
-
-#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");
-
+    // set radio parameter
     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                          LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
@@ -138,171 +120,76 @@
                          LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
                          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");
-    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, 2000000 );
+int main() 
+{  
+    RadioInit();
     
-    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" ); 
-        
-    led = 0;
-        
-    Radio.Rx( RX_TIMEOUT_VALUE );
-    
-    while( 1 )
+    while (1)
     {
         switch( State )
         {
         case RX:
-            if( isMaster == true )
+            // if the receive frame is an ack, then cancel the timer
+            // otherwise process the frame
+            HeaderStruct header;
+            memset(&header, 0, sizeof(header));
+            
+            if (header.fid == 0x01)
             {
-                if( BufferSize > 0 )
-                {
-                    if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
-                    {
-                        led = !led;
-                        debug( "...Pong\r\n" );
-                        // Send the next PING frame            
-                        strcpy( ( char* )Buffer, ( char* )PingMsg );
-                        // We fill the buffer with numbers for the payload 
-                        for( i = 4; i < BufferSize; i++ )
-                        {
-                            Buffer[i] = i - 4;
-                        }
-                        wait_ms( 10 ); 
-                        Radio.Send( Buffer, BufferSize );
-                    }
-                    else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
-                    { // A master already exists then become a slave
-                        debug( "...Ping\r\n" );
-                        led = !led;
-                        isMaster = false;
-                        // Send the next PONG frame            
-                        strcpy( ( char* )Buffer, ( char* )PongMsg );
-                        // We fill the buffer with numbers for the payload 
-                        for( i = 4; i < BufferSize; i++ )
-                        {
-                            Buffer[i] = i - 4;
-                        }
-                        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 );
-                    }    
-                }
+                aloha.AlohaAckTimeout.detach();
             }
-            else
-            {
-                if( BufferSize > 0 )
-                {
-                    if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
-                    {
-                        led = !led;
-                        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 
-                        for( i = 4; i < BufferSize; i++ )
-                        {
-                            Buffer[i] = i - 4;
-                        }
-                        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;
+            
+            // TODO: process the frame
             break;
-        case TX:    
-            led = !led; 
-            if( isMaster == true )  
-            {
-                debug( "Ping...\r\n" );
-            }
-            else
-            {
-                debug( "Pong...\r\n" );
-            }
-            Radio.Rx( RX_TIMEOUT_VALUE );
-            State = LOWPOWER;
+        case TX:
+            // transmit done
+            // then set state to aloha pending and start the timer
+            aloha.state = Aloha::PENDING;
+            aloha.delay = Radio.Random();
+            aloha.AlohaAckTimeout.detach();
+            aloha.AlohaAckTimeout.attach_us(&setExpire, aloha.delay);
+            aloha.attempts += 1;
             break;
         case RX_TIMEOUT:
-            if( isMaster == true )
-            {
-                // Send the next PING frame
-                strcpy( ( char* )Buffer, ( char* )PingMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 ); 
-                Radio.Send( Buffer, BufferSize );
-            }
-            else
-            {
-                Radio.Rx( RX_TIMEOUT_VALUE );  
-            }             
-            State = LOWPOWER;
+            // enter listening mode
             break;
         case RX_ERROR:
-            // We have received a Packet with a CRC error, send reply as if packet was correct
-            if( isMaster == true )
-            {
-                // Send the next PING frame
-                strcpy( ( char* )Buffer, ( char* )PingMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 );  
-                Radio.Send( Buffer, BufferSize );
-            }
-            else
-            {
-                // Send the next PONG frame
-                strcpy( ( char* )Buffer, ( char* )PongMsg );
-                for( i = 4; i < BufferSize; i++ )
-                {
-                    Buffer[i] = i - 4;
-                }
-                wait_ms( 10 );  
-                Radio.Send( Buffer, BufferSize );
-            }
-            State = LOWPOWER;
+            // we don't handle crc failed situation
             break;
         case TX_TIMEOUT:
-            Radio.Rx( RX_TIMEOUT_VALUE );
-            State = LOWPOWER;
+            // we don't handle hardware error
             break;
         case LOWPOWER:
             break;
         default:
-            State = LOWPOWER;
+            break;
+        }
+        
+        switch (aloha.state)
+        {
+        case Aloha::IDLE:
+            // transmit packet if any
+            break;
+        
+        case Aloha::PENDING:
+            // set rx time
             break;
-        }    
+            
+        case Aloha::RETRANSMIT:
+            // send the packet again
+            // and setup the timer with new backoff delay
+            aloha.AlohaAckTimeout.attach(&setExpire, aloha.delay);
+            aloha.state = Aloha::PENDING;
+            break;
+        case Aloha::EXPIRED:
+            // give up the transmission
+            // back to idle
+            aloha.state = Aloha::IDLE;
+            break;
+        }
     }
 }
 
@@ -310,7 +197,7 @@
 {
     Radio.Sleep( );
     State = TX;
-    debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
+    printf( "> OnTxDone\n\r" );
 }
 
 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
@@ -321,14 +208,14 @@
     RssiValue = rssi;
     SnrValue = snr;
     State = RX;
-    debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
+    printf( "> OnRxDone\n\r" );
 }
 
 void OnTxTimeout( void )
 {
     Radio.Sleep( );
     State = TX_TIMEOUT;
-    debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
+    printf( "> OnTxTimeout\n\r" );
 }
 
 void OnRxTimeout( void )
@@ -336,13 +223,12 @@
     Radio.Sleep( );
     Buffer[ BufferSize ] = 0;
     State = RX_TIMEOUT;
-    debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
+    printf( "> OnRxTimeout\n\r" );
 }
 
 void OnRxError( void )
 {
     Radio.Sleep( );
     State = RX_ERROR;
-    debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
+    printf( "> OnRxError\n\r" );
 }
-