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