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