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