1 /*This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
4 * \brief FreeRTOS Real Time Kernel example.
\r
6 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
7 * documentation provides more details of the demo application tasks.
\r
9 * Main. c also creates a task called "Check". This only executes every three
\r
10 * seconds but has the highest priority so is guaranteed to get processor time.
\r
11 * Its main function is to check that all the other tasks are still operational.
\r
12 * Each task that does not flash an LED maintains a unique count that is
\r
13 * incremented each time the task successfully completes its function. Should
\r
14 * any error occur within such a task the count is permanently halted. The
\r
15 * check task inspects the count of each task to ensure it has changed since
\r
16 * the last time the check task executed. If all the count variables have
\r
17 * changed all the tasks are still executing error free, and the check task
\r
18 * toggles an LED. Should any task contain an error at any time the LED toggle
\r
21 * The LED flash and communications test tasks do not maintain a count.
\r
23 * - Compiler: IAR EWAVR32 and GNU GCC for AVR32
\r
24 * - Supported devices: All AVR32 devices with GPIO.
\r
27 * \author Atmel Corporation: http://www.atmel.com \n
\r
28 * Support and FAQ: http://support.atmel.no/
\r
30 *****************************************************************************/
\r
33 FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.
\r
35 This file is part of the FreeRTOS.org distribution.
\r
37 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
38 it under the terms of the GNU General Public License as published by
\r
39 the Free Software Foundation; either version 2 of the License, or
\r
40 (at your option) any later version.
\r
42 FreeRTOS.org is distributed in the hope that it will be useful,
\r
43 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
44 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
45 GNU General Public License for more details.
\r
47 You should have received a copy of the GNU General Public License
\r
48 along with FreeRTOS.org; if not, write to the Free Software
\r
49 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
51 A special exception to the GPL can be applied should you wish to distribute
\r
52 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
53 the source code for any proprietary components. See the licensing section
\r
54 of http://www.FreeRTOS.org for full details of how and when the exception
\r
57 ***************************************************************************
\r
58 ***************************************************************************
\r
60 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
61 * and even write all or part of your application on your behalf. *
\r
62 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
63 * expedite your project. *
\r
65 ***************************************************************************
\r
66 ***************************************************************************
\r
68 Please ensure to read the configuration and relevant port sections of the
\r
69 online documentation.
\r
71 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
74 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
77 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
78 licensing and training services.
\r
86 /* Environment header files. */
\r
89 /* Scheduler header files. */
\r
90 #include "FreeRTOS.h"
\r
93 /* Demo file headers. */
\r
94 #include "partest.h"
\r
96 #include "integer.h"
\r
97 #include "comtest.h"
\r
100 #include "semtest.h"
\r
101 #include "dynamic.h"
\r
102 #include "BlockQ.h"
\r
106 /*! \name Priority definitions for most of the tasks in the demo application.
\r
107 * Some tasks just use the idle priority.
\r
110 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
111 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
112 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
113 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
114 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
115 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
116 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
119 //! Baud rate used by the serial port tasks.
\r
120 #define mainCOM_TEST_BAUD_RATE ( ( unsigned portLONG ) 57600 )
\r
122 //! LED used by the serial port tasks. This is toggled on each character Tx,
\r
123 //! and mainCOM_TEST_LED + 1 is toggled 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. If an error is found at any time the LED toggles faster.
\r
129 #define mainCHECK_TASK_LED ( 6 )
\r
131 //! LED that is set upon error.
\r
132 #define mainERROR_LED ( 7 )
\r
134 //! The period between executions of the check task.
\r
135 #define mainCHECK_PERIOD ( ( portTickType ) 3000 / portTICK_RATE_MS )
\r
137 //! If an error is detected in a task, the vErrorChecks task will enter in an
\r
138 //! infinite loop flashing the LED at this rate.
\r
139 #define mainERROR_FLASH_RATE ( (portTickType) 500 / portTICK_RATE_MS )
\r
141 /*! \name Constants used by the vMemCheckTask() task.
\r
144 #define mainCOUNT_INITIAL_VALUE ( ( unsigned portLONG ) 0 )
\r
145 #define mainNO_TASK ( 0 )
\r
148 /*! \name The size of the memory blocks allocated by the vMemCheckTask() task.
\r
151 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
152 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
153 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 15 )
\r
157 /*-----------------------------------------------------------*/
\r
160 * The task that executes at the highest priority and calls
\r
161 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
164 static void vErrorChecks( void *pvParameters );
\r
167 * Checks that all the demo application tasks are still executing without error
\r
168 * - as described at the top of the file.
\r
170 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );
\r
173 * A task that exercises the memory allocator.
\r
175 static void vMemCheckTask( void *pvParameters );
\r
178 * Called by the check task following the detection of an error to set the
\r
179 * LEDs into a state that shows an error has beeen found.
\r
181 static void prvIndicateError( void );
\r
183 /*-----------------------------------------------------------*/
\r
187 /* Start the crystal oscillator 0 and switch the main clock to it. */
\r
188 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
\r
190 portDBG_TRACE("Starting the FreeRTOS AVR32 UC3 Demo...");
\r
192 /* Setup the LED's for output. */
\r
193 vParTestInitialise();
\r
195 /* Start the standard demo tasks. See the WEB documentation for more
\r
197 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
198 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
199 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
200 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
201 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
202 vStartDynamicPriorityTasks();
\r
203 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
204 vStartMathTasks( tskIDLE_PRIORITY );
\r
206 /* Start the demo tasks defined within this file, specifically the check
\r
207 task as described at the top of this file. */
\r
210 , (const signed portCHAR *)"ErrCheck"
\r
211 , configMINIMAL_STACK_SIZE
\r
213 , mainCHECK_TASK_PRIORITY
\r
216 /* Start the scheduler. */
\r
217 vTaskStartScheduler();
\r
219 /* Will only get here if there was insufficient memory to create the idle
\r
224 /*-----------------------------------------------------------*/
\r
227 * \brief The task function for the "Check" task.
\r
229 static void vErrorChecks( void *pvParameters )
\r
231 static volatile unsigned portLONG ulDummyVariable = 3UL;
\r
232 unsigned portLONG ulMemCheckTaskRunningCount;
\r
233 xTaskHandle xCreatedTask;
\r
234 portBASE_TYPE bSuicidalTask = 0;
\r
236 /* The parameters are not used. Prevent compiler warnings. */
\r
237 ( void ) pvParameters;
\r
239 /* Cycle for ever, delaying then checking all the other tasks are still
\r
240 operating without error.
\r
242 In addition to the standard tests the memory allocator is tested through
\r
243 the dynamic creation and deletion of a task each cycle. Each time the
\r
244 task is created memory must be allocated for its stack. When the task is
\r
245 deleted this memory is returned to the heap. If the task cannot be created
\r
246 then it is likely that the memory allocation failed. */
\r
250 /* Do this only once. */
\r
251 if( bSuicidalTask == 0 )
\r
255 /* This task has to be created last as it keeps account of the number of
\r
256 tasks it expects to see running. However its implementation expects
\r
257 to be called before vTaskStartScheduler(). We're in the case here where
\r
258 vTaskStartScheduler() has already been called (thus the hidden IDLE task
\r
259 has already been spawned). Since vCreateSuicidalTask() supposes that the
\r
260 IDLE task isn't included in the response from uxTaskGetNumberOfTasks(),
\r
261 let the MEM_CHECK task play that role. => this is why vCreateSuicidalTasks()
\r
262 is not called as the last task. */
\r
263 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
266 /* Reset xCreatedTask. This is modified by the task about to be
\r
267 created so we can tell if it is executing correctly or not. */
\r
268 xCreatedTask = mainNO_TASK;
\r
270 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
272 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
274 if( xTaskCreate( vMemCheckTask,
\r
275 ( signed portCHAR * ) "MEM_CHECK",
\r
276 configMINIMAL_STACK_SIZE,
\r
277 ( void * ) &ulMemCheckTaskRunningCount,
\r
278 tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
280 /* Could not create the task - we have probably run out of heap.
\r
281 Don't go any further and flash the LED faster to provide visual
\r
282 feedback of the error. */
\r
283 prvIndicateError();
\r
286 /* Delay until it is time to execute again. */
\r
287 vTaskDelay( mainCHECK_PERIOD );
\r
289 /* Delete the dynamically created task. */
\r
290 if( xCreatedTask != mainNO_TASK )
\r
292 vTaskDelete( xCreatedTask );
\r
295 /* Perform a bit of 32bit maths to ensure the registers used by the
\r
296 integer tasks get some exercise. The result here is not important -
\r
297 see the demo application documentation for more info. */
\r
298 ulDummyVariable *= 3;
\r
300 /* Check all other tasks are still operating without error.
\r
301 Check that vMemCheckTask did increment the counter. */
\r
302 if( ( prvCheckOtherTasksAreStillRunning() != pdFALSE )
\r
303 || ( ulMemCheckTaskRunningCount == mainCOUNT_INITIAL_VALUE ) )
\r
305 /* An error has occurred in one of the tasks.
\r
306 Don't go any further and flash the LED faster to give visual
\r
307 feedback of the error. */
\r
308 prvIndicateError();
\r
312 /* Toggle the LED if everything is okay. */
\r
313 vParTestToggleLED( mainCHECK_TASK_LED );
\r
317 /*-----------------------------------------------------------*/
\r
321 * \brief Checks that all the demo application tasks are still executing without error.
\r
323 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )
\r
325 static portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
327 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
329 xErrorHasOccurred = pdTRUE;
\r
332 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
334 xErrorHasOccurred = pdTRUE;
\r
337 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
339 xErrorHasOccurred = pdTRUE;
\r
342 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
344 xErrorHasOccurred = pdTRUE;
\r
347 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
349 xErrorHasOccurred = pdTRUE;
\r
352 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
354 xErrorHasOccurred = pdTRUE;
\r
357 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
359 xErrorHasOccurred = pdTRUE;
\r
362 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
364 xErrorHasOccurred = pdTRUE;
\r
367 return ( xErrorHasOccurred );
\r
369 /*-----------------------------------------------------------*/
\r
373 * \brief Dynamically created and deleted during each cycle of the vErrorChecks()
\r
374 * task. This is done to check the operation of the memory allocator.
\r
375 * See the top of vErrorChecks for more details.
\r
377 * \param *pvParameters Parameters for the task (can be of any kind)
\r
379 static void vMemCheckTask( void *pvParameters )
\r
381 unsigned portLONG *pulMemCheckTaskRunningCounter;
\r
382 void *pvMem1, *pvMem2, *pvMem3;
\r
383 static portLONG lErrorOccurred = pdFALSE;
\r
385 /* This task is dynamically created then deleted during each cycle of the
\r
386 vErrorChecks task to check the operation of the memory allocator. Each time
\r
387 the task is created memory is allocated for the stack and TCB. Each time
\r
388 the task is deleted this memory is returned to the heap. This task itself
\r
389 exercises the allocator by allocating and freeing blocks.
\r
391 The task executes at the idle priority so does not require a delay.
\r
393 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
394 vErrorChecks() task that this task is still executing without error. */
\r
396 pulMemCheckTaskRunningCounter = ( unsigned portLONG * ) pvParameters;
\r
400 if( lErrorOccurred == pdFALSE )
\r
402 /* We have never seen an error so increment the counter. */
\r
403 ( *pulMemCheckTaskRunningCounter )++;
\r
407 /* There has been an error so reset the counter so the check task
\r
408 can tell that an error occurred. */
\r
409 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
412 /* Allocate some memory - just to give the allocator some extra
\r
413 exercise. This has to be in a critical section to ensure the
\r
414 task does not get deleted while it has memory allocated. */
\r
418 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
420 if( pvMem1 == NULL )
\r
422 lErrorOccurred = pdTRUE;
\r
426 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
427 vPortFree( pvMem1 );
\r
432 /* Again - with a different size block. */
\r
435 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
437 if( pvMem2 == NULL )
\r
439 lErrorOccurred = pdTRUE;
\r
443 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
444 vPortFree( pvMem2 );
\r
449 /* Again - with a different size block. */
\r
452 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
453 if( pvMem3 == NULL )
\r
455 lErrorOccurred = pdTRUE;
\r
459 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
460 vPortFree( pvMem3 );
\r
466 /*-----------------------------------------------------------*/
\r
468 static void prvIndicateError( void )
\r
470 /* The check task has found an error in one of the other tasks.
\r
471 Set the LEDs to a state that indicates this. */
\r
472 vParTestSetLED(mainERROR_LED,pdTRUE);
\r
476 #if( BOARD==EVK1100 )
\r
477 vParTestToggleLED( mainCHECK_TASK_LED );
\r
478 vTaskDelay( mainERROR_FLASH_RATE );
\r
480 #if ( BOARD==EVK1101 )
\r
481 vParTestSetLED( 0, pdTRUE );
\r
482 vParTestSetLED( 1, pdTRUE );
\r
483 vParTestSetLED( 2, pdTRUE );
\r
484 vParTestSetLED( 3, pdTRUE );
\r