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