]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_M4_ATSAM4L_Atmel_Studio/src/main_full.c
Updated pack to FreeRTOS 10.3.1
[cmsis-freertos] / Demo / CORTEX_M4_ATSAM4L_Atmel_Studio / src / main_full.c
1 /*
2  * FreeRTOS Kernel V10.3.1
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /******************************************************************************
29  * NOTE 1:  This project provides two demo applications.  A low power tickless
30  * project, and a more comprehensive test and demo application.  The
31  * configCREATE_LOW_POWER_DEMO setting in FreeRTOSConfig.h is used to
32  * select between the two.  See the notes on using
33  * configCREATE_LOW_POWER_DEMO in FreeRTOSConfig.h.  This file implements
34  * the comprehensive test and demo version.
35  *
36  * NOTE 2:  This file only contains the source code that is specific to the
37  * full demo.  Generic functions, such FreeRTOS hook functions, and functions
38  * required to configure the hardware, are defined in main.c.
39  ******************************************************************************
40  *
41  * main_full() creates all the demo application tasks and a software timer, then
42  * starts the scheduler.  The web documentation provides more details of the
43  * standard demo application tasks, which provide no particular functionality,
44  * but do provide a good example of how to use the FreeRTOS API.
45  *
46  * In addition to the standard demo tasks, the following tasks and tests are
47  * defined and/or created within this file:
48  *
49  * "Check" timer - The check software timer period is initially set to three
50  * seconds.  The callback function associated with the check software timer
51  * checks that all the standard demo tasks are not only still executing, but
52  * are executing without reporting any errors.  If the check software timer
53  * discovers that a task has either stalled, or reported an error, then it
54  * changes its own execution period from the initial three seconds, to just
55  * 200ms.  The check software timer callback function also toggles an LED each
56  * time it is called.  This provides a visual indication of the system status:
57  * If the LED toggles every three seconds, then no issues have been discovered.
58  * If the LED toggles every 200ms, then an issue has been discovered with at
59  * least one task.
60  *
61  * See the documentation page for this demo on the FreeRTOS.org web site for
62  * full information, including hardware setup requirements.
63  */
64
65 /* Standard includes. */
66 #include <stdio.h>
67
68 /* Kernel includes. */
69 #include "FreeRTOS.h"
70 #include "task.h"
71 #include "timers.h"
72 #include "semphr.h"
73
74 /* Standard demo application includes. */
75 #include "PollQ.h"
76 #include "semtest.h"
77 #include "dynamic.h"
78 #include "BlockQ.h"
79 #include "blocktim.h"
80 #include "countsem.h"
81 #include "GenQTest.h"
82 #include "recmutex.h"
83 #include "partest.h"
84
85 /* Atmel library includes. */
86 #include "asf.h"
87
88 /* Priorities for the demo application tasks. */
89 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2UL )
90 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1UL )
91 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2UL )
92
93 /* A block time of zero simply means "don't block". */
94 #define mainDONT_BLOCK                                          ( 0UL )
95
96 /* The period after which the check timer will expire providing no errors
97 have been reported by any of the standard demo tasks.  ms are converted to the
98 equivalent in ticks using the portTICK_PERIOD_MS constant. */
99 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )
100
101 /* The period at which the check timer will expire, in ms, if an error has been
102 reported in one of the standard demo tasks.  ms are converted to the equivalent
103 in ticks using the portTICK_PERIOD_MS constant. */
104 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )
105
106 /* The LED toggled by the check timer. */
107 #define mainCHECK_LED                                           ( 0 )
108
109 /*-----------------------------------------------------------*/
110
111 /*
112  * The check timer callback function, as described at the top of this file.
113  */
114 static void prvCheckTimerCallback( TimerHandle_t xTimer );
115
116 /*-----------------------------------------------------------*/
117
118 void main_full( void )
119 {
120 TimerHandle_t xCheckTimer = NULL;
121
122         /* Start all the other standard demo/test tasks.  They have no particular
123         functionality, but do demonstrate how to use the FreeRTOS API and test the
124         kernel port. */
125         vStartDynamicPriorityTasks();
126         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
127         vCreateBlockTimeTasks();
128         vStartCountingSemaphoreTasks();
129         vStartGenericQueueTasks( tskIDLE_PRIORITY );
130         vStartRecursiveMutexTasks();
131         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
132         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
133
134         /* Create the software timer that performs the 'check' functionality,
135         as described at the top of this file. */
136         xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */
137                                                                 ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */
138                                                                 pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
139                                                                 ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */
140                                                                 prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */
141                                                           );
142
143         if( xCheckTimer != NULL )
144         {
145                 xTimerStart( xCheckTimer, mainDONT_BLOCK );
146         }
147
148         /* Start the scheduler. */
149         vTaskStartScheduler();
150
151         /* If all is well, the scheduler will now be running, and the following line
152         will never be reached.  If the following line does execute, then there was
153         insufficient FreeRTOS heap memory available for the idle and/or timer tasks
154         to be created.  See the memory management section on the FreeRTOS web site
155         for more details. */
156         for( ;; );
157 }
158 /*-----------------------------------------------------------*/
159
160 static void prvCheckTimerCallback( TimerHandle_t xTimer )
161 {
162 static long lChangedTimerPeriodAlready = pdFALSE;
163 unsigned long ulErrorFound = pdFALSE;
164
165         /* Check all the demo tasks (other than the flash tasks) to ensure
166         they are all still running, and that none have detected an error. */
167
168         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
169         {
170                 ulErrorFound = pdTRUE;
171         }
172
173         if( xAreBlockingQueuesStillRunning() != pdTRUE )
174         {
175                 ulErrorFound = pdTRUE;
176         }
177
178         if ( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
179         {
180                 ulErrorFound = pdTRUE;
181         }
182
183         if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
184         {
185                 ulErrorFound = pdTRUE;
186         }
187
188         if ( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
189         {
190                 ulErrorFound = pdTRUE;
191         }
192
193         if( xArePollingQueuesStillRunning() != pdTRUE )
194         {
195                 ulErrorFound = pdTRUE;
196         }
197
198         if( xAreSemaphoreTasksStillRunning() != pdTRUE )
199         {
200                 ulErrorFound = pdTRUE;
201         }
202
203         /* Toggle the check LED to give an indication of the system status.  If
204         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then
205         everything is ok.  A faster toggle indicates an error. */
206         vParTestToggleLED( mainCHECK_LED );
207
208         /* Have any errors been latch in ulErrorFound?  If so, shorten the
209         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.
210         This will result in an increase in the rate at which mainCHECK_LED
211         toggles. */
212         if( ulErrorFound != pdFALSE )
213         {
214                 if( lChangedTimerPeriodAlready == pdFALSE )
215                 {
216                         lChangedTimerPeriodAlready = pdTRUE;
217
218                         /* This call to xTimerChangePeriod() uses a zero block time.
219                         Functions called from inside of a timer callback function must
220                         *never* attempt to block. */
221                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
222                 }
223         }
224 }
225 /*-----------------------------------------------------------*/
226