2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
67 * documentation provides more details of the demo application tasks.
\r
69 * Main.c also creates a task called "Check". This only executes every three
\r
70 * seconds but has the highest priority so is guaranteed to get processor time.
\r
71 * Its main function is to check that all the other tasks are still operational.
\r
72 * Each task (other than the "flash" tasks) maintains a unique count that is
\r
73 * incremented each time the task successfully completes its function. Should
\r
74 * any error occur within such a task the count is permanently halted. The
\r
75 * check task inspects the count of each task to ensure it has changed since
\r
76 * the last time the check task executed. If all the count variables have
\r
77 * changed all the tasks are still executing error free, and the check task
\r
78 * toggles the onboard LED. Should any task contain an error at any time
\r
79 * the LED toggle rate will change from 3 seconds to 500ms.
\r
81 * To check the operation of the memory allocator the check task also
\r
82 * dynamically creates a task before delaying, and deletes it again when it
\r
83 * wakes. If memory cannot be allocated for the new task the call to xTaskCreate
\r
84 * will fail and an error is signalled. The dynamically created task itself
\r
85 * allocates and frees memory just to give the allocator a bit more exercise.
\r
89 /* Standard includes. */
\r
93 /* Scheduler include files. */
\r
94 #include "FreeRTOS.h"
\r
97 /* Demo application file headers. */
\r
99 #include "integer.h"
\r
101 #include "comtest2.h"
\r
102 #include "semtest.h"
\r
104 #include "dynamic.h"
\r
105 #include "BlockQ.h"
\r
106 #include "serial.h"
\r
107 #include "partest.h"
\r
109 /* Priority definitions for most of the tasks in the demo application. Some
\r
110 tasks just use the idle priority. */
\r
111 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
112 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
113 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
114 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
115 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
116 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
118 /* Baud rate used by the serial port tasks (ComTest tasks). */
\r
119 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )
\r
121 /* LED used by the serial port tasks. This is toggled on each character Tx,
\r
122 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */
\r
123 #define mainCOM_TEST_LED ( 3 )
\r
125 /* LED that is toggled by the check task. The check task periodically checks
\r
126 that all the other tasks are operating without error. If no errors are found
\r
127 the LED is toggled with mainCHECK_PERIOD frequency. If an error is found
\r
128 the the toggle rate increases to mainERROR_CHECK_PERIOD. */
\r
129 #define mainCHECK_TASK_LED ( 5 )
\r
130 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
131 #define mainERROR_CHECK_PERIOD ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
133 /* Constants used by the vMemCheckTask() task. */
\r
134 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
135 #define mainNO_TASK ( 0 )
\r
137 /* The size of the memory blocks allocated by the vMemCheckTask() task. */
\r
138 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
139 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
140 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 151 )
\r
143 * The 'Check' task.
\r
145 static void vErrorChecks( void *pvParameters );
\r
148 * Checks the unique counts of other tasks to ensure they are still operational.
\r
150 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount );
\r
153 * Dynamically created and deleted during each cycle of the vErrorChecks()
\r
154 * task. This is done to check the operation of the memory allocator.
\r
155 * See the top of vErrorChecks for more details.
\r
157 static void vMemCheckTask( void *pvParameters );
\r
159 /*-----------------------------------------------------------*/
\r
162 * Start all the tasks then start the scheduler.
\r
166 /* Setup the LED's for output. */
\r
167 vParTestInitialise();
\r
169 /* Start the various standard demo application tasks. */
\r
170 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
171 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
172 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
173 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
174 vStartMathTasks( tskIDLE_PRIORITY );
\r
175 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
176 vStartDynamicPriorityTasks();
\r
177 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
179 /* Start the 'Check' task. */
\r
180 xTaskCreate( vErrorChecks, ( signed char * )"Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
182 /* In this port, to use preemptive scheduler define configUSE_PREEMPTION
\r
183 as 1 in portmacro.h. To use the cooperative scheduler define
\r
184 configUSE_PREEMPTION as 0. */
\r
185 vTaskStartScheduler();
\r
187 /* Should never get here! */
\r
190 /*-----------------------------------------------------------*/
\r
193 * Cycle for ever, delaying then checking all the other tasks are still
\r
194 * operating without error. If an error is detected then the delay period
\r
195 * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so
\r
196 * the on board LED flash rate will increase.
\r
198 * In addition to the standard tests the memory allocator is tested through
\r
199 * the dynamic creation and deletion of a task each cycle. Each time the
\r
200 * task is created memory must be allocated for its stack. When the task is
\r
201 * deleted this memory is returned to the heap. If the task cannot be created
\r
202 * then it is likely that the memory allocation failed. In addition the
\r
203 * dynamically created task allocates and frees memory while it runs.
\r
205 static void vErrorChecks( void *pvParameters )
\r
207 portTickType xDelayPeriod = mainCHECK_PERIOD;
\r
208 volatile unsigned long ulMemCheckTaskRunningCount;
\r
209 xTaskHandle xCreatedTask;
\r
210 portTickType xLastWakeTime;
\r
212 /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()
\r
213 functions correctly. */
\r
214 xLastWakeTime = xTaskGetTickCount();
\r
218 /* Set ulMemCheckTaskRunningCount to a known value so we can check
\r
219 later that it has changed. */
\r
220 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
222 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
224 xCreatedTask = mainNO_TASK;
\r
225 if( xTaskCreate( vMemCheckTask, ( signed char * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
227 /* Could not create the task - we have probably run out of heap. */
\r
228 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
232 /* Delay until it is time to execute again. The delay period is
\r
233 shorter following an error. */
\r
234 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );
\r
237 /* Delete the dynamically created task. */
\r
238 if( xCreatedTask != mainNO_TASK )
\r
240 vTaskDelete( xCreatedTask );
\r
243 /* Check all the standard demo application tasks are executing without
\r
244 error. ulMemCheckTaskRunningCount is checked to ensure it was
\r
245 modified by the task just deleted. */
\r
246 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )
\r
248 /* An error has been detected in one of the tasks - flash faster. */
\r
249 xDelayPeriod = mainERROR_CHECK_PERIOD;
\r
252 vParTestToggleLED( mainCHECK_TASK_LED );
\r
255 /*-----------------------------------------------------------*/
\r
258 * Check each set of tasks in turn to see if they have experienced any
\r
259 * error conditions.
\r
261 static long prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )
\r
263 long lNoErrorsDiscovered = ( long ) pdTRUE;
\r
265 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
267 lNoErrorsDiscovered = pdFALSE;
\r
270 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
272 lNoErrorsDiscovered = pdFALSE;
\r
275 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
277 lNoErrorsDiscovered = pdFALSE;
\r
280 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
282 lNoErrorsDiscovered = pdFALSE;
\r
285 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
287 lNoErrorsDiscovered = pdFALSE;
\r
290 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
292 lNoErrorsDiscovered = pdFALSE;
\r
295 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
297 lNoErrorsDiscovered = pdFALSE;
\r
300 if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )
\r
302 /* The vMemCheckTask task did not increment the counter - it must
\r
304 lNoErrorsDiscovered = pdFALSE;
\r
307 return lNoErrorsDiscovered;
\r
309 /*-----------------------------------------------------------*/
\r
311 static void vMemCheckTask( void *pvParameters )
\r
313 unsigned long *pulMemCheckTaskRunningCounter;
\r
314 void *pvMem1, *pvMem2, *pvMem3;
\r
315 static long lErrorOccurred = pdFALSE;
\r
317 /* This task is dynamically created then deleted during each cycle of the
\r
318 vErrorChecks task to check the operation of the memory allocator. Each time
\r
319 the task is created memory is allocated for the stack and TCB. Each time
\r
320 the task is deleted this memory is returned to the heap. This task itself
\r
321 exercises the allocator by allocating and freeing blocks.
\r
323 The task executes at the idle priority so does not require a delay.
\r
325 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
326 vErrorChecks() task that this task is still executing without error. */
\r
328 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
332 if( lErrorOccurred == pdFALSE )
\r
334 /* We have never seen an error so increment the counter. */
\r
335 ( *pulMemCheckTaskRunningCounter )++;
\r
339 /* Reset the count so an error is detected by the
\r
340 prvCheckOtherTasksAreStillRunning() function. */
\r
341 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
344 /* Allocate some memory - just to give the allocator some extra
\r
345 exercise. This has to be in a critical section to ensure the
\r
346 task does not get deleted while it has memory allocated. */
\r
349 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
350 if( pvMem1 == NULL )
\r
352 lErrorOccurred = pdTRUE;
\r
356 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
357 vPortFree( pvMem1 );
\r
362 /* Again - with a different size block. */
\r
365 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
366 if( pvMem2 == NULL )
\r
368 lErrorOccurred = pdTRUE;
\r
372 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
373 vPortFree( pvMem2 );
\r
378 /* Again - with a different size block. */
\r
381 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
382 if( pvMem3 == NULL )
\r
384 lErrorOccurred = pdTRUE;
\r
388 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
389 vPortFree( pvMem3 );
\r
395 /*-----------------------------------------------------------*/
\r
398 * Called by the startup code. Initial processor setup can be placed in this
\r
401 void hw_initialise (void)
\r