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