]> git.sur5r.net Git - freertos/blob - Demo/HCS12_CodeWarrior_banked/main.c
Update to V4.7.2.
[freertos] / Demo / HCS12_CodeWarrior_banked / main.c
1 \r
2 /*\r
3         FreeRTOS.org V4.7.2 - Copyright (C) 2003-2008 Richard Barry.\r
4 \r
5         This file is part of the FreeRTOS.org distribution.\r
6 \r
7         FreeRTOS.org is free software; you can redistribute it and/or modify\r
8         it under the terms of the GNU General Public License as published by\r
9         the Free Software Foundation; either version 2 of the License, or\r
10         (at your option) any later version.\r
11 \r
12         FreeRTOS.org is distributed in the hope that it will be useful,\r
13         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15         GNU General Public License for more details.\r
16 \r
17         You should have received a copy of the GNU General Public License\r
18         along with FreeRTOS.org; if not, write to the Free Software\r
19         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20 \r
21         A special exception to the GPL can be applied should you wish to distribute\r
22         a combined work that includes FreeRTOS.org, without being obliged to provide\r
23         the source code for any proprietary components.  See the licensing section \r
24         of http://www.FreeRTOS.org for full details of how and when the exception\r
25         can be applied.\r
26 \r
27         ***************************************************************************\r
28 \r
29         Please ensure to read the configuration and relevant port sections of the \r
30         online documentation.\r
31 \r
32         +++ http://www.FreeRTOS.org +++\r
33         Documentation, latest information, license and contact details.  \r
34 \r
35         +++ http://www.SafeRTOS.com +++\r
36         A version that is certified for use in safety critical systems.\r
37 \r
38         +++ http://www.OpenRTOS.com +++\r
39         Commercial support, development, porting, licensing and training services.\r
40 \r
41         ***************************************************************************\r
42 */\r
43 \r
44 \r
45 /*\r
46  *\r
47  * vMain() is effectively the demo application entry point.  It is called by\r
48  * the main() function generated by the Processor Expert application.  \r
49  *\r
50  * vMain() creates all the demo application tasks, then starts the scheduler.\r
51  * The WEB      documentation provides more details of the demo application tasks.\r
52  *\r
53  * Main.c also creates a task called "Check".  This only executes every three \r
54  * seconds but has the highest priority so is guaranteed to get processor time.  \r
55  * Its main function is to check that all the other tasks are still operational.\r
56  * Each task (other than the "flash" tasks) maintains a unique count that is \r
57  * incremented each time the task successfully completes its function.  Should \r
58  * any error occur within such a task the count is permanently halted.  The \r
59  * check task inspects the count of each task to ensure it has changed since\r
60  * the last time the check task executed.  If all the count variables have \r
61  * changed all the tasks are still executing error free, and the check task\r
62  * toggles the onboard LED.  Should any task contain an error at any time \r
63  * the LED toggle rate will change from 3 seconds to 500ms.\r
64  *\r
65  * This file also includes the functionality implemented within the \r
66  * standard demo application file integer.c.  This is done to demonstrate the\r
67  * use of an idle hook.  See the documentation within integer.c for the \r
68  * rationale of the integer task functionality.\r
69  * */\r
70 \r
71 /* Kernel includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 #include "queue.h"\r
75 \r
76 /* Demo application includes. */\r
77 #include "flash.h"\r
78 #include "PollQ.h"\r
79 #include "dynamic.h"\r
80 #include "partest.h"\r
81 #include "comtest2.h"\r
82 #include "BlockQ.h"\r
83 #include "integer.h"\r
84 #include "death.h"\r
85 \r
86 \r
87 /*-----------------------------------------------------------\r
88         Definitions.\r
89 -----------------------------------------------------------*/\r
90 \r
91 /* Priorities assigned to demo application tasks. */\r
92 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
93 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
94 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
95 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
96 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
97 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )\r
98 \r
99 /* LED that is toggled by the check task.  The check task periodically checks\r
100 that all the other tasks are operating without error.  If no errors are found\r
101 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found \r
102 then the toggle rate increases to mainERROR_CHECK_PERIOD. */\r
103 #define mainCHECK_TASK_LED                      ( 7 )\r
104 #define mainCHECK_PERIOD                        ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
105 #define mainERROR_CHECK_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS )\r
106 \r
107 /* The constants used in the idle task calculation. */\r
108 #define intgCONST1                              ( ( portLONG ) 123 )\r
109 #define intgCONST2                              ( ( portLONG ) 234567 )\r
110 #define intgCONST3                              ( ( portLONG ) -3 )\r
111 #define intgCONST4                              ( ( portLONG ) 7 )\r
112 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
113 \r
114 \r
115 /* Baud rate used by the serial port tasks (ComTest tasks).\r
116 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the\r
117 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS\r
118 download this value must be one of the following:\r
119 \r
120 0 to configure for 38400 baud.\r
121 1 to configure for 19200 baud.\r
122 2 to configure for 9600 baud.\r
123 3 to configure for 4800 baud. */\r
124 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned portLONG ) 2 )\r
125 \r
126 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
127 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
128 #define mainCOM_TEST_LED                                ( 3 )\r
129 \r
130 /*-----------------------------------------------------------\r
131         Local functions prototypes.\r
132 -----------------------------------------------------------*/\r
133 \r
134 /*\r
135  * The 'Check' task function.  See the explanation at the top of the file.\r
136  */\r
137 static void vErrorChecks( void* pvParameters );\r
138 \r
139 /*\r
140  * The idle task hook - in which the integer task is implemented.  See the\r
141  * explanation at the top of the file.\r
142  */\r
143 void vApplicationIdleHook( void );\r
144 \r
145 /*\r
146  * Checks the unique counts of other tasks to ensure they are still operational.\r
147  */\r
148 static portLONG prvCheckOtherTasksAreStillRunning( void );\r
149 \r
150 \r
151 \r
152 /*-----------------------------------------------------------\r
153         Local variables.\r
154 -----------------------------------------------------------*/\r
155 \r
156 /* A few tasks are defined within this file.  This flag is used to indicate\r
157 their status.  If an error is detected in one of the locally defined tasks then\r
158 this flag is set to pdTRUE. */\r
159 portBASE_TYPE xLocalError = pdFALSE;\r
160 \r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 /* \r
165  * This is called from the main() function generated by the Processor Expert.\r
166  */\r
167 void vMain( void )\r
168 {\r
169         /* Start some of the standard demo tasks. */\r
170         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
171         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
172         vStartDynamicPriorityTasks();\r
173         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
174         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
175         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
176                 \r
177         /* Start the locally defined tasks.  There is also a task implemented as\r
178         the idle hook. */\r
179         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
180         \r
181         /* Must be the last demo created. */\r
182         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
183 \r
184         /* All the tasks have been created - start the scheduler. */\r
185         vTaskStartScheduler();\r
186         \r
187         /* Should not reach here! */\r
188         for( ;; );\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void vErrorChecks( void *pvParameters )\r
193 {\r
194 portTickType xDelayPeriod = mainCHECK_PERIOD;\r
195 portTickType xLastWakeTime;\r
196 \r
197         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
198         functions correctly. */\r
199         xLastWakeTime = xTaskGetTickCount();\r
200 \r
201         for( ;; )\r
202         {\r
203                 /* Delay until it is time to execute again.  The delay period is \r
204                 shorter following an error. */\r
205                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
206 \r
207                 /* Check all the demo application tasks are executing without \r
208                 error. If an error is found the delay period is shortened - this\r
209                 has the effect of increasing the flash rate of the 'check' task\r
210                 LED. */\r
211                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )\r
212                 {\r
213                         /* An error has been detected in one of the tasks - flash faster. */\r
214                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
215                 }\r
216 \r
217                 /* Toggle the LED each cycle round. */\r
218                 vParTestToggleLED( mainCHECK_TASK_LED );\r
219         }\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 static portLONG prvCheckOtherTasksAreStillRunning( void )\r
224 {\r
225 portBASE_TYPE xAllTasksPassed = pdPASS;\r
226 \r
227         if( xArePollingQueuesStillRunning() != pdTRUE )\r
228         {\r
229                 xAllTasksPassed = pdFAIL;\r
230         }\r
231 \r
232         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
233         {\r
234                 xAllTasksPassed = pdFAIL;\r
235         }\r
236 \r
237         if( xAreComTestTasksStillRunning() != pdTRUE )\r
238         {\r
239                 xAllTasksPassed = pdFALSE;\r
240         }\r
241 \r
242         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
243         {\r
244                 xAllTasksPassed = pdFALSE;\r
245         }\r
246         \r
247         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
248         {\r
249                 xAllTasksPassed = pdFALSE;\r
250         }       \r
251 \r
252     if( xIsCreateTaskStillRunning() != pdTRUE )\r
253     {\r
254         xAllTasksPassed = pdFALSE;\r
255     }\r
256 \r
257         /* Also check the status flag for the tasks defined within this function. */\r
258         if( xLocalError != pdFALSE )\r
259         {\r
260                 xAllTasksPassed = pdFAIL;\r
261         }\r
262         \r
263         return xAllTasksPassed;\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 void vApplicationIdleHook( void )\r
268 {\r
269 /* This variable is effectively set to a constant so it is made volatile to\r
270 ensure the compiler does not just get rid of it. */\r
271 volatile portLONG lValue;\r
272 \r
273         /* Keep performing a calculation and checking the result against a constant. */\r
274 \r
275         /* Perform the calculation.  This will store partial value in\r
276         registers, resulting in a good test of the context switch mechanism. */\r
277         lValue = intgCONST1;\r
278         lValue += intgCONST2;\r
279         lValue *= intgCONST3;\r
280         lValue /= intgCONST4;\r
281 \r
282         /* Did we perform the calculation correctly with no corruption? */\r
283         if( lValue != intgEXPECTED_ANSWER )\r
284         {\r
285                 /* Error! */\r
286                 portENTER_CRITICAL();\r
287                         xLocalError = pdTRUE;\r
288                 portEXIT_CRITICAL();\r
289         }\r
290 \r
291         /* Yield in case cooperative scheduling is being used. */\r
292         #if configUSE_PREEMPTION == 0\r
293         {\r
294                 taskYIELD();\r
295         }\r
296         #endif          \r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 \r
301 \r