]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0_STM32F0518_IAR/main-full.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_M0_STM32F0518_IAR / main-full.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 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 simple blinky style\r
30  * project, and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
32  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
33  * in main.c.  This file implements the comprehensive test and demo version.\r
34  *\r
35  * NOTE 2:  This file only contains the source code that is specific to the\r
36  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
37  * required to configure the hardware, are defined in main.c.\r
38  ******************************************************************************\r
39  *\r
40  * main_full() creates a set of standard demo tasks, some application specific\r
41  * tasks, and four timers.  It then starts the scheduler.  The web documentation\r
42  * provides more details of the standard demo application tasks, which provide\r
43  * no particular functionality, but do provide a good example of how to use the\r
44  * FreeRTOS API.\r
45  *\r
46  * In addition to the standard demo tasks, the following tasks and timer are\r
47  * defined and/or created within this file:\r
48  *\r
49  * "Reg test" tasks - These fill the registers with known values, then check\r
50  * that each register maintains its expected value for the lifetime of the\r
51  * task.  Each task uses a different set of values.  The reg test tasks execute\r
52  * with a very low priority, so get preempted very frequently.  A register\r
53  * containing an unexpected value is indicative of an error in the context\r
54  * switching mechanism.\r
55  *\r
56  * "Flash timers" - A software timer callback function is defined that does\r
57  * nothing but toggle an LED.  Three software timers are created that each\r
58  * use the same callback function, but each toggles a different LED at a\r
59  * different frequency.  One software timer uses LED1, another LED2 and the\r
60  * third LED3.\r
61  *\r
62  * "Check" software timer - The check timer period is initially set to three\r
63  * seconds.  Its callback function checks that all the standard demo tasks, and\r
64  * the register check tasks, are not only still executing, but are executing\r
65  * without reporting any errors.  If the check timer callback discovers that a\r
66  * task has either stalled, or reported an error, then it changes the period of\r
67  * the check timer from the initial three seconds, to just 200ms.  The callback\r
68  * function also toggles LED 4 each time it is called.  This provides a visual\r
69  * indication of the system status:  If the LED toggles every three seconds,\r
70  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
71  * an issue has been discovered with at least one task.\r
72  */\r
73 \r
74 /* Kernel includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "queue.h"\r
78 #include "timers.h"\r
79 \r
80 /* Common demo includes. */\r
81 #include "blocktim.h"\r
82 #include "countsem.h"\r
83 #include "recmutex.h"\r
84 #include "ParTest.h"\r
85 #include "dynamic.h"\r
86 \r
87 /* Hardware includes. */\r
88 #include "stm320518_eval.h"\r
89 \r
90 /* The period after which the check timer will expire provided no errors have\r
91 been reported by any of the standard demo tasks.  ms are converted to the\r
92 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
93 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
94 \r
95 /* The period at which the check timer will expire if an error has been\r
96 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
97 in ticks using the portTICK_PERIOD_MS constant. */\r
98 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
99 \r
100 /* A block time of zero simply means "don't block". */\r
101 #define mainDONT_BLOCK                                          ( 0UL )\r
102 \r
103 /* The base toggle rate used by the flash timers.  Each toggle rate is a\r
104 multiple of this. */\r
105 #define mainFLASH_TIMER_BASE_RATE                       ( 200UL / portTICK_PERIOD_MS )\r
106 \r
107 /* The LED toggle by the check timer. */\r
108 #define mainCHECK_LED                                           ( 3 )\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /*\r
112  * Register check tasks, as described at the top of this file.  The nature of\r
113  * these files necessitates that they are written in an assembly.\r
114  */\r
115 extern void vRegTest1Task( void *pvParameters );\r
116 extern void vRegTest2Task( void *pvParameters );\r
117 \r
118 /*\r
119  * The hardware only has a single LED.  Simply toggle it.\r
120  */\r
121 extern void vMainToggleLED( void );\r
122 \r
123 /*\r
124  * The check timer callback function, as described at the top of this file.\r
125  */\r
126 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
127 \r
128 /*\r
129  * The flash timer callback function, as described at the top of this file.\r
130  * This callback function is assigned to three separate software timers.\r
131  */\r
132 static void prvFlashTimerCallback( TimerHandle_t xTimer );\r
133 \r
134 /*\r
135  * Called by main() to create the comprehensive test/demo application if\r
136  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
137  */\r
138 void main_full( void );\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /* The following two variables are used to communicate the status of the\r
143 register check tasks to the check software timer.  If the variables keep\r
144 incrementing, then the register check tasks have not discovered any errors.  If\r
145 a variable stops incrementing, then an error has been found. */\r
146 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 void main_full( void )\r
151 {\r
152 TimerHandle_t xTimer = NULL;\r
153 unsigned long ulTimer;\r
154 const unsigned long ulTimersToCreate = 3L;\r
155 /* The register test tasks are asm functions that don't use a stack.  The\r
156 stack allocated just has to be large enough to hold the task context, and\r
157 for the additional required for the stack overflow checking to work (if\r
158 configured). */\r
159 const size_t xRegTestStackSize = 25U;\r
160 \r
161         /* Create the standard demo tasks */\r
162         vCreateBlockTimeTasks();\r
163         vStartCountingSemaphoreTasks();\r
164         vStartRecursiveMutexTasks();\r
165         vStartDynamicPriorityTasks();\r
166 \r
167         /* Create the register test tasks as described at the top of this file.\r
168         These are naked functions that don't use any stack.  A stack still has\r
169         to be allocated to hold the task context. */\r
170         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
171                                         "Reg1",                                 /* Text name of the task. */\r
172                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
173                                         NULL,                                   /* The task parameter is not used. */\r
174                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
175                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
176 \r
177         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
178                                         "Reg2",                                 /* Text name of the task. */\r
179                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
180                                         NULL,                                   /* The task parameter is not used. */\r
181                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
182                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
183 \r
184         /* Create the three flash timers. */\r
185         for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )\r
186         {\r
187                 xTimer = xTimerCreate(  "FlashTimer",                           /* A text name, purely to help debugging. */\r
188                                                                 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ),      /* The timer period, in this case 3000ms (3s). */\r
189                                                                 pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
190                                                                 ( void * ) ulTimer,                     /* The ID is used to hold the number of the LED that will be flashed. */\r
191                                                                 prvFlashTimerCallback           /* The callback function that inspects the status of all the other tasks. */\r
192                                                         );\r
193 \r
194                 if( xTimer != NULL )\r
195                 {\r
196                         xTimerStart( xTimer, mainDONT_BLOCK );\r
197                 }\r
198         }\r
199 \r
200         /* Create the software timer that performs the 'check' functionality,\r
201         as described at the top of this file. */\r
202         xTimer = xTimerCreate(  "CheckTimer",                                   /* A text name, purely to help debugging. */\r
203                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
204                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
205                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
206                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
207                                                 );\r
208 \r
209         /* If the software timer was created successfully, start it.  It won't\r
210         actually start running until the scheduler starts.  A block time of\r
211         zero is used in this call, although any value could be used as the block\r
212         time will be ignored because the scheduler has not started yet. */\r
213         if( xTimer != NULL )\r
214         {\r
215                 xTimerStart( xTimer, mainDONT_BLOCK );\r
216         }\r
217 \r
218         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
219         vTaskStartScheduler();\r
220 \r
221         /* If all is well, the scheduler will now be running, and the following\r
222         line will never be reached.  If the following line does execute, then there\r
223         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
224         tasks to be created.  See the memory management section on the FreeRTOS web\r
225         site, or the FreeRTOS tutorial books for more details. */\r
226         for( ;; );\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 /* See the description at the top of this file. */\r
231 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
232 {\r
233 static long lChangedTimerPeriodAlready = pdFALSE;\r
234 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
235 unsigned long ulErrorFound = pdFALSE;\r
236 \r
237         /* Check all the demo and test tasks to ensure that they are all still\r
238         running, and that none have detected an error. */\r
239         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
240         {\r
241                 ulErrorFound |= ( 0x01UL << 0UL );\r
242         }\r
243 \r
244         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
245         {\r
246                 ulErrorFound |= ( 0x01UL << 1UL );\r
247         }\r
248 \r
249         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
250         {\r
251                 ulErrorFound |= ( 0x01UL << 2UL );\r
252         }\r
253 \r
254         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
255         {\r
256                 ulErrorFound |= ( 0x01UL << 3UL );\r
257         }\r
258 \r
259         /* Check that the register test 1 task is still running. */\r
260         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
261         {\r
262                 ulErrorFound |= ( 0x01UL << 4UL );\r
263         }\r
264         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
265 \r
266         /* Check that the register test 2 task is still running. */\r
267         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
268         {\r
269                 ulErrorFound |= ( 0x01UL << 5UL );\r
270         }\r
271         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
272 \r
273         /* Toggle the check LED to give an indication of the system status.  If\r
274         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
275         everything is ok.  A faster toggle indicates an error. */\r
276         vParTestToggleLED( mainCHECK_LED );\r
277 \r
278         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
279         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
280         This will result in an increase in the rate at which mainCHECK_LED\r
281         toggles. */\r
282         if( ulErrorFound != pdFALSE )\r
283         {\r
284                 if( lChangedTimerPeriodAlready == pdFALSE )\r
285                 {\r
286                         lChangedTimerPeriodAlready = pdTRUE;\r
287 \r
288                         /* This call to xTimerChangePeriod() uses a zero block time.\r
289                         Functions called from inside of a timer callback function must\r
290                         *never* attempt to block. */\r
291                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
292                 }\r
293         }\r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 static void prvFlashTimerCallback( TimerHandle_t xTimer )\r
298 {\r
299 unsigned long ulLED;\r
300 \r
301         /* This callback function is assigned to three separate software timers.\r
302         Each timer toggles a different LED.  Obtain the number of the LED that\r
303         this timer is toggling. */\r
304         ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );\r
305 \r
306         /* Toggle the LED. */\r
307         vParTestToggleLED( ulLED );\r
308 }\r
309 \r