]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/HCS12_GCC_banked/main.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / HCS12_GCC_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  * main() 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 #include "cpu.h"\r
100 \r
101 /* special prototypes for memory-banked functions */\r
102 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority );\r
103 portBASE_TYPE xArePollingQueuesStillRunning( void );\r
104 \r
105 /* Demo application includes. */\r
106 #include "flash.h"\r
107 #include "PollQ.h"\r
108 #include "dynamic.h"\r
109 #include "partest.h"\r
110 #include "comtest2.h"\r
111 #include "BlockQ.h"\r
112 #include "integer.h"\r
113 #include "death.h"\r
114 \r
115 \r
116 /*-----------------------------------------------------------\r
117         Definitions.\r
118 -----------------------------------------------------------*/\r
119 \r
120 /* Priorities assigned to demo application tasks. */\r
121 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
122 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
123 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
124 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
125 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
126 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )\r
127 \r
128 /* LED that is toggled by the check task.  The check task periodically checks\r
129 that all the other tasks are operating without error.  If no errors are found\r
130 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found \r
131 then the toggle rate increases to mainERROR_CHECK_PERIOD. */\r
132 #define mainCHECK_TASK_LED                      ( 7 )\r
133 #define mainCHECK_PERIOD                        ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
134 #define mainERROR_CHECK_PERIOD          ( ( portTickType ) 500 / portTICK_RATE_MS )\r
135 \r
136 /* The constants used in the idle task calculation. */\r
137 #define intgCONST1                              ( ( long ) 123 )\r
138 #define intgCONST2                              ( ( long ) 234567 )\r
139 #define intgCONST3                              ( ( long ) -3 )\r
140 #define intgCONST4                              ( ( long ) 7 )\r
141 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
142 \r
143 \r
144 /* Baud rate used by the serial port tasks (ComTest tasks).\r
145 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the\r
146 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS\r
147 download this value must be one of the following:\r
148 \r
149 0 to configure for 38400 baud.\r
150 1 to configure for 19200 baud.\r
151 2 to configure for 9600 baud.\r
152 3 to configure for 4800 baud. */\r
153 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 2 )\r
154 \r
155 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
156 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
157 #define mainCOM_TEST_LED                                ( 3 )\r
158 \r
159 /*-----------------------------------------------------------\r
160         Local functions prototypes.\r
161 -----------------------------------------------------------*/\r
162 \r
163 /*\r
164  * The 'Check' task function.  See the explanation at the top of the file.\r
165  */\r
166 static void ATTR_BANK1 vErrorChecks( void* pvParameters );\r
167 \r
168 /*\r
169  * The idle task hook - in which the integer task is implemented.  See the\r
170  * explanation at the top of the file.\r
171  */\r
172 void ATTR_BANK0 vApplicationIdleHook( void );\r
173 \r
174 /*\r
175  * Checks the unique counts of other tasks to ensure they are still operational.\r
176  */\r
177 static long ATTR_BANK0 prvCheckOtherTasksAreStillRunning( void );\r
178 \r
179 \r
180 \r
181 /*-----------------------------------------------------------\r
182         Local variables.\r
183 -----------------------------------------------------------*/\r
184 \r
185 /* A few tasks are defined within this file.  This flag is used to indicate\r
186 their status.  If an error is detected in one of the locally defined tasks then\r
187 this flag is set to pdTRUE. */\r
188 portBASE_TYPE xLocalError = pdFALSE;\r
189 \r
190 \r
191 /*-----------------------------------------------------------*/\r
192 \r
193 /* This is called from startup. */\r
194 int ATTR_BANK0 main ( void )\r
195 {\r
196         /* Start some of the standard demo tasks. */\r
197         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
198         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
199         vStartDynamicPriorityTasks();\r
200         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
201         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
202         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
203         \r
204         /* Start the locally defined tasks.  There is also a task implemented as\r
205         the idle hook. */\r
206         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
207         \r
208         /* Must be the last demo created. */\r
209         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
210 \r
211         /* All the tasks have been created - start the scheduler. */\r
212         vTaskStartScheduler();\r
213         \r
214         /* Should not reach here! */\r
215         for( ;; );\r
216         return 0;\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static void vErrorChecks( void *pvParameters )\r
221 {\r
222 portTickType xDelayPeriod = mainCHECK_PERIOD;\r
223 portTickType xLastWakeTime;\r
224 \r
225         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
226         functions correctly. */\r
227         xLastWakeTime = xTaskGetTickCount();\r
228 \r
229         for( ;; )\r
230         {\r
231                 /* Delay until it is time to execute again.  The delay period is \r
232                 shorter following an error. */\r
233                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
234 \r
235                 /* Check all the demo application tasks are executing without \r
236                 error. If an error is found the delay period is shortened - this\r
237                 has the effect of increasing the flash rate of the 'check' task\r
238                 LED. */\r
239                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )\r
240                 {\r
241                         /* An error has been detected in one of the tasks - flash faster. */\r
242                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
243                 }\r
244 \r
245                 /* Toggle the LED each cycle round. */\r
246                 vParTestToggleLED( mainCHECK_TASK_LED );\r
247         }\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static long prvCheckOtherTasksAreStillRunning( void )\r
252 {\r
253 portBASE_TYPE xAllTasksPassed = pdPASS;\r
254 \r
255         if( xArePollingQueuesStillRunning() != pdTRUE )\r
256         {\r
257                 xAllTasksPassed = pdFAIL;\r
258         }\r
259 \r
260         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
261         {\r
262                 xAllTasksPassed = pdFAIL;\r
263         }\r
264 \r
265         if( xAreComTestTasksStillRunning() != pdTRUE )\r
266         {\r
267                 xAllTasksPassed = pdFALSE;\r
268         }\r
269 \r
270         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
271         {\r
272                 xAllTasksPassed = pdFALSE;\r
273         }\r
274         \r
275         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
276         {\r
277                 xAllTasksPassed = pdFALSE;\r
278         }       \r
279 \r
280     if( xIsCreateTaskStillRunning() != pdTRUE )\r
281     {\r
282         xAllTasksPassed = pdFALSE;\r
283     }\r
284 \r
285         /* Also check the status flag for the tasks defined within this function. */\r
286         if( xLocalError != pdFALSE )\r
287         {\r
288                 xAllTasksPassed = pdFAIL;\r
289         }\r
290         \r
291         return xAllTasksPassed;\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 void vApplicationIdleHook( void )\r
296 {\r
297 /* This variable is effectively set to a constant so it is made volatile to\r
298 ensure the compiler does not just get rid of it. */\r
299 volatile long lValue;\r
300 \r
301         /* Keep performing a calculation and checking the result against a constant. */\r
302 \r
303         /* Perform the calculation.  This will store partial value in\r
304         registers, resulting in a good test of the context switch mechanism. */\r
305         lValue = intgCONST1;\r
306         lValue += intgCONST2;\r
307         lValue *= intgCONST3;\r
308         lValue /= intgCONST4;\r
309 \r
310         /* Did we perform the calculation correctly with no corruption? */\r
311         if( lValue != intgEXPECTED_ANSWER )\r
312         {\r
313                 /* Error! */\r
314                 portENTER_CRITICAL();\r
315                         xLocalError = pdTRUE;\r
316                 portEXIT_CRITICAL();\r
317         }\r
318 \r
319         /* Yield in case cooperative scheduling is being used. */\r
320         #if configUSE_PREEMPTION == 0\r
321         {\r
322                 taskYIELD();\r
323         }\r
324         #endif          \r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r