Author: jaguarondi
Date: 2008-08-28 18:10:34 +0200 (Thu, 28 Aug 2008)
New Revision: 1628

Modified:
   firmware/rf/trunk/main.c
   firmware/rf/trunk/rf_ctrl.c
   firmware/rf/trunk/rf_ctrl.h
Log:
* This patch adds the next channel in the RF frame so the receiver can update
  its table if it's not correct. This helps keeping the channels in sync when
  there's a lot of collisions.


Modified: firmware/rf/trunk/main.c
===================================================================
--- firmware/rf/trunk/main.c    2008-08-28 14:35:37 UTC (rev 1627)
+++ firmware/rf/trunk/main.c    2008-08-28 16:10:34 UTC (rev 1628)
@@ -112,33 +112,6 @@
             disconnected_flag = false;
             system_start_up();
         }
-#if defined(AFH)
-        uint8_t i;
-        for (i=0; i<AFH_RANGE; i++)
-        {
-            if (AFH_collision[i] > AFH_COLL_TRSH)
-            {
-                AFH_collision[i] = -AFH_COLL_TRSH;
-                AFH_freq[i] += CHANNEL_JUMP;
-                if (AFH_freq[i] > CHANNEL_MAX)
-                {
-                    AFH_freq[i] -= NUMBER_OF_CHANNELS;
-                }
-            }
-            if (AFH_prescaler == 0)
-            {
-                if (AFH_collision[i] > 0)
-                {
-                    AFH_collision[i]--;
-                }
-                else if (AFH_collision[i] < 0)
-                {
-                    AFH_collision[i]++;
-                }
-            }
-        }
-        if (AFH_prescaler == 0)
-            AFH_prescaler++;
-#endif
+        AFH_update();
     }
 }

Modified: firmware/rf/trunk/rf_ctrl.c
===================================================================
--- firmware/rf/trunk/rf_ctrl.c 2008-08-28 14:35:37 UTC (rev 1627)
+++ firmware/rf/trunk/rf_ctrl.c 2008-08-28 16:10:34 UTC (rev 1628)
@@ -29,7 +29,8 @@
 
 uint8_t checksum_tx;
 
-static void update_channel(void);
+static uint8_t next_channel;
+static uint8_t update_channel(uint8_t);
 static void reset_atr2406(void);
 static void stop_rf_timer(void);
 
@@ -75,6 +76,19 @@
             connected = CONNECTED_TIMEOUT;
             good_frame_received = true;
 
+            /* We should have received the next channel */
+#if defined(AFH)
+            uint8_t next_index = (AFH_index + 1) % AFH_RANGE;
+            /* We update channel if the other board already switched to the
+             * next one. This doesn't work when channels are 80 or above but as
+             * this is just an improvement, this shouldn't be too bad. */
+            if (AFH_freq[next_index] < next_channel &&
+                next_channel <= CHANNEL_MAX)
+            {
+                AFH_freq[next_index] = next_channel;
+                AFH_collision[next_index] = 0;
+            }
+#endif
             /* Switch rx buffer */
             uint8_t volatile *p = rf_buffer_rx;
             rf_buffer_rx = rf_buffer_rx_full;
@@ -101,8 +115,12 @@
         /* Configure in TX. */
         /* In frequency hopping, compute the next channel and save it in the RF
          * frame. */
-        update_channel();
+        channel = update_channel(channel);
         rf_buffer_tx[8] =  channel;
+#if defined(AFH)
+        /* Save next channel in the frame */
+        rf_buffer_tx[11] = AFH_freq[(AFH_index + 1) % AFH_RANGE];
+#endif
         init_atr2406(channel,TXEN);//configure ATR2406 to desired channel
         UCSR0A = 0x40;
         UCSR0B = 0x08;
@@ -151,7 +169,7 @@
         spi_request();
         /* In frequency hopping, compute the next channel and save it in the
          * SOF to be recognized. */
-        update_channel();
+        channel = update_channel(channel);
         sof_ary[0] = channel;
         rf_rx_state = 0x00;
         checksum = 0x00;
@@ -177,8 +195,38 @@
     rf_state = rf_state % 5;
 }
 
-static void update_channel(void)
+void AFH_update(void)
 {
+    uint8_t i;
+    for (i=0; i<AFH_RANGE; i++)
+    {
+        if (AFH_collision[i] > AFH_COLL_TRSH)
+        {
+            AFH_collision[i] = -AFH_COLL_TRSH;
+            AFH_freq[i] += CHANNEL_JUMP;
+            while (AFH_freq[i] > CHANNEL_MAX)
+            {
+                AFH_freq[i] -= NUMBER_OF_CHANNELS;
+            }
+        }
+        if (AFH_prescaler == 0)
+        {
+            if (AFH_collision[i] > 0)
+            {
+                AFH_collision[i]--;
+            }
+            else if (AFH_collision[i] < 0)
+            {
+                AFH_collision[i]++;
+            }
+        }
+    }
+    if (AFH_prescaler == 0)
+        AFH_prescaler++;
+}
+
+uint8_t update_channel(uint8_t channel)
+{
 #if defined(NOHOPP)
     channel = TRX_CHANNEL;
 #elif defined(HOPPING)
@@ -197,6 +245,7 @@
         channel = 0;
     }
 #endif
+    return channel;
 }
 
 /*
@@ -458,7 +507,7 @@
 
     data = UDR0 ^ SCRAMBLE_BYTE;
 
-    if(state==0x04)
+    if (state==0x04)
         /* Frame detected, receive data */
     {
         rf_buffer_rx[rf_rx_counter++] = data;
@@ -477,21 +526,24 @@
             clr_rxon();
         }
     }
+    else if (state==3)
+        /* Check next channel */
+    {
+        ++state;
+        /* Initialize the counter here */
+        rf_rx_counter = 0x00;
+#if defined(_SLAVE)
+        ts = TCNT1;
+        OCR1A = ts + T_SYNC - T_LATENCY;
+#endif
+        next_channel = data;
+    }
     else
         /* Check SOF */
     {
-        /* Initialize the counter here */
-        rf_rx_counter = 0x00;
-
         if(data == sof_ary[state])
         {
-            if(++state==0x04)
-            {
-#if defined(_SLAVE)
-                ts = TCNT1;
-                OCR1A = ts + T_SYNC - T_LATENCY;
-#endif
-            }
+            ++state;
         }
         else
         {

Modified: firmware/rf/trunk/rf_ctrl.h
===================================================================
--- firmware/rf/trunk/rf_ctrl.h 2008-08-28 14:35:37 UTC (rev 1627)
+++ firmware/rf/trunk/rf_ctrl.h 2008-08-28 16:10:34 UTC (rev 1628)
@@ -53,6 +53,7 @@
 extern void pwr_dwn_atr2406(void);
 extern void start_rf_timer(void);
 extern void start_rf_timer_scanning(void);
+extern void AFH_update(void);
 
 
 


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Tux-droid-svn mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tux-droid-svn

Reply via email to