3 FreeRTOS V6.1.0 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
9 * + New to FreeRTOS, *
\r
10 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
11 * + Looking for basic training, *
\r
12 * + Wanting to improve your FreeRTOS skills and productivity *
\r
14 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
16 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
17 * http://www.FreeRTOS.org/Documentation *
\r
19 * A pdf reference manual is also available. Both are usually delivered *
\r
20 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
21 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
22 * exceptional circumstances). Thank you for your support! *
\r
24 ***************************************************************************
\r
26 This file is part of the FreeRTOS distribution.
\r
28 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
29 the terms of the GNU General Public License (version 2) as published by the
\r
30 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
31 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
32 a combined work that includes FreeRTOS without being obliged to provide the
\r
33 source code for proprietary components outside of the FreeRTOS kernel.
\r
34 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
35 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
36 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
37 more details. You should have received a copy of the GNU General Public
\r
38 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
39 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
40 by writing to Richard Barry, contact details for whom are available on the
\r
45 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
48 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
51 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
52 licensing and training services.
\r
58 * vMain() is effectively the demo application entry point. It is called by
\r
59 * the main() function generated by the Processor Expert application.
\r
61 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
62 * The WEB documentation provides more details of the demo application tasks.
\r
64 * Main.c also creates a task called "Check". This only executes every three
\r
65 * seconds but has the highest priority so is guaranteed to get processor time.
\r
66 * Its main function is to check that all the other tasks are still operational.
\r
67 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
68 * incremented each time the task successfully completes its function. Should
\r
69 * any error occur within such a task the count is permanently halted. The
\r
70 * check task inspects the count of each task to ensure it has changed since
\r
71 * the last time the check task executed. If all the count variables have
\r
72 * changed all the tasks are still executing error free, and the check task
\r
73 * toggles the onboard LED. Should any task contain an error at any time
\r
74 * the LED toggle rate will change from 3 seconds to 500ms.
\r
76 * This file also includes the functionality implemented within the
\r
77 * standard demo application file integer.c. This is done to demonstrate the
\r
78 * use of an idle hook. See the documentation within integer.c for the
\r
79 * rationale of the integer task functionality.
\r
82 /* Kernel includes. */
\r
83 #include "FreeRTOS.h"
\r
87 /* Demo application includes. */
\r
90 #include "dynamic.h"
\r
91 #include "partest.h"
\r
92 #include "comtest2.h"
\r
94 #include "integer.h"
\r
98 /*-----------------------------------------------------------
\r
100 -----------------------------------------------------------*/
\r
102 /* Priorities assigned to demo application tasks. */
\r
103 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
104 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
105 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
106 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
107 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
108 #define mainDEATH_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
110 /* LED that is toggled by the check task. The check task periodically checks
\r
111 that all the other tasks are operating without error. If no errors are found
\r
112 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
113 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
114 #define mainCHECK_TASK_LED ( 7 )
\r
115 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
116 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
118 /* The constants used in the idle task calculation. */
\r
119 #define intgCONST1 ( ( long ) 123 )
\r
120 #define intgCONST2 ( ( long ) 234567 )
\r
121 #define intgCONST3 ( ( long ) -3 )
\r
122 #define intgCONST4 ( ( long ) 7 )
\r
123 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
126 /* Baud rate used by the serial port tasks (ComTest tasks).
\r
127 IMPORTANT: The function COM0_SetBaudRateValue() which is generated by the
\r
128 Processor Expert is used to set the baud rate. As configured in the FreeRTOS
\r
129 download this value must be one of the following:
\r
131 0 to configure for 38400 baud.
\r
132 1 to configure for 19200 baud.
\r
133 2 to configure for 9600 baud.
\r
134 3 to configure for 4800 baud. */
\r
135 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 2 )
\r
137 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
138 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
139 #define mainCOM_TEST_LED ( 3 )
\r
141 /*-----------------------------------------------------------
\r
142 Local functions prototypes.
\r
143 -----------------------------------------------------------*/
\r
146 * The 'Check' task function. See the explanation at the top of the file.
\r
148 static void vErrorChecks( void* pvParameters );
\r
151 * The idle task hook - in which the integer task is implemented. See the
\r
152 * explanation at the top of the file.
\r
154 void vApplicationIdleHook( void );
\r
157 * Checks the unique counts of other tasks to ensure they are still operational.
\r
159 static long prvCheckOtherTasksAreStillRunning( void );
\r
163 /*-----------------------------------------------------------
\r
165 -----------------------------------------------------------*/
\r
167 /* A few tasks are defined within this file. This flag is used to indicate
\r
168 their status. If an error is detected in one of the locally defined tasks then
\r
169 this flag is set to pdTRUE. */
\r
170 portBASE_TYPE xLocalError = pdFALSE;
\r
173 /*-----------------------------------------------------------*/
\r
176 * This is called from the main() function generated by the Processor Expert.
\r
180 /* Start some of the standard demo tasks. */
\r
181 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
182 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
183 vStartDynamicPriorityTasks();
\r
184 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
185 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
186 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
188 /* Start the locally defined tasks. There is also a task implemented as
\r
190 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
192 /* Must be the last demo created. */
\r
193 vCreateSuicidalTasks( mainDEATH_PRIORITY );
\r
195 /* All the tasks have been created - start the scheduler. */
\r
196 vTaskStartScheduler();
\r
198 /* Should not reach here! */
\r
201 /*-----------------------------------------------------------*/
\r
203 static void vErrorChecks( void *pvParameters )
\r
205 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
206 portTickType xLastWakeTime;
\r
208 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
209 functions correctly. */
\r
210 xLastWakeTime = xTaskGetTickCount();
\r
214 /* Delay until it is time to execute again. The delay period is
\r
215 shorter following an error. */
\r
216 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
218 /* Check all the demo application tasks are executing without
\r
219 error. If an error is found the delay period is shortened - this
\r
220 has the effect of increasing the flash rate of the 'check' task
\r
222 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
224 /* An error has been detected in one of the tasks - flash faster. */
\r
225 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
228 /* Toggle the LED each cycle round. */
\r
229 vParTestToggleLED( mainCHECK_TASK_LED );
\r
232 /*-----------------------------------------------------------*/
\r
234 static long prvCheckOtherTasksAreStillRunning( void )
\r
236 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
238 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
240 xAllTasksPassed = pdFAIL;
\r
243 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
245 xAllTasksPassed = pdFAIL;
\r
248 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
250 xAllTasksPassed = pdFALSE;
\r
253 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
255 xAllTasksPassed = pdFALSE;
\r
258 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
260 xAllTasksPassed = pdFALSE;
\r
263 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
265 xAllTasksPassed = pdFALSE;
\r
268 /* Also check the status flag for the tasks defined within this function. */
\r
269 if( xLocalError != pdFALSE )
\r
271 xAllTasksPassed = pdFAIL;
\r
274 return xAllTasksPassed;
\r
276 /*-----------------------------------------------------------*/
\r
278 void vApplicationIdleHook( void )
\r
280 /* This variable is effectively set to a constant so it is made volatile to
\r
281 ensure the compiler does not just get rid of it. */
\r
282 volatile long lValue;
\r
284 /* Keep performing a calculation and checking the result against a constant. */
\r
286 /* Perform the calculation. This will store partial value in
\r
287 registers, resulting in a good test of the context switch mechanism. */
\r
288 lValue = intgCONST1;
\r
289 lValue += intgCONST2;
\r
290 lValue *= intgCONST3;
\r
291 lValue /= intgCONST4;
\r
293 /* Did we perform the calculation correctly with no corruption? */
\r
294 if( lValue != intgEXPECTED_ANSWER )
\r
297 portENTER_CRITICAL();
\r
298 xLocalError = pdTRUE;
\r
299 portEXIT_CRITICAL();
\r
302 /* Yield in case cooperative scheduling is being used. */
\r
303 #if configUSE_PREEMPTION == 0
\r
309 /*-----------------------------------------------------------*/
\r