]> oss.titaniummirror.com Git - tinyos-2.x.git/commitdiff
Shift timer power state override into McuPowerOverride call.
authorscipio <scipio>
Thu, 14 Dec 2006 01:24:48 +0000 (01:24 +0000)
committerscipio <scipio>
Thu, 14 Dec 2006 01:24:48 +0000 (01:24 +0000)
Introduce a configuration to autowire the power override call
(move module to AsyncP).

tos/chips/atm128/McuSleepC.nc
tos/chips/atm128/timer/HplAtm128Timer0AsyncC.nc
tos/chips/atm128/timer/HplAtm128Timer0AsyncP.nc [new file with mode: 0644]

index 3f358a27a657e8b66a6b511c1ef41df3fe61a308..6418f739e869182a5d013d5a812a9db2b419ce29 100644 (file)
@@ -64,7 +64,6 @@ implementation {
     (1 << SM1)};               /* power down */
 
   mcu_power_t getPowerState() {
     (1 << SM1)};               /* power down */
 
   mcu_power_t getPowerState() {
-    uint8_t diff;
     // Note: we go to sleep even if timer 1, 2, or 3's overflow interrupt
     // is enabled - this allows using these timers as TinyOS "Alarm"s
     // while still having power management.
     // Note: we go to sleep even if timer 1, 2, or 3's overflow interrupt
     // is enabled - this allows using these timers as TinyOS "Alarm"s
     // while still having power management.
@@ -89,20 +88,6 @@ implementation {
     else if (bit_is_set(ADCSR, ADEN)) { 
       return ATM128_POWER_ADC_NR;
     }
     else if (bit_is_set(ADCSR, ADEN)) { 
       return ATM128_POWER_ADC_NR;
     }
-    // How soon for the timer to go off?
-    else if (TIMSK & (1 << OCIE0 | 1 << TOIE0)) {
-      // need to wait for timer 0 updates propagate before sleeping
-      // (we don't need to worry about reentering sleep mode too early,
-      // as the wake ups from timer0 wait at least one TOSC1 cycle
-      // anyway - see the stabiliseTimer0 function in HplAtm128Timer0AsyncC)
-      while (ASSR & (1 << TCN0UB | 1 << OCR0UB | 1 << TCR0UB))
-       ;
-      diff = OCR0 - TCNT0;
-      if (diff < EXT_STANDBY_T0_THRESHOLD ||
-         TCNT0 > 256 - EXT_STANDBY_T0_THRESHOLD) 
-       return ATM128_POWER_EXT_STANDBY;
-      return ATM128_POWER_SAVE;
-    }
     else {
       return ATM128_POWER_DOWN;
     }
     else {
       return ATM128_POWER_DOWN;
     }
index 4faef35f2cfebbc76bbf42895e04372a18f475b7..cd610a3c51e7104ec725dd6e470b54d9808825d9 100644 (file)
@@ -1,40 +1,45 @@
 /// $Id$
 /// $Id$
-
 /*
 /*
- * Copyright (c) 2004-2005 Crossbow Technology, Inc.  All rights reserved.
+ * Copyright (c) 2006 Stanford University.
+ * All rights reserved.
  *
  *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose, without fee, and without written agreement is
- * hereby granted, provided that the above copyright notice, the following
- * two paragraphs and the author appear in all copies of this software.
- * 
- * IN NO EVENT SHALL CROSSBOW TECHNOLOGY OR ANY OF ITS LICENSORS BE LIABLE TO 
- * ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL 
- * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
- * IF CROSSBOW OR ITS LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
- * DAMAGE. 
+ * 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 Stanford University nor the names of
+ *   its contributors may be used to endorse or promote products derived
+ *   from this software without specific prior written permission.
  *
  *
- * CROSSBOW TECHNOLOGY AND ITS LICENSORS SPECIFICALLY DISCLAIM ALL WARRANTIES,
- * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
- * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 
- * ON AN "AS IS" BASIS, AND NEITHER CROSSBOW NOR ANY LICENSOR HAS ANY 
- * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR 
- * MODIFICATIONS.
+ * 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 STANFORD
+ * UNIVERSITY OR ITS 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.
  */
 
 /**
  */
 
 /**
- * HPL interface to Atmega128 timer 0 in ASYNC mode. This is a specialised
- * HPL component that assumes that timer 0 is used in ASYNC mode and
- * includes some workarounds for some of the weirdnesses (delayed overflow
- * interrupt) of that mode.
+ * Simple wrapper around the actual timer implementation that automatically
+ * wires it to McuSleepC for low-power calculations..
  *
  *
- * @author Martin Turon <mturon@xbow.com>
- * @author David Gay <dgay@intel-research.net>
+ * @author Philip Levis
  */
 
 #include <Atm128Timer.h>
 
  */
 
 #include <Atm128Timer.h>
 
-module HplAtm128Timer0AsyncC
+configuration HplAtm128Timer0AsyncC
 {
   provides {
     interface Init @atleastonce();
 {
   provides {
     interface Init @atleastonce();
@@ -46,126 +51,14 @@ module HplAtm128Timer0AsyncC
 }
 implementation
 {
 }
 implementation
 {
-  command error_t Init.init() {
-    SET_BIT(ASSR, AS0);  // set Timer/Counter0 to asynchronous mode
-    return SUCCESS;
-  }
-
-  //=== Read the current timer value. ===================================
-  async command uint8_t  Timer.get() { return TCNT0; }
-
-  //=== Set/clear the current timer value. ==============================
-  async command void Timer.set(uint8_t t)  {
-    while (ASSR & 1 << TCN0UB)
-      ;
-    TCNT0 = t;
-  }
-
-  //=== Read the current timer scale. ===================================
-  async command uint8_t Timer.getScale() { return TCCR0 & 0x7; }
-
-  //=== Turn off the timers. ============================================
-  async command void Timer.off() { call Timer.setScale(AVR_CLOCK_OFF); }
-
-  //=== Write a new timer scale. ========================================
-  async command void Timer.setScale(uint8_t s)  { 
-    Atm128TimerControl_t x = call TimerCtrl.getControl();
-    x.bits.cs = s;
-    call TimerCtrl.setControl(x);  
-  }
-
-  //=== Read the control registers. =====================================
-  async command Atm128TimerControl_t TimerCtrl.getControl() { 
-    return *(Atm128TimerControl_t*)&TCCR0; 
-  }
-
-  //=== Write the control registers. ====================================
-  async command void TimerCtrl.setControl( Atm128TimerControl_t x ) { 
-    while (ASSR & 1 << TCR0UB)
-      ;
-    TCCR0 = x.flat; 
-  }
-
-  //=== Read the interrupt mask. =====================================
-  async command Atm128_TIMSK_t TimerCtrl.getInterruptMask() { 
-    return *(Atm128_TIMSK_t*)&TIMSK; 
-  }
-
-  //=== Write the interrupt mask. ====================================
-  DEFINE_UNION_CAST(TimerMask8_2int, Atm128_TIMSK_t, uint8_t);
-  DEFINE_UNION_CAST(TimerMask16_2int, Atm128_ETIMSK_t, uint8_t);
-
-  async command void TimerCtrl.setInterruptMask( Atm128_TIMSK_t x ) { 
-    TIMSK = TimerMask8_2int(x); 
-  }
-
-  //=== Read the interrupt flags. =====================================
-  async command Atm128_TIFR_t TimerCtrl.getInterruptFlag() { 
-    return *(Atm128_TIFR_t*)&TIFR; 
-  }
-
-  //=== Write the interrupt flags. ====================================
-  DEFINE_UNION_CAST(TimerFlags8_2int, Atm128_TIFR_t, uint8_t);
-  DEFINE_UNION_CAST(TimerFlags16_2int, Atm128_ETIFR_t, uint8_t);
-
-  async command void TimerCtrl.setInterruptFlag( Atm128_TIFR_t x ) { 
-    TIFR = TimerFlags8_2int(x); 
-  }
-
-  //=== Timer 8-bit implementation. ====================================
-  async command void Timer.reset() { TIFR = 1 << TOV0; }
-  async command void Timer.start() { SET_BIT(TIMSK, TOIE0); }
-  async command void Timer.stop()  { CLR_BIT(TIMSK, TOIE0); }
+  components HplAtm128Timer0AsyncP;
+  components McuSleepC;
 
 
-  bool overflowed() {
-    return (call TimerCtrl.getInterruptFlag()).bits.tov0; 
-  }
+  McuSleepC.McuPowerOverride -> HplAtm128Timer0AsyncP;
 
 
-  async command bool Timer.test()  { 
-    return overflowed();
-  }
-  async command bool Timer.isOn()  { 
-    return (call TimerCtrl.getInterruptMask()).bits.toie0; 
-  }
-  async command void Compare.reset() { TIFR = 1 << OCF0; }
-  async command void Compare.start() { SET_BIT(TIMSK,OCIE0); }
-  async command void Compare.stop()  { CLR_BIT(TIMSK,OCIE0); }
-  async command bool Compare.test()  { 
-    return (call TimerCtrl.getInterruptFlag()).bits.ocf0; 
-  }
-  async command bool Compare.isOn()  { 
-    return (call TimerCtrl.getInterruptMask()).bits.ocie0; 
-  }
-
-  //=== Read the compare registers. =====================================
-  async command uint8_t Compare.get()   { return OCR0; }
-
-  //=== Write the compare registers. ====================================
-  async command void Compare.set(uint8_t t)   { 
-    atomic
-      {
-       while (ASSR & 1 << OCR0UB)
-         ;
-       OCR0 = t; 
-      }
-  }
-
-  //=== Timer interrupts signals ========================================
-  void stabiliseTimer0() {
-    TCCR0 = TCCR0;
-    while (ASSR & 1 << TCR0UB)
-      ;
-  }
-
-  default async event void Compare.fired() { }
-  AVR_ATOMIC_HANDLER(SIG_OUTPUT_COMPARE0) {
-    stabiliseTimer0();
-    signal Compare.fired();
-  }
-
-  default async event void Timer.overflow() { }
-  AVR_ATOMIC_HANDLER(SIG_OVERFLOW0) {
-    stabiliseTimer0();
-    signal Timer.overflow();
-  }
+  Init = HplAtm128Timer0AsyncP;
+  Timer = HplAtm128Timer0AsyncP;
+  TimerCtrl = HplAtm128Timer0AsyncP;
+  Compare = HplAtm128Timer0AsyncP;
+  
 }
 }
diff --git a/tos/chips/atm128/timer/HplAtm128Timer0AsyncP.nc b/tos/chips/atm128/timer/HplAtm128Timer0AsyncP.nc
new file mode 100644 (file)
index 0000000..4d81a5e
--- /dev/null
@@ -0,0 +1,202 @@
+/// $Id$
+
+/*
+ * Copyright (c) 2004-2005 Crossbow Technology, Inc.  All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose, without fee, and without written agreement is
+ * hereby granted, provided that the above copyright notice, the following
+ * two paragraphs and the author appear in all copies of this software.
+ * 
+ * IN NO EVENT SHALL CROSSBOW TECHNOLOGY OR ANY OF ITS LICENSORS BE LIABLE TO 
+ * ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL 
+ * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
+ * IF CROSSBOW OR ITS LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
+ * DAMAGE. 
+ *
+ * CROSSBOW TECHNOLOGY AND ITS LICENSORS SPECIFICALLY DISCLAIM ALL WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
+ * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 
+ * ON AN "AS IS" BASIS, AND NEITHER CROSSBOW NOR ANY LICENSOR HAS ANY 
+ * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR 
+ * MODIFICATIONS.
+ */
+
+/**
+ * HPL interface to Atmega128 timer 0 in ASYNC mode. This is a specialised
+ * HPL component that assumes that timer 0 is used in ASYNC mode and
+ * includes some workarounds for some of the weirdnesses (delayed overflow
+ * interrupt) of that mode.
+ *
+ * @author Martin Turon <mturon@xbow.com>
+ * @author David Gay <dgay@intel-research.net>
+ */
+
+#include <Atm128Timer.h>
+
+module HplAtm128Timer0AsyncP {
+  provides {
+    interface Init @atleastonce();
+    // 8-bit Timers
+    interface HplAtm128Timer<uint8_t>   as Timer;
+    interface HplAtm128TimerCtrl8       as TimerCtrl;
+    interface HplAtm128Compare<uint8_t> as Compare;
+    interface McuPowerOverride;
+  }
+}
+implementation
+{
+  command error_t Init.init() {
+    SET_BIT(ASSR, AS0);  // set Timer/Counter0 to asynchronous mode
+    return SUCCESS;
+  }
+
+  //=== Read the current timer value. ===================================
+  async command uint8_t  Timer.get() { return TCNT0; }
+
+  //=== Set/clear the current timer value. ==============================
+  async command void Timer.set(uint8_t t)  {
+    while (ASSR & 1 << TCN0UB)
+      ;
+    TCNT0 = t;
+  }
+
+  //=== Read the current timer scale. ===================================
+  async command uint8_t Timer.getScale() { return TCCR0 & 0x7; }
+
+  //=== Turn off the timers. ============================================
+  async command void Timer.off() { call Timer.setScale(AVR_CLOCK_OFF); }
+
+  //=== Write a new timer scale. ========================================
+  async command void Timer.setScale(uint8_t s)  { 
+    Atm128TimerControl_t x = call TimerCtrl.getControl();
+    x.bits.cs = s;
+    call TimerCtrl.setControl(x);  
+  }
+
+  //=== Read the control registers. =====================================
+  async command Atm128TimerControl_t TimerCtrl.getControl() { 
+    return *(Atm128TimerControl_t*)&TCCR0; 
+  }
+
+  //=== Write the control registers. ====================================
+  async command void TimerCtrl.setControl( Atm128TimerControl_t x ) { 
+    while (ASSR & 1 << TCR0UB)
+      ;
+    TCCR0 = x.flat; 
+  }
+
+  //=== Read the interrupt mask. =====================================
+  async command Atm128_TIMSK_t TimerCtrl.getInterruptMask() { 
+    return *(Atm128_TIMSK_t*)&TIMSK; 
+  }
+
+  //=== Write the interrupt mask. ====================================
+  DEFINE_UNION_CAST(TimerMask8_2int, Atm128_TIMSK_t, uint8_t);
+  DEFINE_UNION_CAST(TimerMask16_2int, Atm128_ETIMSK_t, uint8_t);
+
+  async command void TimerCtrl.setInterruptMask( Atm128_TIMSK_t x ) { 
+    TIMSK = TimerMask8_2int(x); 
+  }
+
+  //=== Read the interrupt flags. =====================================
+  async command Atm128_TIFR_t TimerCtrl.getInterruptFlag() { 
+    return *(Atm128_TIFR_t*)&TIFR; 
+  }
+
+  //=== Write the interrupt flags. ====================================
+  DEFINE_UNION_CAST(TimerFlags8_2int, Atm128_TIFR_t, uint8_t);
+  DEFINE_UNION_CAST(TimerFlags16_2int, Atm128_ETIFR_t, uint8_t);
+
+  async command void TimerCtrl.setInterruptFlag( Atm128_TIFR_t x ) { 
+    TIFR = TimerFlags8_2int(x); 
+  }
+
+  //=== Timer 8-bit implementation. ====================================
+  async command void Timer.reset() { TIFR = 1 << TOV0; }
+  async command void Timer.start() { SET_BIT(TIMSK, TOIE0); }
+  async command void Timer.stop()  { CLR_BIT(TIMSK, TOIE0); }
+
+  bool overflowed() {
+    return (call TimerCtrl.getInterruptFlag()).bits.tov0; 
+  }
+
+  async command bool Timer.test()  { 
+    return overflowed();
+  }
+  async command bool Timer.isOn()  { 
+    return (call TimerCtrl.getInterruptMask()).bits.toie0; 
+  }
+  async command void Compare.reset() { TIFR = 1 << OCF0; }
+  async command void Compare.start() { SET_BIT(TIMSK,OCIE0); }
+  async command void Compare.stop()  { CLR_BIT(TIMSK,OCIE0); }
+  async command bool Compare.test()  { 
+    return (call TimerCtrl.getInterruptFlag()).bits.ocf0; 
+  }
+  async command bool Compare.isOn()  { 
+    return (call TimerCtrl.getInterruptMask()).bits.ocie0; 
+  }
+
+  //=== Read the compare registers. =====================================
+  async command uint8_t Compare.get()   { return OCR0; }
+
+  //=== Write the compare registers. ====================================
+  async command void Compare.set(uint8_t t)   { 
+    atomic
+      {
+       while (ASSR & 1 << OCR0UB)
+         ;
+       OCR0 = t; 
+      }
+  }
+
+  //=== Timer interrupts signals ========================================
+  void stabiliseTimer0() {
+    TCCR0 = TCCR0;
+    while (ASSR & 1 << TCR0UB)
+      ;
+  }
+
+  /**
+   * On the atm128, there is a small latency when waking up from
+   * POWER_SAVE mode. So if a timer is going to go off very soon, it's
+   * better to drop down until EXT_STANDBY, which has a 6 cycle wakeup
+   * latency. This function calculates whether staying in EXT_STANDBY
+   * is needed. If the timer is not running it returns POWER_DOWN.
+   * Please refer to TEP 112 and the atm128 datasheet for details.
+   */
+  
+  async command mcu_power_t McuPowerOverride.lowestState() {
+    uint8_t diff;
+    // We need to make sure that the sleep wakeup latency will not
+    // cause us to miss a timer. POWER_SAVE 
+    if (TIMSK & (1 << OCIE0 | 1 << TOIE0)) {
+      // need to wait for timer 0 updates propagate before sleeping
+      // (we don't need to worry about reentering sleep mode too early,
+      // as the wake ups from timer0 wait at least one TOSC1 cycle
+      // anyway - see the stabiliseTimer0 function in HplAtm128Timer0AsyncC)
+      while (ASSR & (1 << TCN0UB | 1 << OCR0UB | 1 << TCR0UB))
+       ;
+      diff = OCR0 - TCNT0;
+      if (diff < EXT_STANDBY_T0_THRESHOLD ||
+         TCNT0 > 256 - EXT_STANDBY_T0_THRESHOLD) 
+       return ATM128_POWER_EXT_STANDBY;
+      return ATM128_POWER_SAVE;
+    }
+    else {
+      return ATM128_POWER_DOWN;
+    }
+  }
+  
+  default async event void Compare.fired() { }
+  AVR_ATOMIC_HANDLER(SIG_OUTPUT_COMPARE0) {
+    stabiliseTimer0();
+    signal Compare.fired();
+  }
+
+  default async event void Timer.overflow() { }
+  AVR_ATOMIC_HANDLER(SIG_OVERFLOW0) {
+    stabiliseTimer0();
+    signal Timer.overflow();
+  }
+}