]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0_LPC1114_LPCXpresso/RTOSDemo/Source/main-full.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / CORTEX_M0_LPC1114_LPCXpresso / RTOSDemo / Source / main-full.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /******************************************************************************\r
30  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
31  * project, and a more comprehensive test and demo application.  The\r
32  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
33  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
34  * in main.c.  This file implements the comprehensive test and demo version.\r
35  *\r
36  * NOTE 2:  This file only contains the source code that is specific to the\r
37  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
38  * required to configure the hardware, are defined in main.c.\r
39  ******************************************************************************\r
40  *\r
41  * main_full() creates a set of standard demo tasks (including a set of tasks\r
42  * that test the interrupt nesting behaviour), some application specific tasks,\r
43  * and a timer.  It then starts the scheduler.  The web documentation provides\r
44  * more details of the standard demo application tasks, which provide no\r
45  * particular functionality, but do provide a good example of how to use the\r
46  * FreeRTOS API.\r
47  *\r
48  * The interrupt nesting test tasks require that two timers are configured to\r
49  * generate interrupts.  The interrupt service routines are defined in\r
50  * IntQueueTimer.c, and can be used as examples for application writers.  They\r
51  * do not, however, directly demonstrate the use of FreeRTOS safe API functions\r
52  * (those that end in "FromISR").  Therefore, a dummy interrupt implementation\r
53  * called Dummy_IRQHandler() is provided at the end of main.c.\r
54  *\r
55  * In addition to the standard demo tasks, the following tasks and timer are\r
56  * defined and/or created within this file:\r
57  *\r
58  * "Reg test" tasks - These fill the registers with known values, then check\r
59  * that each register maintains its expected value for the lifetime of the\r
60  * task.  Each task uses a different set of values.  The reg test tasks execute\r
61  * with a very low priority, so get preempted very frequently.  A register\r
62  * containing an unexpected value is indicative of an error in the context\r
63  * switching mechanism.\r
64  *\r
65  * "Check" software timer - The check timer period is initially set to three\r
66  * seconds.  Its callback function checks that all the standard demo tasks, and\r
67  * the register check tasks, are not only still executing, but are executing\r
68  * without reporting any errors.  If the check timer callback discovers that a\r
69  * task has either stalled, or reported an error, then it changes the period of\r
70  * the check timer from the initial three seconds, to just 200ms.  The callback\r
71  * function also toggles the LED each time it is called.  This provides a visual\r
72  * indication of the system status:  If the LED toggles every three seconds,\r
73  * then no issues have been discovered.  If the LED toggles every 200ms, then\r
74  * an issue has been discovered with at least one task.\r
75  */\r
76 \r
77 /* Kernel includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "queue.h"\r
81 #include "timers.h"\r
82 \r
83 /* Common demo includes. */\r
84 #include "blocktim.h"\r
85 #include "countsem.h"\r
86 #include "recmutex.h"\r
87 #include "IntQueue.h"\r
88 \r
89 /* Hardware specific includes. */\r
90 #include "lpc11xx.h"\r
91 \r
92 /* The period after which the check timer will expire provided no errors have\r
93 been reported by any of the standard demo tasks.  ms are converted to the\r
94 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
95 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
96 \r
97 /* The period at which the check timer will expire if an error has been\r
98 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
99 in ticks using the portTICK_PERIOD_MS constant. */\r
100 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
101 \r
102 /* A block time of zero simply means "don't block". */\r
103 #define mainDONT_BLOCK                                          ( 0UL )\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /*\r
108  * Register check tasks, as described at the top of this file.  The nature of\r
109  * these files necessitates that they are written in an assembly.\r
110  */\r
111 extern void vRegTest1Task( void *pvParameters );\r
112 extern void vRegTest2Task( void *pvParameters );\r
113 \r
114 /*\r
115  * The hardware only has a single LED.  Simply toggle it.\r
116  */\r
117 extern void vMainToggleLED( void );\r
118 \r
119 /*\r
120  * The check timer callback function, as described at the top of this file.\r
121  */\r
122 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
123 \r
124 /*\r
125  * Called by main() to create the comprehensive test/demo application if\r
126  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is not set to 1.\r
127  */\r
128 void main_full( void );\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /* The following two variables are used to communicate the status of the\r
133 register check tasks to the check software timer.  If the variables keep\r
134 incrementing, then the register check tasks have not discovered any errors.  If\r
135 a variable stops incrementing, then an error has been found. */\r
136 volatile unsigned long ulRegTest1LoopCounter = 0UL, ulRegTest2LoopCounter = 0UL;\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 void main_full( void )\r
141 {\r
142 TimerHandle_t xCheckTimer = NULL;\r
143 /* The register test tasks are asm functions that don't use a stack.  The\r
144 stack allocated just has to be large enough to hold the task context, and\r
145 for the additional required for the stack overflow checking to work (if\r
146 configured). */\r
147 const size_t xRegTestStackSize = 25U;\r
148 \r
149         /* Create the standard demo tasks, including the interrupt nesting test\r
150         tasks. */\r
151         vStartInterruptQueueTasks();\r
152         vCreateBlockTimeTasks();\r
153         vStartCountingSemaphoreTasks();\r
154         vStartRecursiveMutexTasks();\r
155 \r
156         /* Create the register test tasks as described at the top of this file.\r
157         These are naked functions that don't use any stack.  A stack still has\r
158         to be allocated to hold the task context. */\r
159         xTaskCreate(    vRegTest1Task,                  /* Function that implements the task. */\r
160                                         "Reg1",                                 /* Text name of the task. */\r
161                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
162                                         NULL,                                   /* The task parameter is not used. */\r
163                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
164                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
165 \r
166         xTaskCreate(    vRegTest2Task,                  /* Function that implements the task. */\r
167                                         "Reg2",                                 /* Text name of the task. */\r
168                                         xRegTestStackSize,              /* Stack allocated to the task. */\r
169                                         NULL,                                   /* The task parameter is not used. */\r
170                                         tskIDLE_PRIORITY,               /* The priority to assign to the task. */\r
171                                         NULL );                                 /* Don't receive a handle back, it is not needed. */\r
172 \r
173         /* Create the software timer that performs the 'check' functionality,\r
174         as described at the top of this file. */\r
175         xCheckTimer = xTimerCreate( "CheckTimer",                                       /* A text name, purely to help debugging. */\r
176                                                                 ( mainCHECK_TIMER_PERIOD_MS ),  /* The timer period, in this case 3000ms (3s). */\r
177                                                                 pdTRUE,                                                 /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
178                                                                 ( void * ) 0,                                   /* The ID is not used, so can be set to anything. */\r
179                                                                 prvCheckTimerCallback                   /* The callback function that inspects the status of all the other tasks. */\r
180                                                           );\r
181 \r
182         /* If the software timer was created successfully, start it.  It won't\r
183         actually start running until the scheduler starts.  A block time of\r
184         zero is used in this call, although any value could be used as the block\r
185         time will be ignored because the scheduler has not started yet. */\r
186         if( xCheckTimer != NULL )\r
187         {\r
188                 xTimerStart( xCheckTimer, mainDONT_BLOCK );\r
189         }\r
190 \r
191         /* Start the kernel.  From here on, only tasks and interrupts will run. */\r
192         vTaskStartScheduler();\r
193 \r
194         /* If all is well, the scheduler will now be running, and the following\r
195         line will never be reached.  If the following line does execute, then there\r
196         was     insufficient FreeRTOS heap memory available for the idle and/or timer\r
197         tasks to be created.  See the memory management section on the FreeRTOS web\r
198         site, or the FreeRTOS tutorial books for more details. */\r
199         for( ;; );\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /* See the description at the top of this file. */\r
204 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
205 {\r
206 static long lChangedTimerPeriodAlready = pdFALSE;\r
207 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
208 unsigned long ulErrorFound = pdFALSE;\r
209 \r
210         /* Check all the demo and test tasks to ensure that they are all still\r
211         running, and that none have detected an error. */\r
212         if( xAreIntQueueTasksStillRunning() != pdPASS )\r
213         {\r
214                 ulErrorFound |= ( 0x01UL << 0UL );\r
215         }\r
216 \r
217         if( xAreBlockTimeTestTasksStillRunning() != pdPASS )\r
218         {\r
219                 ulErrorFound |= ( 0x01UL << 1UL );\r
220         }\r
221 \r
222         if( xAreCountingSemaphoreTasksStillRunning() != pdPASS )\r
223         {\r
224                 ulErrorFound |= ( 0x01UL << 2UL );\r
225         }\r
226 \r
227         if( xAreRecursiveMutexTasksStillRunning() != pdPASS )\r
228         {\r
229                 ulErrorFound |= ( 0x01UL << 3UL );\r
230         }\r
231 \r
232         /* Check that the register test 1 task is still running. */\r
233         if( ulLastRegTest1Value == ulRegTest1LoopCounter )\r
234         {\r
235                 ulErrorFound |= ( 0x01UL << 4UL );\r
236         }\r
237         ulLastRegTest1Value = ulRegTest1LoopCounter;\r
238 \r
239         /* Check that the register test 2 task is still running. */\r
240         if( ulLastRegTest2Value == ulRegTest2LoopCounter )\r
241         {\r
242                 ulErrorFound |= ( 0x01UL << 5UL );\r
243         }\r
244         ulLastRegTest2Value = ulRegTest2LoopCounter;\r
245 \r
246         /* Toggle the check LED to give an indication of the system status.  If\r
247         the LED toggles every mainCHECK_TIMER_PERIOD_MS milliseconds then\r
248         everything is ok.  A faster toggle indicates an error. */\r
249         vMainToggleLED();\r
250 \r
251         /* Have any errors been latched in ulErrorFound?  If so, shorten the\r
252         period of the check timer to mainERROR_CHECK_TIMER_PERIOD_MS milliseconds.\r
253         This will result in an increase in the rate at which mainCHECK_LED\r
254         toggles. */\r
255         if( ulErrorFound != pdFALSE )\r
256         {\r
257                 if( lChangedTimerPeriodAlready == pdFALSE )\r
258                 {\r
259                         lChangedTimerPeriodAlready = pdTRUE;\r
260 \r
261                         /* This call to xTimerChangePeriod() uses a zero block time.\r
262                         Functions called from inside of a timer callback function must\r
263                         *never* attempt to block. */\r
264                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
265                 }\r
266         }\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r