]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0_STM32F0518_IAR/main-full.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / CORTEX_M0_STM32F0518_IAR / main-full.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
72  * project, and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
74  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
75  * in main.c.  This file implements the comprehensive test and demo version.\r
76  *\r
77  * NOTE 2:  This file only contains the source code that is specific to the\r
78  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
79  * required to configure the hardware, are defined in main.c.\r
80  ******************************************************************************\r
81  *\r
82  * main_full() creates a set of standard demo tasks, some application specific\r
83  * tasks, and four timers.  It then starts the scheduler.  The web documentation\r
84  * provides more details of the standard demo application tasks, which provide\r
85  * no particular functionality, but do provide a good example of how to use the\r
86  * FreeRTOS API.\r
87  *\r
88  * In addition to the standard demo tasks, the following tasks and timer are\r
89  * defined and/or created within this file:\r
90  *\r
91  * "Reg test" tasks - These fill the registers with known values, then check\r
92  * that each register maintains its expected value for the lifetime of the\r
93  * task.  Each task uses a different set of values.  The reg test tasks execute\r
94  * with a very low priority, so get preempted very frequently.  A register\r
95  * containing an unexpected value is indicative of an error in the context\r
96  * switching mechanism.\r
97  *\r
98  * "Flash timers" - A software timer callback function is defined that does\r
99  * nothing but toggle an LED.  Three software timers are created that each\r
100  * use the same callback function, but each toggles a different LED at a\r
101  * different frequency.  One software timer uses LED1, another LED2 and the\r
102  * third LED3.\r
103  *\r
104  * "Check" software timer - The check timer period is initially set to three\r
105  * seconds.  Its callback function checks that all the standard demo tasks, and\r
106  * the register check tasks, are not only still executing, but are executing\r
107  * without reporting any errors.  If the check timer callback discovers that a\r
108  * task has either stalled, or reported an error, then it changes the period of\r
109  * the check timer from the initial three seconds, to just 200ms.  The callback\r
110  * function also toggles LED 4 each time it is called.  This provides a visual\r
111  * indication of the system status:  If the LED toggles every three seconds,\r
112  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
113  * an issue has been discovered with at least one task.\r
114  */\r
115 \r
116 /* Kernel includes. */\r
117 #include "FreeRTOS.h"\r
118 #include "task.h"\r
119 #include "queue.h"\r
120 #include "timers.h"\r
121 \r
122 /* Common demo includes. */\r
123 #include "blocktim.h"\r
124 #include "countsem.h"\r
125 #include "recmutex.h"\r
126 #include "ParTest.h"\r
127 #include "dynamic.h"\r
128 \r
129 /* Hardware includes. */\r
130 #include "stm320518_eval.h"\r
131 \r
132 /* The period after which the check timer will expire provided no errors have\r
133 been reported by any of the standard demo tasks.  ms are converted to the\r
134 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
135 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
136 \r
137 /* The period at which the check timer will expire if an error has been\r
138 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
139 in ticks using the portTICK_PERIOD_MS constant. */\r
140 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
141 \r
142 /* A block time of zero simply means "don't block". */\r
143 #define mainDONT_BLOCK                                          ( 0UL )\r
144 \r
145 /* The base toggle rate used by the flash timers.  Each toggle rate is a\r
146 multiple of this. */\r
147 #define mainFLASH_TIMER_BASE_RATE                       ( 200UL / portTICK_PERIOD_MS )\r
148 \r
149 /* The LED toggle by the check timer. */\r
150 #define mainCHECK_LED                                           ( 3 )\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 /*\r
154  * Register check tasks, as described at the top of this file.  The nature of\r
155  * these files necessitates that they are written in an assembly.\r
156  */\r
157 extern void vRegTest1Task( void *pvParameters );\r
158 extern void vRegTest2Task( void *pvParameters );\r
159 \r
160 /*\r
161  * The hardware only has a single LED.  Simply toggle it.\r
162  */\r
163 extern void vMainToggleLED( void );\r
164 \r
165 /*\r
166  * The check timer callback function, as described at the top of this file.\r
167  */\r
168 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
169 \r
170 /*\r
171  * The flash timer callback function, as described at the top of this file.\r
172  * This callback function is assigned to three separate software timers.\r
173  */\r
174 static void prvFlashTimerCallback( TimerHandle_t xTimer );\r
175 \r
176 /*\r
177  * Called by main() to create the comprehensive test/demo application if\r
178  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
179  */\r
180 void main_full( void );\r
181 \r
182 /*-----------------------------------------------------------*/\r
183 \r
184 /* The following two variables are used to communicate the status of the\r
185 register check tasks to the check software timer.  If the variables keep\r
186 incrementing, then the register check tasks have not discovered any errors.  If\r
187 a variable stops incrementing, then an error has been found. */\r
188 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
189 \r
190 /*-----------------------------------------------------------*/\r
191 \r
192 void main_full( void )\r
193 {\r
194 TimerHandle_t xTimer = NULL;\r
195 unsigned long ulTimer;\r
196 const unsigned long ulTimersToCreate = 3L;\r
197 /* The register test tasks are asm functions that don't use a stack.  The\r
198 stack allocated just has to be large enough to hold the task context, and\r
199 for the additional required for the stack overflow checking to work (if\r
200 configured). */\r
201 const size_t xRegTestStackSize = 25U;\r
202 \r
203         /* Create the standard demo tasks */\r
204         vCreateBlockTimeTasks();\r
205         vStartCountingSemaphoreTasks();\r
206         vStartRecursiveMutexTasks();\r
207         vStartDynamicPriorityTasks();\r
208 \r
209         /* Create the register test tasks as described at the top of this file.\r
210         These are naked functions that don't use any stack.  A stack still has\r
211         to be allocated to hold the task context. */\r
212         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
213                                         "Reg1",                                 /* Text name of the task. */\r
214                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
215                                         NULL,                                   /* The task parameter is not used. */\r
216                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
217                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
218 \r
219         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
220                                         "Reg2",                                 /* Text name of the task. */\r
221                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
222                                         NULL,                                   /* The task parameter is not used. */\r
223                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
224                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
225 \r
226         /* Create the three flash timers. */\r
227         for( ulTimer = 0UL; ulTimer < ulTimersToCreate; ulTimer++ )\r
228         {\r
229                 xTimer = xTimerCreate(  "FlashTimer",                           /* A text name, purely to help debugging. */\r
230                                                                 ( mainFLASH_TIMER_BASE_RATE * ( ulTimer + 1UL ) ),      /* The timer period, in this case 3000ms (3s). */\r
231                                                                 pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
232                                                                 ( void * ) ulTimer,                     /* The ID is used to hold the number of the LED that will be flashed. */\r
233                                                                 prvFlashTimerCallback           /* The callback function that inspects the status of all the other tasks. */\r
234                                                         );\r
235 \r
236                 if( xTimer != NULL )\r
237                 {\r
238                         xTimerStart( xTimer, mainDONT_BLOCK );\r
239                 }\r
240         }\r
241 \r
242         /* Create the software timer that performs the 'check' functionality,\r
243         as described at the top of this file. */\r
244         xTimer = xTimerCreate(  "CheckTimer",                                   /* A text name, purely to help debugging. */\r
245                                                         ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
246                                                         pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
247                                                         ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
248                                                         prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
249                                                 );\r
250 \r
251         /* If the software timer was created successfully, start it.  It won't\r
252         actually start running until the scheduler starts.  A block time of\r
253         zero is used in this call, although any value could be used as the block\r
254         time will be ignored because the scheduler has not started yet. */\r
255         if( xTimer != NULL )\r
256         {\r
257                 xTimerStart( xTimer, mainDONT_BLOCK );\r
258         }\r
259 \r
260         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
261         vTaskStartScheduler();\r
262 \r
263         /* If all is well, the scheduler will now be running, and the following\r
264         line will never be reached.  If the following line does execute, then there\r
265         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
266         tasks to be created.  See the memory management section on the FreeRTOS web\r
267         site, or the FreeRTOS tutorial books for more details. */\r
268         for( ;; );\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 /* See the description at the top of this file. */\r
273 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
274 {\r
275 static long lChangedTimerPeriodAlready = pdFALSE;\r
276 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
277 unsigned long ulErrorFound = pdFALSE;\r
278 \r
279         /* Check all the demo and test tasks to ensure that they are all still\r
280         running, and that none have detected an error. */\r
281         if( xAreDynamicPriorityTasksStillRunning() != pdPASS )\r
282         {\r
283                 ulErrorFound |= ( 0x01UL << 0UL );\r
284         }\r
285 \r
286         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
287         {\r
288                 ulErrorFound |= ( 0x01UL << 1UL );\r
289         }\r
290 \r
291         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
292         {\r
293                 ulErrorFound |= ( 0x01UL << 2UL );\r
294         }\r
295 \r
296         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
297         {\r
298                 ulErrorFound |= ( 0x01UL << 3UL );\r
299         }\r
300 \r
301         /* Check that the register test 1 task is still running. */\r
302         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
303         {\r
304                 ulErrorFound |= ( 0x01UL << 4UL );\r
305         }\r
306         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
307 \r
308         /* Check that the register test 2 task is still running. */\r
309         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
310         {\r
311                 ulErrorFound |= ( 0x01UL << 5UL );\r
312         }\r
313         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
314 \r
315         /* Toggle the check LED to give an indication of the system status.  If\r
316         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
317         everything is ok.  A faster toggle indicates an error. */\r
318         vParTestToggleLED( mainCHECK_LED );\r
319 \r
320         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
321         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
322         This will result in an increase in the rate at which mainCHECK_LED\r
323         toggles. */\r
324         if( ulErrorFound != pdFALSE )\r
325         {\r
326                 if( lChangedTimerPeriodAlready == pdFALSE )\r
327                 {\r
328                         lChangedTimerPeriodAlready = pdTRUE;\r
329 \r
330                         /* This call to xTimerChangePeriod() uses a zero block time.\r
331                         Functions called from inside of a timer callback function must\r
332                         *never* attempt to block. */\r
333                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
334                 }\r
335         }\r
336 }\r
337 /*-----------------------------------------------------------*/\r
338 \r
339 static void prvFlashTimerCallback( TimerHandle_t xTimer )\r
340 {\r
341 unsigned long ulLED;\r
342 \r
343         /* This callback function is assigned to three separate software timers.\r
344         Each timer toggles a different LED.  Obtain the number of the LED that\r
345         this timer is toggling. */\r
346         ulLED = ( unsigned long ) pvTimerGetTimerID( xTimer );\r
347 \r
348         /* Toggle the LED. */\r
349         vParTestToggleLED( ulLED );\r
350 }\r
351 \r