]> git.sur5r.net Git - freertos/blob - Demo/AVR32_UC3/main.c
Update version number.
[freertos] / Demo / AVR32_UC3 / main.c
1 /*This file has been prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief FreeRTOS Real Time Kernel example.\r
5  *\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
8  *\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
19  * will stop.\r
20  *\r
21  * The LED flash and communications test tasks do not maintain a count.\r
22  *\r
23  * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32\r
24  * - Supported devices:  All AVR32 devices with GPIO.\r
25  * - AppNote:\r
26  *\r
27  * \author               Atmel Corporation: http://www.atmel.com \n\r
28  *                       Support and FAQ: http://support.atmel.no/\r
29  *\r
30  *****************************************************************************/\r
31 \r
32 /*\r
33         FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\r
34 \r
35         This file is part of the FreeRTOS.org distribution.\r
36 \r
37         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
38         under the terms of the GNU General Public License (version 2) as published\r
39         by the Free Software Foundation and modified by the FreeRTOS exception.\r
40         **NOTE** The exception to the GPL is included to allow you to distribute a\r
41         combined work that includes FreeRTOS.org without being obliged to provide\r
42         the source code for any proprietary components.  Alternative commercial\r
43         license and support terms are also available upon request.  See the \r
44         licensing section of http://www.FreeRTOS.org for full details.\r
45 \r
46         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
47         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
48         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
49         more details.\r
50 \r
51         You should have received a copy of the GNU General Public License along\r
52         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
53         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
54 \r
55 \r
56         ***************************************************************************\r
57         *                                                                         *\r
58         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
59         *                                                                         *\r
60         * This is a concise, step by step, 'hands on' guide that describes both   *\r
61         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
62         * explains numerous examples that are written using the FreeRTOS API.     *\r
63         * Full source code for all the examples is provided in an accompanying    *\r
64         * .zip file.                                                              *\r
65         *                                                                         *\r
66         ***************************************************************************\r
67 \r
68         1 tab == 4 spaces!\r
69 \r
70         Please ensure to read the configuration and relevant port sections of the\r
71         online documentation.\r
72 \r
73         http://www.FreeRTOS.org - Documentation, latest information, license and\r
74         contact details.\r
75 \r
76         http://www.SafeRTOS.com - A version that is certified for use in safety\r
77         critical systems.\r
78 \r
79         http://www.OpenRTOS.com - Commercial support, development, porting,\r
80         licensing and training services.\r
81 */\r
82 \r
83 \r
84 #include <stdio.h>\r
85 #include <stdlib.h>\r
86 #include <string.h>\r
87 \r
88 /* Environment header files. */\r
89 #include "pm.h"\r
90 \r
91 /* Scheduler header files. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 \r
95 /* Demo file headers. */\r
96 #include "partest.h"\r
97 #include "serial.h"\r
98 #include "integer.h"\r
99 #include "comtest.h"\r
100 #include "flash.h"\r
101 #include "PollQ.h"\r
102 #include "semtest.h"\r
103 #include "dynamic.h"\r
104 #include "BlockQ.h"\r
105 #include "death.h"\r
106 #include "flop.h"\r
107 \r
108 /*! \name Priority definitions for most of the tasks in the demo application.\r
109  * Some tasks just use the idle priority.\r
110  */\r
111 //! @{\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 mainSEM_TEST_PRIORITY     ( tskIDLE_PRIORITY + 1 )\r
116 #define mainBLOCK_Q_PRIORITY      ( tskIDLE_PRIORITY + 3 )\r
117 #define mainCHECK_TASK_PRIORITY   ( tskIDLE_PRIORITY + 4 )\r
118 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )\r
119 //! @}\r
120 \r
121 //! Baud rate used by the serial port tasks.\r
122 #define mainCOM_TEST_BAUD_RATE    ( ( unsigned portLONG ) 57600 )\r
123 \r
124 //! LED used by the serial port tasks.  This is toggled on each character Tx,\r
125 //! and mainCOM_TEST_LED + 1 is toggled on each character Rx.\r
126 #define mainCOM_TEST_LED          ( 3 )\r
127 \r
128 //! LED that is toggled by the check task.  The check task periodically checks\r
129 //! that all the other tasks are operating without error.  If no errors are found\r
130 //! the LED is toggled.  If an error is found at any time the LED toggles faster.\r
131 #define mainCHECK_TASK_LED        ( 6 )\r
132 \r
133 //! LED that is set upon error.\r
134 #define mainERROR_LED             ( 7 )\r
135 \r
136 //! The period between executions of the check task.\r
137 #define mainCHECK_PERIOD          ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
138 \r
139 //! If an error is detected in a task, the vErrorChecks task will enter in an\r
140 //! infinite loop flashing the LED at this rate.\r
141 #define mainERROR_FLASH_RATE      ( (portTickType) 500 / portTICK_RATE_MS )\r
142 \r
143 /*! \name Constants used by the vMemCheckTask() task.\r
144  */\r
145 //! @{\r
146 #define mainCOUNT_INITIAL_VALUE   ( ( unsigned portLONG ) 0 )\r
147 #define mainNO_TASK               ( 0 )\r
148 //! @}\r
149 \r
150 /*! \name The size of the memory blocks allocated by the vMemCheckTask() task.\r
151  */\r
152 //! @{\r
153 #define mainMEM_CHECK_SIZE_1      ( ( size_t ) 51 )\r
154 #define mainMEM_CHECK_SIZE_2      ( ( size_t ) 52 )\r
155 #define mainMEM_CHECK_SIZE_3      ( ( size_t ) 15 )\r
156 //! @}\r
157 \r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /*\r
162  * The task that executes at the highest priority and calls\r
163  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
164  * of the file.\r
165  */\r
166 static void vErrorChecks( void *pvParameters );\r
167 \r
168 /*\r
169  * Checks that all the demo application tasks are still executing without error\r
170  * - as described at the top of the file.\r
171  */\r
172 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );\r
173 \r
174 /*\r
175  * A task that exercises the memory allocator.\r
176  */\r
177 static void vMemCheckTask( void *pvParameters );\r
178 \r
179 /*\r
180  * Called by the check task following the detection of an error to set the\r
181  * LEDs into a state that shows an error has beeen found.\r
182  */\r
183 static void prvIndicateError( void );\r
184 \r
185 /*-----------------------------------------------------------*/\r
186 \r
187 int main( void )\r
188 {\r
189         /* Start the crystal oscillator 0 and switch the main clock to it. */\r
190         pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);\r
191 \r
192         portDBG_TRACE("Starting the FreeRTOS AVR32 UC3 Demo...");\r
193 \r
194         /* Setup the LED's for output. */\r
195         vParTestInitialise();\r
196 \r
197         /* Start the standard demo tasks.  See the WEB documentation for more\r
198         information. */\r
199         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
200         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
201         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
202         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
203         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
204         vStartDynamicPriorityTasks();\r
205         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
206         vStartMathTasks( tskIDLE_PRIORITY );\r
207 \r
208         /* Start the demo tasks defined within this file, specifically the check\r
209         task as described at the top of this file. */\r
210         xTaskCreate(\r
211                 vErrorChecks\r
212                 ,  (const signed portCHAR *)"ErrCheck"\r
213                 ,  configMINIMAL_STACK_SIZE\r
214                 ,  NULL\r
215                 ,  mainCHECK_TASK_PRIORITY\r
216                 ,  NULL );\r
217 \r
218         /* Start the scheduler. */\r
219         vTaskStartScheduler();\r
220 \r
221         /* Will only get here if there was insufficient memory to create the idle\r
222         task. */\r
223 \r
224         return 0;\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 /*!\r
229  * \brief The task function for the "Check" task.\r
230  */\r
231 static void vErrorChecks( void *pvParameters )\r
232 {\r
233 static volatile unsigned portLONG ulDummyVariable = 3UL;\r
234 unsigned portLONG ulMemCheckTaskRunningCount;\r
235 xTaskHandle xCreatedTask;\r
236 portBASE_TYPE bSuicidalTask = 0;\r
237 \r
238         /* The parameters are not used.  Prevent compiler warnings. */\r
239         ( void ) pvParameters;\r
240 \r
241         /* Cycle for ever, delaying then checking all the other tasks are still\r
242         operating without error.\r
243 \r
244         In addition to the standard tests the memory allocator is tested through\r
245         the dynamic creation and deletion of a task each cycle.  Each time the\r
246         task is created memory must be allocated for its stack.  When the task is\r
247         deleted this memory is returned to the heap.  If the task cannot be created\r
248         then it is likely that the memory allocation failed. */\r
249 \r
250         for( ;; )\r
251         {\r
252                 /* Do this only once. */\r
253                 if( bSuicidalTask == 0 )\r
254                 {\r
255                         bSuicidalTask++;\r
256 \r
257                         /* This task has to be created last as it keeps account of the number of\r
258                         tasks it expects to see running. However its implementation expects\r
259                         to be called before vTaskStartScheduler(). We're in the case here where\r
260                         vTaskStartScheduler() has already been called (thus the hidden IDLE task\r
261                         has already been spawned). Since vCreateSuicidalTask() supposes that the\r
262                         IDLE task isn't included in the response from uxTaskGetNumberOfTasks(),\r
263                         let the MEM_CHECK task play that role. => this is why vCreateSuicidalTasks()\r
264                         is not called as the last task. */\r
265                         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
266                 }\r
267 \r
268                 /* Reset xCreatedTask.  This is modified by the task about to be\r
269                 created so we can tell if it is executing correctly or not. */\r
270                 xCreatedTask = mainNO_TASK;\r
271 \r
272                 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a\r
273                 parameter. */\r
274                 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
275 \r
276                 if( xTaskCreate( vMemCheckTask,\r
277                         ( signed portCHAR * ) "MEM_CHECK",\r
278                         configMINIMAL_STACK_SIZE,\r
279                         ( void * ) &ulMemCheckTaskRunningCount,\r
280                         tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
281                 {\r
282                         /* Could not create the task - we have probably run out of heap.\r
283                         Don't go any further and flash the LED faster to provide visual\r
284                         feedback of the error. */\r
285                         prvIndicateError();\r
286                 }\r
287 \r
288                 /* Delay until it is time to execute again. */\r
289                 vTaskDelay( mainCHECK_PERIOD );\r
290 \r
291                 /* Delete the dynamically created task. */\r
292                 if( xCreatedTask != mainNO_TASK )\r
293                 {\r
294                         vTaskDelete( xCreatedTask );\r
295                 }\r
296 \r
297                 /* Perform a bit of 32bit maths to ensure the registers used by the\r
298                 integer tasks get some exercise. The result here is not important -\r
299                 see the demo application documentation for more info. */\r
300                 ulDummyVariable *= 3;\r
301 \r
302                 /* Check all other tasks are still operating without error.\r
303                 Check that vMemCheckTask did increment the counter. */\r
304                 if( ( prvCheckOtherTasksAreStillRunning() != pdFALSE )\r
305                  || ( ulMemCheckTaskRunningCount == mainCOUNT_INITIAL_VALUE ) )\r
306                 {\r
307                         /* An error has occurred in one of the tasks.\r
308                         Don't go any further and flash the LED faster to give visual\r
309                         feedback of the error. */\r
310                         prvIndicateError();\r
311                 }\r
312                 else\r
313                 {\r
314                         /* Toggle the LED if everything is okay. */\r
315                         vParTestToggleLED( mainCHECK_TASK_LED );\r
316                 }\r
317         }\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 \r
322 /*!\r
323  * \brief Checks that all the demo application tasks are still executing without error.\r
324  */\r
325 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )\r
326 {\r
327 static portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
328 \r
329         if( xAreComTestTasksStillRunning() != pdTRUE )\r
330         {\r
331                 xErrorHasOccurred = pdTRUE;\r
332         }\r
333 \r
334         if( xArePollingQueuesStillRunning() != pdTRUE )\r
335         {\r
336                 xErrorHasOccurred = pdTRUE;\r
337         }\r
338 \r
339         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
340         {\r
341                 xErrorHasOccurred = pdTRUE;\r
342         }\r
343 \r
344         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
345         {\r
346                 xErrorHasOccurred = pdTRUE;\r
347         }\r
348 \r
349         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
350         {\r
351                 xErrorHasOccurred = pdTRUE;\r
352         }\r
353 \r
354         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
355         {\r
356                 xErrorHasOccurred = pdTRUE;\r
357         }\r
358 \r
359         if( xAreMathsTaskStillRunning() != pdTRUE )\r
360         {\r
361                 xErrorHasOccurred = pdTRUE;\r
362         }\r
363 \r
364         if( xIsCreateTaskStillRunning() != pdTRUE )\r
365         {\r
366                 xErrorHasOccurred = pdTRUE;\r
367         }\r
368 \r
369         return ( xErrorHasOccurred );\r
370 }\r
371 /*-----------------------------------------------------------*/\r
372 \r
373 \r
374 /*!\r
375  * \brief Dynamically created and deleted during each cycle of the vErrorChecks()\r
376  * task.  This is done to check the operation of the memory allocator.\r
377  * See the top of vErrorChecks for more details.\r
378  *\r
379  * \param *pvParameters Parameters for the task (can be of any kind)\r
380  */\r
381 static void vMemCheckTask( void *pvParameters )\r
382 {\r
383 unsigned portLONG *pulMemCheckTaskRunningCounter;\r
384 void *pvMem1, *pvMem2, *pvMem3;\r
385 static portLONG lErrorOccurred = pdFALSE;\r
386 \r
387         /* This task is dynamically created then deleted during each cycle of the\r
388         vErrorChecks task to check the operation of the memory allocator.  Each time\r
389         the task is created memory is allocated for the stack and TCB.  Each time\r
390         the task is deleted this memory is returned to the heap.  This task itself\r
391         exercises the allocator by allocating and freeing blocks.\r
392 \r
393         The task executes at the idle priority so does not require a delay.\r
394 \r
395         pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
396         vErrorChecks() task that this task is still executing without error. */\r
397 \r
398         pulMemCheckTaskRunningCounter = ( unsigned portLONG * ) pvParameters;\r
399 \r
400         for( ;; )\r
401         {\r
402                 if( lErrorOccurred == pdFALSE )\r
403                 {\r
404                         /* We have never seen an error so increment the counter. */\r
405                         ( *pulMemCheckTaskRunningCounter )++;\r
406                 }\r
407                 else\r
408                 {\r
409                         /* There has been an error so reset the counter so the check task\r
410                         can tell that an error occurred. */\r
411                         *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;\r
412                 }\r
413 \r
414                 /* Allocate some memory - just to give the allocator some extra\r
415                 exercise.  This has to be in a critical section to ensure the\r
416                 task does not get deleted while it has memory allocated. */\r
417 \r
418                 vTaskSuspendAll();\r
419                 {\r
420                         pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
421 \r
422                         if( pvMem1 == NULL )\r
423                         {\r
424                                 lErrorOccurred = pdTRUE;\r
425                         }\r
426                         else\r
427                         {\r
428                                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
429                                 vPortFree( pvMem1 );\r
430                         }\r
431                 }\r
432                 xTaskResumeAll();\r
433 \r
434                 /* Again - with a different size block. */\r
435                 vTaskSuspendAll();\r
436                 {\r
437                         pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
438 \r
439                         if( pvMem2 == NULL )\r
440                         {\r
441                                 lErrorOccurred = pdTRUE;\r
442                         }\r
443                         else\r
444                         {\r
445                                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
446                                 vPortFree( pvMem2 );\r
447                         }\r
448                 }\r
449                 xTaskResumeAll();\r
450 \r
451                 /* Again - with a different size block. */\r
452                 vTaskSuspendAll();\r
453                 {\r
454                         pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
455                         if( pvMem3 == NULL )\r
456                         {\r
457                                 lErrorOccurred = pdTRUE;\r
458                         }\r
459                         else\r
460                         {\r
461                                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
462                                 vPortFree( pvMem3 );\r
463                         }\r
464                 }\r
465                 xTaskResumeAll();\r
466         }\r
467 }\r
468 /*-----------------------------------------------------------*/\r
469 \r
470 static void prvIndicateError( void )\r
471 {\r
472         /* The check task has found an error in one of the other tasks.\r
473         Set the LEDs to a state that indicates this. */\r
474         vParTestSetLED(mainERROR_LED,pdTRUE);\r
475 \r
476         for(;;)\r
477         {\r
478                 #if( BOARD==EVK1100 )\r
479                         vParTestToggleLED( mainCHECK_TASK_LED );\r
480                         vTaskDelay( mainERROR_FLASH_RATE );\r
481                 #endif\r
482                 #if ( BOARD==EVK1101 )\r
483                         vParTestSetLED( 0, pdTRUE );\r
484                         vParTestSetLED( 1, pdTRUE );\r
485                         vParTestSetLED( 2, pdTRUE );\r
486                         vParTestSetLED( 3, pdTRUE );\r
487                 #endif\r
488         }\r
489 }\r
490 \r