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