2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
68 * documentation provides more details of the demo application tasks.
\r
70 * Main.c also creates a task called "Check". This only executes every three
\r
71 * seconds but has the highest priority so is guaranteed to get processor time.
\r
72 * Its main function is to check that all the other tasks are still operational.
\r
73 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
74 * incremented each time the task successfully completes its function. Should
\r
75 * any error occur within such a task the count is permanently halted. The
\r
76 * check task inspects the count of each task to ensure it has changed since
\r
77 * the last time the check task executed. If all the count variables have
\r
78 * changed all the tasks are still executing error free, and the check task
\r
79 * toggles the onboard LED. Should any task contain an error at any time
\r
80 * the LED toggle rate will change from 3 seconds to 500ms.
\r
82 * To check the operation of the memory allocator the check task also
\r
83 * dynamically creates a task before delaying, and deletes it again when it
\r
84 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
85 * will fail and an error is signalled. The dynamically created task itself
\r
86 * allocates and frees memory just to give the allocator a bit more exercise.
\r
90 /* Standard includes. */
\r
94 /* Scheduler include files. */
\r
95 #include "FreeRTOS.h"
\r
98 /* Demo application file headers. */
\r
100 #include "integer.h"
\r
102 #include "comtest2.h"
\r
103 #include "semtest.h"
\r
105 #include "dynamic.h"
\r
106 #include "BlockQ.h"
\r
107 #include "serial.h"
\r
108 #include "partest.h"
\r
110 /* Priority definitions for most of the tasks in the demo application. Some
\r
111 tasks just use the idle priority. */
\r
112 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
113 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
114 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
115 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
116 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
117 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
119 /* Baud rate used by the serial port tasks (ComTest tasks). */
\r
120 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
122 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
123 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
124 #define mainCOM_TEST_LED ( 3 )
\r
126 /* LED that is toggled by the check task. The check task periodically checks
\r
127 that all the other tasks are operating without error. If no errors are found
\r
128 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
129 the the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
130 #define mainCHECK_TASK_LED ( 5 )
\r
131 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
132 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
134 /* Constants used by the vMemCheckTask() task. */
\r
135 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
136 #define mainNO_TASK ( 0 )
\r
138 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
139 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
140 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
141 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
144 * The 'Check' task.
\r
146 static void vErrorChecks( void *pvParameters );
\r
149 * Checks the unique counts of other tasks to ensure they are still operational.
\r
151 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
154 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
155 * task. This is done to check the operation of the memory allocator.
\r
156 * See the top of vErrorChecks for more details.
\r
158 static void vMemCheckTask( void *pvParameters );
\r
160 /*-----------------------------------------------------------*/
\r
163 * Start all the tasks then start the scheduler.
\r
167 /* Setup the LED's for output. */
\r
168 vParTestInitialise();
\r
170 /* Start the various standard demo application tasks. */
\r
171 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
172 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
173 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
174 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
175 vStartMathTasks( tskIDLE_PRIORITY );
\r
176 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
177 vStartDynamicPriorityTasks();
\r
178 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
180 /* Start the 'Check' task. */
\r
181 xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
183 /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
\r
184 as 1 in portmacro.h. To use the cooperative scheduler define
\r
185 configUSE_PREEMPTION as 0. */
\r
186 vTaskStartScheduler();
\r
188 /* Should never get here! */
\r
191 /*-----------------------------------------------------------*/
\r
194 * Cycle for ever, delaying then checking all the other tasks are still
\r
195 * operating without error. If an error is detected then the delay period
\r
196 * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so
\r
197 * the on board LED flash rate will increase.
\r
199 * In addition to the standard tests the memory allocator is tested through
\r
200 * the dynamic creation and deletion of a task each cycle. Each time the
\r
201 * task is created memory must be allocated for its stack. When the task is
\r
202 * deleted this memory is returned to the heap. If the task cannot be created
\r
203 * then it is likely that the memory allocation failed. In addition the
\r
204 * dynamically created task allocates and frees memory while it runs.
\r
206 static void vErrorChecks( void *pvParameters )
\r
208 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
209 volatile unsigned long ulMemCheckTaskRunningCount;
\r
210 xTaskHandle xCreatedTask;
\r
211 portTickType xLastWakeTime;
\r
213 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
214 functions correctly. */
\r
215 xLastWakeTime = xTaskGetTickCount();
\r
219 /* Set ulMemCheckTaskRunningCount to a known value so we can check
\r
220 later that it has changed. */
\r
221 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
223 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
225 xCreatedTask = mainNO_TASK;
\r
226 if( xTaskCreate( vMemCheckTask, "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
228 /* Could not create the task - we have probably run out of heap. */
\r
229 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
233 /* Delay until it is time to execute again. The delay period is
\r
234 shorter following an error. */
\r
235 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
238 /* Delete the dynamically created task. */
\r
239 if( xCreatedTask != mainNO_TASK )
\r
241 vTaskDelete( xCreatedTask );
\r
244 /* Check all the standard demo application tasks are executing without
\r
245 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
246 modified by the task just deleted. */
\r
247 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
249 /* An error has been detected in one of the tasks - flash faster. */
\r
250 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
253 vParTestToggleLED( mainCHECK_TASK_LED );
\r
256 /*-----------------------------------------------------------*/
\r
259 * Check each set of tasks in turn to see if they have experienced any
\r
260 * error conditions.
\r
262 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
264 long lNoErrorsDiscovered = ( long ) pdTRUE;
\r
266 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
268 lNoErrorsDiscovered = pdFALSE;
\r
271 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
273 lNoErrorsDiscovered = pdFALSE;
\r
276 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
278 lNoErrorsDiscovered = pdFALSE;
\r
281 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
283 lNoErrorsDiscovered = pdFALSE;
\r
286 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
288 lNoErrorsDiscovered = pdFALSE;
\r
291 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
293 lNoErrorsDiscovered = pdFALSE;
\r
296 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
298 lNoErrorsDiscovered = pdFALSE;
\r
301 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
303 /* The vMemCheckTask task did not increment the counter - it must
\r
305 lNoErrorsDiscovered = pdFALSE;
\r
308 return lNoErrorsDiscovered;
\r
310 /*-----------------------------------------------------------*/
\r
312 static void vMemCheckTask( void *pvParameters )
\r
314 unsigned long *pulMemCheckTaskRunningCounter;
\r
315 void *pvMem1, *pvMem2, *pvMem3;
\r
316 static long lErrorOccurred = pdFALSE;
\r
318 /* This task is dynamically created then deleted during each cycle of the
\r
319 vErrorChecks task to check the operation of the memory allocator. Each time
\r
320 the task is created memory is allocated for the stack and TCB. Each time
\r
321 the task is deleted this memory is returned to the heap. This task itself
\r
322 exercises the allocator by allocating and freeing blocks.
\r
324 The task executes at the idle priority so does not require a delay.
\r
326 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
327 vErrorChecks() task that this task is still executing without error. */
\r
329 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
333 if( lErrorOccurred == pdFALSE )
\r
335 /* We have never seen an error so increment the counter. */
\r
336 ( *pulMemCheckTaskRunningCounter )++;
\r
340 /* Reset the count so an error is detected by the
\r
341 prvCheckOtherTasksAreStillRunning() function. */
\r
342 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
345 /* Allocate some memory - just to give the allocator some extra
\r
346 exercise. This has to be in a critical section to ensure the
\r
347 task does not get deleted while it has memory allocated. */
\r
350 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
351 if( pvMem1 == NULL )
\r
353 lErrorOccurred = pdTRUE;
\r
357 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
358 vPortFree( pvMem1 );
\r
363 /* Again - with a different size block. */
\r
366 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
367 if( pvMem2 == NULL )
\r
369 lErrorOccurred = pdTRUE;
\r
373 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
374 vPortFree( pvMem2 );
\r
379 /* Again - with a different size block. */
\r
382 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
383 if( pvMem3 == NULL )
\r
385 lErrorOccurred = pdTRUE;
\r
389 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
390 vPortFree( pvMem3 );
\r
396 /*-----------------------------------------------------------*/
\r
399 * Called by the startup code. Initial processor setup can be placed in this
\r
402 void hw_initialise (void)
\r