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