2 FreeRTOS V7.1.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
56 * documentation provides more details of the demo application tasks.
\r
58 * Main.c also creates a task called "Check". This only executes every three
\r
59 * seconds but has the highest priority so is guaranteed to get processor time.
\r
60 * Its main function is to check that all the other tasks are still operational.
\r
61 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
62 * incremented each time the task successfully completes its function. Should
\r
63 * any error occur within such a task the count is permanently halted. The
\r
64 * check task inspects the count of each task to ensure it has changed since
\r
65 * the last time the check task executed. If all the count variables have
\r
66 * changed all the tasks are still executing error free, and the check task
\r
67 * toggles the onboard LED. Should any task contain an error at any time
\r
68 * the LED toggle rate will change from 3 seconds to 500ms.
\r
70 * To check the operation of the memory allocator the check task also
\r
71 * dynamically creates a task before delaying, and deletes it again when it
\r
72 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
73 * will fail and an error is signalled. The dynamically created task itself
\r
74 * allocates and frees memory just to give the allocator a bit more exercise.
\r
78 /* Standard includes. */
\r
82 /* Scheduler include files. */
\r
83 #include "FreeRTOS.h"
\r
86 /* Demo application file headers. */
\r
88 #include "integer.h"
\r
90 #include "comtest2.h"
\r
91 #include "semtest.h"
\r
93 #include "dynamic.h"
\r
96 #include "partest.h"
\r
98 /* Priority definitions for most of the tasks in the demo application. Some
\r
99 tasks just use the idle priority. */
\r
100 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
101 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
102 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
103 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
104 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
105 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
107 /* Baud rate used by the serial port tasks (ComTest tasks). */
\r
108 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
110 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
111 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
112 #define mainCOM_TEST_LED ( 3 )
\r
114 /* LED that is toggled by the check task. The check task periodically checks
\r
115 that all the other tasks are operating without error. If no errors are found
\r
116 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
117 the the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
118 #define mainCHECK_TASK_LED ( 5 )
\r
119 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
120 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
122 /* Constants used by the vMemCheckTask() task. */
\r
123 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
124 #define mainNO_TASK ( 0 )
\r
126 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
127 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
128 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
129 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
132 * The 'Check' task.
\r
134 static void vErrorChecks( void *pvParameters );
\r
137 * Checks the unique counts of other tasks to ensure they are still operational.
\r
139 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
142 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
143 * task. This is done to check the operation of the memory allocator.
\r
144 * See the top of vErrorChecks for more details.
\r
146 static void vMemCheckTask( void *pvParameters );
\r
148 /*-----------------------------------------------------------*/
\r
151 * Start all the tasks then start the scheduler.
\r
155 /* Setup the LED's for output. */
\r
156 vParTestInitialise();
\r
158 /* Start the various standard demo application tasks. */
\r
159 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
160 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
161 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
162 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
163 vStartMathTasks( tskIDLE_PRIORITY );
\r
164 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
165 vStartDynamicPriorityTasks();
\r
166 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
168 /* Start the 'Check' task. */
\r
169 xTaskCreate( vErrorChecks, ( signed char * )"Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
171 /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
\r
172 as 1 in portmacro.h. To use the cooperative scheduler define
\r
173 configUSE_PREEMPTION as 0. */
\r
174 vTaskStartScheduler();
\r
176 /* Should never get here! */
\r
179 /*-----------------------------------------------------------*/
\r
182 * Cycle for ever, delaying then checking all the other tasks are still
\r
183 * operating without error. If an error is detected then the delay period
\r
184 * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so
\r
185 * the on board LED flash rate will increase.
\r
187 * In addition to the standard tests the memory allocator is tested through
\r
188 * the dynamic creation and deletion of a task each cycle. Each time the
\r
189 * task is created memory must be allocated for its stack. When the task is
\r
190 * deleted this memory is returned to the heap. If the task cannot be created
\r
191 * then it is likely that the memory allocation failed. In addition the
\r
192 * dynamically created task allocates and frees memory while it runs.
\r
194 static void vErrorChecks( void *pvParameters )
\r
196 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
197 volatile unsigned long ulMemCheckTaskRunningCount;
\r
198 xTaskHandle xCreatedTask;
\r
199 portTickType xLastWakeTime;
\r
201 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
202 functions correctly. */
\r
203 xLastWakeTime = xTaskGetTickCount();
\r
207 /* Set ulMemCheckTaskRunningCount to a known value so we can check
\r
208 later that it has changed. */
\r
209 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
211 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
213 xCreatedTask = mainNO_TASK;
\r
214 if( xTaskCreate( vMemCheckTask, ( signed char * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
216 /* Could not create the task - we have probably run out of heap. */
\r
217 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
221 /* Delay until it is time to execute again. The delay period is
\r
222 shorter following an error. */
\r
223 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
226 /* Delete the dynamically created task. */
\r
227 if( xCreatedTask != mainNO_TASK )
\r
229 vTaskDelete( xCreatedTask );
\r
232 /* Check all the standard demo application tasks are executing without
\r
233 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
234 modified by the task just deleted. */
\r
235 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
237 /* An error has been detected in one of the tasks - flash faster. */
\r
238 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
241 vParTestToggleLED( mainCHECK_TASK_LED );
\r
244 /*-----------------------------------------------------------*/
\r
247 * Check each set of tasks in turn to see if they have experienced any
\r
248 * error conditions.
\r
250 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
252 long lNoErrorsDiscovered = ( long ) pdTRUE;
\r
254 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
256 lNoErrorsDiscovered = pdFALSE;
\r
259 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
261 lNoErrorsDiscovered = pdFALSE;
\r
264 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
266 lNoErrorsDiscovered = pdFALSE;
\r
269 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
271 lNoErrorsDiscovered = pdFALSE;
\r
274 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
276 lNoErrorsDiscovered = pdFALSE;
\r
279 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
281 lNoErrorsDiscovered = pdFALSE;
\r
284 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
286 lNoErrorsDiscovered = pdFALSE;
\r
289 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
291 /* The vMemCheckTask task did not increment the counter - it must
\r
293 lNoErrorsDiscovered = pdFALSE;
\r
296 return lNoErrorsDiscovered;
\r
298 /*-----------------------------------------------------------*/
\r
300 static void vMemCheckTask( void *pvParameters )
\r
302 unsigned long *pulMemCheckTaskRunningCounter;
\r
303 void *pvMem1, *pvMem2, *pvMem3;
\r
304 static long lErrorOccurred = pdFALSE;
\r
306 /* This task is dynamically created then deleted during each cycle of the
\r
307 vErrorChecks task to check the operation of the memory allocator. Each time
\r
308 the task is created memory is allocated for the stack and TCB. Each time
\r
309 the task is deleted this memory is returned to the heap. This task itself
\r
310 exercises the allocator by allocating and freeing blocks.
\r
312 The task executes at the idle priority so does not require a delay.
\r
314 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
315 vErrorChecks() task that this task is still executing without error. */
\r
317 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
321 if( lErrorOccurred == pdFALSE )
\r
323 /* We have never seen an error so increment the counter. */
\r
324 ( *pulMemCheckTaskRunningCounter )++;
\r
328 /* Reset the count so an error is detected by the
\r
329 prvCheckOtherTasksAreStillRunning() function. */
\r
330 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
333 /* Allocate some memory - just to give the allocator some extra
\r
334 exercise. This has to be in a critical section to ensure the
\r
335 task does not get deleted while it has memory allocated. */
\r
338 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
339 if( pvMem1 == NULL )
\r
341 lErrorOccurred = pdTRUE;
\r
345 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
346 vPortFree( pvMem1 );
\r
351 /* Again - with a different size block. */
\r
354 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
355 if( pvMem2 == NULL )
\r
357 lErrorOccurred = pdTRUE;
\r
361 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
362 vPortFree( pvMem2 );
\r
367 /* Again - with a different size block. */
\r
370 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
371 if( pvMem3 == NULL )
\r
373 lErrorOccurred = pdTRUE;
\r
377 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
378 vPortFree( pvMem3 );
\r
384 /*-----------------------------------------------------------*/
\r
387 * Called by the startup code. Initial processor setup can be placed in this
\r
390 void hw_initialise (void)
\r