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