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