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