]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MZ_MPLAB/main_full.c
Prepare for V9.0.0 release:
[freertos] / FreeRTOS / Demo / PIC32MZ_MPLAB / main_full.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 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 #include "flop_mz.h"\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /* The period after which the check timer will expire, in ms, provided no errors\r
151 have been reported by any of the standard demo tasks.  ms are converted to the\r
152 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
153 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
154 \r
155 /* The period at which the check timer will expire, in ms, if an error has been\r
156 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
157 in ticks using the portTICK_PERIOD_MS constant. */\r
158 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
159 \r
160 /* The priorities of the various demo application tasks. */\r
161 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
162 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
163 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
164 #define mainINTEGER_TASK_PRIORITY                       ( tskIDLE_PRIORITY )\r
165 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
166 #define mainQUEUE_OVERWRITE_TASK_PRIORITY       ( tskIDLE_PRIORITY )\r
167 #define mainFLOP_TASK_PRIORITY                          ( tskIDLE_PRIORITY )\r
168 \r
169 /* The LED controlled by the 'check' software timer. */\r
170 #define mainCHECK_LED                                           ( 2 )\r
171 \r
172 /* The number of LEDs that should be controlled by the flash software timer\r
173 standard demo.  In this case it is only 1 as the starter kit has three LEDs, one\r
174 of which is controlled by the check timer and one of which is controlled by the\r
175 ISR triggered task. */\r
176 #define mainNUM_FLASH_TIMER_LEDS                        ( 1 )\r
177 \r
178 /* Misc. */\r
179 #define mainDONT_BLOCK                                          ( 0 )\r
180 \r
181 /* The frequency at which the "high frequency interrupt" interrupt will\r
182 occur. */\r
183 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
184 \r
185 /*-----------------------------------------------------------*/\r
186 \r
187 /*\r
188  * The check timer callback function, as described at the top of this file.\r
189  */\r
190 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
191 \r
192 /*\r
193  * It is important to ensure the high frequency timer test does not start before\r
194  * the kernel.  It is therefore started from inside a software timer callback\r
195  * function, which will not execute until the timer service/daemon task is\r
196  * executing.  A one-shot timer is used, so the callback function will only\r
197  * execute once (unless it is manually reset/restarted).\r
198  */\r
199 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer );\r
200 \r
201 /*\r
202  * Tasks that test the context switch mechanism by filling the processor\r
203  * registers with known values, then checking that the values contained\r
204  * within the registers is as expected.  The tasks are likely to get swapped\r
205  * in and out between setting the register values and checking the register\r
206  * values.\r
207  */\r
208 static void prvRegTestTask1( void *pvParameters );\r
209 static void prvRegTestTask2( void *pvParameters );\r
210 \r
211 /*\r
212  * The task that is periodically triggered by an interrupt, as described at the\r
213  * top of this file.\r
214  */\r
215 extern void vStartISRTriggeredTask( void );\r
216 \r
217 /*-----------------------------------------------------------*/\r
218 \r
219 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively\r
220 on each iteration of their function.  These are used to detect errors in the\r
221 reg test tasks. */\r
222 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
223 \r
224 /*-----------------------------------------------------------*/\r
225 \r
226 /*\r
227  * Create the demo tasks then start the scheduler.\r
228  */\r
229 int main_full( void )\r
230 {\r
231 TimerHandle_t xTimer = NULL;\r
232 \r
233         /* Create all the other standard demo tasks. */\r
234         vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );\r
235         vCreateBlockTimeTasks();\r
236         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
237         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
238         vStartQueuePeekTasks();\r
239         vStartInterruptQueueTasks();\r
240         vStartISRTriggeredTask();\r
241         vStartCountingSemaphoreTasks();\r
242         vStartDynamicPriorityTasks();\r
243         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_TASK_PRIORITY );\r
244         vStartQueueSetTasks();\r
245         vStartRecursiveMutexTasks();\r
246         vStartEventGroupTasks();\r
247         vStartMathTasks( mainFLOP_TASK_PRIORITY );\r
248 \r
249         /* Create the tasks defined within this file. */\r
250         xTaskCreate( prvRegTestTask1,                   /* The function that implements the task. */\r
251                                 "Reg1",                                         /* Text name for the task to assist debugger - not used by FreeRTOS itself. */\r
252                                 configMINIMAL_STACK_SIZE,       /* The stack size to allocate for the task - specified in words not bytes. */\r
253                                 NULL,                                           /* The parameter to pass into the task - not used in this case so set to NULL. */\r
254                                 tskIDLE_PRIORITY,                       /* The priority to assign to the task. */\r
255                                 NULL );                                         /* Used to obtain a handle to the task being created - not used in this case so set to NULL. */\r
256 \r
257         xTaskCreate( prvRegTestTask2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
258 \r
259         /* Create the software timer that performs the 'check' functionality, as\r
260         described at the top of this file. */\r
261         xTimer = xTimerCreate(  "CheckTimer",/* A text name, purely to help debugging. */\r
262                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
263                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
264                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
265                                                         prvCheckTimerCallback );                        /* The callback function that inspects the status of all the other tasks. */\r
266 \r
267         if( xTimer != NULL )\r
268         {\r
269                 xTimerStart( xTimer, mainDONT_BLOCK );\r
270         }\r
271 \r
272         /* A software timer is also used to start the high frequency timer test.\r
273         This is to ensure the test does not start before the kernel.  This time a\r
274         one shot software timer is used. */\r
275         xTimer = xTimerCreate( "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );\r
276         if( xTimer != NULL )\r
277         {\r
278                 xTimerStart( xTimer, mainDONT_BLOCK );\r
279         }\r
280 \r
281         /* Finally start the scheduler. */\r
282         vTaskStartScheduler();\r
283 \r
284         /* If all is well, the scheduler will now be running, and the following line\r
285         will never be reached.  If the following line does execute, then there was\r
286         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
287         to be created.  See the memory management section on the FreeRTOS web site\r
288         for more details.  http://www.freertos.org/a00111.html */\r
289         for( ;; );\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static void prvRegTestTask1( void *pvParameters )\r
294 {\r
295 extern void vRegTest1( volatile unsigned long * );\r
296 \r
297         /* Avoid compiler warnings. */\r
298         ( void ) pvParameters;\r
299 \r
300         /* Must be called before any hardware floating point operations are\r
301         performed to let the RTOS portable layer know that this task requires\r
302         a floating point context. */\r
303         portTASK_USES_FLOATING_POINT();\r
304 \r
305         /* Pass the address of the RegTest1 loop counter into the test function,\r
306         which is necessarily implemented in assembler. */\r
307         vRegTest1( &ulRegTest1Cycles );\r
308 \r
309         /* vRegTest1 should never exit! */\r
310         vTaskDelete( NULL );\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 static void prvRegTestTask2( void *pvParameters )\r
315 {\r
316 extern void vRegTest2( volatile unsigned long * );\r
317 \r
318         /* Avoid compiler warnings. */\r
319         ( void ) pvParameters;\r
320 \r
321         /* Must be called before any hardware floating point operations are\r
322         performed to let the RTOS portable layer know that this task requires\r
323         a floating point context. */\r
324         portTASK_USES_FLOATING_POINT();\r
325 \r
326         /* Pass the address of the RegTest2 loop counter into the test function,\r
327         which is necessarily implemented in assembler. */\r
328         vRegTest2( &ulRegTest2Cycles );\r
329 \r
330         /* vRegTest1 should never exit! */\r
331         vTaskDelete( NULL );\r
332 }\r
333 /*-----------------------------------------------------------*/\r
334 \r
335 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
336 {\r
337 static long lChangedTimerPeriodAlready = pdFALSE;\r
338 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulLastHighFrequencyTimerInterrupts = 0;\r
339 static const unsigned long ulExpectedHighFrequencyInterrupts = ( ( mainTEST_INTERRUPT_FREQUENCY / 1000UL ) * mainCHECK_TIMER_PERIOD_MS ) - 10; /* 10 allows for a margin of error. */\r
340 unsigned long ulErrorOccurred = pdFALSE;\r
341 \r
342 /* The count of the high frequency timer interrupts. */\r
343 extern unsigned long ulHighFrequencyTimerInterrupts;\r
344 \r
345         /* Avoid compiler warnings. */\r
346         ( void ) xTimer;\r
347 \r
348         /* Check that the register test 1 task is still running. */\r
349         if( ulLastRegTest1Value == ulRegTest1Cycles )\r
350         {\r
351                 ulErrorOccurred |= ( 0x01UL << 1UL );\r
352         }\r
353         ulLastRegTest1Value = ulRegTest1Cycles;\r
354 \r
355 \r
356         /* Check that the register test 2 task is still running. */\r
357         if( ulLastRegTest2Value == ulRegTest2Cycles )\r
358         {\r
359                 ulErrorOccurred |= ( 0x01UL << 2UL );\r
360         }\r
361         ulLastRegTest2Value = ulRegTest2Cycles;\r
362 \r
363         /* Have any of the standard demo tasks detected an error in their\r
364         operation? */\r
365         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
366         {\r
367                 ulErrorOccurred |= ( 0x01UL << 3UL );\r
368         }\r
369         else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
370         {\r
371                 ulErrorOccurred |= ( 0x01UL << 4UL );\r
372         }\r
373         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
374         {\r
375                 ulErrorOccurred |= ( 0x01UL << 5UL );\r
376         }\r
377         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
378         {\r
379                 ulErrorOccurred |= ( 0x01UL << 6UL );\r
380         }\r
381         else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
382         {\r
383                 ulErrorOccurred |= ( 0x01UL << 7UL );\r
384         }\r
385         else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
386         {\r
387                 ulErrorOccurred |= ( 0x01UL << 8UL );\r
388         }\r
389         else if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
390         {\r
391                 ulErrorOccurred |= ( 0x01UL << 9UL );\r
392         }\r
393         else if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )\r
394         {\r
395                 ulErrorOccurred |= ( 0x01UL << 10UL );\r
396         }\r
397         else if( xAreQueueSetTasksStillRunning() != pdTRUE )\r
398         {\r
399                 ulErrorOccurred |= ( 0x01UL << 11UL );\r
400         }\r
401         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
402         {\r
403                 ulErrorOccurred |= ( 0x01UL << 12UL );\r
404         }\r
405         else if( xAreEventGroupTasksStillRunning() != pdTRUE )\r
406         {\r
407                 ulErrorOccurred |= ( 0x01UL << 13UL );\r
408         }\r
409         else if( xAreMathsTaskStillRunning() != pdTRUE )\r
410         {\r
411                 ulErrorOccurred |= ( 0x01UL << 15UL );\r
412         }\r
413 \r
414         /* Ensure the expected number of high frequency interrupts have occurred. */\r
415         if( ulLastHighFrequencyTimerInterrupts != 0 )\r
416         {\r
417                 if( ( ulHighFrequencyTimerInterrupts - ulLastHighFrequencyTimerInterrupts ) < ulExpectedHighFrequencyInterrupts )\r
418                 {\r
419                         ulErrorOccurred |= ( 0x01UL << 14UL );\r
420                 }\r
421         }\r
422         ulLastHighFrequencyTimerInterrupts = ulHighFrequencyTimerInterrupts;\r
423 \r
424         if( ulErrorOccurred != pdFALSE )\r
425         {\r
426                 /* An error occurred.  Increase the frequency at which the check timer\r
427                 toggles its LED to give visual feedback of the potential error\r
428                 condition. */\r
429                 if( lChangedTimerPeriodAlready == pdFALSE )\r
430                 {\r
431                         lChangedTimerPeriodAlready = pdTRUE;\r
432 \r
433                         /* This call to xTimerChangePeriod() uses a zero block time.\r
434                         Functions called from inside of a timer callback function must\r
435                         *never* attempt to block as to do so could impact other software\r
436                         timers. */\r
437                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
438                 }\r
439         }\r
440 \r
441         vParTestToggleLED( mainCHECK_LED );\r
442 }\r
443 /*-----------------------------------------------------------*/\r
444 \r
445 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer )\r
446 {\r
447 void vSetupTimerTest( unsigned short usFrequencyHz );\r
448 \r
449         /* Avoid compiler warnings. */\r
450         ( void ) xTimer;\r
451 \r
452         /* Setup the high frequency, high priority, timer test.  It is setup in this\r
453         software timer callback to ensure it does not start before the kernel does.\r
454         This is a one shot timer - so the setup routine will only be executed once. */\r
455         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
456 }\r
457 /*-----------------------------------------------------------*/\r
458 \r