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