]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tos/platforms/telosb/mac/tkn154/timer/Alarm32khzTo62500hzTransformC.nc
Merge TinyOS 2.1.1 into master.
[tinyos-2.x.git] / tos / platforms / telosb / mac / tkn154 / timer / Alarm32khzTo62500hzTransformC.nc
index c51d1122b316d3e0f634bf8d65019db3a5971687..e601285549487f9288a26f2e8b1e9e98afa16bd2 100644 (file)
@@ -1,17 +1,91 @@
-// here we introduce an error
-generic module Alarm32khzTo62500hzTransformC()
+/*
+ * Copyright (c) 2008, Technische Universitaet Berlin
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without 
+ * modification, are permitted provided that the following conditions 
+ * are met:
+ * - Redistributions of source code must retain the above copyright notice,
+ *   this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright 
+ *   notice, this list of conditions and the following disclaimer in the 
+ *   documentation and/or other materials provided with the distribution.
+ * - Neither the name of the Technische Universitaet Berlin nor the names 
+ *   of its contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
+ * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * - Revision -------------------------------------------------------------
+ * $Date$
+ * @author Jan Hauer <hauer@tkn.tu-berlin.de>
+ * ========================================================================
+ */
+
+module Alarm32khzTo62500hzTransformC
+
 {
-  provides interface Alarm<T62500hz,uint32_t> as Alarm;
-  uses interface Alarm<T32khz,uint32_t> as AlarmFrom;
+  provides interface Alarm<T62500hz,uint32_t> as Alarm[ uint8_t num ];
+  uses interface Alarm<T32khz,uint32_t> as AlarmFrom[ uint8_t num ];
 }
 implementation
 {
-  async command void Alarm.start(uint32_t dt){ call AlarmFrom.start(dt >> 1);}
-  async command void Alarm.stop(){ call AlarmFrom.stop();}
-  async event void AlarmFrom.fired(){ signal Alarm.fired();}
-  async command bool Alarm.isRunning(){ return call AlarmFrom.isRunning();}
-  async command void Alarm.startAt(uint32_t t0, uint32_t dt){ call AlarmFrom.startAt(t0 >> 1, dt >> 1);}
-  async command uint32_t Alarm.getNow(){ return call AlarmFrom.getNow() << 1;}
-  async command uint32_t Alarm.getAlarm(){ return call AlarmFrom.getAlarm() << 1;}
-  default async event void Alarm.fired(){}
+
+  /**
+   * TelosB lacks a clock that satisfies the precision and accuracy 
+   * requirements of the IEEE 802.15.4 standard (62500 Hz, +-40 ppm).
+   * As a workaround, we cast one tick of the 32768 Hz clock to two
+   * IEEE 802.15.4 symbols, which introduces a small (5%) error.
+   * As a consequence the timing of the beacon interval and slotted 
+   * CSMA-CA algorithm is not standard-compliant anymore.
+   */
+
+#warning "Warning: MAC timing is not standard compliant!"
+
+  async command void Alarm.start[ uint8_t num ](uint32_t dt){ call AlarmFrom.start[num](dt >> 1);}
+  async command void Alarm.stop[ uint8_t num ](){ call AlarmFrom.stop[num]();}
+  async event void AlarmFrom.fired[ uint8_t num ](){ signal Alarm.fired[num]();}
+  async command bool Alarm.isRunning[ uint8_t num ](){ return call AlarmFrom.isRunning[num]();}
+  async command uint32_t Alarm.getAlarm[ uint8_t num ](){ return call AlarmFrom.getAlarm[num]() << 1;}
+
+  async command uint32_t Alarm.getNow[ uint8_t num ](){ 
+    // this might shift out the most significant bit
+    // that's why Alarm.startAt() is converted to a Alarm.start()
+    return call AlarmFrom.getNow[num]() << 1; 
+  }
+
+  async command void Alarm.startAt[ uint8_t num ](uint32_t t0, uint32_t dt){
+    // t0 occured before "now"
+    atomic {
+      uint32_t now = call Alarm.getNow[num](), elapsed;
+      if (t0 < now)
+        elapsed = now - t0;
+      else
+        elapsed = ~(t0 - now) + 1;
+      if (elapsed > dt)
+        dt = elapsed;
+      dt -= elapsed;
+      call Alarm.start[num](dt);
+    }
+  }
+
+  /******************** Defaults ****************************/
+
+  default async command void AlarmFrom.start[ uint8_t num ](uint32_t dt){ }
+  default async command void AlarmFrom.stop[ uint8_t num ](){ }
+  default async command bool AlarmFrom.isRunning[ uint8_t num ](){ return FALSE;}
+  default async event void Alarm.fired[ uint8_t num ](){}
+  default async command void AlarmFrom.startAt[ uint8_t num ](uint32_t t0, uint32_t dt){ }
+  default async command uint32_t AlarmFrom.getNow[ uint8_t num ](){ return 0;}
+  default async command uint32_t AlarmFrom.getAlarm[ uint8_t num ](){ return 0;}
 }