]> begriffs open source - freertos/blob - FreeRTOS/Demo/MB96340_Softune/FreeRTOS_96348hs_SK16FX100PMC/Src/main.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / MB96340_Softune / FreeRTOS_96348hs_SK16FX100PMC / Src / main.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
71  * documentation provides more details of the demo application tasks.\r
72  * \r
73  * In addition to the standard demo tasks, the follow demo specific tasks are\r
74  * create:\r
75  *\r
76  * The "Check" task.  This only executes every three seconds but has the highest \r
77  * priority so is guaranteed to get processor time.  Its main function is to \r
78  * check that all the other tasks are still operational.  Most tasks maintain \r
79  * a unique count that is incremented each time the task successfully completes \r
80  * its function.  Should any error occur within such a task the count is \r
81  * permanently halted.  The check task inspects the count of each task to ensure \r
82  * it has changed since the last time the check task executed.  If all the count \r
83  * variables have changed all the tasks are still executing error free, and the \r
84  * check task toggles the onboard LED.  Should any task contain an error at any time \r
85  * the LED toggle rate will change from 3 seconds to 500ms.\r
86  *\r
87  * The "Trace Utility" task.  This can be used to obtain trace and debug \r
88  * information via UART1.\r
89  */\r
90 \r
91 \r
92 /* Scheduler includes. */\r
93 #include "FreeRTOS.h"\r
94 #include "task.h"\r
95 #include "semphr.h"\r
96 \r
97 /* Demo application includes. */\r
98 #include "flash.h"\r
99 #include "integer.h"\r
100 #include "comtest2.h"\r
101 #include "PollQ.h"\r
102 #include "semtest.h"\r
103 #include "BlockQ.h"\r
104 #include "dynamic.h"\r
105 #include "flop.h"\r
106 #include "GenQTest.h"\r
107 #include "QPeek.h"\r
108 #include "blocktim.h"\r
109 #include "death.h"\r
110 #include "taskutility.h"\r
111 #include "partest.h"\r
112 #include "crflash.h"\r
113 #include "watchdog.h"\r
114 \r
115 /* Library includes. */\r
116 #include <watchdog.h>\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /* Demo task priorities. */\r
121 #define WTC_TASK_PRIORITY                       ( tskIDLE_PRIORITY + 5 )\r
122 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 4 )\r
123 #define TASK_UTILITY_PRIORITY           ( tskIDLE_PRIORITY )\r
124 #define mainSEM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 3 )\r
125 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
126 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
127 #define mainQUEUE_BLOCK_PRIORITY        ( tskIDLE_PRIORITY + 2 )\r
128 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )\r
129 #define mainLED_TASK_PRIORITY           ( tskIDLE_PRIORITY + 1 )\r
130 #define mainGENERIC_QUEUE_PRIORITY      ( tskIDLE_PRIORITY )\r
131 \r
132 /* Baud rate used by the COM test tasks. */\r
133 #define mainCOM_TEST_BAUD_RATE  ( ( unsigned long ) 19200 )\r
134 \r
135 /* The frequency at which the 'Check' tasks executes.  See the comments at the \r
136 top of the page.  When the system is operating error free the 'Check' task\r
137 toggles an LED every three seconds.  If an error is discovered in any task the\r
138 rate is increased to 500 milliseconds.  [in this case the '*' characters on the \r
139 LCD represent LED's] */\r
140 #define mainNO_ERROR_CHECK_DELAY        ( (portTickType) 3000 / portTICK_RATE_MS )\r
141 #define mainERROR_CHECK_DELAY           ( (portTickType) 500 / portTICK_RATE_MS )\r
142 \r
143 /* LED assignments for the demo tasks. */\r
144 #define mainNUM_FLASH_CO_ROUTINES       8\r
145 #define mainCOM_TEST_LED                        0x05\r
146 #define mainCHECK_TEST_LED                      0x07\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /* \r
151  * The function that implements the Check task.  See the comments at the head\r
152  * of the page for implementation details.\r
153  */\r
154 static void     vErrorChecks( void *pvParameters );\r
155 \r
156 /*\r
157  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
158  * to be operating without error - otherwise returns pdFAIL.\r
159  */\r
160 static short prvCheckOtherTasksAreStillRunning( void );\r
161 \r
162 /*\r
163  * Perform any hardware setup necessary for the demo.\r
164  */\r
165 static void prvSetupHardware( void );\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void main( void )\r
170 {\r
171         InitIrqLevels();                /* Initialize interrupts */\r
172         __set_il( 7 );                  /* Allow all levels      */\r
173 \r
174         prvSetupHardware();\r
175 \r
176         #if WATCHDOG == WTC_IN_TASK\r
177                 vStartWatchdogTask( WTC_TASK_PRIORITY );\r
178         #endif\r
179 \r
180         /* Start the standard demo application tasks. */\r
181         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
182         vStartIntegerMathTasks( tskIDLE_PRIORITY );     \r
183         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
184         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
185         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
186         vStartDynamicPriorityTasks();\r
187         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
188         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
189         vCreateBlockTimeTasks();\r
190 \r
191         /* The definition INCLUDE_TraceListTasks is set within FreeRTOSConfig.h. */\r
192         #if INCLUDE_TraceListTasks == 1\r
193                 vUtilityStartTraceTask( TASK_UTILITY_PRIORITY );\r
194         #else\r
195                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
196         #endif\r
197 \r
198         /* Start the 'Check' task which is defined in this file. */\r
199         xTaskCreate( vErrorChecks, (signed char *) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
200 \r
201         /* The suicide tasks must be started last as they record the number of other\r
202         tasks that exist within the system.  The value is then used to ensure at run\r
203         time the number of tasks that exists is within expected bounds. */\r
204         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
205 \r
206         /* Now start the scheduler.  Following this call the created tasks should\r
207         be executing. */        \r
208         vTaskStartScheduler( );\r
209         \r
210         /* vTaskStartScheduler() will only return if an error occurs while the \r
211         idle task is being created. */\r
212         for( ;; );\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 static void prvSetupHardware( void )\r
217 {\r
218         /* Initialise the port used by the LEDs. */\r
219         vParTestInitialise();\r
220 \r
221         /* See watchdog.h for definitions relating to the watchdog use. */\r
222         #if WATCHDOG != WTC_NONE\r
223                 InitWatchdog();\r
224         #endif\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static void vErrorChecks( void *pvParameters )\r
229 {\r
230 portTickType xDelayPeriod = mainNO_ERROR_CHECK_DELAY;\r
231 \r
232         /* Just to remove compiler warnings. */\r
233         ( void ) pvParameters;\r
234 \r
235         /* Cycle for ever, delaying then checking all the other tasks are still\r
236         operating without error. */\r
237         for( ;; )\r
238         {\r
239                 /* Wait until it is time to check again.  The time we wait here depends\r
240                 on whether an error has been detected or not.  When an error is \r
241                 detected the time is shortened resulting in a faster LED flash rate. */\r
242                 vTaskDelay( xDelayPeriod );\r
243 \r
244                 /* See if the other tasks are all ok. */\r
245                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
246                 {\r
247                         /* An error occurred in one of the tasks so shorten the delay \r
248                         period - which has the effect of increasing the frequency of the\r
249                         LED toggle. */\r
250                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
251                 }\r
252 \r
253                 /* Flash! */\r
254                 vParTestToggleLED( mainCHECK_TEST_LED );\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static short prvCheckOtherTasksAreStillRunning( void )\r
260 {\r
261         static short    sNoErrorFound = pdTRUE;\r
262 \r
263         /* The demo tasks maintain a count that increments every cycle of the task\r
264         provided that the task has never encountered an error.  This function \r
265         checks the counts maintained by the tasks to ensure they are still being\r
266         incremented.  A count remaining at the same value between calls therefore\r
267         indicates that an error has been detected.  Only tasks that do not flash\r
268         an LED are checked. */\r
269         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
270         {\r
271                 sNoErrorFound = pdFALSE;\r
272         }\r
273 \r
274         if( xArePollingQueuesStillRunning() != pdTRUE )\r
275         {\r
276                 sNoErrorFound = pdFALSE;\r
277         }\r
278 \r
279         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
280         {\r
281                 sNoErrorFound = pdFALSE;\r
282         }\r
283 \r
284         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
285         {\r
286                 sNoErrorFound = pdFALSE;\r
287         }\r
288 \r
289         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
290         {\r
291                 sNoErrorFound = pdFALSE;\r
292         }\r
293 \r
294         if( xAreFlashCoRoutinesStillRunning() != pdTRUE )\r
295         {\r
296                 sNoErrorFound = pdFALSE;\r
297         }\r
298 \r
299         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
300         {\r
301                 sNoErrorFound = pdFALSE;\r
302         }\r
303 \r
304         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
305         {\r
306                 sNoErrorFound = pdFALSE;\r
307         }\r
308 \r
309         if( xIsCreateTaskStillRunning() != pdTRUE )\r
310         {\r
311                 sNoErrorFound = pdFALSE;\r
312         }\r
313 \r
314         #if INCLUDE_TraceListTasks == 0\r
315         {\r
316                 if( xAreComTestTasksStillRunning() != pdTRUE )\r
317                 {\r
318                         sNoErrorFound = pdFALSE;\r
319                 }\r
320         }\r
321         #endif\r
322 \r
323         return sNoErrorFound;\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 /* Idle hook function. */\r
328 #if configUSE_IDLE_HOOK == 1\r
329         void vApplicationIdleHook( void )\r
330         {\r
331                 /* Are we using the idle task to kick the watchdog?  See watchdog.h\r
332                 for watchdog kicking options. Note this is for demonstration only\r
333                 and is not a suggested method of servicing the watchdog in a real\r
334                 application. */\r
335                 #if WATCHDOG == WTC_IN_IDLE\r
336                         Kick_Watchdog();\r
337                 #endif\r
338 \r
339                 vCoRoutineSchedule();\r
340         }\r
341 #else\r
342         #if WATCHDOG == WTC_IN_IDLE\r
343                 #error configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the idle task hook.\r
344         #endif\r
345 #endif\r
346 \r
347 /*-----------------------------------------------------------*/\r
348 \r
349 /* Tick hook function. */\r
350 #if configUSE_TICK_HOOK == 1\r
351         void vApplicationTickHook( void )\r
352         {\r
353                 /* Are we using the tick to kick the watchdog?  See watchdog.h\r
354                 for watchdog kicking options.  Note this is for demonstration\r
355                 only and is not a suggested method of servicing the watchdog in\r
356                 a real application. */\r
357                 #if WATCHDOG == WTC_IN_TICK\r
358                         Kick_Watchdog();\r
359                 #endif\r
360         }\r
361 #else\r
362         #if WATCHDOG == WTC_IN_TICK\r
363                 #error configUSE_TICK_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the tick hook.\r
364         #endif\r
365 #endif\r
366 /*-----------------------------------------------------------*/\r