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