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