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 V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
36 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
38 This file is part of the FreeRTOS distribution.
\r
40 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
41 the terms of the GNU General Public License (version 2) as published by the
\r
42 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
44 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
45 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
46 >>! obliged to provide the source code for proprietary components !<<
\r
47 >>! outside of the FreeRTOS kernel. !<<
\r
49 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
50 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
51 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
52 link: http://www.freertos.org/a00114.html
\r
56 ***************************************************************************
\r
58 * Having a problem? Start by reading the FAQ "My application does *
\r
59 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
61 * http://www.FreeRTOS.org/FAQHelp.html *
\r
63 ***************************************************************************
\r
65 ***************************************************************************
\r
67 * FreeRTOS provides completely free yet professionally developed, *
\r
68 * robust, strictly quality controlled, supported, and cross *
\r
69 * platform software that is more than just the market leader, it *
\r
70 * is the industry's de facto standard. *
\r
72 * Help yourself get started quickly while simultaneously helping *
\r
73 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
74 * tutorial book, reference manual, or both: *
\r
75 * http://www.FreeRTOS.org/Documentation *
\r
77 ***************************************************************************
\r
79 ***************************************************************************
\r
81 * Investing in training allows your team to be as productive as *
\r
82 * possible as early as possible, lowering your overall development *
\r
83 * cost, and enabling you to bring a more robust product to market *
\r
84 * earlier than would otherwise be possible. Richard Barry is both *
\r
85 * the architect and key author of FreeRTOS, and so also the world's *
\r
86 * leading authority on what is the world's most popular real time *
\r
87 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
88 * from Richard ensures your team will gain directly from his in-depth *
\r
89 * product knowledge and years of usage experience. Contact Real Time *
\r
90 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
91 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
93 ***************************************************************************
\r
95 ***************************************************************************
\r
97 * You are receiving this top quality software for free. Please play *
\r
98 * fair and reciprocate by reporting any suspected issues and *
\r
99 * participating in the community forum: *
\r
100 * http://www.FreeRTOS.org/support *
\r
104 ***************************************************************************
\r
106 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
107 license and Real Time Engineers Ltd. contact details.
\r
109 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
110 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
111 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
113 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
114 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
116 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
117 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
118 licenses offer ticketed support, indemnification and commercial middleware.
\r
120 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
121 engineered and independently SIL3 certified version for use in safety and
\r
122 mission critical applications that require provable dependability.
\r
129 #include <stdlib.h>
\r
130 #include <string.h>
\r
132 /* Environment header files. */
\r
135 /* Scheduler header files. */
\r
136 #include "FreeRTOS.h"
\r
139 /* Demo file headers. */
\r
140 #include "partest.h"
\r
141 #include "serial.h"
\r
142 #include "integer.h"
\r
143 #include "comtest.h"
\r
146 #include "semtest.h"
\r
147 #include "dynamic.h"
\r
148 #include "BlockQ.h"
\r
152 /*! \name Priority definitions for most of the tasks in the demo application.
\r
153 * Some tasks just use the idle priority.
\r
156 #define mainLED_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
157 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
158 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
159 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
160 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
161 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 4 )
\r
162 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
165 //! Baud rate used by the serial port tasks.
\r
166 #define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 57600 )
\r
168 //! LED used by the serial port tasks. This is toggled on each character Tx,
\r
169 //! and mainCOM_TEST_LED + 1 is toggled on each character Rx.
\r
170 #define mainCOM_TEST_LED ( 3 )
\r
172 //! LED that is toggled by the check task. The check task periodically checks
\r
173 //! that all the other tasks are operating without error. If no errors are found
\r
174 //! the LED is toggled. If an error is found at any time the LED toggles faster.
\r
175 #define mainCHECK_TASK_LED ( 6 )
\r
177 //! LED that is set upon error.
\r
178 #define mainERROR_LED ( 7 )
\r
180 //! The period between executions of the check task.
\r
181 #define mainCHECK_PERIOD ( ( TickType_t ) 3000 / portTICK_PERIOD_MS )
\r
183 //! If an error is detected in a task, the vErrorChecks task will enter in an
\r
184 //! infinite loop flashing the LED at this rate.
\r
185 #define mainERROR_FLASH_RATE ( (TickType_t) 500 / portTICK_PERIOD_MS )
\r
187 /*! \name Constants used by the vMemCheckTask() task.
\r
190 #define mainCOUNT_INITIAL_VALUE ( ( unsigned long ) 0 )
\r
191 #define mainNO_TASK ( 0 )
\r
194 /*! \name The size of the memory blocks allocated by the vMemCheckTask() task.
\r
197 #define mainMEM_CHECK_SIZE_1 ( ( size_t ) 51 )
\r
198 #define mainMEM_CHECK_SIZE_2 ( ( size_t ) 52 )
\r
199 #define mainMEM_CHECK_SIZE_3 ( ( size_t ) 15 )
\r
203 /*-----------------------------------------------------------*/
\r
206 * The task that executes at the highest priority and calls
\r
207 * prvCheckOtherTasksAreStillRunning(). See the description at the top
\r
210 static void vErrorChecks( void *pvParameters );
\r
213 * Checks that all the demo application tasks are still executing without error
\r
214 * - as described at the top of the file.
\r
216 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );
\r
219 * A task that exercises the memory allocator.
\r
221 static void vMemCheckTask( void *pvParameters );
\r
224 * Called by the check task following the detection of an error to set the
\r
225 * LEDs into a state that shows an error has beeen found.
\r
227 static void prvIndicateError( void );
\r
229 /*-----------------------------------------------------------*/
\r
233 /* Start the crystal oscillator 0 and switch the main clock to it. */
\r
234 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
\r
236 portDBG_TRACE("Starting the FreeRTOS AVR32 UC3 Demo...");
\r
238 /* Setup the LED's for output. */
\r
239 vParTestInitialise();
\r
241 /* Start the standard demo tasks. See the WEB documentation for more
\r
243 vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
\r
244 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
245 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
246 vStartIntegerMathTasks( tskIDLE_PRIORITY );
\r
247 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
248 vStartDynamicPriorityTasks();
\r
249 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
250 vStartMathTasks( tskIDLE_PRIORITY );
\r
252 /* Start the demo tasks defined within this file, specifically the check
\r
253 task as described at the top of this file. */
\r
257 , configMINIMAL_STACK_SIZE
\r
259 , mainCHECK_TASK_PRIORITY
\r
262 /* Start the scheduler. */
\r
263 vTaskStartScheduler();
\r
265 /* Will only get here if there was insufficient memory to create the idle
\r
270 /*-----------------------------------------------------------*/
\r
273 * \brief The task function for the "Check" task.
\r
275 static void vErrorChecks( void *pvParameters )
\r
277 static volatile unsigned long ulDummyVariable = 3UL;
\r
278 unsigned long ulMemCheckTaskRunningCount;
\r
279 TaskHandle_t xCreatedTask;
\r
280 portBASE_TYPE bSuicidalTask = 0;
\r
282 /* The parameters are not used. Prevent compiler warnings. */
\r
283 ( void ) pvParameters;
\r
285 /* Cycle for ever, delaying then checking all the other tasks are still
\r
286 operating without error.
\r
288 In addition to the standard tests the memory allocator is tested through
\r
289 the dynamic creation and deletion of a task each cycle. Each time the
\r
290 task is created memory must be allocated for its stack. When the task is
\r
291 deleted this memory is returned to the heap. If the task cannot be created
\r
292 then it is likely that the memory allocation failed. */
\r
296 /* Do this only once. */
\r
297 if( bSuicidalTask == 0 )
\r
301 /* This task has to be created last as it keeps account of the number of
\r
302 tasks it expects to see running. However its implementation expects
\r
303 to be called before vTaskStartScheduler(). We're in the case here where
\r
304 vTaskStartScheduler() has already been called (thus the hidden IDLE task
\r
305 has already been spawned). Since vCreateSuicidalTask() supposes that the
\r
306 IDLE task isn't included in the response from uxTaskGetNumberOfTasks(),
\r
307 let the MEM_CHECK task play that role. => this is why vCreateSuicidalTasks()
\r
308 is not called as the last task. */
\r
309 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
312 /* Reset xCreatedTask. This is modified by the task about to be
\r
313 created so we can tell if it is executing correctly or not. */
\r
314 xCreatedTask = mainNO_TASK;
\r
316 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a
\r
318 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;
\r
320 if( xTaskCreate( vMemCheckTask,
\r
322 configMINIMAL_STACK_SIZE,
\r
323 ( void * ) &ulMemCheckTaskRunningCount,
\r
324 tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )
\r
326 /* Could not create the task - we have probably run out of heap.
\r
327 Don't go any further and flash the LED faster to provide visual
\r
328 feedback of the error. */
\r
329 prvIndicateError();
\r
332 /* Delay until it is time to execute again. */
\r
333 vTaskDelay( mainCHECK_PERIOD );
\r
335 /* Delete the dynamically created task. */
\r
336 if( xCreatedTask != mainNO_TASK )
\r
338 vTaskDelete( xCreatedTask );
\r
341 /* Perform a bit of 32bit maths to ensure the registers used by the
\r
342 integer tasks get some exercise. The result here is not important -
\r
343 see the demo application documentation for more info. */
\r
344 ulDummyVariable *= 3;
\r
346 /* Check all other tasks are still operating without error.
\r
347 Check that vMemCheckTask did increment the counter. */
\r
348 if( ( prvCheckOtherTasksAreStillRunning() != pdFALSE )
\r
349 || ( ulMemCheckTaskRunningCount == mainCOUNT_INITIAL_VALUE ) )
\r
351 /* An error has occurred in one of the tasks.
\r
352 Don't go any further and flash the LED faster to give visual
\r
353 feedback of the error. */
\r
354 prvIndicateError();
\r
358 /* Toggle the LED if everything is okay. */
\r
359 vParTestToggleLED( mainCHECK_TASK_LED );
\r
363 /*-----------------------------------------------------------*/
\r
367 * \brief Checks that all the demo application tasks are still executing without error.
\r
369 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )
\r
371 static portBASE_TYPE xErrorHasOccurred = pdFALSE;
\r
373 if( xAreComTestTasksStillRunning() != pdTRUE )
\r
375 xErrorHasOccurred = pdTRUE;
\r
378 if( xArePollingQueuesStillRunning() != pdTRUE )
\r
380 xErrorHasOccurred = pdTRUE;
\r
383 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
385 xErrorHasOccurred = pdTRUE;
\r
388 if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
390 xErrorHasOccurred = pdTRUE;
\r
393 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
395 xErrorHasOccurred = pdTRUE;
\r
398 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
400 xErrorHasOccurred = pdTRUE;
\r
403 if( xAreMathsTaskStillRunning() != pdTRUE )
\r
405 xErrorHasOccurred = pdTRUE;
\r
408 if( xIsCreateTaskStillRunning() != pdTRUE )
\r
410 xErrorHasOccurred = pdTRUE;
\r
413 return ( xErrorHasOccurred );
\r
415 /*-----------------------------------------------------------*/
\r
419 * \brief Dynamically created and deleted during each cycle of the vErrorChecks()
\r
420 * task. This is done to check the operation of the memory allocator.
\r
421 * See the top of vErrorChecks for more details.
\r
423 * \param *pvParameters Parameters for the task (can be of any kind)
\r
425 static void vMemCheckTask( void *pvParameters )
\r
427 unsigned long *pulMemCheckTaskRunningCounter;
\r
428 void *pvMem1, *pvMem2, *pvMem3;
\r
429 static long lErrorOccurred = pdFALSE;
\r
431 /* This task is dynamically created then deleted during each cycle of the
\r
432 vErrorChecks task to check the operation of the memory allocator. Each time
\r
433 the task is created memory is allocated for the stack and TCB. Each time
\r
434 the task is deleted this memory is returned to the heap. This task itself
\r
435 exercises the allocator by allocating and freeing blocks.
\r
437 The task executes at the idle priority so does not require a delay.
\r
439 pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the
\r
440 vErrorChecks() task that this task is still executing without error. */
\r
442 pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;
\r
446 if( lErrorOccurred == pdFALSE )
\r
448 /* We have never seen an error so increment the counter. */
\r
449 ( *pulMemCheckTaskRunningCounter )++;
\r
453 /* There has been an error so reset the counter so the check task
\r
454 can tell that an error occurred. */
\r
455 *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;
\r
458 /* Allocate some memory - just to give the allocator some extra
\r
459 exercise. This has to be in a critical section to ensure the
\r
460 task does not get deleted while it has memory allocated. */
\r
464 pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );
\r
466 if( pvMem1 == NULL )
\r
468 lErrorOccurred = pdTRUE;
\r
472 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );
\r
473 vPortFree( pvMem1 );
\r
478 /* Again - with a different size block. */
\r
481 pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );
\r
483 if( pvMem2 == NULL )
\r
485 lErrorOccurred = pdTRUE;
\r
489 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );
\r
490 vPortFree( pvMem2 );
\r
495 /* Again - with a different size block. */
\r
498 pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );
\r
499 if( pvMem3 == NULL )
\r
501 lErrorOccurred = pdTRUE;
\r
505 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );
\r
506 vPortFree( pvMem3 );
\r
512 /*-----------------------------------------------------------*/
\r
514 static void prvIndicateError( void )
\r
516 /* The check task has found an error in one of the other tasks.
\r
517 Set the LEDs to a state that indicates this. */
\r
518 vParTestSetLED(mainERROR_LED,pdTRUE);
\r
522 #if( BOARD==EVK1100 )
\r
523 vParTestToggleLED( mainCHECK_TASK_LED );
\r
524 vTaskDelay( mainERROR_FLASH_RATE );
\r
526 #if ( BOARD==EVK1101 )
\r
527 vParTestSetLED( 0, pdTRUE );
\r
528 vParTestSetLED( 1, pdTRUE );
\r
529 vParTestSetLED( 2, pdTRUE );
\r
530 vParTestSetLED( 3, pdTRUE );
\r