From aede9c82ea9e35ba71582bdee4075b8ca397dcbe Mon Sep 17 00:00:00 2001 From: mmaroti Date: Thu, 5 Jun 2008 20:44:21 +0000 Subject: [PATCH] RF230: timesync changes to lower memory usage --- tos/chips/rf230/RF230LayerP.nc | 25 ++++++++++-------- tos/chips/rf230/RF230Packet.h | 3 +-- tos/chips/rf230/RF230PacketP.nc | 3 ++- tos/chips/rf230/TimeSyncMessage.h | 9 ++++--- tos/chips/rf230/TimeSyncMessageP.nc | 39 ++++++++++++++--------------- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/tos/chips/rf230/RF230LayerP.nc b/tos/chips/rf230/RF230LayerP.nc index 85058a77..a6acb2b3 100644 --- a/tos/chips/rf230/RF230LayerP.nc +++ b/tos/chips/rf230/RF230LayerP.nc @@ -25,6 +25,7 @@ #include #include #include +#include module RF230LayerP { @@ -392,17 +393,11 @@ implementation uint8_t* data; uint8_t header; uint32_t time32; + void* timesync; if( cmd != CMD_NONE || state != STATE_RX_ON || ! isSpiAcquired() || radioIrq ) return EBUSY; - if( call RF230Config.requiresRssiCca(msg) - && (readRegister(RF230_PHY_RSSI) & RF230_RSSI_MASK) > ((rssiClear + rssiBusy) >> 3) ) - return EBUSY; - - writeRegister(RF230_TRX_STATE, RF230_PLL_ON); - - // do something useful, just to wait a little length = (call PacketTransmitPower.isSet(msg) ? call PacketTransmitPower.get(msg) : RF230_DEF_RFPOWER) & RF230_TX_PWR_MASK; @@ -412,7 +407,15 @@ implementation writeRegister(RF230_PHY_TX_PWR, RF230_TX_AUTO_CRC_ON | txPower); } + if( call RF230Config.requiresRssiCca(msg) + && (readRegister(RF230_PHY_RSSI) & RF230_RSSI_MASK) > ((rssiClear + rssiBusy) >> 3) ) + return EBUSY; + + writeRegister(RF230_TRX_STATE, RF230_PLL_ON); + + // do something useful, just to wait a little time32 = call LocalTime.get(); + timesync = call PacketTimeSyncOffset.isSet(msg) ? msg->data + call PacketTimeSyncOffset.get(msg) : 0; // we have missed an incoming message in this short amount of time if( (readRegister(RF230_TRX_STATUS) & RF230_TRX_STATUS_MASK) != RF230_PLL_ON ) @@ -458,8 +461,8 @@ implementation time32 += (int16_t)(time) - (int16_t)(time32); - if( call PacketTimeSyncOffset.isSet(msg) ) - ((timesync_footer_t*)(msg->data + call PacketTimeSyncOffset.get(msg)))->time_offset += time32; + if( timesync != 0 ) + *(timesync_relative_t*)timesync = (*(timesync_absolute_t*)timesync) - time32; do { call HplRF230.spiSplitReadWrite(*(data++)); @@ -498,8 +501,8 @@ implementation } #endif - if( call PacketTimeSyncOffset.isSet(msg) ) - ((timesync_footer_t*)(msg->data + call PacketTimeSyncOffset.get(msg)))->time_offset -= time32; + if( timesync != 0 ) + *(timesync_absolute_t*)timesync = (*(timesync_relative_t*)timesync) + time32; call PacketTimeStamp.set(msg, time32); diff --git a/tos/chips/rf230/RF230Packet.h b/tos/chips/rf230/RF230Packet.h index ab020b5e..7d952fe1 100644 --- a/tos/chips/rf230/RF230Packet.h +++ b/tos/chips/rf230/RF230Packet.h @@ -25,13 +25,12 @@ #define __RF230PACKET_H__ #include -#include typedef ieee154_header_t rf230packet_header_t; typedef nx_struct rf230packet_footer_t { - timesync_footer_t timesync; + // the time stamp is not recorded here, time stamped messaged cannot have max length } rf230packet_footer_t; typedef struct rf230packet_metadata_t diff --git a/tos/chips/rf230/RF230PacketP.nc b/tos/chips/rf230/RF230PacketP.nc index 6bfb2c2d..d2f2c150 100644 --- a/tos/chips/rf230/RF230PacketP.nc +++ b/tos/chips/rf230/RF230PacketP.nc @@ -22,6 +22,7 @@ */ #include +#include module RF230PacketP { @@ -250,7 +251,7 @@ implementation async command uint8_t PacketTimeSyncOffset.get(message_t* msg) { - return call IEEE154Packet.getLength(msg) - PACKET_LENGTH_INCREASE - sizeof(timesync_footer_t); + return call IEEE154Packet.getLength(msg) - PACKET_LENGTH_INCREASE - sizeof(timesync_absolute_t); } async command void PacketTimeSyncOffset.clear(message_t* msg) diff --git a/tos/chips/rf230/TimeSyncMessage.h b/tos/chips/rf230/TimeSyncMessage.h index 2ef2fb02..38fba022 100644 --- a/tos/chips/rf230/TimeSyncMessage.h +++ b/tos/chips/rf230/TimeSyncMessage.h @@ -24,9 +24,10 @@ #ifndef __TIMESYNCMESSAGE_H__ #define __TIMESYNCMESSAGE_H__ -typedef nx_struct timesync_footer_t -{ - nx_int32_t time_offset; // in micorsec -} timesync_footer_t; +// this value is sent in the air +typedef nx_int32_t timesync_relative_t; + +// this value is stored in memory +typedef uint32_t timesync_absolute_t; #endif//__TIMESYNCMESSAGE_H__ diff --git a/tos/chips/rf230/TimeSyncMessageP.nc b/tos/chips/rf230/TimeSyncMessageP.nc index a80906a6..a586b23a 100644 --- a/tos/chips/rf230/TimeSyncMessageP.nc +++ b/tos/chips/rf230/TimeSyncMessageP.nc @@ -54,10 +54,10 @@ module TimeSyncMessageP implementation { // TODO: change the Packet.payloadLength and Packet.maxPayloadLength commands to async - inline timesync_footer_t* getFooter(message_t* msg) + inline void* getFooter(message_t* msg) { - // we use the payload length we export (the smaller one) - return (timesync_footer_t*)(msg->data + call Packet.payloadLength(msg)); + // we use the payload length that we export (the smaller one) + return msg->data + call Packet.payloadLength(msg); } /*----------------- Packet -----------------*/ @@ -69,34 +69,33 @@ implementation command void Packet.setPayloadLength(message_t* msg, uint8_t len) { - call SubPacket.setPayloadLength(msg, len + sizeof(timesync_footer_t)); + call SubPacket.setPayloadLength(msg, len + sizeof(timesync_relative_t)); } command uint8_t Packet.payloadLength(message_t* msg) { - return call SubPacket.payloadLength(msg) - sizeof(timesync_footer_t); + return call SubPacket.payloadLength(msg) - sizeof(timesync_relative_t); } command uint8_t Packet.maxPayloadLength() { - return call SubPacket.maxPayloadLength() - sizeof(timesync_footer_t); + return call SubPacket.maxPayloadLength() - sizeof(timesync_relative_t); } command void* Packet.getPayload(message_t* msg, uint8_t len) { - return call SubPacket.getPayload(msg, len + sizeof(timesync_footer_t)); + return call SubPacket.getPayload(msg, len + sizeof(timesync_relative_t)); } /*----------------- TimeSyncAMSendRadio -----------------*/ command error_t TimeSyncAMSendRadio.send[am_id_t id](am_addr_t addr, message_t* msg, uint8_t len, uint32_t event_time) { - timesync_footer_t* footer = (timesync_footer_t*)(msg->data + len); - footer->time_offset = (nx_int32_t)event_time; + *(timesync_absolute_t*)(msg->data + len) = event_time; call PacketTimeSyncOffset.set(msg, len); - return call SubSend.send[id](addr, msg, len + sizeof(timesync_footer_t)); + return call SubSend.send[id](addr, msg, len + sizeof(timesync_relative_t)); } command error_t TimeSyncAMSendRadio.cancel[am_id_t id](message_t* msg) @@ -110,12 +109,12 @@ implementation command uint8_t TimeSyncAMSendRadio.maxPayloadLength[am_id_t id]() { - return call SubSend.maxPayloadLength[id]() - sizeof(timesync_footer_t); + return call SubSend.maxPayloadLength[id]() - sizeof(timesync_relative_t); } command void* TimeSyncAMSendRadio.getPayload[am_id_t id](message_t* msg, uint8_t len) { - return call SubSend.getPayload[id](msg, len + sizeof(timesync_footer_t)); + return call SubSend.getPayload[id](msg, len + sizeof(timesync_relative_t)); } /*----------------- TimeSyncAMSendMilli -----------------*/ @@ -159,31 +158,31 @@ implementation async command bool TimeSyncPacketRadio.isValid(message_t* msg) { - timesync_footer_t* footer = getFooter(msg); + timesync_relative_t* timesync = getFooter(msg); - return call PacketTimeStampRadio.isValid(msg) && footer->time_offset != 0x80000000L; + return call PacketTimeStampRadio.isValid(msg) && *timesync != 0x80000000L; } async command uint32_t TimeSyncPacketRadio.eventTime(message_t* msg) { - timesync_footer_t* footer = getFooter(msg); + timesync_relative_t* timesync = getFooter(msg); - return (int32_t)(footer->time_offset) + call PacketTimeStampRadio.timestamp(msg); + return (*timesync) + call PacketTimeStampRadio.timestamp(msg); } /*----------------- TimeSyncPacketMilli -----------------*/ async command bool TimeSyncPacketMilli.isValid(message_t* msg) { - timesync_footer_t* footer = getFooter(msg); + timesync_relative_t* timesync = getFooter(msg); - return call PacketTimeStampMilli.isValid(msg) && footer->time_offset != 0x80000000L; + return call PacketTimeStampMilli.isValid(msg) && *timesync != 0x80000000L; } async command uint32_t TimeSyncPacketMilli.eventTime(message_t* msg) { - timesync_footer_t* footer = getFooter(msg); + timesync_relative_t* timesync = getFooter(msg); - return ((int32_t)(footer->time_offset) << 10) + call PacketTimeStampMilli.timestamp(msg); + return ((int32_t)(*timesync) << 10) + call PacketTimeStampMilli.timestamp(msg); } } -- 2.39.2