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