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