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