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