3 * FreeRTOS Kernel V10.0.1
4 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7 * this software and associated documentation files (the "Software"), to deal in
8 * the Software without restriction, including without limitation the rights to
9 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10 * the Software, and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * http://www.FreeRTOS.org
24 * http://aws.amazon.com/freertos
32 * vMain() is effectively the demo application entry point. It is called by
33 * the main() function generated by the Processor Expert application.
35 * vMain() creates all the demo application tasks, then starts the scheduler.
36 * The WEB documentation provides more details of the demo application tasks.
38 * Main.c also creates a task called "Check". This only executes every three
39 * seconds but has the highest priority so is guaranteed to get processor time.
40 * Its main function is to check that all the other tasks are still operational.
41 * Each task (other than the "flash" tasks) maintains a unique count that is
42 * incremented each time the task successfully completes its function. Should
43 * any error occur within such a task the count is permanently halted. The
44 * check task inspects the count of each task to ensure it has changed since
45 * the last time the check task executed. If all the count variables have
46 * changed all the tasks are still executing error free, and the check task
47 * toggles the onboard LED. Should any task contain an error at any time
48 * the LED toggle rate will change from 3 seconds to 500ms.
50 * This file also includes the functionality implemented within the
51 * standard demo application file integer.c. This is done to demonstrate the
52 * use of an idle hook. See the documentation within integer.c for the
53 * rationale of the integer task functionality.
56 /* Kernel includes. */
61 /* Demo application includes. */
72 /*-----------------------------------------------------------
74 -----------------------------------------------------------*/
76 /* Priorities assigned to demo application tasks. */
77 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
78 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
79 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
80 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
81 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
82 #define mainDEATH_PRIORITY ( tskIDLE_PRIORITY + 1 )
84 /* LED that is toggled by the check task. The check task periodically checks
85 that all the other tasks are operating without error. If no errors are found
86 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
87 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
88 #define mainCHECK_TASK_LED ( 7 )
89 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
90 #define mainERROR_CHECK_PERIOD ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
92 /* The constants used in the idle task calculation. */
93 #define intgCONST1 ( ( long ) 123 )
94 #define intgCONST2 ( ( long ) 234567 )
95 #define intgCONST3 ( ( long ) -3 )
96 #define intgCONST4 ( ( long ) 7 )
97 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
100 /* Baud rate used by the serial port tasks (ComTest tasks).
101 IMPORTANT: The function COM0_SetBaudRateValue() which is generated by the
102 Processor Expert is used to set the baud rate. As configured in the FreeRTOS
103 download this value must be one of the following:
105 0 to configure for 38400 baud.
106 1 to configure for 19200 baud.
107 2 to configure for 9600 baud.
108 3 to configure for 4800 baud. */
109 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 2 )
111 /* LED used by the serial port tasks. This is toggled on each character Tx,
112 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
113 #define mainCOM_TEST_LED ( 3 )
115 /*-----------------------------------------------------------
116 Local functions prototypes.
117 -----------------------------------------------------------*/
120 * The 'Check' task function. See the explanation at the top of the file.
122 static void vErrorChecks( void* pvParameters );
125 * The idle task hook - in which the integer task is implemented. See the
126 * explanation at the top of the file.
128 void vApplicationIdleHook( void );
131 * Checks the unique counts of other tasks to ensure they are still operational.
133 static long prvCheckOtherTasksAreStillRunning( void );
137 /*-----------------------------------------------------------
139 -----------------------------------------------------------*/
141 /* A few tasks are defined within this file. This flag is used to indicate
142 their status. If an error is detected in one of the locally defined tasks then
143 this flag is set to pdTRUE. */
144 portBASE_TYPE xLocalError = pdFALSE;
147 /*-----------------------------------------------------------*/
150 * This is called from the main() function generated by the Processor Expert.
154 /* Start some of the standard demo tasks. */
155 vStartLEDFlashTasks( mainFLASH_PRIORITY );
156 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
157 vStartDynamicPriorityTasks();
158 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
159 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
160 vStartIntegerMathTasks( tskIDLE_PRIORITY );
162 /* Start the locally defined tasks. There is also a task implemented as
164 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
166 /* Must be the last demo created. */
167 vCreateSuicidalTasks( mainDEATH_PRIORITY );
169 /* All the tasks have been created - start the scheduler. */
170 vTaskStartScheduler();
172 /* Should not reach here! */
175 /*-----------------------------------------------------------*/
177 static void vErrorChecks( void *pvParameters )
179 TickType_t xDelayPeriod = mainCHECK_PERIOD;
180 TickType_t xLastWakeTime;
182 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
183 functions correctly. */
184 xLastWakeTime = xTaskGetTickCount();
188 /* Delay until it is time to execute again. The delay period is
189 shorter following an error. */
190 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
192 /* Check all the demo application tasks are executing without
193 error. If an error is found the delay period is shortened - this
194 has the effect of increasing the flash rate of the 'check' task
196 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
198 /* An error has been detected in one of the tasks - flash faster. */
199 xDelayPeriod = mainERROR_CHECK_PERIOD;
202 /* Toggle the LED each cycle round. */
203 vParTestToggleLED( mainCHECK_TASK_LED );
206 /*-----------------------------------------------------------*/
208 static long prvCheckOtherTasksAreStillRunning( void )
210 portBASE_TYPE xAllTasksPassed = pdPASS;
212 if( xArePollingQueuesStillRunning() != pdTRUE )
214 xAllTasksPassed = pdFAIL;
217 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
219 xAllTasksPassed = pdFAIL;
222 if( xAreComTestTasksStillRunning() != pdTRUE )
224 xAllTasksPassed = pdFALSE;
227 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
229 xAllTasksPassed = pdFALSE;
232 if( xAreBlockingQueuesStillRunning() != pdTRUE )
234 xAllTasksPassed = pdFALSE;
237 if( xIsCreateTaskStillRunning() != pdTRUE )
239 xAllTasksPassed = pdFALSE;
242 /* Also check the status flag for the tasks defined within this function. */
243 if( xLocalError != pdFALSE )
245 xAllTasksPassed = pdFAIL;
248 return xAllTasksPassed;
250 /*-----------------------------------------------------------*/
252 void vApplicationIdleHook( void )
254 /* This variable is effectively set to a constant so it is made volatile to
255 ensure the compiler does not just get rid of it. */
256 volatile long lValue;
258 /* Keep performing a calculation and checking the result against a constant. */
260 /* Perform the calculation. This will store partial value in
261 registers, resulting in a good test of the context switch mechanism. */
263 lValue += intgCONST2;
264 lValue *= intgCONST3;
265 lValue /= intgCONST4;
267 /* Did we perform the calculation correctly with no corruption? */
268 if( lValue != intgEXPECTED_ANSWER )
271 portENTER_CRITICAL();
272 xLocalError = pdTRUE;
276 /* Yield in case cooperative scheduling is being used. */
277 #if configUSE_PREEMPTION == 0
283 /*-----------------------------------------------------------*/