]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/tda5250/mac/CsmaMacP.nc
switch to metric MHz for SMCLK, adjust constants, include SmclkManager
[tinyos-2.x.git] / tos / chips / tda5250 / mac / CsmaMacP.nc
index e221db75eaf0d707a075139b0dfec113189ca5b6..e19e50d145dbd4f957d17994ebf4b046328c984a 100644 (file)
@@ -69,6 +69,8 @@ module CsmaMacP {
         interface Resource as RssiAdcResource;
 
         interface Random;
+
+        interface Timer<TMilli> as ReRxTimer;
         
         interface Alarm<T32khz, uint16_t> as Timer;
         async command am_addr_t amAddress();
@@ -85,26 +87,27 @@ implementation
 {
 
     enum {
-        BYTE_TIME=13,                // byte at 38400 kBit/s, 4b6b encoded
-        PREAMBLE_BYTE_TIME=9,        // byte at 38400 kBit/s, no coding
-        PHY_HEADER_TIME=35,          // 4 Phy Preamble at 38400
+        BYTE_TIME=21,                 // byte at 23405 kBit/s, 4b6b encoded
+        PREAMBLE_BYTE_TIME=14,        // byte at 23405 kBit/s, no coding
+        PHY_HEADER_TIME=84,           // 6 Phy Preamble at 23405 bits/s
+
         SUB_HEADER_TIME=PHY_HEADER_TIME + sizeof(tda5250_header_t)*BYTE_TIME,
-        SUB_FOOTER_TIME=2*BYTE_TIME, // 2 bytes crc 38400 kBit/s with 4b6b encoding
+        SUB_FOOTER_TIME=2*BYTE_TIME, // 2 bytes crc 
         MAXTIMERVALUE=0xFFFF,        // helps to compute backoff
         DATA_DETECT_TIME=17,
-        RX_SETUP_TIME=111,    // time to set up receiver
-        TX_SETUP_TIME=69,     // time to set up transmitter
+        RX_SETUP_TIME=102,    // time to set up receiver
+        TX_SETUP_TIME=58,     // time to set up transmitter
         ADDED_DELAY = 30,
-        RX_ACK_TIMEOUT=RX_SETUP_TIME + PHY_HEADER_TIME + 4 + 3*ADDED_DELAY,
-        TX_GAP_TIME=RX_ACK_TIMEOUT + TX_SETUP_TIME + 11,
-        MIN_BACKOFF_MASK=0x3F,   // about txrx_turnaround time
+        RX_ACK_TIMEOUT=RX_SETUP_TIME + PHY_HEADER_TIME + 2*ADDED_DELAY,
+        TX_GAP_TIME=RX_ACK_TIMEOUT + TX_SETUP_TIME + 33,
         MAX_SHORT_RETRY=7,
         MAX_LONG_RETRY=4,
+        BACKOFF_MASK=0xFFF,  // minimum time around one packet time
         MIN_PREAMBLE_BYTES=2,
         TOKEN_ACK_FLAG = 64,
         TOKEN_ACK_MASK = 0x3f,
         INVALID_SNR = 0xffff,
-        MSG_TABLE_ENTRIES=5,
+        MSG_TABLE_ENTRIES=20,
         MAX_AGE=2*MAX_LONG_RETRY*MAX_SHORT_RETRY,
     };
     
@@ -149,7 +152,6 @@ implementation
     uint8_t flags;
     uint8_t seqNo;
     
-    uint16_t slotMask;
     uint16_t restLaufzeit;
 
     /* duplicate suppression */
@@ -306,12 +308,13 @@ implementation
     }
 
     /**************** Helper functions ********/
+
+    task void postponeReRx() {
+        call ReRxTimer.startOneShot(5000);
+    }
+    
     uint16_t backoff(uint8_t counter) {
-        uint16_t mask = MIN_BACKOFF_MASK;
-        unsigned i;
-        for(i = 0; i < counter; i++) {
-            mask = (mask << 1) + 1;
-        }
+        uint16_t mask = BACKOFF_MASK >> (MAX_LONG_RETRY - counter);
         return (call Random.rand16() & mask);
     }
 
@@ -325,7 +328,10 @@ implementation
                 restLaufzeit = restLaufzeit - now;
             }
             else {
-                restLaufzeit +=  MAXTIMERVALUE - now;
+                restLaufzeit =  (uint16_t)(-1) - restLaufzeit + now;
+            }
+            if(restLaufzeit > BACKOFF_MASK) {
+                restLaufzeit = call Random.rand16() & 0xFF;
             }
             setFlag(&flags, RESUME_BACKOFF);
         }
@@ -485,7 +491,6 @@ implementation
             shortRetryCounter = 0;
             longRetryCounter = 0;
             flags = 0;
-            slotMask = MIN_BACKOFF_MASK;
             for(i = 0; i < MSG_TABLE_ENTRIES; i++) {
                 knownMsgTable[i].age = MAX_AGE;
             }
@@ -578,6 +583,7 @@ implementation
     }
 
     async event void RadioModes.RxModeDone() {
+        post postponeReRx();
         atomic {
             if(macState == SW_RX) {
                 storeOldState(21);
@@ -606,6 +612,7 @@ implementation
     }
 
     async event void RadioModes.TxModeDone() {
+        post postponeReRx();
         atomic {
             if(macState == SW_TX) {
                 storeOldState(30);
@@ -680,7 +687,7 @@ implementation
     
     /****** PacketSerializer events **********************/
     async event void PacketReceive.receiveDetected() {
-      if(macState <= RX_ACK) {
+        if(macState <= RX_ACK) {
             storeOldState(60);
             interruptBackoffTimer();
             if(macState == CCA) computeBackoff();
@@ -950,15 +957,29 @@ implementation
     
     async event void ChannelMonitorData.getSnrDone(int16_t data) {
     }
-
     
     /***** unused Radio Modes events **************************/
     
     async event void RadioModes.TimerModeDone() {}
-    async event void RadioModes.SleepModeDone() {}
+
+    async event void RadioModes.SleepModeDone() {
+        atomic setRxMode();
+    }
+    
     async event void RadioModes.SelfPollingModeDone() {}
     async event void RadioModes.PWDDDInterrupt() {}
 
+    event void ReRxTimer.fired() {
+        atomic {
+            if((macState == RX) && (call RadioModes.SleepMode() == SUCCESS)) {
+                // ok 
+            }
+            else {
+                post postponeReRx();
+            }
+        }
+    }
+    
     /***** abused TimeStamping events **************************/
     async event void RadioTimeStamping.receivedSFD( uint16_t time ) {
         if(macState == RX_P) call ChannelMonitor.rxSuccess();