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