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