]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/main_full.c
Ensure both one-shot and auto-reload are written consistently with a hyphen in comments.
[freertos] / FreeRTOS / Demo / PIC32MX_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 defined and/or created within this file:\r
44  *\r
45  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
46  * is permitted to access the display directly.  Other tasks wishing to write a\r
47  * message to the LCD send the message on a queue to the LCD task instead of\r
48  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
49  * for messages - waking and displaying the messages as they arrive.\r
50  * **NOTE** The LCD driver has a dependency on the PLIB library, which is no\r
51  * longer provided by Microchip, so LCD functionality has been removed from this\r
52  * demo - however the source files remain in the distribution.\r
53  *\r
54  * "Check" timer - The check software timer period is initially set to three\r
55  * seconds.  The callback function associated with the check software timer\r
56  * checks that all the standard demo tasks, and the register check tasks, are\r
57  * not only still executing, but are executing without reporting any errors.  If\r
58  * the check software timer discovers that a task has either stalled, or\r
59  * reported an error, then it changes its own execution period from the initial\r
60  * three seconds, to just 200ms.  The check software timer callback function\r
61  * also writes a status message to the LCD (via the LCD task).  If all the demo\r
62  * tasks are executing with their expected behaviour then the check task writes\r
63  * a count of the number of times the high frequency interrupt has incremented\r
64  * ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.\r
65  *\r
66  * "Register test" tasks - These tasks are used in part to test the kernel port.\r
67  * They set each processor register to a known value, then check that the\r
68  * register still contains that value.  Each of the tasks sets the registers\r
69  * to different values, and will get swapping in and out between setting and\r
70  * then subsequently checking the register values.  Discovery of an incorrect\r
71  * value would be indicative of an error in the task switching mechanism.\r
72  *\r
73  * By way of demonstration, the demo application defines\r
74  * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY\r
75  * to be 1, and all other interrupts as follows:\r
76  *\r
77  *      + The UART is allocated a priority of 2. This means it can interrupt the\r
78  *    RTOS tick, and can also safely use queues.\r
79  *    **NOTE** The UART driver has a dependency on the PLIB library, which is no\r
80  *    longer provided by Microchip, so UART functionality has been removed from\r
81  *    this demo - however the source files remain in the distribution.\r
82  *\r
83  *  + Two timers are configured to generate interrupts just to test the nesting\r
84  *    and queue access mechanisms. These timers are allocated priorities 2 and 3\r
85  *    respectively. Even though they both access the same two queues, the\r
86  *    priority 3 interrupt can safely interrupt the priority 2 interrupt. Both\r
87  *    can interrupt the RTOS tick.\r
88  *  + Finally a high frequency timer interrupt is configured to use priority 4 -\r
89  *    therefore kernel activity will never prevent the high frequency timer from\r
90  *    executing immediately that the interrupt is raised (within the limitations\r
91  *    of the hardware itself). It would not be safe to access a queue from this\r
92  *    interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
93  *\r
94  * See the online documentation for this demo for more information on interrupt\r
95  * usage.\r
96  */\r
97 \r
98 /* Standard includes. */\r
99 #include <stdio.h>\r
100 \r
101 /* Scheduler includes. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 #include "queue.h"\r
105 #include "timers.h"\r
106 \r
107 /* Demo application includes. */\r
108 #include "partest.h"\r
109 #include "blocktim.h"\r
110 #include "flash_timer.h"\r
111 #include "semtest.h"\r
112 #include "GenQTest.h"\r
113 #include "QPeek.h"\r
114 #include "lcd.h"\r
115 #include "timertest.h"\r
116 #include "IntQueue.h"\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /* The period after which the check timer will expire, in ms, provided no errors\r
121 have been reported by any of the standard demo tasks.  ms are converted to the\r
122 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
123 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
124 \r
125 /* The period at which the check timer will expire, in ms, if an error has been\r
126 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
127 in ticks using the portTICK_PERIOD_MS constant. */\r
128 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
129 \r
130 /* The priorities of the various demo application tasks. */\r
131 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
132 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
133 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
134 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
135 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
136 \r
137 /* Misc. */\r
138 #define mainDONT_BLOCK                                          ( 0 )\r
139 \r
140 /* Dimension the buffer used to hold the value of the high frequency timer\r
141 count when it is converted to a string. */\r
142 #define mainMAX_STRING_LENGTH                           ( 20 )\r
143 \r
144 /* The frequency at which the "fast interrupt test" interrupt will occur. */\r
145 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
146 \r
147 /* The number of timer clocks expected to occur between each "fast interrupt\r
148 test" interrupt. */\r
149 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )\r
150 \r
151 /* The number of nano seconds between each core clock. */\r
152 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )\r
153 \r
154 /* The number of LEDs that should be controlled by the flash software timer\r
155 standard demo and the LED to be toggle by the check task.  The starter kit only\r
156 has three LEDs so when the demo is configured to run on the starter kit there\r
157 is one less flash timer so the check task can use the third LED. */\r
158 #ifdef PIC32_STARTER_KIT\r
159         #define mainNUM_FLASH_TIMER_LEDS                        ( 2 )\r
160         #define mainCHECK_LED                                           ( 2 )\r
161 #else\r
162         #define mainNUM_FLASH_TIMER_LEDS                        ( 3 )\r
163         #define mainCHECK_LED                                           ( 7 )\r
164 #endif\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /*\r
169  * The check timer callback function, as described at the top of this file.\r
170  */\r
171 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
172 \r
173 /*\r
174  * It is important to ensure the high frequency timer test does not start before\r
175  * the kernel.  It is therefore started from inside a software timer callback\r
176  * function, which will not execute until the timer service/daemon task is\r
177  * executing.  A one-shot timer is used, so the callback function will only\r
178  * execute once (unless it is manually reset/restarted).\r
179  */\r
180 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer );\r
181 \r
182 /*\r
183  * Tasks that test the context switch mechanism by filling the processor\r
184  * registers with known values, then checking that the values contained\r
185  * within the registers is as expected.  The tasks are likely to get swapped\r
186  * in and out between setting the register values and checking the register\r
187  * values.\r
188  */\r
189 static void prvRegTestTask1( void *pvParameters );\r
190 static void prvRegTestTask2( void *pvParameters );\r
191 \r
192 /*-----------------------------------------------------------*/\r
193 \r
194 /* The queue used to send messages to the LCD task. */\r
195 static QueueHandle_t xLCDQueue;\r
196 \r
197 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively on\r
198 each iteration of their function.  This is used to detect either task stopping\r
199 their execution.. */\r
200 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
201 \r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /*\r
205  * Create the demo tasks then start the scheduler.\r
206  */\r
207 int main_full( void )\r
208 {\r
209 TimerHandle_t xTimer = NULL;\r
210 \r
211         /* Create all the other standard demo tasks. */\r
212         vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );\r
213     vCreateBlockTimeTasks();\r
214     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
215     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
216     vStartQueuePeekTasks();\r
217         vStartInterruptQueueTasks();\r
218 \r
219         /* Create the tasks defined within this file. */\r
220         xTaskCreate( prvRegTestTask1, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
221         xTaskCreate( prvRegTestTask2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
222 \r
223         /* Create the software timer that performs the 'check' functionality, as\r
224         described at the top of this file. */\r
225         xTimer = xTimerCreate(  "CheckTimer",/* A text name, purely to help debugging. */\r
226                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
227                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
228                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
229                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
230                                                 );\r
231 \r
232         if( xTimer != NULL )\r
233         {\r
234                 xTimerStart( xTimer, mainDONT_BLOCK );\r
235         }\r
236 \r
237         /* A software timer is also used to start the high frequency timer test.\r
238         This is to ensure the test does not start before the kernel.  This time a\r
239         one-shot software timer is used. */\r
240         xTimer = xTimerCreate( "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );\r
241         if( xTimer != NULL )\r
242         {\r
243                 xTimerStart( xTimer, mainDONT_BLOCK );\r
244         }\r
245 \r
246         /* Finally start the scheduler. */\r
247         vTaskStartScheduler();\r
248 \r
249         /* If all is well, the scheduler will now be running, and the following line\r
250         will never be reached.  If the following line does execute, then there was\r
251         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
252         to be created.  See the memory management section on the FreeRTOS web site\r
253         for more details. */\r
254         for( ;; );\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void prvRegTestTask1( void *pvParameters )\r
259 {\r
260 extern void vRegTest1( volatile unsigned long * );\r
261 \r
262         /* Avoid compiler warnings. */\r
263         ( void ) pvParameters;\r
264 \r
265         /* Pass the address of the RegTest1 loop counter into the test function,\r
266         which is necessarily implemented in assembler. */\r
267         vRegTest1( &ulRegTest1Cycles );\r
268 \r
269         /* vRegTest1 should never exit! */\r
270         vTaskDelete( NULL );\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 static void prvRegTestTask2( void *pvParameters )\r
275 {\r
276 extern void vRegTest2( volatile unsigned long * );\r
277 \r
278         /* Avoid compiler warnings. */\r
279         ( void ) pvParameters;\r
280 \r
281         /* Pass the address of the RegTest2 loop counter into the test function,\r
282         which is necessarily implemented in assembler. */\r
283         vRegTest2( &ulRegTest2Cycles );\r
284 \r
285         /* vRegTest1 should never exit! */\r
286         vTaskDelete( NULL );\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
291 {\r
292 static long lChangedTimerPeriodAlready = pdFALSE;\r
293 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
294 \r
295 /* Buffer into which the high frequency timer count is written as a string. */\r
296 static char cStringBuffer[ mainMAX_STRING_LENGTH ];\r
297 \r
298 /* The count of the high frequency timer interrupts. */\r
299 extern unsigned long ulHighFrequencyTimerInterrupts;\r
300 static xLCDMessage xMessage = { ( 200 / portTICK_PERIOD_MS ), cStringBuffer };\r
301 \r
302         /* Check that the register test 1 task is still running. */\r
303         if( ulLastRegTest1Value == ulRegTest1Cycles )\r
304         {\r
305                 xMessage.pcMessage = "Error: Reg test2";\r
306         }\r
307         ulLastRegTest1Value = ulRegTest1Cycles;\r
308 \r
309 \r
310         /* Check that the register test 2 task is still running. */\r
311         if( ulLastRegTest2Value == ulRegTest2Cycles )\r
312         {\r
313                 xMessage.pcMessage = "Error: Reg test3";\r
314         }\r
315         ulLastRegTest2Value = ulRegTest2Cycles;\r
316 \r
317 \r
318         /* Have any of the standard demo tasks detected an error in their\r
319         operation? */\r
320         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
321         {\r
322                 xMessage.pcMessage = "Error: Gen Q";\r
323         }\r
324         else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
325         {\r
326                 xMessage.pcMessage = "Error: Q Peek";\r
327         }\r
328         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
329         {\r
330                 xMessage.pcMessage = "Error: Blck time";\r
331         }\r
332         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
333         {\r
334                 xMessage.pcMessage = "Error: Sem test";\r
335         }\r
336         else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
337         {\r
338                 xMessage.pcMessage = "Error: Int queue";\r
339         }\r
340 \r
341         if( xMessage.pcMessage != cStringBuffer )\r
342         {\r
343                 /* An error string has been logged.  If the timer period has not yet\r
344                 been changed it should be changed now.  Increasing the frequency of the\r
345                 LED gives visual feedback of the error status. */\r
346                 if( lChangedTimerPeriodAlready == pdFALSE )\r
347                 {\r
348                         lChangedTimerPeriodAlready = pdTRUE;\r
349 \r
350                         /* This call to xTimerChangePeriod() uses a zero block time.\r
351                         Functions called from inside of a timer callback function must\r
352                         *never* attempt to block as to do so could impact other software\r
353                         timers. */\r
354                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
355                 }\r
356         }\r
357         else\r
358         {\r
359                 /* Write the ulHighFrequencyTimerInterrupts value to the string\r
360                 buffer.  It will only be displayed if no errors have been detected. */\r
361                 sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );\r
362         }\r
363 \r
364         vParTestToggleLED( mainCHECK_LED );\r
365 }\r
366 /*-----------------------------------------------------------*/\r
367 \r
368 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer )\r
369 {\r
370         /* Setup the high frequency, high priority, timer test.  It is setup in this\r
371         software timer callback to ensure it does not start before the kernel does.\r
372         This is a one-shot timer - so the setup routine will only be executed once. */\r
373         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
374 }\r
375 \r