2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
31 * documentation provides more details of the demo application tasks.
\r
33 * In addition to the standard demo tasks, the follow demo specific tasks are
\r
36 * The "Check" task. This only executes every three seconds but has the highest
\r
37 * priority so is guaranteed to get processor time. Its main function is to
\r
38 * check that all the other tasks are still operational. Most tasks maintain
\r
39 * a unique count that is incremented each time the task successfully completes
\r
40 * its function. Should any error occur within such a task the count is
\r
41 * permanently halted. The check task inspects the count of each task to ensure
\r
42 * it has changed since the last time the check task executed. If all the count
\r
43 * variables have changed all the tasks are still executing error free, and the
\r
44 * check task toggles the onboard LED. Should any task contain an error at any time
\r
45 * the LED toggle rate will change from 3 seconds to 500ms.
\r
47 * The "Register Check" tasks. These tasks fill the CPU registers with known
\r
48 * values, then check that each register still contains the expected value 0 the
\r
49 * discovery of an unexpected value being indicative of an error in the RTOS
\r
50 * context switch mechanism. The register check tasks operate at low priority
\r
51 * so are switched in and out frequently.
\r
53 * The "Trace Utility" task. This can be used to obtain trace and debug
\r
54 * information via UART5.
\r
58 /* Hardware specific includes. */
\r
59 #include "mb91467d.h"
\r
60 #include "vectors.h"
\r
61 #include "watchdog.h"
\r
63 /* Scheduler includes. */
\r
64 #include "FreeRTOS.h"
\r
67 /* Demo app includes. */
\r
69 #include "integer.h"
\r
70 #include "comtest2.h"
\r
71 #include "semtest.h"
\r
73 #include "dynamic.h"
\r
75 #include "GenQTest.h"
\r
77 #include "blocktim.h"
\r
79 #include "taskutility.h"
\r
80 #include "partest.h"
\r
81 #include "crflash.h"
\r
83 /* Demo task priorities. */
\r
84 #define mainWATCHDOG_TASK_PRIORITY ( tskIDLE_PRIORITY + 5 )
\r
85 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
86 #define mainUTILITY_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
87 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
88 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
89 #define mainQUEUE_BLOCK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
90 #define mainDEATH_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
91 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
92 #define mainGENERIC_QUEUE_PRIORITY ( tskIDLE_PRIORITY )
\r
94 /* Baud rate used by the COM test tasks. */
\r
95 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 19200 )
\r
97 /* The frequency at which the 'Check' tasks executes. See the comments at the
\r
98 top of the page. When the system is operating error free the 'Check' task
\r
99 toggles an LED every three seconds. If an error is discovered in any task the
\r
100 rate is increased to 500 milliseconds. [in this case the '*' characters on the
\r
101 LCD represent LEDs]*/
\r
102 #define mainNO_ERROR_CHECK_DELAY ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
103 #define mainERROR_CHECK_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
105 /* The total number of LEDs available. */
\r
106 #define mainNO_CO_ROUTINE_LEDs ( 8 )
\r
108 /* The first LED used by the comtest tasks. */
\r
109 #define mainCOM_TEST_LED ( 0x05 )
\r
111 /* The LED used by the check task. */
\r
112 #define mainCHECK_TEST_LED ( 0x07 )
\r
114 /* The number of interrupt levels to use. */
\r
115 #define mainINTERRUPT_LEVELS ( 31 )
\r
117 /* The number of 'flash' co-routines to create - each toggles a different LED. */
\r
118 #define mainNUM_FLASH_CO_ROUTINES ( 8 )
\r
120 /*---------------------------------------------------------------------------*/
\r
123 * The function that implements the Check task. See the comments at the head
\r
124 * of the page for implementation details.
\r
126 static void prvErrorChecks( void *pvParameters );
\r
129 * Called by the Check task. Returns pdPASS if all the other tasks are found
\r
130 * to be operating without error - otherwise returns pdFAIL.
\r
132 static short prvCheckOtherTasksAreStillRunning( void );
\r
135 * Setup the microcontroller as used by this demo.
\r
137 static void prvSetupHardware( void );
\r
140 * Tasks that test the context switch mechanism by filling the CPU registers
\r
141 * with known values then checking that each register contains the value
\r
142 * expected. Each of the two tasks use different values, and as low priority
\r
143 * tasks, get swapped in and out regularly.
\r
145 static void vFirstRegisterTestTask( void *pvParameters );
\r
146 static void vSecondRegisterTestTask( void *pvParameters );
\r
148 /*---------------------------------------------------------------------------*/
\r
150 /* The variable that is set to true should an error be found in one of the
\r
151 register test tasks. */
\r
152 unsigned long ulRegTestError = pdFALSE;
\r
154 /*---------------------------------------------------------------------------*/
\r
156 /* Start all the demo application tasks, then start the scheduler. */
\r
159 /* Initialise the hardware ready for the demo. */
\r
160 prvSetupHardware();
\r
162 /* Start the standard demo application tasks. */
\r
163 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
164 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
165 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );
\r
166 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
167 vStartBlockingQueueTasks ( mainQUEUE_BLOCK_PRIORITY );
\r
168 vStartDynamicPriorityTasks();
\r
169 vStartMathTasks( tskIDLE_PRIORITY );
\r
170 vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
\r
171 vStartQueuePeekTasks();
\r
172 vCreateBlockTimeTasks();
\r
173 vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );
\r
175 /* Start the 'Check' task which is defined in this file. */
\r
176 xTaskCreate( prvErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
178 /* Start the 'Register Test' tasks as described at the top of this file. */
\r
179 xTaskCreate( vFirstRegisterTestTask, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
180 xTaskCreate( vSecondRegisterTestTask, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
182 /* Start the task that write trace information to the UART. */
\r
183 vUtilityStartTraceTask( mainUTILITY_TASK_PRIORITY );
\r
185 /* If we are going to service the watchdog from within a task, then create
\r
187 #if WATCHDOG == WTC_IN_TASK
\r
188 vStartWatchdogTask( mainWATCHDOG_TASK_PRIORITY );
\r
191 /* The suicide tasks must be started last as they record the number of other
\r
192 tasks that exist within the system. The value is then used to ensure at run
\r
193 time the number of tasks that exists is within expected bounds. */
\r
194 vCreateSuicidalTasks( mainDEATH_PRIORITY );
\r
196 /* Now start the scheduler. Following this call the created tasks should
\r
198 vTaskStartScheduler( );
\r
200 /* vTaskStartScheduler() will only return if an error occurs while the
\r
201 idle task is being created. */
\r
204 /*-----------------------------------------------------------*/
\r
206 static void prvErrorChecks( void *pvParameters )
\r
208 TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY, xLastExecutionTime;
\r
210 /* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
\r
211 works correctly. */
\r
212 xLastExecutionTime = xTaskGetTickCount();
\r
214 /* Cycle for ever, delaying then checking all the other tasks are still
\r
215 operating without error. */
\r
218 /* Wait until it is time to check again. The time we wait here depends
\r
219 on whether an error has been detected or not. When an error is
\r
220 detected the time is shortened resulting in a faster LED flash rate. */
\r
221 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
222 vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );
\r
224 /* See if the other tasks are all ok. */
\r
225 if( prvCheckOtherTasksAreStillRunning() != pdPASS )
\r
227 /* An error occurred in one of the tasks so shorten the delay
\r
228 period - which has the effect of increasing the frequency of the
\r
230 xDelayPeriod = mainERROR_CHECK_DELAY;
\r
234 vParTestToggleLED( mainCHECK_TEST_LED );
\r
237 /*-----------------------------------------------------------*/
\r
239 static short prvCheckOtherTasksAreStillRunning( void )
\r
241 portBASE_TYPE lReturn = pdPASS;
\r
243 /* The demo tasks maintain a count that increments every cycle of the task
\r
244 provided that the task has never encountered an error. This function
\r
245 checks the counts maintained by the tasks to ensure they are still being
\r
246 incremented. A count remaining at the same value between calls therefore
\r
247 indicates that an error has been detected. */
\r
249 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
254 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
259 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
264 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
269 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
274 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
279 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
284 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
289 if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
294 if ( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
299 /* Have the register test tasks found any errors? */
\r
300 if( ulRegTestError != pdFALSE )
\r
307 /*-----------------------------------------------------------*/
\r
309 static void prvSetupHardware( void )
\r
311 /* Allow all interrupt levels. */
\r
312 __set_il( mainINTERRUPT_LEVELS );
\r
314 /* Initialise interrupts. */
\r
317 /* Initialise the ports used by the LEDs. */
\r
318 vParTestInitialise();
\r
320 /* If we are going to use the watchdog, then initialise it now. */
\r
321 #if WATCHDOG != WTC_NONE
\r
325 /*-----------------------------------------------------------*/
\r
327 /* Idle hook function. */
\r
328 #if configUSE_IDLE_HOOK == 1
\r
329 void vApplicationIdleHook( void )
\r
331 /* Are we using the idle task to kick the watchdog? See watchdog.h
\r
332 for watchdog kicking options. Note this is for demonstration only
\r
333 and is not a suggested method of servicing the watchdog in a real
\r
335 #if WATCHDOG == WTC_IN_IDLE
\r
339 vCoRoutineSchedule();
\r
342 #if WATCHDOG == WTC_IN_IDLE
\r
343 #error configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the idle task hook.
\r
347 /*-----------------------------------------------------------*/
\r
349 /* Tick hook function. */
\r
350 #if configUSE_TICK_HOOK == 1
\r
351 void vApplicationTickHook( void )
\r
353 /* Are we using the tick to kick the watchdog? See watchdog.h
\r
354 for watchdog kicking options. Note this is for demonstration
\r
355 only and is not a suggested method of servicing the watchdog in
\r
356 a real application. */
\r
357 #if WATCHDOG == WTC_IN_TICK
\r
362 #if WATCHDOG == WTC_IN_TICK
\r
363 #error configUSE_TICK_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the tick hook.
\r
366 /*-----------------------------------------------------------*/
\r
368 static void vFirstRegisterTestTask( void *pvParameters )
\r
370 extern volatile unsigned long ulCriticalNesting;
\r
372 /* Fills the registers with known values (different to the values
\r
373 used in vSecondRegisterTestTask()), then checks that the registers still
\r
374 all contain the expected value. This is done to test the context save
\r
375 and restore mechanism as this task is swapped onto and off of the CPU. */
\r
380 ;Load known values into each register.
\r
381 LDI #0x11111111, R0
\r
382 LDI #0x22222222, R1
\r
383 LDI #0x33333333, R2
\r
384 LDI #0x44444444, R3
\r
385 LDI #0x55555555, R4
\r
386 LDI #0x66666666, R5
\r
387 LDI #0x77777777, R6
\r
388 LDI #0x88888888, R7
\r
389 LDI #0x99999999, R8
\r
390 LDI #0xaaaaaaaa, R9
\r
391 LDI #0xbbbbbbbb, R10
\r
392 LDI #0xcccccccc, R11
\r
393 LDI #0xdddddddd, R12
\r
395 ;Check each register still contains the expected value.
\r
396 LDI #0x11111111, R13
\r
398 BNE First_Set_Error
\r
400 LDI #0x22222222, R13
\r
402 BNE First_Set_Error
\r
404 LDI #0x33333333, R13
\r
406 BNE First_Set_Error
\r
408 LDI #0x44444444, R13
\r
410 BNE First_Set_Error
\r
412 LDI #0x55555555, R13
\r
414 BNE First_Set_Error
\r
416 LDI #0x66666666, R13
\r
418 BNE First_Set_Error
\r
420 LDI #0x77777777, R13
\r
422 BNE First_Set_Error
\r
424 LDI #0x88888888, R13
\r
426 BNE First_Set_Error
\r
428 LDI #0x99999999, R13
\r
430 BNE First_Set_Error
\r
432 LDI #0xaaaaaaaa, R13
\r
434 BNE First_Set_Error
\r
436 LDI #0xbbbbbbbb, R13
\r
438 BNE First_Set_Error
\r
440 LDI #0xcccccccc, R13
\r
442 BNE First_Set_Error
\r
444 LDI #0xdddddddd, R13
\r
446 BNE First_Set_Error
\r
448 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
464 /*-----------------------------------------------------------*/
\r
466 static void vSecondRegisterTestTask( void *pvParameters )
\r
468 extern volatile unsigned long ulCriticalNesting;
\r
470 /* Fills the registers with known values (different to the values
\r
471 used in vFirstRegisterTestTask()), then checks that the registers still
\r
472 all contain the expected value. This is done to test the context save
\r
473 and restore mechanism as this task is swapped onto and off of the CPU. */
\r
478 ;Load known values into each register.
\r
479 LDI #0x11111111, R1
\r
480 LDI #0x22222222, R2
\r
482 LDI #0x33333333, R3
\r
483 LDI #0x44444444, R4
\r
484 LDI #0x55555555, R5
\r
485 LDI #0x66666666, R6
\r
486 LDI #0x77777777, R7
\r
487 LDI #0x88888888, R8
\r
488 LDI #0x99999999, R9
\r
490 LDI #0xaaaaaaaa, R10
\r
491 LDI #0xbbbbbbbb, R11
\r
492 LDI #0xcccccccc, R12
\r
493 LDI #0xdddddddd, R0
\r
495 ;Check each register still contains the expected value.
\r
496 LDI #0x11111111, R13
\r
498 BNE Second_Set_Error
\r
500 LDI #0x22222222, R13
\r
502 BNE Second_Set_Error
\r
504 LDI #0x33333333, R13
\r
506 BNE Second_Set_Error
\r
508 LDI #0x44444444, R13
\r
510 BNE Second_Set_Error
\r
512 LDI #0x55555555, R13
\r
514 BNE Second_Set_Error
\r
518 LDI #0x66666666, R13
\r
520 BNE Second_Set_Error
\r
522 LDI #0x77777777, R13
\r
524 BNE Second_Set_Error
\r
526 LDI #0x88888888, R13
\r
528 BNE Second_Set_Error
\r
530 LDI #0x99999999, R13
\r
532 BNE Second_Set_Error
\r
536 LDI #0xaaaaaaaa, R13
\r
538 BNE Second_Set_Error
\r
540 LDI #0xbbbbbbbb, R13
\r
542 BNE Second_Set_Error
\r
544 LDI #0xcccccccc, R13
\r
546 BNE Second_Set_Error
\r
548 LDI #0xdddddddd, R13
\r
550 BNE Second_Set_Error
\r
552 BRA Second_Start_Next_Loop
\r
556 ; Latch that an error has occurred.
\r
557 LDI #_ulRegTestError, R0
\r
558 LDI #0x00000001, R1
\r
562 Second_Start_Next_Loop:
\r
568 /*-----------------------------------------------------------*/
\r