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