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