demos_source/code_TestWatchdog.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 
00046 /* This this the only include needed in your code .*/
00047 #include "femtoos_code.h"
00048 
00049 /* Define the delays for the different blinker tasks. These regular values makes it
00050  * extra clear when tasks are getting hindered. */
00051 #define delay07  1600U
00052 #define delay06   800U
00053 #define delay05   400U
00054 #define delay04   200U
00055 #define delay03  1600U
00056 #define delay02   800U
00057 #define delay01   400U
00058 #define delay00   200U
00059 
00060 static Tbool bSync = false;
00061 
00062 /* This is called once at system boot, and before the creating of any of
00063  * the tasks. Use it to initialize the hardware. */
00064 void appBoot(void)
00065 { devLedDRR  |= 0xFF;
00066   devSwitchDRR  &= 0xFC;
00067   devLedPORT |= 0xFF; }
00068 
00069 /* Method to set the value of the led. Here you don't need access protection since the devLedPORT operations
00070  * are compiled to sbi/cbi instructions which atomic operations! Only if called with constants. */
00071 #define setLed(lednr,state)  if (state) { devLedPORT |= (1 << lednr); } else { devLedPORT &=  ~(1 << lednr); }
00072 
00073 
00074 /* This is the offending task. After it has been blinking for a while (32 x 200 ms)
00075  * it hits the infinite loop. The watchdog is not fed anymore. */
00076 
00077 #if (preTaskDefined(LEDtask0))
00078 
00079 void appLoop_LEDtask0(void)
00080 { Tuint08 led  = 0;
00081   Tuint08 count = 32;
00082   while (count--)
00083   { taskFeedWatchdog();
00084     led = 1 - led;
00085     setLed(0,led);
00086     taskDelayFromNow(delay00); }
00087    while (1); }
00088 
00089 
00090 /* After a while, when the watchdog is not being fed, it starts barking, by calling
00091  * the routine below. Note, within this routine, you are in OS space, so do not
00092  * call ant taskXXX() methodes. It is save however to call genXXXX() methodes.
00093  * We do so here to lower the priority, or, when that is already at its minimum,
00094  * to suspend the task.
00095  *
00096  * Note that if an appInit_LEDtask0 would be present (but it is not in this case)
00097  * it will not be called by the OS at barking. If needed, you can do so yourself
00098  * from this place.
00099  */
00100 
00101 void appBark_LEDtask0(void)
00102 { devLedPORT |= 0x01;
00103   Tuint08 lastPrio = genGetPriorityOnName(LEDtask0);
00104   if(lastPrio>0)
00105   { --lastPrio;
00106     genSetPriorityOnName(LEDtask0,lastPrio); }
00107   else
00108   { genSuspendOnName(LEDtask0); } }
00109 
00110 #endif
00111 
00112 
00113 /* Regular blinking leds do not have watchdog protection in this demo. */
00114 
00115 #if (preTaskDefined(LEDtask1))
00116 
00117 void appLoop_LEDtask1(void)
00118 { Tuint08 led  = false;
00119   while (true)
00120   { led = !led;
00121     setLed(1,led);
00122     taskDelayFromNow(delay01);
00123     led = !led;
00124     setLed(1,led);
00125     taskDelayFromNow(delay01); } }
00126 
00127 #endif
00128 
00129 
00130 #if (preTaskDefined(LEDtask2))
00131 
00132 void appLoop_LEDtask2(void)
00133 { Tuint08 led  = false;
00134   while (true)
00135   { led = !led;
00136     setLed(2,led);
00137     taskDelayFromNow(delay02);
00138     led = !led;
00139     setLed(2,led);
00140     taskDelayFromNow(delay02);  } }
00141 
00142 #endif
00143 
00144 
00145 /* LEDtask3 can be synchronized with LEDtask7 by pushing a button. You see that
00146  * as soon as the bSync variable gets true, this task starts waiting for an
00147  * other task on a specific point. We did not synchronize the access on bSync
00148  * since the concurrent modification does not lead to errors in this case. */
00149 
00150 #if (preTaskDefined(LEDtask3))
00151 
00152 void appLoop_LEDtask3(void)
00153 { Tuint08 led  = false;
00154   while (true)
00155   { led = !led;
00156     setLed(3,led);
00157     taskDelayFromNow(delay03);
00158     led = !led;
00159     setLed(3,led);
00160     taskDelayFromNow(delay03);
00161     if (bSync)
00162     { taskWaitForOtherTaskOnName(TrefPunt);
00163       bSync = false; } } }
00164 
00165 #endif
00166 
00167 
00168 #if (preTaskDefined(LEDtask4))
00169 
00170 void appLoop_LEDtask4(void)
00171 { Tuint08 led  = false;
00172   while (true)
00173   { led = !led;
00174     setLed(4,led);
00175     taskDelayFromNow(delay04);
00176     led = !led;
00177     setLed(4,led);
00178     taskDelayFromNow(delay04); } }
00179 
00180 #endif
00181 
00182 
00183 #if (preTaskDefined(LEDtask5))
00184 
00185 void appLoop_LEDtask5(void)
00186 { Tuint08 led  = false;
00187   while (true)
00188   { led = !led;
00189     setLed(5,led);
00190     taskDelayFromNow(delay05);
00191     led = !led;
00192     setLed(5,led);
00193     taskDelayFromNow(delay05); } }
00194 
00195 #endif
00196 
00197 
00198 #if (preTaskDefined(LEDtask6))
00199 
00200 void appLoop_LEDtask6(void)
00201 { Tuint08 led  = false;
00202   while (true)
00203   { led = !led;
00204     setLed(6,led);
00205     taskDelayFromNow(delay06);
00206     led = !led;
00207     setLed(6,led);
00208     taskDelayFromNow(delay06);  } }
00209 
00210 #endif
00211 
00212 
00213 /* LEDtask7 can be synchronized with LEDtask3 by pushing a button. You see that
00214  * as soon as the bSync variable gets true, this task starts waiting for an
00215  * other task on a specific point. We did not synchronize the access on bSync
00216  * since the concurrent modification does not lead to errors in this case. */
00217 
00218 #if (preTaskDefined(LEDtask7))
00219 
00220 void appLoop_LEDtask7(void)
00221 { Tuint08 led  = false;
00222   while (true)
00223   { led = !led;
00224     setLed(7,led);
00225     taskDelayFromNow(delay07);
00226     led = !led;
00227     setLed(7,led);
00228     taskDelayFromNow(delay07);
00229     if (bSync)
00230     { taskWaitForOtherTaskOnName(TrefPunt);
00231       bSync = false; }
00232     } }
00233 
00234 #endif
00235 
00236 
00237 /* This is the task which waits for the synchronizer button to be pushed. If
00238  * so, the bSync will be set and  LEDtask7, LEDtask3 will be waiting for each
00239  * other. */
00240 
00241 #if (preTaskDefined(Synchronize))
00242 
00243 void appLoop_Synchronize(void)
00244 { Tuint08 button  = devSwitchPIN & 01;
00245   Tuint08 lastbutton  = button;
00246   while (true)
00247   { button = devSwitchPIN & 01;
00248     if (button != lastbutton) { bSync = true;  }
00249     taskDelayFromNow(100); } }
00250 
00251 #endif
00252 
00253 
00254 /* By pushing the other button, we can revive the task, LEDtask0, that was
00255  * suspended by the watchdog. The priority is restored and the task is
00256  * resumed. Because the infinite loop is still present, the watchdog bark
00257  * cycle will repeat itself. */
00258 
00259 #if (preTaskDefined(Release))
00260 
00261 void appLoop_Release(void)
00262 { Tuint08 button  = devSwitchPIN & 02;
00263   Tuint08 lastbutton  = button;
00264   while (true)
00265   { button = devSwitchPIN & 02;
00266     if (button != lastbutton)
00267     { genSetPriorityOnName(LEDtask0,3);
00268       genResumeOnName(LEDtask0); }
00269     taskDelayFromNow(100); } }
00270 
00271 #endif
00272 

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