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