Basic MAC data interface for LoRa transceiver

Dependencies:   L2Frame crc

Dependents:   LoRaBaseStation LoRaTerminal

Revision:
40:271fa9e98589
Parent:
39:0da3ee6a297d
Child:
41:37c1d616a848
--- a/AlohaTransceiver.cpp	Wed Sep 14 07:47:18 2016 +0000
+++ b/AlohaTransceiver.cpp	Wed Sep 14 08:12:26 2016 +0000
@@ -6,7 +6,8 @@
 #include "RingBuffer.h"
 
 #define ALLOW_CALLBACK_DEBUG  1
-#define CSMA_CA_BACKOFF_TIME 1000 // in ms
+#define CSMA_BACKOFF_BASE 900 // in ms
+#define CSMA_BACKOFF_RANGE 500 // in ms
 #define CSMA_CA_CHANNEL_THRESHOLD -80 // in dbm
 
 #define SET_FLAG(t, x) (t) |= 1 << (x)
@@ -86,6 +87,9 @@
     // reset CSMA backoff state
     isBackoff = false;
     
+    // set default CSMA backoff period
+    CSMABackoffPeriod = 1000;
+    
     
     // configure properties
 #if USE_MODEM_LORA == 1
@@ -286,7 +290,7 @@
                     // perform CSMA backoff routine
                     if (isBackoff == true) // if the transceiver is already in the backoff state, then wait until timer expires
                     {
-                        if (CSMABackoffTimer.read_ms() > CSMA_CA_BACKOFF_TIME)
+                        if (CSMABackoffTimer.read_ms() > CSMABackoffPeriod)
                         {
                             isBackoff = false;
                             CSMABackoffTimer.stop();
@@ -355,10 +359,15 @@
                         }
                         else // if channel if not free, then start the timer, set the backoff state to true
                         {
+                            isBackoff = true;
+                            
+                            // generate random backoff delay
+                            CSMABackoffPeriod = Radio.Random() % CSMA_BACKOFF_RANGE + CSMA_BACKOFF_BASE;
+                            
 #ifdef DEBUG_ALOHA
-                            printf("CSMA/CA::Channel is not free, wait for 1s\r\n");
+                            printf("CSMA/CA::Channel is not free, wait for %d ms\r\n", CSMABackoffPeriod);
 #endif
-                            isBackoff = true;
+                            
                             CSMABackoffTimer.reset();
                             CSMABackoffTimer.start();
                         }