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