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