]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/chips/rf2xx/rf212/RF212DriverLayerP.nc
minor cleanup
[tinyos-2.x.git] / tos / chips / rf2xx / rf212 / RF212DriverLayerP.nc
index 6950dd6f6f5edab572dc283a34817b7f89c39db2..218066ea714d07ea5a64b54c4eb21b5c870c42a6 100644 (file)
@@ -24,7 +24,7 @@
 #include <RF212DriverLayer.h>
 #include <Tasklet.h>
 #include <RadioAssert.h>
-#include <GenericTimeSyncMessage.h>
+#include <TimeSyncMessageLayer.h>
 #include <RadioConfig.h>
 
 module RF212DriverLayerP
@@ -38,6 +38,7 @@ module RF212DriverLayerP
                interface RadioSend;
                interface RadioReceive;
                interface RadioCCA;
+               interface RadioPacket;
 
                interface PacketField<uint8_t> as PacketTransmitPower;
                interface PacketField<uint8_t> as PacketRSSI;
@@ -60,9 +61,8 @@ module RF212DriverLayerP
                interface BusyWait<TMicro, uint16_t>;
                interface LocalTime<TRadio>;
 
-               interface RF212DriverConfig;
+               interface RF212DriverConfig as Config;
 
-               interface PacketData<rf212_metadata_t> as PacketRF212Metadata;
                interface PacketFlag as TransmitPowerFlag;
                interface PacketFlag as RSSIFlag;
                interface PacketFlag as TimeSyncFlag;
@@ -80,6 +80,21 @@ module RF212DriverLayerP
 
 implementation
 {
+       rf212_header_t* getHeader(message_t* msg)
+       {
+               return ((void*)msg) + call Config.headerLength(msg);
+       }
+
+       void* getPayload(message_t* msg)
+       {
+               return ((void*)msg) + call RadioPacket.headerLength(msg);
+       }
+
+       rf212_metadata_t* getMeta(message_t* msg)
+       {
+               return ((void*)msg) + sizeof(message_t) - call RadioPacket.metadataLength(msg);
+       }
+
 /*----------------- STATE -----------------*/
 
        tasklet_norace uint8_t state;
@@ -420,7 +435,7 @@ implementation
                        writeRegister(RF212_PHY_TX_PWR, txPower);
                }
 
-               if( call RF212DriverConfig.requiresRssiCca(msg) 
+               if( call Config.requiresRssiCca(msg) 
                                && (readRegister(RF212_PHY_RSSI) & RF212_RSSI_MASK) > ((rssiClear + rssiBusy) >> 3) )
                        return EBUSY;
 
@@ -428,7 +443,7 @@ implementation
 
                // do something useful, just to wait a little
                time32 = call LocalTime.get();
-               timesync = call PacketTimeSyncOffset.isSet(msg) ? msg->data + call PacketTimeSyncOffset.get(msg) : 0;
+               timesync = call PacketTimeSyncOffset.isSet(msg) ? ((void*)msg) + call PacketTimeSyncOffset.get(msg) : 0;
 
                // we have missed an incoming message in this short amount of time
                if( (readRegister(RF212_TRX_STATUS) & RF212_TRX_STATUS_MASK) != RF212_PLL_ON )
@@ -442,7 +457,7 @@ implementation
                atomic
                {
                        call SLP_TR.set();
-                       time = call RadioAlarm.getNow() + TX_SFD_DELAY;
+                       time = call RadioAlarm.getNow();
                }
                call SLP_TR.clr();
 
@@ -451,8 +466,8 @@ implementation
                call SELN.clr();
                call FastSpiByte.splitWrite(RF212_CMD_FRAME_WRITE);
 
-               length = call RF212DriverConfig.getLength(msg);
-               data = call RF212DriverConfig.getPayload(msg);
+               data = getPayload(msg);
+               length = getHeader(msg)->length;
 
                // length | data[0] ... data[length-3] | automatically generated FCS
                call FastSpiByte.splitReadWrite(length);
@@ -460,7 +475,7 @@ implementation
                // the FCS is atomatically generated (2 bytes)
                length -= 2;
 
-               header = call RF212DriverConfig.getHeaderLength();
+               header = call Config.headerPreloadLength();
                if( header > length )
                        header = length;
 
@@ -472,15 +487,13 @@ implementation
                }
                while( --header != 0 );
 
-               time32 += (int16_t)(time) - (int16_t)(time32);
+               time32 += (int16_t)(time + TX_SFD_DELAY) - (int16_t)(time32);
 
                if( timesync != 0 )
                        *(timesync_relative_t*)timesync = (*(timesync_absolute_t*)timesync) - time32;
 
-               do {
+               while( length-- != 0 )
                        call FastSpiByte.splitReadWrite(*(data++));
-               }
-               while( --length != 0 );
 
                // wait for the SPI transfer to finish
                call FastSpiByte.splitRead();
@@ -501,24 +514,25 @@ implementation
                // go back to RX_ON state when finished
                writeRegister(RF212_TRX_STATE, RF212_RX_ON);
 
+               if( timesync != 0 )
+                       *(timesync_absolute_t*)timesync = (*(timesync_relative_t*)timesync) + time32;
+
+               call PacketTimeStamp.set(msg, time32);
+
 #ifdef RADIO_DEBUG_MESSAGES
                if( call DiagMsg.record() )
                {
-                       length = call RF212DriverConfig.getLength(msg);
+                       length = getHeader(msg)->length;
 
-                       call DiagMsg.str("tx");
-                       call DiagMsg.uint16(time);
-                       call DiagMsg.uint8(length);
-                       call DiagMsg.hex8s(data, length - 2);
+                       call DiagMsg.str('t');
+                       call DiagMsg.uint32(call PacketTimeStamp.isValid(rxMsg) ? call PacketTimeStamp.timestamp(rxMsg) : 0);
+                       call DiagMsg.uint16(call RadioAlarm.getNow());
+                       call DiagMsg.int8(length);
+                       call DiagMsg.hex8s(getPayload(msg), length - 2);
                        call DiagMsg.send();
                }
 #endif
 
-               if( timesync != 0 )
-                       *(timesync_absolute_t*)timesync = (*(timesync_relative_t*)timesync) + time32;
-
-               call PacketTimeStamp.set(msg, time32);
-
                // wait for the TRX_END interrupt
                state = STATE_BUSY_TX_2_RX_ON;
                cmd = CMD_TRANSMIT;
@@ -554,7 +568,7 @@ implementation
        inline void downloadMessage()
        {
                uint8_t length;
-               uint8_t crc = 0;
+               bool crcValid = FALSE;
 
                call SELN.clr();
                call FastSpiByte.write(RF212_CMD_FRAME_READ);
@@ -563,7 +577,7 @@ implementation
                length = call FastSpiByte.write(0);
 
                // if correct length
-               if( length >= 3 && length <= call RF212DriverConfig.getMaxLength() )
+               if( length >= 3 && length <= call RadioPacket.maxPayloadLength() + 2 )
                {
                        uint8_t read;
                        uint8_t* data;
@@ -571,14 +585,13 @@ implementation
                        // initiate the reading
                        call FastSpiByte.splitWrite(0);
 
-                       call RF212DriverConfig.setLength(rxMsg, length);
-                       data = call RF212DriverConfig.getPayload(rxMsg);
-                       crc = 0;
+                       data = getPayload(rxMsg);
+                       getHeader(rxMsg)->length = length;
 
                        // we do not store the CRC field
                        length -= 2;
 
-                       read = call RF212DriverConfig.getHeaderLength();
+                       read = call Config.headerPreloadLength();
                        if( length < read )
                                read = length;
 
@@ -599,7 +612,7 @@ implementation
 
                                call PacketLinkQuality.set(rxMsg, call FastSpiByte.splitReadWrite(0));
                                call FastSpiByte.splitReadWrite(0);     // ED
-                               crc = call FastSpiByte.splitRead() & RF212_RX_CRC_VALID;        // RX_STATUS
+                               crcValid = call FastSpiByte.splitRead() & RF212_RX_CRC_VALID;   // RX_STATUS
                        }
                }
 
@@ -609,14 +622,15 @@ implementation
 #ifdef RADIO_DEBUG_MESSAGES
                if( call DiagMsg.record() )
                {
-                       length = call RF212DriverConfig.getLength(rxMsg);
+                       length = getHeader(rxMsg)->length;
 
-                       call DiagMsg.str("rx");
+                       call DiagMsg.str('r');
                        call DiagMsg.uint32(call PacketTimeStamp.isValid(rxMsg) ? call PacketTimeStamp.timestamp(rxMsg) : 0);
                        call DiagMsg.uint16(call RadioAlarm.getNow());
-                       call DiagMsg.uint8(crc == 0);
-                       call DiagMsg.uint8(length);
-                       call DiagMsg.hex8s(call RF212DriverConfig.getPayload(rxMsg), length - 2);
+                       call DiagMsg.int8(crcValid ? length : -length);
+                       call DiagMsg.hex8s(getPayload(rxMsg), length - 2);
+                       call DiagMsg.int8(call PacketRSSI.isSet(rxMsg) ? call PacketRSSI.get(rxMsg) : -1);
+                       call DiagMsg.uint8(call PacketLinkQuality.isSet(rxMsg) ? call PacketLinkQuality.get(rxMsg) : 0);
                        call DiagMsg.send();
                }
 #endif
@@ -624,7 +638,7 @@ implementation
                cmd = CMD_NONE;
 
                // signal only if it has passed the CRC check
-               if( crc != 0 )
+               if( crcValid )
                        rxMsg = signal RadioReceive.receive(rxMsg);
        }
 
@@ -674,6 +688,17 @@ implementation
                        }
 #endif
 
+#ifdef RF212_RSSI_ENERGY
+                       if( irq & RF212_IRQ_TRX_END )
+                       {
+                               if( irq == RF212_IRQ_TRX_END || 
+                                       (irq == (RF212_IRQ_RX_START | RF212_IRQ_TRX_END) && cmd == CMD_NONE) )
+                                       call PacketRSSI.set(rxMsg, readRegister(RF212_PHY_ED_LEVEL));
+                               else
+                                       call PacketRSSI.clear(rxMsg);
+                       }
+#endif
+
                        if( irq & RF212_IRQ_PLL_LOCK )
                        {
                                if( cmd == CMD_TURNON || cmd == CMD_CHANNEL )
@@ -756,12 +781,7 @@ implementation
                                else if( cmd == CMD_RECEIVE )
                                {
                                        ASSERT( state == STATE_RX_ON || state == STATE_PLL_ON_2_RX_ON );
-#ifdef RF212_RSSI_ENERGY
-                                       if( irq == RF212_IRQ_TRX_END )
-                                               call PacketRSSI.set(rxMsg, readRegister(RF212_PHY_ED_LEVEL));
-                                       else
-                                               call PacketRSSI.clear(rxMsg);
-#endif
+
                                        if( state == STATE_PLL_ON_2_RX_ON )
                                        {
                                                ASSERT( (readRegister(RF212_TRX_STATUS) & RF212_TRX_STATUS_MASK) == RF212_PLL_ON );
@@ -823,13 +843,45 @@ implementation
                        call SpiResource.release();
        }
 
-/*----------------- PACKET -----------------*/
+/*----------------- RadioPacket -----------------*/
+       
+       async command uint8_t RadioPacket.headerLength(message_t* msg)
+       {
+               return call Config.headerLength(msg) + sizeof(rf212_header_t);
+       }
+
+       async command uint8_t RadioPacket.payloadLength(message_t* msg)
+       {
+               return getHeader(msg)->length - 2;
+       }
+
+       async command void RadioPacket.setPayloadLength(message_t* msg, uint8_t length)
+       {
+               ASSERT( 1 <= length && length <= 125 );
+               ASSERT( call RadioPacket.headerLength(msg) + length + call RadioPacket.metadataLength(msg) <= sizeof(message_t) );
+
+               // we add the length of the CRC, which is automatically generated
+               getHeader(msg)->length = length + 2;
+       }
+
+       async command uint8_t RadioPacket.maxPayloadLength()
+       {
+               ASSERT( call Config.maxPayloadLength() <= 125 );
+
+               return call Config.maxPayloadLength() - sizeof(rf212_header_t);
+       }
 
-       async event void PacketRF212Metadata.clear(message_t* msg)
+       async command uint8_t RadioPacket.metadataLength(message_t* msg)
        {
+               return call Config.metadataLength(msg) + sizeof(rf212_metadata_t);
        }
 
-// --- TransmitPower
+       async command void RadioPacket.clear(message_t* msg)
+       {
+               // all flags are automatically cleared
+       }
+
+/*----------------- PacketTransmitPower -----------------*/
 
        async command bool PacketTransmitPower.isSet(message_t* msg)
        {
@@ -838,7 +890,7 @@ implementation
 
        async command uint8_t PacketTransmitPower.get(message_t* msg)
        {
-               return (call PacketRF212Metadata.get(msg))->power;
+               return getMeta(msg)->power;
        }
 
        async command void PacketTransmitPower.clear(message_t* msg)
@@ -849,10 +901,10 @@ implementation
        async command void PacketTransmitPower.set(message_t* msg, uint8_t value)
        {
                call TransmitPowerFlag.set(msg);
-               (call PacketRF212Metadata.get(msg))->power = value;
+               getMeta(msg)->power = value;
        }
 
-// --- RSSI
+/*----------------- PacketRSSI -----------------*/
 
        async command bool PacketRSSI.isSet(message_t* msg)
        {
@@ -861,7 +913,7 @@ implementation
 
        async command uint8_t PacketRSSI.get(message_t* msg)
        {
-               return (call PacketRF212Metadata.get(msg))->rssi;
+               return getMeta(msg)->rssi;
        }
 
        async command void PacketRSSI.clear(message_t* msg)
@@ -875,17 +927,10 @@ implementation
                call TransmitPowerFlag.clear(msg);
 
                call RSSIFlag.set(msg);
-               (call PacketRF212Metadata.get(msg))->rssi = value;
+               getMeta(msg)->rssi = value;
        }
 
-// --- TimeSyncOffset
-
-       enum
-       {
-               PACKET_LENGTH_INCREASE =
-                       sizeof(rf212packet_header_t) - 1        // the 8-bit length field is not counted
-                       + sizeof(ieee154_footer_t),             // the CRC is not stored in memory
-       };
+/*----------------- PacketTimeSyncOffset -----------------*/
 
        async command bool PacketTimeSyncOffset.isSet(message_t* msg)
        {
@@ -894,7 +939,7 @@ implementation
 
        async command uint8_t PacketTimeSyncOffset.get(message_t* msg)
        {
-               return call RF212DriverConfig.getLength(msg) - PACKET_LENGTH_INCREASE - sizeof(timesync_absolute_t);
+               return call RadioPacket.headerLength(msg) + call RadioPacket.payloadLength(msg) - sizeof(timesync_absolute_t);
        }
 
        async command void PacketTimeSyncOffset.clear(message_t* msg)
@@ -905,12 +950,12 @@ implementation
        async command void PacketTimeSyncOffset.set(message_t* msg, uint8_t value)
        {
                // we do not store the value, the time sync field is always the last 4 bytes
-               ASSERT( call RF212DriverConfig.getLength(msg) - PACKET_LENGTH_INCREASE - sizeof(timesync_absolute_t) == value );
+               ASSERT( call PacketTimeSyncOffset.get(msg) == value );
 
                call TimeSyncFlag.set(msg);
        }
 
-// --- LinkQuality
+/*----------------- PacketLinkQuality -----------------*/
 
        async command bool PacketLinkQuality.isSet(message_t* msg)
        {
@@ -919,7 +964,7 @@ implementation
 
        async command uint8_t PacketLinkQuality.get(message_t* msg)
        {
-               return (call PacketRF212Metadata.get(msg))->lqi;
+               return getMeta(msg)->lqi;
        }
 
        async command void PacketLinkQuality.clear(message_t* msg)
@@ -928,6 +973,6 @@ implementation
 
        async command void PacketLinkQuality.set(message_t* msg, uint8_t value)
        {
-               (call PacketRF212Metadata.get(msg))->lqi = value;
+               getMeta(msg)->lqi = value;
        }
 }