]> git.sur5r.net Git - freertos/blob - Demo/MB91460_Softune/SRC/main.c
Small mods, and update file headers.
[freertos] / Demo / MB91460_Softune / SRC / main.c
1 /*\r
2         FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 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     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 \r
51 /*\r
52  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
53  * documentation provides more details of the demo application tasks.\r
54  * \r
55  * In addition to the standard demo tasks, the follow demo specific tasks are\r
56  * create:\r
57  *\r
58  * The "Check" task.  This only executes every three seconds but has the highest \r
59  * priority so is guaranteed to get processor time.  Its main function is to \r
60  * check that all the other tasks are still operational.  Most tasks maintain \r
61  * a unique count that is incremented each time the task successfully completes \r
62  * its function.  Should any error occur within such a task the count is \r
63  * permanently halted.  The check task inspects the count of each task to ensure \r
64  * it has changed since the last time the check task executed.  If all the count \r
65  * variables have changed all the tasks are still executing error free, and the \r
66  * check task toggles the onboard LED.  Should any task contain an error at any time \r
67  * the LED toggle rate will change from 3 seconds to 500ms.\r
68  *\r
69  * The "Register Check" tasks.  These tasks fill the CPU registers with known\r
70  * values, then check that each register still contains the expected value 0 the\r
71  * discovery of an unexpected value being indicative of an error in the RTOS\r
72  * context switch mechanism.  The register check tasks operate at low priority\r
73  * so are switched in and out frequently.\r
74  *\r
75  * The "Trace Utility" task.  This can be used to obtain trace and debug \r
76  * information via UART5.\r
77  */\r
78 \r
79 \r
80 /* Hardware specific includes. */\r
81 #include "mb91467d.h"\r
82 #include "vectors.h"\r
83 #include "watchdog.h"\r
84 \r
85 /* Scheduler includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 \r
89 /* Demo app includes. */\r
90 #include "flash.h"\r
91 #include "integer.h"\r
92 #include "comtest2.h"\r
93 #include "semtest.h"\r
94 #include "BlockQ.h"\r
95 #include "dynamic.h"\r
96 #include "flop.h"\r
97 #include "GenQTest.h"\r
98 #include "QPeek.h"\r
99 #include "blocktim.h"\r
100 #include "death.h"\r
101 #include "taskutility.h"\r
102 #include "partest.h"\r
103 #include "crflash.h"\r
104         \r
105 /* Demo task priorities. */\r
106 #define mainWATCHDOG_TASK_PRIORITY              ( tskIDLE_PRIORITY + 5 )\r
107 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 4 )\r
108 #define mainUTILITY_TASK_PRIORITY               ( tskIDLE_PRIORITY )\r
109 #define mainSEM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
110 #define mainCOM_TEST_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 LEDs]*/\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 /* The total number of LEDs available. */\r
128 #define mainNO_CO_ROUTINE_LEDs  ( 8 )\r
129 \r
130 /* The first LED used by the comtest tasks. */\r
131 #define mainCOM_TEST_LED                ( 0x05 )\r
132 \r
133 /* The LED used by the check task. */\r
134 #define mainCHECK_TEST_LED              ( 0x07 )\r
135 \r
136 /* The number of interrupt levels to use. */\r
137 #define mainINTERRUPT_LEVELS    ( 31 )\r
138 \r
139 /* The number of 'flash' co-routines to create - each toggles a different LED. */\r
140 #define mainNUM_FLASH_CO_ROUTINES       ( 8 )\r
141 \r
142 /*---------------------------------------------------------------------------*/\r
143 \r
144 /* \r
145  * The function that implements the Check task.  See the comments at the head\r
146  * of the page for implementation details.\r
147  */ \r
148 static void prvErrorChecks( void *pvParameters );\r
149 \r
150 /*\r
151  * Called by the Check task.  Returns pdPASS if all the other tasks are found\r
152  * to be operating without error - otherwise returns pdFAIL.\r
153  */\r
154 static portSHORT prvCheckOtherTasksAreStillRunning( void );\r
155 \r
156 /* \r
157  * Setup the microcontroller as used by this demo. \r
158  */\r
159 static void prvSetupHardware( void );\r
160 \r
161 /*\r
162  * Tasks that test the context switch mechanism by filling the CPU registers\r
163  * with known values then checking that each register contains the value\r
164  * expected.  Each of the two tasks use different values, and as low priority\r
165  * tasks, get swapped in and out regularly.\r
166  */\r
167 static void vFirstRegisterTestTask( void *pvParameters );\r
168 static void vSecondRegisterTestTask( void *pvParameters );\r
169 \r
170 /*---------------------------------------------------------------------------*/\r
171 \r
172 /* The variable that is set to true should an error be found in one of the \r
173 register test tasks. */\r
174 unsigned portLONG ulRegTestError = pdFALSE;\r
175 \r
176 /*---------------------------------------------------------------------------*/\r
177 \r
178 /* Start all the demo application tasks, then start the scheduler. */\r
179 void main(void)\r
180 {\r
181         /* Initialise the hardware ready for the demo. */       \r
182         prvSetupHardware();\r
183 \r
184         /* Start the standard demo application tasks. */\r
185         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );   \r
186         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
187         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );\r
188         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
189         vStartBlockingQueueTasks ( mainQUEUE_BLOCK_PRIORITY );  \r
190         vStartDynamicPriorityTasks();   \r
191         vStartMathTasks( tskIDLE_PRIORITY );    \r
192         vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );\r
193         vStartQueuePeekTasks();\r
194         vCreateBlockTimeTasks();\r
195         vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );\r
196 \r
197         /* Start the 'Check' task which is defined in this file. */\r
198         xTaskCreate( prvErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );    \r
199 \r
200         /* Start the 'Register Test' tasks as described at the top of this file. */\r
201         xTaskCreate( vFirstRegisterTestTask, ( signed portCHAR * ) "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
202         xTaskCreate( vSecondRegisterTestTask, ( signed portCHAR * ) "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
203 \r
204         /* Start the task that write trace information to the UART. */  \r
205         vUtilityStartTraceTask( mainUTILITY_TASK_PRIORITY );\r
206 \r
207         /* If we are going to service the watchdog from within a task, then create\r
208         the task here. */       \r
209         #if WATCHDOG == WTC_IN_TASK     \r
210                 vStartWatchdogTask( mainWATCHDOG_TASK_PRIORITY );\r
211         #endif          \r
212         \r
213         /* The suicide tasks must be started last as they record the number of other\r
214         tasks that exist within the system.  The value is then used to ensure at run\r
215         time the number of tasks that exists is within expected bounds. */\r
216         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
217 \r
218         /* Now start the scheduler.  Following this call the created tasks should\r
219         be executing. */        \r
220         vTaskStartScheduler( );\r
221         \r
222         /* vTaskStartScheduler() will only return if an error occurs while the \r
223         idle task is being created. */\r
224         for( ;; );\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 static void prvErrorChecks( void *pvParameters )\r
229 {\r
230 portTickType xDelayPeriod = mainNO_ERROR_CHECK_DELAY, xLastExecutionTime;\r
231 \r
232         /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()\r
233         works correctly. */\r
234         xLastExecutionTime = xTaskGetTickCount();\r
235 \r
236         /* Cycle for ever, delaying then checking all the other tasks are still\r
237         operating without error. */\r
238         for( ;; )\r
239         {\r
240                 /* Wait until it is time to check again.  The time we wait here depends\r
241                 on whether an error has been detected or not.  When an error is \r
242                 detected the time is shortened resulting in a faster LED flash rate. */\r
243                 /* Perform this check every mainCHECK_DELAY milliseconds. */\r
244                 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );\r
245 \r
246                 /* See if the other tasks are all ok. */\r
247                 if( prvCheckOtherTasksAreStillRunning() != pdPASS )\r
248                 {\r
249                         /* An error occurred in one of the tasks so shorten the delay \r
250                         period - which has the effect of increasing the frequency of the\r
251                         LED toggle. */\r
252                         xDelayPeriod = mainERROR_CHECK_DELAY;\r
253                 }\r
254 \r
255                 /* Flash! */\r
256                 vParTestToggleLED( mainCHECK_TEST_LED );\r
257         }\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static portSHORT prvCheckOtherTasksAreStillRunning( void )\r
262 {\r
263 portBASE_TYPE lReturn = pdPASS;\r
264 \r
265         /* The demo tasks maintain a count that increments every cycle of the task\r
266         provided that the task has never encountered an error.  This function \r
267         checks the counts maintained by the tasks to ensure they are still being\r
268         incremented.  A count remaining at the same value between calls therefore\r
269         indicates that an error has been detected. */\r
270 \r
271         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
272         {\r
273                 lReturn = pdFAIL;\r
274         }\r
275 \r
276         if( xAreComTestTasksStillRunning() != pdTRUE )\r
277         {\r
278                 lReturn = pdFAIL;\r
279         }\r
280         \r
281         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
282         {\r
283                 lReturn = pdFAIL;\r
284         }\r
285         \r
286         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
287         {\r
288                 lReturn = pdFAIL;\r
289         }\r
290         \r
291         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
292         {\r
293                 lReturn = pdFAIL;\r
294         }\r
295         \r
296         if( xAreMathsTaskStillRunning() != pdTRUE )\r
297         {\r
298                 lReturn = pdFAIL;\r
299         }\r
300         \r
301         if( xIsCreateTaskStillRunning() != pdTRUE )\r
302         {\r
303                 lReturn = pdFAIL;\r
304         }\r
305         \r
306         if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
307         {\r
308                 lReturn = pdFAIL;\r
309         }\r
310         \r
311         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
312         {\r
313                 lReturn = pdFAIL;\r
314         }\r
315         \r
316         if ( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
317         {\r
318                 lReturn = pdFAIL;\r
319         }\r
320 \r
321         /* Have the register test tasks found any errors? */\r
322         if( ulRegTestError != pdFALSE )\r
323         {\r
324                 lReturn = pdFAIL;\r
325         }\r
326 \r
327         return lReturn;\r
328 }\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 static void prvSetupHardware( void )\r
332 {\r
333         /* Allow all interrupt levels. */\r
334         __set_il( mainINTERRUPT_LEVELS );\r
335 \r
336         /* Initialise interrupts. */\r
337         InitIrqLevels();\r
338 \r
339         /* Initialise the ports used by the LEDs. */\r
340         vParTestInitialise();\r
341 \r
342         /* If we are going to use the watchdog, then initialise it now. */\r
343         #if WATCHDOG != WTC_NONE        \r
344                 InitWatchdog();\r
345         #endif\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 /* Idle hook function. */\r
350 #if configUSE_IDLE_HOOK == 1\r
351         void vApplicationIdleHook( void )\r
352         {\r
353                 /* Are we using the idle task to kick the watchdog?  See watchdog.h\r
354                 for watchdog kicking options. Note this is for demonstration only\r
355                 and is not a suggested method of servicing the watchdog in a real\r
356                 application. */\r
357                 #if WATCHDOG == WTC_IN_IDLE\r
358                         Kick_Watchdog();\r
359                 #endif\r
360 \r
361                 vCoRoutineSchedule();\r
362         }\r
363 #else\r
364         #if WATCHDOG == WTC_IN_IDLE\r
365                 #error configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the idle task hook.\r
366         #endif\r
367 #endif\r
368 \r
369 /*-----------------------------------------------------------*/\r
370 \r
371 /* Tick hook function. */\r
372 #if configUSE_TICK_HOOK == 1\r
373         void vApplicationTickHook( void )\r
374         {\r
375                 /* Are we using the tick to kick the watchdog?  See watchdog.h\r
376                 for watchdog kicking options.  Note this is for demonstration\r
377                 only and is not a suggested method of servicing the watchdog in\r
378                 a real application. */\r
379                 #if WATCHDOG == WTC_IN_TICK\r
380                         Kick_Watchdog();\r
381                 #endif\r
382         }\r
383 #else\r
384         #if WATCHDOG == WTC_IN_TICK\r
385                 #error configUSE_TICK_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the tick hook.\r
386         #endif\r
387 #endif\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 static void vFirstRegisterTestTask( void *pvParameters )\r
391 {\r
392 extern volatile unsigned portLONG ulCriticalNesting;\r
393 \r
394         /* Fills the registers with known values (different to the values\r
395         used in vSecondRegisterTestTask()), then checks that the registers still\r
396         all contain the expected value.  This is done to test the context save\r
397         and restore mechanism as this task is swapped onto and off of the CPU. */\r
398 \r
399         for( ;; )\r
400         {\r
401                 #pragma asm\r
402                         ;Load known values into each register.\r
403                         LDI     #0x11111111, R0\r
404                         LDI     #0x22222222, R1\r
405                         LDI     #0x33333333, R2\r
406                         LDI #0x44444444, R3\r
407                         LDI     #0x55555555, R4\r
408                         LDI     #0x66666666, R5\r
409                         LDI     #0x77777777, R6\r
410                         LDI     #0x88888888, R7\r
411                         LDI     #0x99999999, R8\r
412                         LDI     #0xaaaaaaaa, R9\r
413                         LDI     #0xbbbbbbbb, R10\r
414                         LDI     #0xcccccccc, R11\r
415                         LDI     #0xdddddddd, R12\r
416                         \r
417                         ;Check each register still contains the expected value.\r
418                         LDI #0x11111111, R13\r
419                         CMP R13, R0\r
420                         BNE First_Set_Error\r
421 \r
422                         LDI #0x22222222, R13\r
423                         CMP R13, R1\r
424                         BNE First_Set_Error\r
425 \r
426                         LDI #0x33333333, R13\r
427                         CMP R13, R2\r
428                         BNE First_Set_Error\r
429 \r
430                         LDI #0x44444444, R13\r
431                         CMP R13, R3\r
432                         BNE First_Set_Error\r
433 \r
434                         LDI #0x55555555, R13\r
435                         CMP R13, R4\r
436                         BNE First_Set_Error\r
437 \r
438                         LDI #0x66666666, R13\r
439                         CMP R13, R5\r
440                         BNE First_Set_Error\r
441 \r
442                         LDI #0x77777777, R13\r
443                         CMP R13, R6\r
444                         BNE First_Set_Error\r
445 \r
446                         LDI #0x88888888, R13\r
447                         CMP R13, R7\r
448                         BNE First_Set_Error\r
449 \r
450                         LDI #0x99999999, R13\r
451                         CMP R13, R8\r
452                         BNE First_Set_Error\r
453 \r
454                         LDI #0xaaaaaaaa, R13\r
455                         CMP R13, R9\r
456                         BNE First_Set_Error\r
457 \r
458                         LDI #0xbbbbbbbb, R13\r
459                         CMP R13, R10\r
460                         BNE First_Set_Error\r
461 \r
462                         LDI #0xcccccccc, R13\r
463                         CMP R13, R11\r
464                         BNE First_Set_Error\r
465 \r
466                         LDI #0xdddddddd, R13\r
467                         CMP R13, R12\r
468                         BNE First_Set_Error\r
469 \r
470                         BRA First_Start_Next_Loop\r
471 \r
472                 First_Set_Error:\r
473 \r
474                         ; Latch that an error has occurred.\r
475                         LDI #_ulRegTestError, R0                        \r
476                         LDI #0x00000001, R1\r
477                         ST R1, @R0\r
478 \r
479 \r
480                 First_Start_Next_Loop:\r
481 \r
482 \r
483                 #pragma endasm\r
484         }\r
485 }\r
486 /*-----------------------------------------------------------*/\r
487 \r
488 static void vSecondRegisterTestTask( void *pvParameters )\r
489 {\r
490 extern volatile unsigned portLONG ulCriticalNesting;\r
491 \r
492         /* Fills the registers with known values (different to the values\r
493         used in vFirstRegisterTestTask()), then checks that the registers still\r
494         all contain the expected value.  This is done to test the context save\r
495         and restore mechanism as this task is swapped onto and off of the CPU. */\r
496 \r
497         for( ;; )\r
498         {\r
499                 #pragma asm\r
500                         ;Load known values into each register.\r
501                         LDI     #0x11111111, R1\r
502                         LDI     #0x22222222, R2\r
503                         INT #40H\r
504                         LDI     #0x33333333, R3\r
505                         LDI #0x44444444, R4\r
506                         LDI     #0x55555555, R5\r
507                         LDI     #0x66666666, R6\r
508                         LDI     #0x77777777, R7\r
509                         LDI     #0x88888888, R8\r
510                         LDI     #0x99999999, R9\r
511                         INT #40H\r
512                         LDI     #0xaaaaaaaa, R10\r
513                         LDI     #0xbbbbbbbb, R11\r
514                         LDI     #0xcccccccc, R12\r
515                         LDI     #0xdddddddd, R0\r
516                         \r
517                         ;Check each register still contains the expected value.\r
518                         LDI #0x11111111, R13\r
519                         CMP R13, R1\r
520                         BNE Second_Set_Error\r
521 \r
522                         LDI #0x22222222, R13\r
523                         CMP R13, R2\r
524                         BNE Second_Set_Error\r
525 \r
526                         LDI #0x33333333, R13\r
527                         CMP R13, R3\r
528                         BNE Second_Set_Error\r
529 \r
530                         LDI #0x44444444, R13\r
531                         CMP R13, R4\r
532                         BNE Second_Set_Error\r
533 \r
534                         LDI #0x55555555, R13\r
535                         CMP R13, R5\r
536                         BNE Second_Set_Error\r
537 \r
538                         INT #40H\r
539 \r
540                         LDI #0x66666666, R13\r
541                         CMP R13, R6\r
542                         BNE Second_Set_Error\r
543 \r
544                         LDI #0x77777777, R13\r
545                         CMP R13, R7\r
546                         BNE Second_Set_Error\r
547 \r
548                         LDI #0x88888888, R13\r
549                         CMP R13, R8\r
550                         BNE Second_Set_Error\r
551 \r
552                         LDI #0x99999999, R13\r
553                         CMP R13, R9\r
554                         BNE Second_Set_Error\r
555 \r
556                         INT #40H\r
557 \r
558                         LDI #0xaaaaaaaa, R13\r
559                         CMP R13, R10\r
560                         BNE Second_Set_Error\r
561 \r
562                         LDI #0xbbbbbbbb, R13\r
563                         CMP R13, R11\r
564                         BNE Second_Set_Error\r
565 \r
566                         LDI #0xcccccccc, R13\r
567                         CMP R13, R12\r
568                         BNE Second_Set_Error\r
569 \r
570                         LDI #0xdddddddd, R13\r
571                         CMP R13, R0\r
572                         BNE Second_Set_Error\r
573 \r
574                         BRA Second_Start_Next_Loop\r
575 \r
576                 Second_Set_Error:\r
577 \r
578                         ; Latch that an error has occurred.\r
579                         LDI #_ulRegTestError, R0                        \r
580                         LDI #0x00000001, R1\r
581                         ST R1, @R0\r
582 \r
583 \r
584                 Second_Start_Next_Loop:\r
585 \r
586 \r
587                 #pragma endasm\r
588         }\r
589 }\r
590 /*-----------------------------------------------------------*/\r
591 \r
592 \r