2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
57 * documentation provides more details of the demo application tasks.
\r
59 * In addition to the standard demo tasks, the follow demo specific tasks are
\r
62 * The "Check" task. This only executes every three seconds but has the highest
\r
63 * priority so is guaranteed to get processor time. Its main function is to
\r
64 * check that all the other tasks are still operational. Most tasks maintain
\r
65 * a unique count that is incremented each time the task successfully completes
\r
66 * its function. Should any error occur within such a task the count is
\r
67 * permanently halted. The check task inspects the count of each task to ensure
\r
68 * it has changed since the last time the check task executed. If all the count
\r
69 * variables have changed all the tasks are still executing error free, and the
\r
70 * check task toggles the onboard LED. Should any task contain an error at any time
\r
71 * the LED toggle rate will change from 3 seconds to 500ms.
\r
73 * The "Register Check" tasks. These tasks fill the CPU registers with known
\r
74 * values, then check that each register still contains the expected value 0 the
\r
75 * discovery of an unexpected value being indicative of an error in the RTOS
\r
76 * context switch mechanism. The register check tasks operate at low priority
\r
77 * so are switched in and out frequently.
\r
79 * The "Trace Utility" task. This can be used to obtain trace and debug
\r
80 * information via UART5.
\r
84 /* Hardware specific includes. */
\r
85 #include "mb91467d.h"
\r
86 #include "vectors.h"
\r
87 #include "watchdog.h"
\r
89 /* Scheduler includes. */
\r
90 #include "FreeRTOS.h"
\r
93 /* Demo app includes. */
\r
95 #include "integer.h"
\r
96 #include "comtest2.h"
\r
97 #include "semtest.h"
\r
99 #include "dynamic.h"
\r
101 #include "GenQTest.h"
\r
103 #include "blocktim.h"
\r
105 #include "taskutility.h"
\r
106 #include "partest.h"
\r
107 #include "crflash.h"
\r
109 /* Demo task priorities. */
\r
110 #define mainWATCHDOG_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
111 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
112 #define mainUTILITY_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
113 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
114 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
115 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
116 #define mainDEATH_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
117 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
118 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
120 /* Baud rate used by the COM test tasks. */
\r
121 #define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 19200 )
\r
123 /* The frequency at which the 'Check' tasks executes. See the comments at the
\r
124 top of the page. When the system is operating error free the 'Check' task
\r
125 toggles an LED every three seconds. If an error is discovered in any task the
\r
126 rate is increased to 500 milliseconds. [in this case the '*' characters on the
\r
127 LCD represent LEDs]*/
\r
128 #define mainNO_ERROR_CHECK_DELAY ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
129 #define mainERROR_CHECK_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
131 /* The total number of LEDs available. */
\r
132 #define mainNO_CO_ROUTINE_LEDs ( 8 )
\r
134 /* The first LED used by the comtest tasks. */
\r
135 #define mainCOM_TEST_LED ( 0x05 )
\r
137 /* The LED used by the check task. */
\r
138 #define mainCHECK_TEST_LED ( 0x07 )
\r
140 /* The number of interrupt levels to use. */
\r
141 #define mainINTERRUPT_LEVELS ( 31 )
\r
143 /* The number of 'flash' co-routines to create - each toggles a different LED. */
\r
144 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
146 /*---------------------------------------------------------------------------*/
\r
149 * The function that implements the Check task. See the comments at the head
\r
150 * of the page for implementation details.
\r
152 static void prvErrorChecks( void *pvParameters );
\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
158 static portSHORT prvCheckOtherTasksAreStillRunning( void );
\r
161 * Setup the microcontroller as used by this demo.
\r
163 static void prvSetupHardware( void );
\r
166 * Tasks that test the context switch mechanism by filling the CPU registers
\r
167 * with known values then checking that each register contains the value
\r
168 * expected. Each of the two tasks use different values, and as low priority
\r
169 * tasks, get swapped in and out regularly.
\r
171 static void vFirstRegisterTestTask( void *pvParameters );
\r
172 static void vSecondRegisterTestTask( void *pvParameters );
\r
174 /*---------------------------------------------------------------------------*/
\r
176 /* The variable that is set to true should an error be found in one of the
\r
177 register test tasks. */
\r
178 unsigned portLONG ulRegTestError = pdFALSE;
\r
180 /*---------------------------------------------------------------------------*/
\r
182 /* Start all the demo application tasks, then start the scheduler. */
\r
185 /* Initialise the hardware ready for the demo. */
\r
186 prvSetupHardware();
\r
188 /* Start the standard demo application tasks. */
\r
189 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
190 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
191 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );
\r
192 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
193 vStartBlockingQueueTasks ( mainQUEUE_BLOCK_PRIORITY );
\r
194 vStartDynamicPriorityTasks();
\r
195 vStartMathTasks( tskIDLE_PRIORITY );
\r
196 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
197 vStartQueuePeekTasks();
\r
198 vCreateBlockTimeTasks();
\r
199 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
201 /* Start the 'Check' task which is defined in this file. */
\r
202 xTaskCreate( prvErrorChecks, ( signed portCHAR * ) "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
204 /* Start the 'Register Test' tasks as described at the top of this file. */
\r
205 xTaskCreate( vFirstRegisterTestTask, ( signed portCHAR * ) "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
206 xTaskCreate( vSecondRegisterTestTask, ( signed portCHAR * ) "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
208 /* Start the task that write trace information to the UART. */
\r
209 vUtilityStartTraceTask( mainUTILITY_TASK_PRIORITY );
\r
211 /* If we are going to service the watchdog from within a task, then create
\r
213 #if WATCHDOG == WTC_IN_TASK
\r
214 vStartWatchdogTask( mainWATCHDOG_TASK_PRIORITY );
\r
217 /* The suicide tasks must be started last as they record the number of other
\r
218 tasks that exist within the system. The value is then used to ensure at run
\r
219 time the number of tasks that exists is within expected bounds. */
\r
220 vCreateSuicidalTasks( mainDEATH_PRIORITY );
\r
222 /* Now start the scheduler. Following this call the created tasks should
\r
224 vTaskStartScheduler( );
\r
226 /* vTaskStartScheduler() will only return if an error occurs while the
\r
227 idle task is being created. */
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvErrorChecks( void *pvParameters )
\r
234 portTickType xDelayPeriod = mainNO_ERROR_CHECK_DELAY, xLastExecutionTime;
\r
236 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
237 works correctly. */
\r
238 xLastExecutionTime = xTaskGetTickCount();
\r
240 /* Cycle for ever, delaying then checking all the other tasks are still
\r
241 operating without error. */
\r
244 /* Wait until it is time to check again. The time we wait here depends
\r
245 on whether an error has been detected or not. When an error is
\r
246 detected the time is shortened resulting in a faster LED flash rate. */
\r
247 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
248 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\r
250 /* See if the other tasks are all ok. */
\r
251 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
\r
253 /* An error occurred in one of the tasks so shorten the delay
\r
254 period - which has the effect of increasing the frequency of the
\r
256 xDelayPeriod = mainERROR_CHECK_DELAY;
\r
260 vParTestToggleLED( mainCHECK_TEST_LED );
\r
263 /*-----------------------------------------------------------*/
\r
265 static portSHORT prvCheckOtherTasksAreStillRunning( void )
\r
267 portBASE_TYPE lReturn = pdPASS;
\r
269 /* The demo tasks maintain a count that increments every cycle of the task
\r
270 provided that the task has never encountered an error. This function
\r
271 checks the counts maintained by the tasks to ensure they are still being
\r
272 incremented. A count remaining at the same value between calls therefore
\r
273 indicates that an error has been detected. */
\r
275 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
280 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
285 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
290 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
295 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
300 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
305 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
310 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
315 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
320 if ( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
325 /* Have the register test tasks found any errors? */
\r
326 if( ulRegTestError != pdFALSE )
\r
333 /*-----------------------------------------------------------*/
\r
335 static void prvSetupHardware( void )
\r
337 /* Allow all interrupt levels. */
\r
338 __set_il( mainINTERRUPT_LEVELS );
\r
340 /* Initialise interrupts. */
\r
343 /* Initialise the ports used by the LEDs. */
\r
344 vParTestInitialise();
\r
346 /* If we are going to use the watchdog, then initialise it now. */
\r
347 #if WATCHDOG != WTC_NONE
\r
351 /*-----------------------------------------------------------*/
\r
353 /* Idle hook function. */
\r
354 #if configUSE_IDLE_HOOK == 1
\r
355 void vApplicationIdleHook( void )
\r
357 /* Are we using the idle task to kick the watchdog? See watchdog.h
\r
358 for watchdog kicking options. Note this is for demonstration only
\r
359 and is not a suggested method of servicing the watchdog in a real
\r
361 #if WATCHDOG == WTC_IN_IDLE
\r
365 vCoRoutineSchedule();
\r
368 #if WATCHDOG == WTC_IN_IDLE
\r
369 #error configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the idle task hook.
\r
373 /*-----------------------------------------------------------*/
\r
375 /* Tick hook function. */
\r
376 #if configUSE_TICK_HOOK == 1
\r
377 void vApplicationTickHook( void )
\r
379 /* Are we using the tick to kick the watchdog? See watchdog.h
\r
380 for watchdog kicking options. Note this is for demonstration
\r
381 only and is not a suggested method of servicing the watchdog in
\r
382 a real application. */
\r
383 #if WATCHDOG == WTC_IN_TICK
\r
388 #if WATCHDOG == WTC_IN_TICK
\r
389 #error configUSE_TICK_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the tick hook.
\r
392 /*-----------------------------------------------------------*/
\r
394 static void vFirstRegisterTestTask( void *pvParameters )
\r
396 extern volatile unsigned portLONG ulCriticalNesting;
\r
398 /* Fills the registers with known values (different to the values
\r
399 used in vSecondRegisterTestTask()), then checks that the registers still
\r
400 all contain the expected value. This is done to test the context save
\r
401 and restore mechanism as this task is swapped onto and off of the CPU. */
\r
406 ;Load known values into each register.
\r
407 LDI #0x11111111, R0
\r
408 LDI #0x22222222, R1
\r
409 LDI #0x33333333, R2
\r
410 LDI #0x44444444, R3
\r
411 LDI #0x55555555, R4
\r
412 LDI #0x66666666, R5
\r
413 LDI #0x77777777, R6
\r
414 LDI #0x88888888, R7
\r
415 LDI #0x99999999, R8
\r
416 LDI #0xaaaaaaaa, R9
\r
417 LDI #0xbbbbbbbb, R10
\r
418 LDI #0xcccccccc, R11
\r
419 LDI #0xdddddddd, R12
\r
421 ;Check each register still contains the expected value.
\r
422 LDI #0x11111111, R13
\r
424 BNE First_Set_Error
\r
426 LDI #0x22222222, R13
\r
428 BNE First_Set_Error
\r
430 LDI #0x33333333, R13
\r
432 BNE First_Set_Error
\r
434 LDI #0x44444444, R13
\r
436 BNE First_Set_Error
\r
438 LDI #0x55555555, R13
\r
440 BNE First_Set_Error
\r
442 LDI #0x66666666, R13
\r
444 BNE First_Set_Error
\r
446 LDI #0x77777777, R13
\r
448 BNE First_Set_Error
\r
450 LDI #0x88888888, R13
\r
452 BNE First_Set_Error
\r
454 LDI #0x99999999, R13
\r
456 BNE First_Set_Error
\r
458 LDI #0xaaaaaaaa, R13
\r
460 BNE First_Set_Error
\r
462 LDI #0xbbbbbbbb, R13
\r
464 BNE First_Set_Error
\r
466 LDI #0xcccccccc, R13
\r
468 BNE First_Set_Error
\r
470 LDI #0xdddddddd, R13
\r
472 BNE First_Set_Error
\r
474 BRA First_Start_Next_Loop
\r
478 ; Latch that an error has occurred.
\r
479 LDI #_ulRegTestError, R0
\r
480 LDI #0x00000001, R1
\r
484 First_Start_Next_Loop:
\r
490 /*-----------------------------------------------------------*/
\r
492 static void vSecondRegisterTestTask( void *pvParameters )
\r
494 extern volatile unsigned portLONG ulCriticalNesting;
\r
496 /* Fills the registers with known values (different to the values
\r
497 used in vFirstRegisterTestTask()), then checks that the registers still
\r
498 all contain the expected value. This is done to test the context save
\r
499 and restore mechanism as this task is swapped onto and off of the CPU. */
\r
504 ;Load known values into each register.
\r
505 LDI #0x11111111, R1
\r
506 LDI #0x22222222, R2
\r
508 LDI #0x33333333, R3
\r
509 LDI #0x44444444, R4
\r
510 LDI #0x55555555, R5
\r
511 LDI #0x66666666, R6
\r
512 LDI #0x77777777, R7
\r
513 LDI #0x88888888, R8
\r
514 LDI #0x99999999, R9
\r
516 LDI #0xaaaaaaaa, R10
\r
517 LDI #0xbbbbbbbb, R11
\r
518 LDI #0xcccccccc, R12
\r
519 LDI #0xdddddddd, R0
\r
521 ;Check each register still contains the expected value.
\r
522 LDI #0x11111111, R13
\r
524 BNE Second_Set_Error
\r
526 LDI #0x22222222, R13
\r
528 BNE Second_Set_Error
\r
530 LDI #0x33333333, R13
\r
532 BNE Second_Set_Error
\r
534 LDI #0x44444444, R13
\r
536 BNE Second_Set_Error
\r
538 LDI #0x55555555, R13
\r
540 BNE Second_Set_Error
\r
544 LDI #0x66666666, R13
\r
546 BNE Second_Set_Error
\r
548 LDI #0x77777777, R13
\r
550 BNE Second_Set_Error
\r
552 LDI #0x88888888, R13
\r
554 BNE Second_Set_Error
\r
556 LDI #0x99999999, R13
\r
558 BNE Second_Set_Error
\r
562 LDI #0xaaaaaaaa, R13
\r
564 BNE Second_Set_Error
\r
566 LDI #0xbbbbbbbb, R13
\r
568 BNE Second_Set_Error
\r
570 LDI #0xcccccccc, R13
\r
572 BNE Second_Set_Error
\r
574 LDI #0xdddddddd, R13
\r
576 BNE Second_Set_Error
\r
578 BRA Second_Start_Next_Loop
\r
582 ; Latch that an error has occurred.
\r
583 LDI #_ulRegTestError, R0
\r
584 LDI #0x00000001, R1
\r
588 Second_Start_Next_Loop:
\r
594 /*-----------------------------------------------------------*/
\r