]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MZ_MPLAB/main_full.c
a176abf172882187c897266d3bb17e70f12615a9
[freertos] / FreeRTOS / Demo / PIC32MZ_MPLAB / main_full.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /******************************************************************************\r
67  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
68  * project, and a more comprehensive test and demo application.  The\r
69  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
70  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
71  * in main.c.  This file implements the comprehensive test and demo version.\r
72  *\r
73  * NOTE 2:  This file only contains the source code that is specific to the\r
74  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
75  * required to configure the hardware, are defined in main.c.\r
76  ******************************************************************************\r
77  *\r
78  * main_full() creates all the demo application tasks and software timers, then\r
79  * starts the scheduler.  The WEB documentation provides more details of the\r
80  * standard demo application tasks.  In addition to the standard demo tasks, the\r
81  * following tasks and tests are also defined:\r
82  *\r
83  * "Register test" tasks - These tasks are used in part to test the kernel port.\r
84  * They set each processor register to a known value, then check that the\r
85  * register still contains that value.  Each of the tasks sets the registers\r
86  * to different values, and will get swapping in and out between setting and\r
87  * then subsequently checking the register values.  Discovery of an incorrect\r
88  * value would be indicative of an error in the task switching mechanism.\r
89  *\r
90  * "ISR triggered task" - This is provided as an example of how to write a\r
91  * FreeRTOS compatible interrupt service routine.  See the comments in\r
92  * ISRTriggeredTask.c.\r
93  *\r
94  * "High Frequency Timer Test" - The high frequency timer is created to test\r
95  * the interrupt nesting method.  The standard demo interrupt nesting test tasks\r
96  * are created with priorities at or below configMAX_SYSCALL_INTERRUPT_PRIORITY\r
97  * because they use interrupt safe FreeRTOS API functions.  The high frequency\r
98  * time is created with a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY,\r
99  * so cannot us the same API functions.\r
100 \r
101  * By way of demonstration, the demo application defines\r
102  * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY\r
103  * to be 1, and all other interrupts as follows:\r
104  *\r
105  * See the online documentation for this demo for more information on interrupt\r
106  * usage.\r
107  *\r
108  * "Check" timer - The check software timer period is initially set to three\r
109  * seconds.  The callback function associated with the check software timer\r
110  * checks that all the standard demo tasks, and the register check tasks, are\r
111  * not only still executing, but are executing without reporting any errors.  If\r
112  * the check software timer discovers that a task has either stalled, or\r
113  * reported an error, then it changes its own execution period from the initial\r
114  * three seconds, to just 200ms.  The check software timer also toggle LED\r
115  * mainCHECK_LED;  If mainCHECK_LED toggles every 3 seconds, no errors have\r
116  * been detected.  If mainCHECK_LED toggles every 200ms then an error has been\r
117  * detected in at least one task.\r
118  *\r
119  */\r
120 \r
121 /* Scheduler includes. */\r
122 #include "FreeRTOS.h"\r
123 #include "task.h"\r
124 #include "queue.h"\r
125 #include "semphr.h"\r
126 #include "timers.h"\r
127 \r
128 /* Demo application includes. */\r
129 #include "partest.h"\r
130 #include "blocktim.h"\r
131 #include "flash_timer.h"\r
132 #include "semtest.h"\r
133 #include "GenQTest.h"\r
134 #include "QPeek.h"\r
135 #include "IntQueue.h"\r
136 #include "countsem.h"\r
137 #include "dynamic.h"\r
138 #include "QueueOverwrite.h"\r
139 #include "QueueSet.h"\r
140 #include "recmutex.h"\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /* The period after which the check timer will expire, in ms, provided no errors\r
145 have been reported by any of the standard demo tasks.  ms are converted to the\r
146 equivalent in ticks using the portTICK_RATE_MS constant. */\r
147 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
148 \r
149 /* The period at which the check timer will expire, in ms, if an error has been\r
150 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
151 in ticks using the portTICK_RATE_MS constant. */\r
152 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
153 \r
154 /* The priorities of the various demo application tasks. */\r
155 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
156 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
157 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
158 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
159 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
160 #define mainQUEUE_OVERWRITE_TASK_PRIORITY       ( tskIDLE_PRIORITY )\r
161 \r
162 /* The LED controlled by the 'check' software timer. */\r
163 #define mainCHECK_LED                                           ( 2 )\r
164 \r
165 /* The number of LEDs that should be controlled by the flash software timer\r
166 standard demo.  In this case it is only 1 as the starter kit has three LEDs, one\r
167 of which is controlled by the check timer and one of which is controlled by the\r
168 ISR triggered task. */\r
169 #define mainNUM_FLASH_TIMER_LEDS                        ( 1 )\r
170 \r
171 /* Misc. */\r
172 #define mainDONT_BLOCK                                          ( 0 )\r
173 \r
174 /* The frequency at which the "high frequency interrupt" interrupt will\r
175 occur. */\r
176 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
177 \r
178 /*-----------------------------------------------------------*/\r
179 \r
180 /*\r
181  * The check timer callback function, as described at the top of this file.\r
182  */\r
183 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
184 \r
185 /*\r
186  * It is important to ensure the high frequency timer test does not start before\r
187  * the kernel.  It is therefore started from inside a software timer callback\r
188  * function, which will not execute until the timer service/daemon task is\r
189  * executing.  A one-shot timer is used, so the callback function will only\r
190  * execute once (unless it is manually reset/restarted).\r
191  */\r
192 static void prvSetupHighFrequencyTimerTest( xTimerHandle xTimer );\r
193 \r
194 /*\r
195  * Tasks that test the context switch mechanism by filling the processor\r
196  * registers with known values, then checking that the values contained\r
197  * within the registers is as expected.  The tasks are likely to get swapped\r
198  * in and out between setting the register values and checking the register\r
199  * values.\r
200  */\r
201 static void prvRegTestTask1( void *pvParameters );\r
202 static void prvRegTestTask2( void *pvParameters );\r
203 \r
204 /*\r
205  * The task that is periodically triggered by an interrupt, as described at the\r
206  * top of this file.\r
207  */\r
208 extern void vStartISRTriggeredTask( void );\r
209 \r
210 /*-----------------------------------------------------------*/\r
211 \r
212 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively\r
213 on each iteration of their function.  These are used to detect errors in the\r
214 reg test tasks. */\r
215 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
216 \r
217 /*-----------------------------------------------------------*/\r
218 \r
219 /*\r
220  * Create the demo tasks then start the scheduler.\r
221  */\r
222 int main_full( void )\r
223 {\r
224 xTimerHandle xTimer = NULL;\r
225 \r
226         /* Create all the other standard demo tasks. */\r
227         vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );\r
228         vCreateBlockTimeTasks();\r
229         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
230         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
231         vStartQueuePeekTasks();\r
232         vStartInterruptQueueTasks();\r
233         vStartISRTriggeredTask();\r
234         vStartCountingSemaphoreTasks();\r
235         vStartDynamicPriorityTasks();\r
236         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_TASK_PRIORITY );\r
237         vStartQueueSetTasks();\r
238         vStartRecursiveMutexTasks();\r
239 \r
240         /* Create the tasks defined within this file. */\r
241         xTaskCreate( prvRegTestTask1,                                           /* The function that implements the task. */\r
242                                 ( const signed char * const ) "Reg1",   /* Text name for the task to assist debugger - not used by FreeRTOS itself. */\r
243                                 configMINIMAL_STACK_SIZE,                               /* The stack size to allocate for the task - specified in words not bytes. */\r
244                                 NULL,                                                                   /* The parameter to pass into the task - not used in this case so set to NULL. */\r
245                                 tskIDLE_PRIORITY,                                               /* The priority to assign to the task. */\r
246                                 NULL );                                                                 /* Used to obtain a handle to the task being created - not used in this case so set to NULL. */\r
247 \r
248         xTaskCreate( prvRegTestTask2, ( const signed char * const ) "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
249 \r
250         /* Create the software timer that performs the 'check' functionality, as\r
251         described at the top of this file. */\r
252         xTimer = xTimerCreate(  ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
253                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
254                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
255                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
256                                                         prvCheckTimerCallback );                        /* The callback function that inspects the status of all the other tasks. */\r
257 \r
258         if( xTimer != NULL )\r
259         {\r
260                 xTimerStart( xTimer, mainDONT_BLOCK );\r
261         }\r
262 \r
263         /* A software timer is also used to start the high frequency timer test.\r
264         This is to ensure the test does not start before the kernel.  This time a\r
265         one shot software timer is used. */\r
266         xTimer = xTimerCreate( ( const signed char * ) "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );\r
267         if( xTimer != NULL )\r
268         {\r
269                 xTimerStart( xTimer, mainDONT_BLOCK );\r
270         }\r
271 \r
272         /* Finally start the scheduler. */\r
273         vTaskStartScheduler();\r
274 \r
275         /* If all is well, the scheduler will now be running, and the following line\r
276         will never be reached.  If the following line does execute, then there was\r
277         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
278         to be created.  See the memory management section on the FreeRTOS web site\r
279         for more details.  http://www.freertos.org/a00111.html */\r
280         for( ;; );\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 static void prvRegTestTask1( void *pvParameters )\r
285 {\r
286 extern void vRegTest1( volatile unsigned long * );\r
287 \r
288         /* Avoid compiler warnings. */\r
289         ( void ) pvParameters;\r
290 \r
291         /* Pass the address of the RegTest1 loop counter into the test function,\r
292         which is necessarily implemented in assembler. */\r
293         vRegTest1( &ulRegTest1Cycles );\r
294 \r
295         /* vRegTest1 should never exit! */\r
296         vTaskDelete( NULL );\r
297 }\r
298 /*-----------------------------------------------------------*/\r
299 \r
300 static void prvRegTestTask2( void *pvParameters )\r
301 {\r
302 extern void vRegTest2( volatile unsigned long * );\r
303 \r
304         /* Avoid compiler warnings. */\r
305         ( void ) pvParameters;\r
306 \r
307         /* Pass the address of the RegTest2 loop counter into the test function,\r
308         which is necessarily implemented in assembler. */\r
309         vRegTest2( &ulRegTest2Cycles );\r
310 \r
311         /* vRegTest1 should never exit! */\r
312         vTaskDelete( NULL );\r
313 }\r
314 /*-----------------------------------------------------------*/\r
315 \r
316 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
317 {\r
318 static long lChangedTimerPeriodAlready = pdFALSE;\r
319 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulLastHighFrequencyTimerInterrupts = 0;\r
320 static const unsigned long ulExpectedHighFrequencyInterrupts = ( ( mainTEST_INTERRUPT_FREQUENCY / 1000UL ) * mainCHECK_TIMER_PERIOD_MS ) - 10; /* 10 allows for a margin of error. */\r
321 unsigned long ulErrorOccurred = pdFALSE;\r
322 \r
323 /* The count of the high frequency timer interrupts. */\r
324 extern unsigned long ulHighFrequencyTimerInterrupts;\r
325 \r
326         /* Avoid compiler warnings. */\r
327         ( void ) xTimer;\r
328 \r
329         /* Check that the register test 1 task is still running. */\r
330         if( ulLastRegTest1Value == ulRegTest1Cycles )\r
331         {\r
332                 ulErrorOccurred |= ( 0x01UL << 1UL );\r
333         }\r
334         ulLastRegTest1Value = ulRegTest1Cycles;\r
335 \r
336 \r
337         /* Check that the register test 2 task is still running. */\r
338         if( ulLastRegTest2Value == ulRegTest2Cycles )\r
339         {\r
340                 ulErrorOccurred |= ( 0x01UL << 2UL );\r
341         }\r
342         ulLastRegTest2Value = ulRegTest2Cycles;\r
343 \r
344         /* Have any of the standard demo tasks detected an error in their\r
345         operation? */\r
346         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
347         {\r
348                 ulErrorOccurred |= ( 0x01UL << 3UL );\r
349         }\r
350         else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
351         {\r
352                 ulErrorOccurred |= ( 0x01UL << 4UL );\r
353         }\r
354         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
355         {\r
356                 ulErrorOccurred |= ( 0x01UL << 5UL );\r
357         }\r
358         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
359         {\r
360                 ulErrorOccurred |= ( 0x01UL << 6UL );\r
361         }\r
362         else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
363         {\r
364                 ulErrorOccurred |= ( 0x01UL << 7UL );\r
365         }\r
366         else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
367         {\r
368                 ulErrorOccurred |= ( 0x01UL << 8UL );\r
369         }\r
370         else if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
371         {\r
372                 ulErrorOccurred |= ( 0x01UL << 9UL );\r
373         }\r
374         else if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )\r
375         {\r
376                 ulErrorOccurred |= ( 0x01UL << 10UL );\r
377         }\r
378         else if( xAreQueueSetTasksStillRunning() != pdTRUE )\r
379         {\r
380                 ulErrorOccurred |= ( 0x01UL << 11UL );\r
381         }\r
382         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
383         {\r
384                 ulErrorOccurred |= ( 0x01UL << 12UL );\r
385         }\r
386 \r
387         /* Ensure the expected number of high frequency interrupts have occurred. */\r
388         if( ulLastHighFrequencyTimerInterrupts != 0 )\r
389         {\r
390                 if( ( ulHighFrequencyTimerInterrupts - ulLastHighFrequencyTimerInterrupts ) < ulExpectedHighFrequencyInterrupts )\r
391                 {\r
392                         ulErrorOccurred |= ( 0x01UL << 13UL );\r
393                 }\r
394         }\r
395         ulLastHighFrequencyTimerInterrupts = ulHighFrequencyTimerInterrupts;\r
396 \r
397         if( ulErrorOccurred != pdFALSE )\r
398         {\r
399                 /* An error occurred.  Increase the frequency at which the check timer\r
400                 toggles its LED to give visual feedback of the potential error\r
401                 condition. */\r
402                 if( lChangedTimerPeriodAlready == pdFALSE )\r
403                 {\r
404                         lChangedTimerPeriodAlready = pdTRUE;\r
405 \r
406                         /* This call to xTimerChangePeriod() uses a zero block time.\r
407                         Functions called from inside of a timer callback function must\r
408                         *never* attempt to block as to do so could impact other software\r
409                         timers. */\r
410                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
411                 }\r
412         }\r
413 \r
414         vParTestToggleLED( mainCHECK_LED );\r
415 }\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 static void prvSetupHighFrequencyTimerTest( xTimerHandle xTimer )\r
419 {\r
420 void vSetupTimerTest( unsigned short usFrequencyHz );\r
421 \r
422         /* Avoid compiler warnings. */\r
423         ( void ) xTimer;\r
424 \r
425         /* Setup the high frequency, high priority, timer test.  It is setup in this\r
426         software timer callback to ensure it does not start before the kernel does.\r
427         This is a one shot timer - so the setup routine will only be executed once. */\r
428         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
429 }\r
430 /*-----------------------------------------------------------*/\r
431 \r