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