]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MEC14xx_MPLAB/src/Full_Demo/main_full.c
13c7e21e1f9b24df52153f170bf8e7d71a8c3c7f
[freertos] / FreeRTOS / Demo / PIC32MEC14xx_MPLAB / src / Full_Demo / 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 as 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  * "High Frequency Timer Test" - The high frequency timer is created to test\r
53  * the interrupt nesting method.  The standard demo interrupt nesting test tasks\r
54  * are created with priorities at or below configMAX_SYSCALL_INTERRUPT_PRIORITY\r
55  * because they use interrupt safe FreeRTOS API functions.  The high frequency\r
56  * time is created with a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY,\r
57  * so cannot us the same API functions.\r
58  *\r
59  * "Check" timer - The check software timer period is initially set to three\r
60  * seconds.  The callback function associated with the check software timer\r
61  * checks that all the standard demo tasks, and the register check tasks, are\r
62  * not only still executing, but are executing without reporting any errors.  If\r
63  * the check software timer discovers that a task has either stalled, or\r
64  * reported an error, then it changes its own execution period from the initial\r
65  * three seconds, to just 200ms.  The check software timer also toggle LED\r
66  * mainCHECK_LED;  If mainCHECK_LED toggles every 3 seconds, no errors have\r
67  * been detected.  If mainCHECK_LED toggles every 200ms then an error has been\r
68  * detected in at least one task.\r
69  *\r
70  */\r
71 \r
72 /* Scheduler includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 #include "queue.h"\r
76 #include "semphr.h"\r
77 #include "timers.h"\r
78 \r
79 /* Demo application includes. */\r
80 #include "blocktim.h"\r
81 #include "semtest.h"\r
82 #include "GenQTest.h"\r
83 #include "IntQueue.h"\r
84 #include "countsem.h"\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 /* The period after which the check timer will expire, in ms, provided no errors\r
89 have been reported by any of the standard demo tasks.  ms are converted to the\r
90 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
91 #define mainCHECK_TIMER_PERIOD_MS                       pdMS_TO_TICKS( 3000UL )\r
92 \r
93 /* The period at which the check timer will expire, in ms, if an error has been\r
94 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
95 in ticks using the portTICK_PERIOD_MS constant. */\r
96 #define mainERROR_CHECK_TIMER_PERIOD_MS         pdMS_TO_TICKS( 200UL )\r
97 \r
98 /* The priorities of the various demo application tasks. */\r
99 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
100 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
101 \r
102 /* The LED controlled by the 'check' software timer. */\r
103 #define mainCHECK_LED                                           ( 2 )\r
104 \r
105 /* Misc. */\r
106 #define mainDONT_BLOCK                                          ( 0 )\r
107 \r
108 /* The frequency at which the "high frequency interrupt" interrupt will\r
109 occur. */\r
110 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /*\r
115  * The check timer callback function, as described at the top of this file.\r
116  */\r
117 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
118 \r
119 /*\r
120  * It is important to ensure the high frequency timer test does not start before\r
121  * the kernel.  It is therefore started from inside a software timer callback\r
122  * function, which will not execute until the timer service/daemon task is\r
123  * executing.  A one-shot timer is used, so the callback function will only\r
124  * execute once (unless it is manually reset/restarted).\r
125  */\r
126 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer );\r
127 \r
128 /*\r
129  * Tasks that test the context switch mechanism by filling the processor\r
130  * registers with known values, then checking that the values contained\r
131  * within the registers is as expected.  The tasks are likely to get swapped\r
132  * in and out between setting the register values and checking the register\r
133  * values.\r
134  */\r
135 static void prvRegTestTask1( void *pvParameters );\r
136 static void prvRegTestTask2( void *pvParameters );\r
137 \r
138 /*\r
139  * LED toggle function that uses a critical section to ensure thread safety.\r
140  */\r
141 extern void vToggleLED( uint8_t ucLED );\r
142 \r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively\r
146 on each iteration of their function.  These are used to detect errors in the\r
147 reg test tasks. */\r
148 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
149 \r
150 /*-----------------------------------------------------------*/\r
151 \r
152 /*\r
153  * Create the demo tasks then start the scheduler.\r
154  */\r
155 int main_full( void )\r
156 {\r
157 TimerHandle_t xTimer = NULL;\r
158 \r
159         /* Create all the other standard demo tasks. */\r
160         vCreateBlockTimeTasks();\r
161         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
162         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
163         vStartInterruptQueueTasks();\r
164         vStartCountingSemaphoreTasks();\r
165 \r
166         /* Create the tasks defined within this file. */\r
167         xTaskCreate( prvRegTestTask1,                   /* The function that implements the task. */\r
168                                 "Reg1",                                         /* Text name for the task to assist debugger - not used by FreeRTOS itself. */\r
169                                 configMINIMAL_STACK_SIZE,       /* The stack size to allocate for the task - specified in words not bytes. */\r
170                                 NULL,                                           /* The parameter to pass into the task - not used in this case so set to NULL. */\r
171                                 tskIDLE_PRIORITY,                       /* The priority to assign to the task. */\r
172                                 NULL );                                         /* Used to obtain a handle to the task being created - not used in this case so set to NULL. */\r
173 \r
174         xTaskCreate( prvRegTestTask2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
175 \r
176         /* Create the software timer that performs the 'check' functionality, as\r
177         described at the top of this file. */\r
178         xTimer = xTimerCreate(  "CheckTimer",/* A text name, purely to help debugging. */\r
179                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
180                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
181                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
182                                                         prvCheckTimerCallback );                        /* The callback function that inspects the status of all the other tasks. */\r
183 \r
184         if( xTimer != NULL )\r
185         {\r
186                 xTimerStart( xTimer, mainDONT_BLOCK );\r
187         }\r
188 \r
189         /* A software timer is also used to start the high frequency timer test.\r
190         This is to ensure the test does not start before the kernel.  This time a\r
191         one shot software timer is used. */\r
192         xTimer = xTimerCreate( "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );\r
193         if( xTimer != NULL )\r
194         {\r
195                 xTimerStart( xTimer, mainDONT_BLOCK );\r
196         }\r
197 \r
198         /* Finally start the scheduler. */\r
199         vTaskStartScheduler();\r
200 \r
201         /* If all is well, the scheduler will now be running, and the following line\r
202         will never be reached.  If the following line does execute, then there was\r
203         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
204         to be created.  See the memory management section on the FreeRTOS web site\r
205         for more details.  http://www.freertos.org/a00111.html */\r
206         for( ;; );\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 static void prvRegTestTask1( void *pvParameters )\r
211 {\r
212 extern void vRegTest1( volatile unsigned long * );\r
213 \r
214         /* Avoid compiler warnings. */\r
215         ( void ) pvParameters;\r
216 \r
217         /* Pass the address of the RegTest1 loop counter into the test function,\r
218         which is necessarily implemented in assembler. */\r
219         vRegTest1( &ulRegTest1Cycles );\r
220 \r
221         /* vRegTest1 should never exit! */\r
222         vTaskDelete( NULL );\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 static void prvRegTestTask2( void *pvParameters )\r
227 {\r
228 extern void vRegTest2( volatile unsigned long * );\r
229 \r
230         /* Avoid compiler warnings. */\r
231         ( void ) pvParameters;\r
232 \r
233         /* Pass the address of the RegTest2 loop counter into the test function,\r
234         which is necessarily implemented in assembler. */\r
235         vRegTest2( &ulRegTest2Cycles );\r
236 \r
237         /* vRegTest1 should never exit! */\r
238         vTaskDelete( NULL );\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
243 {\r
244 static long lChangedTimerPeriodAlready = pdFALSE;\r
245 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulLastHighFrequencyTimerInterrupts = 0;\r
246 static const unsigned long ulExpectedHighFrequencyInterrupts = ( ( mainTEST_INTERRUPT_FREQUENCY / 1000UL ) * mainCHECK_TIMER_PERIOD_MS ) - 10; /* 10 allows for a margin of error. */\r
247 unsigned long ulErrorOccurred = pdFALSE;\r
248 /* The count of the high frequency timer interrupts. */\r
249 extern unsigned long ulHighFrequencyTimerInterrupts;\r
250 \r
251         /* Avoid compiler warnings. */\r
252         ( void ) xTimer;\r
253 \r
254         /* Check that the register test 1 task is still running. */\r
255         if( ulLastRegTest1Value == ulRegTest1Cycles )\r
256         {\r
257                 ulErrorOccurred |= ( 0x01UL << 1UL );\r
258         }\r
259         ulLastRegTest1Value = ulRegTest1Cycles;\r
260 \r
261         /* Check that the register test 2 task is still running. */\r
262         if( ulLastRegTest2Value == ulRegTest2Cycles )\r
263         {\r
264                 ulErrorOccurred |= ( 0x01UL << 2UL );\r
265         }\r
266         ulLastRegTest2Value = ulRegTest2Cycles;\r
267 \r
268         /* Have any of the standard demo tasks detected an error in their\r
269         operation? */\r
270         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
271         {\r
272                 ulErrorOccurred |= ( 0x01UL << 3UL );\r
273         }\r
274         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
275         {\r
276                 ulErrorOccurred |= ( 0x01UL << 4UL );\r
277         }\r
278         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
279         {\r
280                 ulErrorOccurred |= ( 0x01UL << 5UL );\r
281         }\r
282         else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
283         {\r
284                 ulErrorOccurred |= ( 0x01UL << 6UL );\r
285         }\r
286         else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
287         {\r
288                 ulErrorOccurred |= ( 0x01UL << 7UL );\r
289         }\r
290 \r
291         /* Ensure the expected number of high frequency interrupts have occurred. */\r
292         if( ulLastHighFrequencyTimerInterrupts != 0 )\r
293         {\r
294                 if( ( ulHighFrequencyTimerInterrupts - ulLastHighFrequencyTimerInterrupts ) < ulExpectedHighFrequencyInterrupts )\r
295                 {\r
296                         ulErrorOccurred |= ( 0x01UL << 8UL );\r
297                 }\r
298         }\r
299         ulLastHighFrequencyTimerInterrupts = ulHighFrequencyTimerInterrupts;\r
300 \r
301         if( ulErrorOccurred != pdFALSE )\r
302         {\r
303                 /* An error occurred.  Increase the frequency at which the check timer\r
304                 toggles its LED to give visual feedback of the potential error\r
305                 condition. */\r
306                 if( lChangedTimerPeriodAlready == pdFALSE )\r
307                 {\r
308                         lChangedTimerPeriodAlready = pdTRUE;\r
309 \r
310                         /* This call to xTimerChangePeriod() uses a zero block time.\r
311                         Functions called from inside of a timer callback function must\r
312                         *never* attempt to block as to do so could impact other software\r
313                         timers. */\r
314                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
315                 }\r
316         }\r
317 \r
318         vToggleLED( mainCHECK_LED );\r
319 }\r
320 /*-----------------------------------------------------------*/\r
321 \r
322 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer )\r
323 {\r
324 void vSetupTimerTest( unsigned short usFrequencyHz );\r
325 \r
326         /* Prevent compiler warnings */\r
327         (void) xTimer;\r
328 \r
329         /* Setup the high frequency, high priority, timer test.  It is setup in this\r
330         software timer callback to ensure it does not start before the kernel does.\r
331         This is a one shot timer - so the setup routine will only be executed once. */\r
332 \r
333         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r