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