demos_source/code_TestSleep.c

Go to the documentation of this file.
00001 /*
00002  * Femto OS v 0.91 - Copyright (C) 2008-2009 Ruud Vlaming
00003  *
00004  * This file is part of the Femto OS distribution.
00005  *
00006  * This program is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, version 3 of the License.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Please note that, due to the GPLv3 license, for application of this
00019  * work and/or combined work in embedded systems special obligations apply.
00020  * If these are not to you liking, please know the Femto OS is dual
00021  * licensed. A commercial license and support are available.
00022  * See http://www.femtoos.org/ for details.
00023  */
00024 
00025 
00037 /* This this the only include needed in your code .*/
00038 #include "femtoos_code.h"
00039 
00040 
00041 /* Delays of the blinkers, they are choosen in such a way that the leds blink shortly
00042  * and than pause for a longer time. The two leds blink with a slightly different frequency
00043  * so, as time goes by, the are getting more and more out of phase. In the beginning both
00044  * leds can be handled in the same wake period, later on the system must be woken for
00045  * both tasks seperately. */
00046 #define delay05 31000U
00047 #define delay04 30500U
00048 #define delay03 30000U
00049 #define delay02  1500U
00050 #define delay01  1000U
00051 #define delay00   250U
00052 #define delayFLASH 50U
00053 
00054 /* If only two leds burn continuously you have not connected the button
00055  * to the correct interrupt pin. See the pin-configuration for your device
00056  * and connect the switch to INT0 */
00057 
00058 #if (defSysGCCstartup != cfgReplace) && (devINT != cfgUndefined)
00059   #define IntPortSetup()   do { devSwitchDRR = 0x00; devEIR = preBitSet1(0x00,devINT); } while(false)
00060   void devSigExternalInt(void) __attribute__ ( ( signal, naked, used, externally_visible ) );
00061   void devSigExternalInt(void) { asm("reti"); }
00062 #else
00063 /* Games with interrupts are not possible when we use cfgReplace, since there is no interrupt
00064  * vector table. So pushing the button will not cause a wake-up in the demo. Furthermore,
00065  * if the device has no watchdog interrupt, the system cannot wake up at all in this case.*/
00066   #define IntPortSetup()
00067 #endif
00068 
00069 
00070 /* This is called once at system boot, and before the creating of any of
00071  * the tasks. Use it to initialize the hardware. */
00072 void appBoot(void)
00073 { devLedDRR    = 0xFF;
00074   devLedPORT   = 0xFF;
00075   IntPortSetup(); }
00076 
00077 
00078 /* During sleep, the device wakes up very shortly every second (configurable), to see if
00079  * he must wake up already. This is the hook if you want to do something yourself in that
00080  * period. We very shortly blink a led, it is just visible in the dark. */
00081 void appTickSleep(void)
00082 { devLedPORT &= ~0x40;
00083   portNop();
00084   portNop();
00085   portNop();
00086   devLedPORT |= 0x40; }
00087 
00088 
00089 /* This is the hook to do something when you enter sleep. In this case we switch
00090  * off the leftmost led. */
00091 void appEnterSleep(void) { devLedPORT |= 0x80; }
00092 
00093 
00094 /* This is the hook to do something when you exit sleep. In this case we switch
00095  * on the leftmost led, so we can see when the device is awake. */
00096 void appExitSleep(void)  { devLedPORT &= ~0x80; }
00097 
00098 
00099 /* Method to set the value of the led. Here you don't need access protection since the devLedPORT operations
00100  * are compiled to sbi/cbi instructions which atomic operations! Only if called with constants. */
00101 #define setLed(lednr,state)  if (state) { devLedPORT &=  ~(1 << lednr); } else { devLedPORT |= (1 << lednr); }
00102 
00103 
00104 /* Method to flash a particular led once. */
00105 void flashLED(Tuint08 ledNR)
00106 { setLed(ledNR,1);
00107   taskDelayFromNow(delayFLASH);
00108   setLed(ledNR,0); }
00109 
00110 
00111 /* These are the flashers. If we have cfgUseLowPowerOnDelay activated the Femto OS decides
00112  * when the delay is long enough to put the device to sleep. It will wake up just before the
00113  * first task is due. If not (when no dedicated watchdog interrupt is present) we must
00114  * manually put the task to sleep. When all tasks sleep, the device is put into low power mode. */
00115 
00116 #if (preTaskDefined(LEDtask0))
00117 
00118 void appLoop_LEDtask0(void)
00119 { while (true)
00120   { flashLED(0);
00121     taskDelayFromNow(delay02);
00122     flashLED(0);
00123     #if (cfgUseLowPowerOnDelay == cfgTrue)
00124       taskDelayFromNow(delay05);
00125     #else
00126       taskSleep();
00127     #endif
00128   } }
00129 
00130 #endif
00131 
00132 #if (preTaskDefined(LEDtask1))
00133 
00134 void appLoop_LEDtask1(void)
00135 { while (true)
00136   { flashLED(1);
00137     taskDelayFromNow(delay01);
00138     flashLED(1);
00139     #if (cfgUseLowPowerOnDelay == cfgTrue)
00140       taskDelayFromNow(delay04);
00141     #else
00142       taskSleep();
00143     #endif
00144   } }
00145 
00146 #endif
00147 
00148 
00149 /* This tasks just blinks every time the device gets woken. */
00150 
00151 #if (preTaskDefined(LEDtask2))
00152 
00153 void appLoop_LEDtask2(void)
00154 { while (true)
00155   { Tuint08 count = 8;
00156     while (count--)
00157     { flashLED(2);
00158       taskDelayFromNow(delay00); }
00159     taskSleep(); } }
00160 
00161 #endif
00162 

Generated on Fri Oct 16 00:05:21 2009 for FemtoOS by  doxygen 1.5.2