]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR32_UC3/main.c
a13a45f779480340cd66ec000156456f40c1241b
[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 Kernel V10.3.0\r
34  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
35  *\r
36  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
37  * this software and associated documentation files (the "Software"), to deal in\r
38  * the Software without restriction, including without limitation the rights to\r
39  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
40  * the Software, and to permit persons to whom the Software is furnished to do so,\r
41  * subject to the following conditions:\r
42  *\r
43  * The above copyright notice and this permission notice shall be included in all\r
44  * copies or substantial portions of the Software.\r
45  *\r
46  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
47  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
48  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
49  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
50  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
51  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
52  *\r
53  * http://www.FreeRTOS.org\r
54  * http://aws.amazon.com/freertos\r
55  *\r
56  * 1 tab == 4 spaces!\r
57  */\r
58 \r
59 \r
60 #include <stdio.h>\r
61 #include <stdlib.h>\r
62 #include <string.h>\r
63 \r
64 /* Environment header files. */\r
65 #include "pm.h"\r
66 \r
67 /* Scheduler header files. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 \r
71 /* Demo file headers. */\r
72 #include "partest.h"\r
73 #include "serial.h"\r
74 #include "integer.h"\r
75 #include "comtest.h"\r
76 #include "flash.h"\r
77 #include "PollQ.h"\r
78 #include "semtest.h"\r
79 #include "dynamic.h"\r
80 #include "BlockQ.h"\r
81 #include "death.h"\r
82 #include "flop.h"\r
83 \r
84 /*! \name Priority definitions for most of the tasks in the demo application.\r
85  * Some tasks just use the idle priority.\r
86  */\r
87 //! @{\r
88 #define mainLED_TASK_PRIORITY     ( tskIDLE_PRIORITY + 1 )\r
89 #define mainCOM_TEST_PRIORITY     ( tskIDLE_PRIORITY + 2 )\r
90 #define mainQUEUE_POLL_PRIORITY   ( tskIDLE_PRIORITY + 2 )\r
91 #define mainSEM_TEST_PRIORITY     ( tskIDLE_PRIORITY + 1 )\r
92 #define mainBLOCK_Q_PRIORITY      ( tskIDLE_PRIORITY + 3 )\r
93 #define mainCHECK_TASK_PRIORITY   ( tskIDLE_PRIORITY + 4 )\r
94 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )\r
95 //! @}\r
96 \r
97 //! Baud rate used by the serial port tasks.\r
98 #define mainCOM_TEST_BAUD_RATE    ( ( unsigned long ) 57600 )\r
99 \r
100 //! LED used by the serial port tasks.  This is toggled on each character Tx,\r
101 //! and mainCOM_TEST_LED + 1 is toggled on each character Rx.\r
102 #define mainCOM_TEST_LED          ( 3 )\r
103 \r
104 //! LED that is toggled by the check task.  The check task periodically checks\r
105 //! that all the other tasks are operating without error.  If no errors are found\r
106 //! the LED is toggled.  If an error is found at any time the LED toggles faster.\r
107 #define mainCHECK_TASK_LED        ( 6 )\r
108 \r
109 //! LED that is set upon error.\r
110 #define mainERROR_LED             ( 7 )\r
111 \r
112 //! The period between executions of the check task.\r
113 #define mainCHECK_PERIOD          ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
114 \r
115 //! If an error is detected in a task, the vErrorChecks task will enter in an\r
116 //! infinite loop flashing the LED at this rate.\r
117 #define mainERROR_FLASH_RATE      ( (TickType_t) 500 / portTICK_PERIOD_MS )\r
118 \r
119 /*! \name Constants used by the vMemCheckTask() task.\r
120  */\r
121 //! @{\r
122 #define mainCOUNT_INITIAL_VALUE   ( ( unsigned long ) 0 )\r
123 #define mainNO_TASK               ( 0 )\r
124 //! @}\r
125 \r
126 /*! \name The size of the memory blocks allocated by the vMemCheckTask() task.\r
127  */\r
128 //! @{\r
129 #define mainMEM_CHECK_SIZE_1      ( ( size_t ) 51 )\r
130 #define mainMEM_CHECK_SIZE_2      ( ( size_t ) 52 )\r
131 #define mainMEM_CHECK_SIZE_3      ( ( size_t ) 15 )\r
132 //! @}\r
133 \r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /*\r
138  * The task that executes at the highest priority and calls\r
139  * prvCheckOtherTasksAreStillRunning().  See the description at the top\r
140  * of the file.\r
141  */\r
142 static void vErrorChecks( void *pvParameters );\r
143 \r
144 /*\r
145  * Checks that all the demo application tasks are still executing without error\r
146  * - as described at the top of the file.\r
147  */\r
148 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void );\r
149 \r
150 /*\r
151  * A task that exercises the memory allocator.\r
152  */\r
153 static void vMemCheckTask( void *pvParameters );\r
154 \r
155 /*\r
156  * Called by the check task following the detection of an error to set the\r
157  * LEDs into a state that shows an error has beeen found.\r
158  */\r
159 static void prvIndicateError( void );\r
160 \r
161 /*-----------------------------------------------------------*/\r
162 \r
163 int main( void )\r
164 {\r
165         /* Start the crystal oscillator 0 and switch the main clock to it. */\r
166         pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);\r
167 \r
168         portDBG_TRACE("Starting the FreeRTOS AVR32 UC3 Demo...");\r
169 \r
170         /* Setup the LED's for output. */\r
171         vParTestInitialise();\r
172 \r
173         /* Start the standard demo tasks.  See the WEB documentation for more\r
174         information. */\r
175         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
176         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
177         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
178         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
179         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
180         vStartDynamicPriorityTasks();\r
181         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
182         vStartMathTasks( tskIDLE_PRIORITY );\r
183 \r
184         /* Start the demo tasks defined within this file, specifically the check\r
185         task as described at the top of this file. */\r
186         xTaskCreate(\r
187                 vErrorChecks\r
188                 ,  "ErrCheck"\r
189                 ,  configMINIMAL_STACK_SIZE\r
190                 ,  NULL\r
191                 ,  mainCHECK_TASK_PRIORITY\r
192                 ,  NULL );\r
193 \r
194         /* Start the scheduler. */\r
195         vTaskStartScheduler();\r
196 \r
197         /* Will only get here if there was insufficient memory to create the idle\r
198         task. */\r
199 \r
200         return 0;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /*!\r
205  * \brief The task function for the "Check" task.\r
206  */\r
207 static void vErrorChecks( void *pvParameters )\r
208 {\r
209 static volatile unsigned long ulDummyVariable = 3UL;\r
210 unsigned long ulMemCheckTaskRunningCount;\r
211 TaskHandle_t xCreatedTask;\r
212 portBASE_TYPE bSuicidalTask = 0;\r
213 \r
214         /* The parameters are not used.  Prevent compiler warnings. */\r
215         ( void ) pvParameters;\r
216 \r
217         /* Cycle for ever, delaying then checking all the other tasks are still\r
218         operating without error.\r
219 \r
220         In addition to the standard tests the memory allocator is tested through\r
221         the dynamic creation and deletion of a task each cycle.  Each time the\r
222         task is created memory must be allocated for its stack.  When the task is\r
223         deleted this memory is returned to the heap.  If the task cannot be created\r
224         then it is likely that the memory allocation failed. */\r
225 \r
226         for( ;; )\r
227         {\r
228                 /* Do this only once. */\r
229                 if( bSuicidalTask == 0 )\r
230                 {\r
231                         bSuicidalTask++;\r
232 \r
233                         /* This task has to be created last as it keeps account of the number of\r
234                         tasks it expects to see running. However its implementation expects\r
235                         to be called before vTaskStartScheduler(). We're in the case here where\r
236                         vTaskStartScheduler() has already been called (thus the hidden IDLE task\r
237                         has already been spawned). Since vCreateSuicidalTask() supposes that the\r
238                         IDLE task isn't included in the response from uxTaskGetNumberOfTasks(),\r
239                         let the MEM_CHECK task play that role. => this is why vCreateSuicidalTasks()\r
240                         is not called as the last task. */\r
241                         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
242                 }\r
243 \r
244                 /* Reset xCreatedTask.  This is modified by the task about to be\r
245                 created so we can tell if it is executing correctly or not. */\r
246                 xCreatedTask = mainNO_TASK;\r
247 \r
248                 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a\r
249                 parameter. */\r
250                 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
251 \r
252                 if( xTaskCreate( vMemCheckTask,\r
253                         "MEM_CHECK",\r
254                         configMINIMAL_STACK_SIZE,\r
255                         ( void * ) &ulMemCheckTaskRunningCount,\r
256                         tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
257                 {\r
258                         /* Could not create the task - we have probably run out of heap.\r
259                         Don't go any further and flash the LED faster to provide visual\r
260                         feedback of the error. */\r
261                         prvIndicateError();\r
262                 }\r
263 \r
264                 /* Delay until it is time to execute again. */\r
265                 vTaskDelay( mainCHECK_PERIOD );\r
266 \r
267                 /* Delete the dynamically created task. */\r
268                 if( xCreatedTask != mainNO_TASK )\r
269                 {\r
270                         vTaskDelete( xCreatedTask );\r
271                 }\r
272 \r
273                 /* Perform a bit of 32bit maths to ensure the registers used by the\r
274                 integer tasks get some exercise. The result here is not important -\r
275                 see the demo application documentation for more info. */\r
276                 ulDummyVariable *= 3;\r
277 \r
278                 /* Check all other tasks are still operating without error.\r
279                 Check that vMemCheckTask did increment the counter. */\r
280                 if( ( prvCheckOtherTasksAreStillRunning() != pdFALSE )\r
281                  || ( ulMemCheckTaskRunningCount == mainCOUNT_INITIAL_VALUE ) )\r
282                 {\r
283                         /* An error has occurred in one of the tasks.\r
284                         Don't go any further and flash the LED faster to give visual\r
285                         feedback of the error. */\r
286                         prvIndicateError();\r
287                 }\r
288                 else\r
289                 {\r
290                         /* Toggle the LED if everything is okay. */\r
291                         vParTestToggleLED( mainCHECK_TASK_LED );\r
292                 }\r
293         }\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 \r
298 /*!\r
299  * \brief Checks that all the demo application tasks are still executing without error.\r
300  */\r
301 static portBASE_TYPE prvCheckOtherTasksAreStillRunning( void )\r
302 {\r
303 static portBASE_TYPE xErrorHasOccurred = pdFALSE;\r
304 \r
305         if( xAreComTestTasksStillRunning() != pdTRUE )\r
306         {\r
307                 xErrorHasOccurred = pdTRUE;\r
308         }\r
309 \r
310         if( xArePollingQueuesStillRunning() != pdTRUE )\r
311         {\r
312                 xErrorHasOccurred = pdTRUE;\r
313         }\r
314 \r
315         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
316         {\r
317                 xErrorHasOccurred = pdTRUE;\r
318         }\r
319 \r
320         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
321         {\r
322                 xErrorHasOccurred = pdTRUE;\r
323         }\r
324 \r
325         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
326         {\r
327                 xErrorHasOccurred = pdTRUE;\r
328         }\r
329 \r
330         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
331         {\r
332                 xErrorHasOccurred = pdTRUE;\r
333         }\r
334 \r
335         if( xAreMathsTaskStillRunning() != pdTRUE )\r
336         {\r
337                 xErrorHasOccurred = pdTRUE;\r
338         }\r
339 \r
340         if( xIsCreateTaskStillRunning() != pdTRUE )\r
341         {\r
342                 xErrorHasOccurred = pdTRUE;\r
343         }\r
344 \r
345         return ( xErrorHasOccurred );\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 \r
350 /*!\r
351  * \brief Dynamically created and deleted during each cycle of the vErrorChecks()\r
352  * task.  This is done to check the operation of the memory allocator.\r
353  * See the top of vErrorChecks for more details.\r
354  *\r
355  * \param *pvParameters Parameters for the task (can be of any kind)\r
356  */\r
357 static void vMemCheckTask( void *pvParameters )\r
358 {\r
359 unsigned long *pulMemCheckTaskRunningCounter;\r
360 void *pvMem1, *pvMem2, *pvMem3;\r
361 static long lErrorOccurred = pdFALSE;\r
362 \r
363         /* This task is dynamically created then deleted during each cycle of the\r
364         vErrorChecks task to check the operation of the memory allocator.  Each time\r
365         the task is created memory is allocated for the stack and TCB.  Each time\r
366         the task is deleted this memory is returned to the heap.  This task itself\r
367         exercises the allocator by allocating and freeing blocks.\r
368 \r
369         The task executes at the idle priority so does not require a delay.\r
370 \r
371         pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
372         vErrorChecks() task that this task is still executing without error. */\r
373 \r
374         pulMemCheckTaskRunningCounter = ( unsigned long * ) pvParameters;\r
375 \r
376         for( ;; )\r
377         {\r
378                 if( lErrorOccurred == pdFALSE )\r
379                 {\r
380                         /* We have never seen an error so increment the counter. */\r
381                         ( *pulMemCheckTaskRunningCounter )++;\r
382                 }\r
383                 else\r
384                 {\r
385                         /* There has been an error so reset the counter so the check task\r
386                         can tell that an error occurred. */\r
387                         *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;\r
388                 }\r
389 \r
390                 /* Allocate some memory - just to give the allocator some extra\r
391                 exercise.  This has to be in a critical section to ensure the\r
392                 task does not get deleted while it has memory allocated. */\r
393 \r
394                 vTaskSuspendAll();\r
395                 {\r
396                         pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
397 \r
398                         if( pvMem1 == NULL )\r
399                         {\r
400                                 lErrorOccurred = pdTRUE;\r
401                         }\r
402                         else\r
403                         {\r
404                                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
405                                 vPortFree( pvMem1 );\r
406                         }\r
407                 }\r
408                 xTaskResumeAll();\r
409 \r
410                 /* Again - with a different size block. */\r
411                 vTaskSuspendAll();\r
412                 {\r
413                         pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
414 \r
415                         if( pvMem2 == NULL )\r
416                         {\r
417                                 lErrorOccurred = pdTRUE;\r
418                         }\r
419                         else\r
420                         {\r
421                                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
422                                 vPortFree( pvMem2 );\r
423                         }\r
424                 }\r
425                 xTaskResumeAll();\r
426 \r
427                 /* Again - with a different size block. */\r
428                 vTaskSuspendAll();\r
429                 {\r
430                         pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
431                         if( pvMem3 == NULL )\r
432                         {\r
433                                 lErrorOccurred = pdTRUE;\r
434                         }\r
435                         else\r
436                         {\r
437                                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
438                                 vPortFree( pvMem3 );\r
439                         }\r
440                 }\r
441                 xTaskResumeAll();\r
442         }\r
443 }\r
444 /*-----------------------------------------------------------*/\r
445 \r
446 static void prvIndicateError( void )\r
447 {\r
448         /* The check task has found an error in one of the other tasks.\r
449         Set the LEDs to a state that indicates this. */\r
450         vParTestSetLED(mainERROR_LED,pdTRUE);\r
451 \r
452         for(;;)\r
453         {\r
454                 #if( BOARD==EVK1100 )\r
455                         vParTestToggleLED( mainCHECK_TASK_LED );\r
456                         vTaskDelay( mainERROR_FLASH_RATE );\r
457                 #endif\r
458                 #if ( BOARD==EVK1101 )\r
459                         vParTestSetLED( 0, pdTRUE );\r
460                         vParTestSetLED( 1, pdTRUE );\r
461                         vParTestSetLED( 2, pdTRUE );\r
462                         vParTestSetLED( 3, pdTRUE );\r
463                 #endif\r
464         }\r
465 }\r
466 \r