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