1 /* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
\r
2 /* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
\r
3 /* ELIGIBILITY FOR ANY PURPOSES. */
\r
4 /* (C) Fujitsu Microelectronics Europe GmbH */
\r
5 /*------------------------------------------------------------------------
\r
8 - See README.TXT for project description and disclaimer.
\r
9 -------------------------------------------------------------------------*/
\r
13 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
14 * documentation provides more details of the demo application tasks.
\r
16 * Main.c also creates a task called "Check". This only executes every three
\r
17 * seconds but has the highest priority so is guaranteed to get processor time.
\r
18 * Its main function is to check that all the other tasks are still operational.
\r
19 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
20 * incremented each time the task successfully completes its function. Should
\r
21 * any error occur within such a task the count is permanently halted. The
\r
22 * check task inspects the count of each task to ensure it has changed since
\r
23 * the last time the check task executed. If all the count variables have
\r
24 * changed all the tasks are still executing error free, and the check task
\r
25 * toggles the onboard LED. Should any task contain an error at any time
\r
26 * the LED toggle rate will change from 3 seconds to 500ms.
\r
31 /* Hardware specific includes. */
\r
32 #include "mb91467d.h"
\r
33 #include "vectors.h"
\r
34 #include "watchdog.h"
\r
36 /* Scheduler includes. */
\r
37 #include "FreeRTOS.h"
\r
40 /* Demo app includes. */
\r
42 #include "integer.h"
\r
43 #include "comtest2.h"
\r
45 #include "semtest.h"
\r
47 #include "dynamic.h"
\r
49 #include "GenQTest.h"
\r
51 #include "BlockTim.h"
\r
53 #include "taskutility.h"
\r
54 #include "partest.h"
\r
56 /* Demo task priorities. */
\r
57 #define mainWATCHDOG_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
58 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
59 #define mainUTILITY_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
60 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
61 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
62 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
63 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
64 #define mainDEATH_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
65 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
66 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
68 /* Baud rate used by the COM test tasks. */
\r
69 #define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 19200 )
\r
71 /* The frequency at which the 'Check' tasks executes. See the comments at the
\r
72 top of the page. When the system is operating error free the 'Check' task
\r
73 toggles an LED every three seconds. If an error is discovered in any task the
\r
74 rate is increased to 500 milliseconds. [in this case the '*' characters on the
\r
75 LCD represent LED's]*/
\r
76 #define mainNO_ERROR_CHECK_DELAY ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
77 #define mainERROR_CHECK_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
79 /* The total number of LEDs available. */
\r
80 #define mainNO_CO_ROUTINE_LEDs ( 8 )
\r
82 /* The first LED used by the comtest tasks. */
\r
83 #define mainCOM_TEST_LED ( 0x05 )
\r
85 /* The LED used by the check task. */
\r
86 #define mainCHECK_TEST_LED ( 0x07 )
\r
88 /* The number of interrupt levels to use. */
\r
89 #define mainINTERRUPT_LEVELS ( 31 )
\r
91 /*---------------------------------------------------------------------------*/
\r
94 * The function that implements the Check task. See the comments at the head
\r
95 * of the page for implementation details.
\r
97 static void vErrorChecks( void *pvParameters );
\r
100 * Called by the Check task. Returns pdPASS if all the other tasks are found
\r
101 * to be operating without error - otherwise returns pdFAIL.
\r
103 static portSHORT prvCheckOtherTasksAreStillRunning( void );
\r
106 * Setup the microcontroller as used by this demo.
\r
108 static void prvSetupHardware( void );
\r
111 * Tasks that test the context switch mechanism by filling the CPU registers
\r
112 * with known values then checking that each register contains the value
\r
113 * expected. Each of the two tasks use different values, and as low priority
\r
114 * tasks, get swapped in and out regularly.
\r
116 static void vFirstRegisterTestTask( void *pvParameters );
\r
117 static void vSecondRegisterTestTask( void *pvParameters );
\r
119 /*---------------------------------------------------------------------------*/
\r
121 /* The variable that is set to true should an error be found in one of the
\r
122 register test tasks. */
\r
123 unsigned portLONG ulRegTestError = pdFALSE;
\r
125 /* Variables used to ensure the register check tasks are still executing. */
\r
126 static volatile unsigned portLONG ulRegTest1Counter = 0UL, ulRegTest2Counter = 0UL;
\r
128 /*---------------------------------------------------------------------------*/
\r
130 /* Start all the demo application tasks, then start the scheduler. */
\r
133 /* Initialise the hardware ready for the demo. */
\r
134 prvSetupHardware();
\r
136 /* Start the standard demo application tasks. */
\r
137 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
138 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
139 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );
\r
140 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
141 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
142 vStartBlockingQueueTasks ( mainQUEUE_BLOCK_PRIORITY );
\r
143 vStartDynamicPriorityTasks();
\r
144 vStartMathTasks( tskIDLE_PRIORITY );
\r
145 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
146 vStartQueuePeekTasks();
\r
147 vCreateBlockTimeTasks();
\r
149 /* Start the 'Check' task which is defined in this file. */
\r
150 xTaskCreate( vErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
152 xTaskCreate( vFirstRegisterTestTask, ( signed portCHAR * ) "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
153 xTaskCreate( vSecondRegisterTestTask, ( signed portCHAR * ) "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
155 /* Start the task that write trace information to the UART. */
\r
156 vUtilityStartTraceTask( mainUTILITY_TASK_PRIORITY );
\r
158 /* If we are going to service the watchdog from within a task, then create
\r
160 #if WATCHDOG == WTC_IN_TASK
\r
161 vStartWatchdogTask( mainWATCHDOG_TASK_PRIORITY );
\r
164 /* The suicide tasks must be started last as they record the number of other
\r
165 tasks that exist within the system. The value is then used to ensure at run
\r
166 time the number of tasks that exists is within expected bounds. */
\r
167 vCreateSuicidalTasks( mainDEATH_PRIORITY );
\r
169 /* Now start the scheduler. Following this call the created tasks should
\r
171 vTaskStartScheduler( );
\r
173 /* vTaskStartScheduler() will only return if an error occurs while the
\r
174 idle task is being created. */
\r
177 /*-----------------------------------------------------------*/
\r
179 static void vErrorChecks( void *pvParameters )
\r
181 portTickType xDelayPeriod = mainNO_ERROR_CHECK_DELAY, xLastExecutionTime;
\r
183 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
184 works correctly. */
\r
185 xLastExecutionTime = xTaskGetTickCount();
\r
187 /* Cycle for ever, delaying then checking all the other tasks are still
\r
188 operating without error. */
\r
191 /* Wait until it is time to check again. The time we wait here depends
\r
192 on whether an error has been detected or not. When an error is
\r
193 detected the time is shortened resulting in a faster LED flash rate. */
\r
194 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
195 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\r
197 /* See if the other tasks are all ok. */
\r
198 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
\r
200 /* An error occurred in one of the tasks so shorten the delay
\r
201 period - which has the effect of increasing the frequency of the
\r
203 xDelayPeriod = mainERROR_CHECK_DELAY;
\r
207 vParTestToggleLED( mainCHECK_TEST_LED );
\r
210 /*-----------------------------------------------------------*/
\r
212 static portSHORT prvCheckOtherTasksAreStillRunning( void )
\r
214 portBASE_TYPE lReturn = pdPASS;
\r
215 static unsigned portLONG ulLastRegTest1Counter = 0UL, ulLastRegTest2Counter = 0UL;
\r
217 /* The demo tasks maintain a count that increments every cycle of the task
\r
218 provided that the task has never encountered an error. This function
\r
219 checks the counts maintained by the tasks to ensure they are still being
\r
220 incremented. A count remaining at the same value between calls therefore
\r
221 indicates that an error has been detected. */
\r
223 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
228 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
233 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
238 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
243 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
248 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
253 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
258 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
263 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
268 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
273 if ( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
278 /* Have the register test tasks found any errors? */
\r
279 if( ulRegTestError != pdFALSE )
\r
284 /* Are the register test tasks still running? */
\r
285 if( ulLastRegTest1Counter == ulRegTest1Counter )
\r
290 if( ulLastRegTest2Counter == ulRegTest2Counter )
\r
295 ulLastRegTest1Counter = ulRegTest1Counter;
\r
296 ulLastRegTest2Counter = ulRegTest2Counter;
\r
300 /*-----------------------------------------------------------*/
\r
302 static void prvSetupHardware( void )
\r
304 /* Allow all interrupt levels. */
\r
305 __set_il( mainINTERRUPT_LEVELS );
\r
307 /* Initialise interrupts. */
\r
310 /* Initialise the ports used by the LEDs. */
\r
311 vParTestInitialise();
\r
313 /* If we are going to use the watchdog, then initialise it now. */
\r
314 #if WATCHDOG != WTC_NONE
\r
318 /*-----------------------------------------------------------*/
\r
320 /* The below callback function is called from Delayed ISR if configUSE_IDLE_HOOK
\r
321 is configured as 1. */
\r
322 #if configUSE_IDLE_HOOK == 1
\r
323 void vApplicationIdleHook( void )
\r
325 /* Are we using the idle task to kick the watchdog? */
\r
326 #if WATCHDOG == WTC_IN_IDLE
\r
330 #if configUSE_CO_ROUTINES == 1
\r
331 vCoRoutineSchedule();
\r
335 /*-----------------------------------------------------------*/
\r
338 The below callback function is called from Tick ISR if configUSE_TICK_HOOK
\r
339 is configured as 1. */
\r
340 #if configUSE_TICK_HOOK == 1
\r
341 void vApplicationTickHook( void )
\r
343 #if WATCHDOG == WTC_IN_TICK
\r
348 /*-----------------------------------------------------------*/
\r
350 static void vFirstRegisterTestTask( void *pvParameters )
\r
352 extern volatile unsigned portLONG ulCriticalNesting;
\r
354 /* Fills the registers with known values (different to the values
\r
355 used in vSecondRegisterTestTask()), then checks that the registers still
\r
356 all contain the expected value. This is done to test the context save
\r
357 and restore mechanism as this task is swapped onto and off of the CPU.
\r
359 The critical nesting depth is also saved as part of the context so also
\r
360 check this maintains an expected value. */
\r
361 ulCriticalNesting = 0x12345678;
\r
366 ;Load known values into each register.
\r
367 LDI #0x11111111, R0
\r
368 LDI #0x22222222, R1
\r
369 LDI #0x33333333, R2
\r
370 LDI #0x44444444, R3
\r
371 LDI #0x55555555, R4
\r
372 LDI #0x66666666, R5
\r
373 LDI #0x77777777, R6
\r
374 LDI #0x88888888, R7
\r
375 LDI #0x99999999, R8
\r
376 LDI #0xaaaaaaaa, R9
\r
377 LDI #0xbbbbbbbb, R10
\r
378 LDI #0xcccccccc, R11
\r
379 LDI #0xdddddddd, R12
\r
381 ;Check each register still contains the expected value.
\r
382 LDI #0x11111111, R13
\r
384 BNE First_Set_Error
\r
387 LDI #0x22222222, R13
\r
389 BNE First_Set_Error
\r
392 LDI #0x33333333, R13
\r
394 BNE First_Set_Error
\r
397 LDI #0x44444444, R13
\r
399 BNE First_Set_Error
\r
402 LDI #0x55555555, R13
\r
404 BNE First_Set_Error
\r
407 LDI #0x66666666, R13
\r
409 BNE First_Set_Error
\r
412 LDI #0x77777777, R13
\r
414 BNE First_Set_Error
\r
417 LDI #0x88888888, R13
\r
419 BNE First_Set_Error
\r
422 LDI #0x99999999, R13
\r
424 BNE First_Set_Error
\r
427 LDI #0xaaaaaaaa, R13
\r
429 BNE First_Set_Error
\r
432 LDI #0xbbbbbbbb, R13
\r
434 BNE First_Set_Error
\r
437 LDI #0xcccccccc, R13
\r
439 BNE First_Set_Error
\r
442 LDI #0xdddddddd, R13
\r
444 BNE First_Set_Error
\r
447 BRA First_Start_Next_Loop
\r
452 ; Latch that an error has occurred.
\r
453 LDI #_ulRegTestError, R0
\r
454 LDI #0x00000001, R1
\r
458 First_Start_Next_Loop:
\r
463 ulRegTest1Counter++;
\r
465 if( ulCriticalNesting != 0x12345678 )
\r
467 ulRegTestError = pdTRUE;
\r
471 /*-----------------------------------------------------------*/
\r
473 static void vSecondRegisterTestTask( void *pvParameters )
\r
475 extern volatile unsigned portLONG ulCriticalNesting;
\r
477 /* Fills the registers with known values (different to the values
\r
478 used in vFirstRegisterTestTask()), then checks that the registers still
\r
479 all contain the expected value. This is done to test the context save
\r
480 and restore mechanism as this task is swapped onto and off of the CPU.
\r
482 The critical nesting depth is also saved as part of the context so also
\r
483 check this maintains an expected value. */
\r
484 ulCriticalNesting = 0x87654321;
\r
489 ;Load known values into each register.
\r
490 LDI #0x11111111, R1
\r
491 LDI #0x22222222, R2
\r
492 LDI #0x33333333, R3
\r
493 LDI #0x44444444, R4
\r
494 LDI #0x55555555, R5
\r
495 LDI #0x66666666, R6
\r
496 LDI #0x77777777, R7
\r
497 LDI #0x88888888, R8
\r
498 LDI #0x99999999, R9
\r
499 LDI #0xaaaaaaaa, R10
\r
500 LDI #0xbbbbbbbb, R11
\r
501 LDI #0xcccccccc, R12
\r
502 LDI #0xdddddddd, R0
\r
504 ;Check each register still contains the expected value.
\r
505 LDI #0x11111111, R13
\r
507 BNE Second_Set_Error
\r
510 LDI #0x22222222, R13
\r
512 BNE Second_Set_Error
\r
515 LDI #0x33333333, R13
\r
517 BNE Second_Set_Error
\r
520 LDI #0x44444444, R13
\r
522 BNE Second_Set_Error
\r
525 LDI #0x55555555, R13
\r
527 BNE Second_Set_Error
\r
530 LDI #0x66666666, R13
\r
532 BNE Second_Set_Error
\r
535 LDI #0x77777777, R13
\r
537 BNE Second_Set_Error
\r
540 LDI #0x88888888, R13
\r
542 BNE Second_Set_Error
\r
545 LDI #0x99999999, R13
\r
547 BNE Second_Set_Error
\r
550 LDI #0xaaaaaaaa, R13
\r
552 BNE Second_Set_Error
\r
555 LDI #0xbbbbbbbb, R13
\r
557 BNE Second_Set_Error
\r
560 LDI #0xcccccccc, R13
\r
562 BNE Second_Set_Error
\r
565 LDI #0xdddddddd, R13
\r
567 BNE Second_Set_Error
\r
570 BRA Second_Start_Next_Loop
\r
575 ; Latch that an error has occurred.
\r
576 LDI #_ulRegTestError, R0
\r
577 LDI #0x00000001, R1
\r
581 Second_Start_Next_Loop:
\r
586 ulRegTest2Counter++;
\r
588 if( ulCriticalNesting != 0x87654321 )
\r
590 ulRegTestError = pdTRUE;
\r
594 /*-----------------------------------------------------------*/
\r