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