]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MZ_MPLAB/main_full.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / PIC32MZ_MPLAB / 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 all the demo application tasks and software timers, then\r
83  * starts the scheduler.  The WEB documentation provides more details of the\r
84  * standard demo application tasks.  In addition to the standard demo tasks, the\r
85  * following tasks and tests are also defined:\r
86  *\r
87  * "Register test" tasks - These tasks are used in part to test the kernel port.\r
88  * They set each processor register to a known value, then check that the\r
89  * register still contains that value.  Each of the tasks sets the registers\r
90  * to different values, and will get swapping in and out between setting and\r
91  * then subsequently checking the register values.  Discovery of an incorrect\r
92  * value would be indicative of an error in the task switching mechanism.\r
93  *\r
94  * "ISR triggered task" - This is provided as an example of how to write a\r
95  * FreeRTOS compatible interrupt service routine.  See the comments in\r
96  * ISRTriggeredTask.c.\r
97  *\r
98  * "High Frequency Timer Test" - The high frequency timer is created to test\r
99  * the interrupt nesting method.  The standard demo interrupt nesting test tasks\r
100  * are created with priorities at or below configMAX_SYSCALL_INTERRUPT_PRIORITY\r
101  * because they use interrupt safe FreeRTOS API functions.  The high frequency\r
102  * time is created with a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY,\r
103  * so cannot us the same API functions.\r
104 \r
105  * By way of demonstration, the demo application defines\r
106  * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY\r
107  * to be 1, and all other interrupts as follows:\r
108  *\r
109  * See the online documentation for this demo for more information on interrupt\r
110  * usage.\r
111  *\r
112  * "Check" timer - The check software timer period is initially set to three\r
113  * seconds.  The callback function associated with the check software timer\r
114  * checks that all the standard demo tasks, and the register check tasks, are\r
115  * not only still executing, but are executing without reporting any errors.  If\r
116  * the check software timer discovers that a task has either stalled, or\r
117  * reported an error, then it changes its own execution period from the initial\r
118  * three seconds, to just 200ms.  The check software timer also toggle LED\r
119  * mainCHECK_LED;  If mainCHECK_LED toggles every 3 seconds, no errors have\r
120  * been detected.  If mainCHECK_LED toggles every 200ms then an error has been\r
121  * detected in at least one task.\r
122  *\r
123  */\r
124 \r
125 /* Scheduler includes. */\r
126 #include "FreeRTOS.h"\r
127 #include "task.h"\r
128 #include "queue.h"\r
129 #include "semphr.h"\r
130 #include "timers.h"\r
131 \r
132 /* Demo application includes. */\r
133 #include "partest.h"\r
134 #include "blocktim.h"\r
135 #include "flash_timer.h"\r
136 #include "semtest.h"\r
137 #include "GenQTest.h"\r
138 #include "QPeek.h"\r
139 #include "IntQueue.h"\r
140 #include "countsem.h"\r
141 #include "dynamic.h"\r
142 #include "QueueOverwrite.h"\r
143 #include "QueueSet.h"\r
144 #include "recmutex.h"\r
145 #include "EventGroupsDemo.h"\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 /* The period after which the check timer will expire, in ms, provided no errors\r
150 have been reported by any of the standard demo tasks.  ms are converted to the\r
151 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
152 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
153 \r
154 /* The period at which the check timer will expire, in ms, if an error has been\r
155 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
156 in ticks using the portTICK_PERIOD_MS constant. */\r
157 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
158 \r
159 /* The priorities of the various demo application tasks. */\r
160 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
161 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
162 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
163 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
164 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
165 #define mainQUEUE_OVERWRITE_TASK_PRIORITY       ( tskIDLE_PRIORITY )\r
166 \r
167 /* The LED controlled by the 'check' software timer. */\r
168 #define mainCHECK_LED                                           ( 2 )\r
169 \r
170 /* The number of LEDs that should be controlled by the flash software timer\r
171 standard demo.  In this case it is only 1 as the starter kit has three LEDs, one\r
172 of which is controlled by the check timer and one of which is controlled by the\r
173 ISR triggered task. */\r
174 #define mainNUM_FLASH_TIMER_LEDS                        ( 1 )\r
175 \r
176 /* Misc. */\r
177 #define mainDONT_BLOCK                                          ( 0 )\r
178 \r
179 /* The frequency at which the "high frequency interrupt" interrupt will\r
180 occur. */\r
181 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /*\r
186  * The check timer callback function, as described at the top of this file.\r
187  */\r
188 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
189 \r
190 /*\r
191  * It is important to ensure the high frequency timer test does not start before\r
192  * the kernel.  It is therefore started from inside a software timer callback\r
193  * function, which will not execute until the timer service/daemon task is\r
194  * executing.  A one-shot timer is used, so the callback function will only\r
195  * execute once (unless it is manually reset/restarted).\r
196  */\r
197 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer );\r
198 \r
199 /*\r
200  * Tasks that test the context switch mechanism by filling the processor\r
201  * registers with known values, then checking that the values contained\r
202  * within the registers is as expected.  The tasks are likely to get swapped\r
203  * in and out between setting the register values and checking the register\r
204  * values.\r
205  */\r
206 static void prvRegTestTask1( void *pvParameters );\r
207 static void prvRegTestTask2( void *pvParameters );\r
208 \r
209 /*\r
210  * The task that is periodically triggered by an interrupt, as described at the\r
211  * top of this file.\r
212  */\r
213 extern void vStartISRTriggeredTask( void );\r
214 \r
215 /*-----------------------------------------------------------*/\r
216 \r
217 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively\r
218 on each iteration of their function.  These are used to detect errors in the\r
219 reg test tasks. */\r
220 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
221 \r
222 /*-----------------------------------------------------------*/\r
223 \r
224 /*\r
225  * Create the demo tasks then start the scheduler.\r
226  */\r
227 int main_full( void )\r
228 {\r
229 TimerHandle_t xTimer = NULL;\r
230 \r
231         /* Create all the other standard demo tasks. */\r
232         vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );\r
233         vCreateBlockTimeTasks();\r
234         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
235         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
236         vStartQueuePeekTasks();\r
237         vStartInterruptQueueTasks();\r
238         vStartISRTriggeredTask();\r
239         vStartCountingSemaphoreTasks();\r
240         vStartDynamicPriorityTasks();\r
241         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_TASK_PRIORITY );\r
242         vStartQueueSetTasks();\r
243         vStartRecursiveMutexTasks();\r
244         vStartEventGroupTasks();\r
245 \r
246         /* Create the tasks defined within this file. */\r
247         xTaskCreate( prvRegTestTask1,                   /* The function that implements the task. */\r
248                                 "Reg1",                                         /* Text name for the task to assist debugger - not used by FreeRTOS itself. */\r
249                                 configMINIMAL_STACK_SIZE,       /* The stack size to allocate for the task - specified in words not bytes. */\r
250                                 NULL,                                           /* The parameter to pass into the task - not used in this case so set to NULL. */\r
251                                 tskIDLE_PRIORITY,                       /* The priority to assign to the task. */\r
252                                 NULL );                                         /* Used to obtain a handle to the task being created - not used in this case so set to NULL. */\r
253 \r
254         xTaskCreate( prvRegTestTask2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
255 \r
256         /* Create the software timer that performs the 'check' functionality, as\r
257         described at the top of this file. */\r
258         xTimer = xTimerCreate(  "CheckTimer",/* A text name, purely to help debugging. */\r
259                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
260                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
261                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
262                                                         prvCheckTimerCallback );                        /* The callback function that inspects the status of all the other tasks. */\r
263 \r
264         if( xTimer != NULL )\r
265         {\r
266                 xTimerStart( xTimer, mainDONT_BLOCK );\r
267         }\r
268 \r
269         /* A software timer is also used to start the high frequency timer test.\r
270         This is to ensure the test does not start before the kernel.  This time a\r
271         one shot software timer is used. */\r
272         xTimer = xTimerCreate( "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );\r
273         if( xTimer != NULL )\r
274         {\r
275                 xTimerStart( xTimer, mainDONT_BLOCK );\r
276         }\r
277 \r
278         /* Finally start the scheduler. */\r
279         vTaskStartScheduler();\r
280 \r
281         /* If all is well, the scheduler will now be running, and the following line\r
282         will never be reached.  If the following line does execute, then there was\r
283         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
284         to be created.  See the memory management section on the FreeRTOS web site\r
285         for more details.  http://www.freertos.org/a00111.html */\r
286         for( ;; );\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 static void prvRegTestTask1( void *pvParameters )\r
291 {\r
292 extern void vRegTest1( volatile unsigned long * );\r
293 \r
294         /* Avoid compiler warnings. */\r
295         ( void ) pvParameters;\r
296 \r
297         /* Pass the address of the RegTest1 loop counter into the test function,\r
298         which is necessarily implemented in assembler. */\r
299         vRegTest1( &ulRegTest1Cycles );\r
300 \r
301         /* vRegTest1 should never exit! */\r
302         vTaskDelete( NULL );\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 static void prvRegTestTask2( void *pvParameters )\r
307 {\r
308 extern void vRegTest2( volatile unsigned long * );\r
309 \r
310         /* Avoid compiler warnings. */\r
311         ( void ) pvParameters;\r
312 \r
313         /* Pass the address of the RegTest2 loop counter into the test function,\r
314         which is necessarily implemented in assembler. */\r
315         vRegTest2( &ulRegTest2Cycles );\r
316 \r
317         /* vRegTest1 should never exit! */\r
318         vTaskDelete( NULL );\r
319 }\r
320 /*-----------------------------------------------------------*/\r
321 \r
322 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
323 {\r
324 static long lChangedTimerPeriodAlready = pdFALSE;\r
325 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulLastHighFrequencyTimerInterrupts = 0;\r
326 static const unsigned long ulExpectedHighFrequencyInterrupts = ( ( mainTEST_INTERRUPT_FREQUENCY / 1000UL ) * mainCHECK_TIMER_PERIOD_MS ) - 10; /* 10 allows for a margin of error. */\r
327 unsigned long ulErrorOccurred = pdFALSE;\r
328 \r
329 /* The count of the high frequency timer interrupts. */\r
330 extern unsigned long ulHighFrequencyTimerInterrupts;\r
331 \r
332         /* Avoid compiler warnings. */\r
333         ( void ) xTimer;\r
334 \r
335         /* Check that the register test 1 task is still running. */\r
336         if( ulLastRegTest1Value == ulRegTest1Cycles )\r
337         {\r
338                 ulErrorOccurred |= ( 0x01UL << 1UL );\r
339         }\r
340         ulLastRegTest1Value = ulRegTest1Cycles;\r
341 \r
342 \r
343         /* Check that the register test 2 task is still running. */\r
344         if( ulLastRegTest2Value == ulRegTest2Cycles )\r
345         {\r
346                 ulErrorOccurred |= ( 0x01UL << 2UL );\r
347         }\r
348         ulLastRegTest2Value = ulRegTest2Cycles;\r
349 \r
350         /* Have any of the standard demo tasks detected an error in their\r
351         operation? */\r
352         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
353         {\r
354                 ulErrorOccurred |= ( 0x01UL << 3UL );\r
355         }\r
356         else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
357         {\r
358                 ulErrorOccurred |= ( 0x01UL << 4UL );\r
359         }\r
360         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
361         {\r
362                 ulErrorOccurred |= ( 0x01UL << 5UL );\r
363         }\r
364         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
365         {\r
366                 ulErrorOccurred |= ( 0x01UL << 6UL );\r
367         }\r
368         else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
369         {\r
370                 ulErrorOccurred |= ( 0x01UL << 7UL );\r
371         }\r
372         else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
373         {\r
374                 ulErrorOccurred |= ( 0x01UL << 8UL );\r
375         }\r
376         else if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
377         {\r
378                 ulErrorOccurred |= ( 0x01UL << 9UL );\r
379         }\r
380         else if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )\r
381         {\r
382                 ulErrorOccurred |= ( 0x01UL << 10UL );\r
383         }\r
384         else if( xAreQueueSetTasksStillRunning() != pdTRUE )\r
385         {\r
386                 ulErrorOccurred |= ( 0x01UL << 11UL );\r
387         }\r
388         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
389         {\r
390                 ulErrorOccurred |= ( 0x01UL << 12UL );\r
391         }\r
392         else if( xAreEventGroupTasksStillRunning() != pdTRUE )\r
393         {\r
394                 ulErrorOccurred |= ( 0x01UL << 13UL );\r
395         }\r
396 \r
397         /* Ensure the expected number of high frequency interrupts have occurred. */\r
398         if( ulLastHighFrequencyTimerInterrupts != 0 )\r
399         {\r
400                 if( ( ulHighFrequencyTimerInterrupts - ulLastHighFrequencyTimerInterrupts ) < ulExpectedHighFrequencyInterrupts )\r
401                 {\r
402                         ulErrorOccurred |= ( 0x01UL << 14UL );\r
403                 }\r
404         }\r
405         ulLastHighFrequencyTimerInterrupts = ulHighFrequencyTimerInterrupts;\r
406 \r
407         if( ulErrorOccurred != pdFALSE )\r
408         {\r
409                 /* An error occurred.  Increase the frequency at which the check timer\r
410                 toggles its LED to give visual feedback of the potential error\r
411                 condition. */\r
412                 if( lChangedTimerPeriodAlready == pdFALSE )\r
413                 {\r
414                         lChangedTimerPeriodAlready = pdTRUE;\r
415 \r
416                         /* This call to xTimerChangePeriod() uses a zero block time.\r
417                         Functions called from inside of a timer callback function must\r
418                         *never* attempt to block as to do so could impact other software\r
419                         timers. */\r
420                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
421                 }\r
422         }\r
423 \r
424         vParTestToggleLED( mainCHECK_LED );\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer )\r
429 {\r
430 void vSetupTimerTest( unsigned short usFrequencyHz );\r
431 \r
432         /* Avoid compiler warnings. */\r
433         ( void ) xTimer;\r
434 \r
435         /* Setup the high frequency, high priority, timer test.  It is setup in this\r
436         software timer callback to ensure it does not start before the kernel does.\r
437         This is a one shot timer - so the setup routine will only be executed once. */\r
438         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
439 }\r
440 /*-----------------------------------------------------------*/\r
441 \r