3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 * Creates all the demo application tasks, then starts the scheduler. The WEB
30 * documentation provides more details of the standard demo application tasks.
31 * In addition to the standard demo tasks, the following tasks and tests are
32 * defined and/or created within this file:
34 * "Check" task - This only executes every three seconds but has a high priority
35 * to ensure it gets processor time. Its main function is to check that all the
36 * standard demo tasks are still operational. If everything is running as
37 * expected then the check task will toggle an LED every 3 seconds. An error
38 * being discovered in any task will cause the toggle rate to increase to 500ms.
40 * "Reg test" tasks - These fill the registers with known values, then check
41 * that each register still contains its expected value. Each task uses
42 * different values. The tasks run with very low priority so get preempted very
43 * frequently. A register containing an unexpected value is indicative of an
44 * error in the context switching mechanism.
48 /* Standard include files. */
52 /* Scheduler include files. */
56 /* Demo file headers. */
57 #include <intrinsics.h>
70 * Priority definitions for most of the tasks in the demo application. Some
71 * tasks just use the idle priority.
73 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 1 )
74 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
75 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
76 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
77 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
78 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
79 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
80 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
81 #define mainCOMTEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
83 /* Passed into the check task just as a test that the parameter passing
84 mechanism is working correctly. */
85 #define mainCHECK_PARAMETER ( ( void * ) 0x12345678 )
87 /* The period between executions of the check task. */
88 #define mainNO_ERROR_DELAY ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
89 #define mainERROR_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
91 /* There are no spare LEDs for the comtest tasks, so this is just set to an
93 #define mainCOMTEST_LED ( 4 )
95 /* The baud rate used by the comtest task. */
96 #define mainBAUD_RATE ( 9600 )
98 /*-----------------------------------------------------------*/
100 /* The implementation of the 'check' task as described at the top of this file. */
101 static void prvCheckTask( void *pvParameters );
103 /* Just sets up the LED outputs. Most generic setup is done in
104 __low_level_init(). */
105 static void prvSetupHardware( void );
107 /* The RegTest functions as described at the top of this file. */
108 extern void vRegTest1( void *pvParameters );
109 extern void vRegTest2( void *pvParameters );
111 /* A variable that will get set to fail if a RegTest task finds an error. The
112 variable is inspected by the 'Check' task. */
113 static volatile long lRegTestStatus = pdPASS;
115 /*-----------------------------------------------------------*/
117 /* Create all the demo tasks then start the scheduler. */
120 /* Just sets up the LED outputs. */
123 /* Standard demo tasks. */
124 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
125 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
126 vStartQueuePeekTasks();
128 /* Create the check task as described at the top of this file. */
129 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, mainCHECK_PARAMETER, mainCHECK_TASK_PRIORITY, NULL );
131 /* Create the RegTest tasks as described at the top of this file. */
132 xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
133 xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
135 #ifdef __IAR_V850ES_Fx3__
137 /* The extra IO required for the com test and led flashing tasks is only
138 available on the application board, not the target boards. */
139 vAltStartComTestTasks( mainCOMTEST_PRIORITY, mainBAUD_RATE, mainCOMTEST_LED );
140 vStartLEDFlashTasks( mainFLASH_PRIORITY );
142 /* The Fx3 also has enough RAM to run loads more tasks. */
143 vStartRecursiveMutexTasks();
144 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
145 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
149 /* The suicide tasks must be created last as they need to know how many
150 tasks were running prior to their creation in order to ascertain whether
151 or not the correct/expected number of tasks are running at any given time. */
152 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
154 /* Start the scheduler. */
155 vTaskStartScheduler();
157 /* If this line is reached then vTaskStartScheduler() returned because there
158 was insufficient heap memory remaining for the idle task to be created. */
161 /*-----------------------------------------------------------*/
163 static void prvCheckTask( void *pvParameters )
165 TickType_t xDelayPeriod = mainNO_ERROR_DELAY, xLastWakeTime;
166 unsigned portBASE_TYPE uxLEDToUse = 0;
168 /* Ensure parameter is passed in correctly. */
169 if( pvParameters != mainCHECK_PARAMETER )
171 xDelayPeriod = mainERROR_DELAY;
174 /* Initialise xLastWakeTime before it is used. After this point it is not
175 written to directly. */
176 xLastWakeTime = xTaskGetTickCount();
178 /* Cycle for ever, delaying then checking all the other tasks are still
179 operating without error. */
182 /* Wait until it is time to check all the other tasks again. */
183 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
185 if( lRegTestStatus != pdPASS )
187 xDelayPeriod = mainERROR_DELAY;
190 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
192 xDelayPeriod = mainERROR_DELAY;
195 if( xAreQueuePeekTasksStillRunning() != pdTRUE )
197 xDelayPeriod = mainERROR_DELAY;
200 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
202 xDelayPeriod = mainERROR_DELAY;
205 if( xIsCreateTaskStillRunning() != pdTRUE )
207 xDelayPeriod = mainERROR_DELAY;
210 /* The Fx3 runs more tasks, so more checks are performed. */
211 #ifdef __IAR_V850ES_Fx3__
213 if( xAreComTestTasksStillRunning() != pdTRUE )
215 xDelayPeriod = mainERROR_DELAY;
218 if( xArePollingQueuesStillRunning() != pdTRUE )
220 xDelayPeriod = mainERROR_DELAY;
223 if( xAreBlockingQueuesStillRunning() != pdTRUE )
225 xDelayPeriod = mainERROR_DELAY;
228 if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
230 xDelayPeriod = mainERROR_DELAY;
233 /* The application board has more LEDs and uses the flash tasks
234 so the check task instead uses LED3 as LED3 is still spare. */
239 /* Toggle the LED. The toggle rate will depend on whether or not an
240 error has been found in any tasks. */
241 vParTestToggleLED( uxLEDToUse );
244 /*-----------------------------------------------------------*/
246 static void prvSetupHardware( void )
248 /* Setup the LED outputs. */
249 vParTestInitialise();
251 /* Any additional hardware configuration can be added here. */
253 /*-----------------------------------------------------------*/
255 void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
260 /* This will be called if a task overflows its stack. pxCurrentTCB
261 can be inspected to see which is the offending task. */
264 /*-----------------------------------------------------------*/
266 void vRegTestFailed( void )
268 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
269 inspected by the check task. */
270 lRegTestStatus = pdFAIL;
272 /* Do not return from here as the reg test tasks clobber all registers so
273 function calls may not function correctly. */