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