2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS 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; 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 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
51 * documentation provides more details of the demo application tasks.
\r
53 * Main.c also creates a task called "Check". This only executes every three
\r
54 * seconds but has the highest priority so is guaranteed to get processor time.
\r
55 * Its main function is to check that all the other tasks are still operational.
\r
56 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
57 * incremented each time the task successfully completes its function. Should
\r
58 * any error occur within such a task the count is permanently halted. The
\r
59 * check task inspects the count of each task to ensure it has changed since
\r
60 * the last time the check task executed. If all the count variables have
\r
61 * changed all the tasks are still executing error free, and the check task
\r
62 * toggles the onboard LED. Should any task contain an error at any time
\r
63 * the LED toggle rate will change from 3 seconds to 500ms.
\r
65 * To check the operation of the memory allocator the check task also
\r
66 * dynamically creates a task before delaying, and deletes it again when it
\r
67 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
68 * will fail and an error is signalled. The dynamically created task itself
\r
69 * allocates and frees memory just to give the allocator a bit more exercise.
\r
73 /* Standard includes. */
\r
77 /* Scheduler include files. */
\r
78 #include "FreeRTOS.h"
\r
81 /* Demo application file headers. */
\r
83 #include "integer.h"
\r
85 #include "comtest2.h"
\r
86 #include "semtest.h"
\r
88 #include "dynamic.h"
\r
91 #include "partest.h"
\r
93 /* Priority definitions for most of the tasks in the demo application. Some
\r
94 tasks just use the idle priority. */
\r
95 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
96 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
97 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
98 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
99 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
100 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
102 /* Baud rate used by the serial port tasks (ComTest tasks). */
\r
103 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
105 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
106 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
107 #define mainCOM_TEST_LED ( 3 )
\r
109 /* LED that is toggled by the check task. The check task periodically checks
\r
110 that all the other tasks are operating without error. If no errors are found
\r
111 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
112 the the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
113 #define mainCHECK_TASK_LED ( 5 )
\r
114 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
115 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
117 /* Constants used by the vMemCheckTask() task. */
\r
118 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
119 #define mainNO_TASK ( 0 )
\r
121 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
122 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
123 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
124 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
127 * The 'Check' task.
\r
129 static void vErrorChecks( void *pvParameters );
\r
132 * Checks the unique counts of other tasks to ensure they are still operational.
\r
134 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
137 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
138 * task. This is done to check the operation of the memory allocator.
\r
139 * See the top of vErrorChecks for more details.
\r
141 static void vMemCheckTask( void *pvParameters );
\r
143 /*-----------------------------------------------------------*/
\r
146 * Start all the tasks then start the scheduler.
\r
150 /* Setup the LED's for output. */
\r
151 vParTestInitialise();
\r
153 /* Start the various standard demo application tasks. */
\r
154 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
155 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
156 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
157 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
158 vStartMathTasks( tskIDLE_PRIORITY );
\r
159 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
160 vStartDynamicPriorityTasks();
\r
161 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
163 /* Start the 'Check' task. */
\r
164 xTaskCreate( vErrorChecks, ( signed char * )"Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
166 /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
\r
167 as 1 in portmacro.h. To use the cooperative scheduler define
\r
168 configUSE_PREEMPTION as 0. */
\r
169 vTaskStartScheduler();
\r
171 /* Should never get here! */
\r
174 /*-----------------------------------------------------------*/
\r
177 * Cycle for ever, delaying then checking all the other tasks are still
\r
178 * operating without error. If an error is detected then the delay period
\r
179 * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so
\r
180 * the on board LED flash rate will increase.
\r
182 * In addition to the standard tests the memory allocator is tested through
\r
183 * the dynamic creation and deletion of a task each cycle. Each time the
\r
184 * task is created memory must be allocated for its stack. When the task is
\r
185 * deleted this memory is returned to the heap. If the task cannot be created
\r
186 * then it is likely that the memory allocation failed. In addition the
\r
187 * dynamically created task allocates and frees memory while it runs.
\r
189 static void vErrorChecks( void *pvParameters )
\r
191 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
192 volatile unsigned long ulMemCheckTaskRunningCount;
\r
193 xTaskHandle xCreatedTask;
\r
194 portTickType xLastWakeTime;
\r
196 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
197 functions correctly. */
\r
198 xLastWakeTime = xTaskGetTickCount();
\r
202 /* Set ulMemCheckTaskRunningCount to a known value so we can check
\r
203 later that it has changed. */
\r
204 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
206 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
208 xCreatedTask = mainNO_TASK;
\r
209 if( xTaskCreate( vMemCheckTask, ( signed char * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
211 /* Could not create the task - we have probably run out of heap. */
\r
212 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
216 /* Delay until it is time to execute again. The delay period is
\r
217 shorter following an error. */
\r
218 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
221 /* Delete the dynamically created task. */
\r
222 if( xCreatedTask != mainNO_TASK )
\r
224 vTaskDelete( xCreatedTask );
\r
227 /* Check all the standard demo application tasks are executing without
\r
228 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
229 modified by the task just deleted. */
\r
230 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
232 /* An error has been detected in one of the tasks - flash faster. */
\r
233 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
236 vParTestToggleLED( mainCHECK_TASK_LED );
\r
239 /*-----------------------------------------------------------*/
\r
242 * Check each set of tasks in turn to see if they have experienced any
\r
243 * error conditions.
\r
245 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
247 long lNoErrorsDiscovered = ( long ) pdTRUE;
\r
249 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
251 lNoErrorsDiscovered = pdFALSE;
\r
254 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
256 lNoErrorsDiscovered = pdFALSE;
\r
259 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
261 lNoErrorsDiscovered = pdFALSE;
\r
264 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
266 lNoErrorsDiscovered = pdFALSE;
\r
269 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
271 lNoErrorsDiscovered = pdFALSE;
\r
274 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
276 lNoErrorsDiscovered = pdFALSE;
\r
279 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
281 lNoErrorsDiscovered = pdFALSE;
\r
284 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
286 /* The vMemCheckTask task did not increment the counter - it must
\r
288 lNoErrorsDiscovered = pdFALSE;
\r
291 return lNoErrorsDiscovered;
\r
293 /*-----------------------------------------------------------*/
\r
295 static void vMemCheckTask( void *pvParameters )
\r
297 unsigned long *pulMemCheckTaskRunningCounter;
\r
298 void *pvMem1, *pvMem2, *pvMem3;
\r
299 static long lErrorOccurred = pdFALSE;
\r
301 /* This task is dynamically created then deleted during each cycle of the
\r
302 vErrorChecks task to check the operation of the memory allocator. Each time
\r
303 the task is created memory is allocated for the stack and TCB. Each time
\r
304 the task is deleted this memory is returned to the heap. This task itself
\r
305 exercises the allocator by allocating and freeing blocks.
\r
307 The task executes at the idle priority so does not require a delay.
\r
309 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
310 vErrorChecks() task that this task is still executing without error. */
\r
312 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
316 if( lErrorOccurred == pdFALSE )
\r
318 /* We have never seen an error so increment the counter. */
\r
319 ( *pulMemCheckTaskRunningCounter )++;
\r
323 /* Reset the count so an error is detected by the
\r
324 prvCheckOtherTasksAreStillRunning() function. */
\r
325 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
328 /* Allocate some memory - just to give the allocator some extra
\r
329 exercise. This has to be in a critical section to ensure the
\r
330 task does not get deleted while it has memory allocated. */
\r
333 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
334 if( pvMem1 == NULL )
\r
336 lErrorOccurred = pdTRUE;
\r
340 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
341 vPortFree( pvMem1 );
\r
346 /* Again - with a different size block. */
\r
349 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
350 if( pvMem2 == NULL )
\r
352 lErrorOccurred = pdTRUE;
\r
356 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
357 vPortFree( pvMem2 );
\r
362 /* Again - with a different size block. */
\r
365 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
366 if( pvMem3 == NULL )
\r
368 lErrorOccurred = pdTRUE;
\r
372 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
373 vPortFree( pvMem3 );
\r
379 /*-----------------------------------------------------------*/
\r
382 * Called by the startup code. Initial processor setup can be placed in this
\r
385 void hw_initialise (void)
\r