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