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