3 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.
\r
5 This file is part of the FreeRTOS.org distribution.
\r
7 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
8 under the terms of the GNU General Public License (version 2) as published
\r
9 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
11 combined work that includes FreeRTOS.org without being obliged to provide
\r
12 the source code for any proprietary components. Alternative commercial
\r
13 license and support terms are also available upon request. See the
\r
14 licensing section of http://www.FreeRTOS.org for full details.
\r
16 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
30 * This is a concise, step by step, 'hands on' guide that describes both *
\r
31 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
32 * explains numerous examples that are written using the FreeRTOS API. *
\r
33 * Full source code for all the examples is provided in an accompanying *
\r
36 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
56 * vMain() is effectively the demo application entry point. It is called by
\r
57 * the main() function generated by the Processor Expert application.
\r
59 * vMain() creates all the demo application tasks, then starts the scheduler.
\r
60 * The WEB documentation provides more details of the demo application tasks.
\r
62 * Main.c also creates a task called "Check". This only executes every three
\r
63 * seconds but has the highest priority so is guaranteed to get processor time.
\r
64 * Its main function is to check that all the other tasks are still operational.
\r
65 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
66 * incremented each time the task successfully completes its function. Should
\r
67 * any error occur within such a task the count is permanently halted. The
\r
68 * check task inspects the count of each task to ensure it has changed since
\r
69 * the last time the check task executed. If all the count variables have
\r
70 * changed all the tasks are still executing error free, and the check task
\r
71 * toggles the onboard LED. Should any task contain an error at any time
\r
72 * the LED toggle rate will change from 3 seconds to 500ms.
\r
74 * This file also includes the functionality implemented within the
\r
75 * standard demo application file integer.c. This is done to demonstrate the
\r
76 * use of an idle hook. See the documentation within integer.c for the
\r
77 * rationale of the integer task functionality.
\r
80 /* Kernel includes. */
\r
81 #include "FreeRTOS.h"
\r
85 /* Demo application includes. */
\r
88 #include "dynamic.h"
\r
89 #include "partest.h"
\r
90 #include "comtest2.h"
\r
92 #include "integer.h"
\r
96 /*-----------------------------------------------------------
\r
98 -----------------------------------------------------------*/
\r
100 /* Priorities assigned to demo application tasks. */
\r
101 #define mainFLASH_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
102 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
103 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
104 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
105 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
106 #define mainDEATH_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
108 /* LED that is toggled by the check task. The check task periodically checks
\r
109 that all the other tasks are operating without error. If no errors are found
\r
110 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
111 then the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
112 #define mainCHECK_TASK_LED ( 7 )
\r
113 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
114 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
116 /* The constants used in the idle task calculation. */
\r
117 #define intgCONST1 ( ( portLONG ) 123 )
\r
118 #define intgCONST2 ( ( portLONG ) 234567 )
\r
119 #define intgCONST3 ( ( portLONG ) -3 )
\r
120 #define intgCONST4 ( ( portLONG ) 7 )
\r
121 #define intgEXPECTED_ANSWER ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )
\r
124 /* Baud rate used by the serial port tasks (ComTest tasks).
\r
125 IMPORTANT: The function COM0_SetBaudRateValue() which is generated by the
\r
126 Processor Expert is used to set the baud rate. As configured in the FreeRTOS
\r
127 download this value must be one of the following:
\r
129 0 to configure for 38400 baud.
\r
130 1 to configure for 19200 baud.
\r
131 2 to configure for 9600 baud.
\r
132 3 to configure for 4800 baud. */
\r
133 #define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 2 )
\r
135 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
136 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
137 #define mainCOM_TEST_LED ( 3 )
\r
139 /*-----------------------------------------------------------
\r
140 Local functions prototypes.
\r
141 -----------------------------------------------------------*/
\r
144 * The 'Check' task function. See the explanation at the top of the file.
\r
146 static void vErrorChecks( void* pvParameters );
\r
149 * The idle task hook - in which the integer task is implemented. See the
\r
150 * explanation at the top of the file.
\r
152 void vApplicationIdleHook( void );
\r
155 * Checks the unique counts of other tasks to ensure they are still operational.
\r
157 static portLONG prvCheckOtherTasksAreStillRunning( void );
\r
161 /*-----------------------------------------------------------
\r
163 -----------------------------------------------------------*/
\r
165 /* A few tasks are defined within this file. This flag is used to indicate
\r
166 their status. If an error is detected in one of the locally defined tasks then
\r
167 this flag is set to pdTRUE. */
\r
168 portBASE_TYPE xLocalError = pdFALSE;
\r
171 /*-----------------------------------------------------------*/
\r
174 * This is called from the main() function generated by the Processor Expert.
\r
178 /* Start some of the standard demo tasks. */
\r
179 vStartLEDFlashTasks( mainFLASH_PRIORITY );
\r
180 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
181 vStartDynamicPriorityTasks();
\r
182 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
183 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
184 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
186 /* Start the locally defined tasks. There is also a task implemented as
\r
188 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
190 /* Must be the last demo created. */
\r
191 vCreateSuicidalTasks( mainDEATH_PRIORITY );
\r
193 /* All the tasks have been created - start the scheduler. */
\r
194 vTaskStartScheduler();
\r
196 /* Should not reach here! */
\r
199 /*-----------------------------------------------------------*/
\r
201 static void vErrorChecks( void *pvParameters )
\r
203 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
204 portTickType xLastWakeTime;
\r
206 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
207 functions correctly. */
\r
208 xLastWakeTime = xTaskGetTickCount();
\r
212 /* Delay until it is time to execute again. The delay period is
\r
213 shorter following an error. */
\r
214 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
216 /* Check all the demo application tasks are executing without
\r
217 error. If an error is found the delay period is shortened - this
\r
218 has the effect of increasing the flash rate of the 'check' task
\r
220 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )
\r
222 /* An error has been detected in one of the tasks - flash faster. */
\r
223 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
226 /* Toggle the LED each cycle round. */
\r
227 vParTestToggleLED( mainCHECK_TASK_LED );
\r
230 /*-----------------------------------------------------------*/
\r
232 static portLONG prvCheckOtherTasksAreStillRunning( void )
\r
234 portBASE_TYPE xAllTasksPassed = pdPASS;
\r
236 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
238 xAllTasksPassed = pdFAIL;
\r
241 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
243 xAllTasksPassed = pdFAIL;
\r
246 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
248 xAllTasksPassed = pdFALSE;
\r
251 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
253 xAllTasksPassed = pdFALSE;
\r
256 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
258 xAllTasksPassed = pdFALSE;
\r
261 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
263 xAllTasksPassed = pdFALSE;
\r
266 /* Also check the status flag for the tasks defined within this function. */
\r
267 if( xLocalError != pdFALSE )
\r
269 xAllTasksPassed = pdFAIL;
\r
272 return xAllTasksPassed;
\r
274 /*-----------------------------------------------------------*/
\r
276 void vApplicationIdleHook( void )
\r
278 /* This variable is effectively set to a constant so it is made volatile to
\r
279 ensure the compiler does not just get rid of it. */
\r
280 volatile portLONG lValue;
\r
282 /* Keep performing a calculation and checking the result against a constant. */
\r
284 /* Perform the calculation. This will store partial value in
\r
285 registers, resulting in a good test of the context switch mechanism. */
\r
286 lValue = intgCONST1;
\r
287 lValue += intgCONST2;
\r
288 lValue *= intgCONST3;
\r
289 lValue /= intgCONST4;
\r
291 /* Did we perform the calculation correctly with no corruption? */
\r
292 if( lValue != intgEXPECTED_ANSWER )
\r
295 portENTER_CRITICAL();
\r
296 xLocalError = pdTRUE;
\r
297 portEXIT_CRITICAL();
\r
300 /* Yield in case cooperative scheduling is being used. */
\r
301 #if configUSE_PREEMPTION == 0
\r
307 /*-----------------------------------------------------------*/
\r