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