]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/main_full.c
Set the MSVC Win32 demo back to create the simply blinky demo by default.
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / main_full.c
1 /*\r
2     FreeRTOS V7.5.3 - 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 defined and/or created within this file:\r
82  *\r
83  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
84  * is permitted to access the display directly.  Other tasks wishing to write a\r
85  * message to the LCD send the message on a queue to the LCD task instead of\r
86  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
87  * for messages - waking and displaying the messages as they arrive.\r
88  *\r
89  * "Check" timer - The check software timer period is initially set to three\r
90  * seconds.  The callback function associated with the check software timer\r
91  * checks that all the standard demo tasks, and the register check tasks, are\r
92  * not only still executing, but are executing without reporting any errors.  If\r
93  * the check software timer discovers that a task has either stalled, or\r
94  * reported an error, then it changes its own execution period from the initial\r
95  * three seconds, to just 200ms.  The check software timer callback function\r
96  * also writes a status message to the LCD (via the LCD task).  If all the demo \r
97  * tasks are executing with their expected behaviour then the check task writes \r
98  * a count of the number of times the high frequency interrupt has incremented\r
99  * ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.\r
100  *\r
101  * "Register test" tasks - These tasks are used in part to test the kernel port.\r
102  * They set each processor register to a known value, then check that the \r
103  * register still contains that value.  Each of the tasks sets the registers\r
104  * to different values, and will get swapping in and out between setting and \r
105  * then subsequently checking the register values.  Discovery of an incorrect\r
106  * value would be indicative of an error in the task switching mechanism.\r
107  *\r
108  * By way of demonstration, the demo application defines \r
109  * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY \r
110  * to be 1, and all other interrupts as follows:\r
111  *\r
112  *      + The UART is allocated a priority of 2. This means it can interrupt the \r
113  *    RTOS tick, and can also safely use queues.\r
114  *  + Two timers are configured to generate interrupts just to test the nesting \r
115  *    and queue access mechanisms. These timers are allocated priorities 2 and 3 \r
116  *    respectively. Even though they both access the same two queues, the \r
117  *    priority 3 interrupt can safely interrupt the priority 2 interrupt. Both \r
118  *    can interrupt the RTOS tick.\r
119  *  + Finally a high frequency timer interrupt is configured to use priority 4 - \r
120  *    therefore kernel activity will never prevent the high frequency timer from \r
121  *    executing immediately that the interrupt is raised (within the limitations \r
122  *    of the hardware itself). It would not be safe to access a queue from this \r
123  *    interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY. \r
124  *\r
125  * See the online documentation for this demo for more information on interrupt\r
126  * usage.\r
127  */\r
128 \r
129 /* Standard includes. */\r
130 #include <stdio.h>\r
131 \r
132 /* Scheduler includes. */\r
133 #include "FreeRTOS.h"\r
134 #include "task.h"\r
135 #include "queue.h"\r
136 #include "timers.h"\r
137 \r
138 /* Demo application includes. */\r
139 #include "partest.h"\r
140 #include "blocktim.h"\r
141 #include "flash_timer.h"\r
142 #include "semtest.h"\r
143 #include "GenQTest.h"\r
144 #include "QPeek.h"\r
145 #include "lcd.h"\r
146 #include "comtest2.h"\r
147 #include "timertest.h"\r
148 #include "IntQueue.h"\r
149 \r
150 /*-----------------------------------------------------------*/\r
151 \r
152 /* The period after which the check timer will expire, in ms, provided no errors\r
153 have been reported by any of the standard demo tasks.  ms are converted to the\r
154 equivalent in ticks using the portTICK_RATE_MS constant. */\r
155 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
156 \r
157 /* The period at which the check timer will expire, in ms, if an error has been\r
158 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
159 in ticks using the portTICK_RATE_MS constant. */\r
160 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
161 \r
162 /* The priorities of the various demo application tasks. */\r
163 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
164 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
165 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
166 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
167 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
168 \r
169 /* The LED controlled by the 'check' software timer. */\r
170 #define mainCHECK_LED                                           ( 7 )\r
171 \r
172 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
173 See the comtest.c file for more information. */\r
174 #define mainCOM_TEST_LED                                        ( 4 )\r
175 \r
176 /* Baud rate used by the comtest tasks. */\r
177 #define mainCOM_TEST_BAUD_RATE                          ( 115200 )\r
178 \r
179 /* Misc. */\r
180 #define mainDONT_BLOCK                                          ( 0 )\r
181 \r
182 /* Dimension the buffer used to hold the value of the high frequency timer \r
183 count when it is converted to a string. */\r
184 #define mainMAX_STRING_LENGTH                           ( 20 )\r
185 \r
186 /* The frequency at which the "fast interrupt test" interrupt will occur. */\r
187 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
188 \r
189 /* The number of timer clocks expected to occur between each "fast interrupt \r
190 test" interrupt. */\r
191 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )\r
192 \r
193 /* The number of nano seconds between each core clock. */\r
194 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )\r
195 \r
196 /* The number of LEDs that should be controlled by the flash software timer\r
197 standard demo. */\r
198 #define mainNUM_FLASH_TIMER_LEDS                        ( 3 )\r
199 \r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /*\r
203  * The check timer callback function, as described at the top of this file.\r
204  */\r
205 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
206 \r
207 /*\r
208  * It is important to ensure the high frequency timer test does not start before\r
209  * the kernel.  It is therefore started from inside a software timer callback\r
210  * function, which will not execute until the timer service/daemon task is\r
211  * executing.  A one-shot timer is used, so the callback function will only\r
212  * execute once (unless it is manually reset/restarted).\r
213  */\r
214 static void prvSetupHighFrequencyTimerTest( xTimerHandle xTimer );\r
215 \r
216 /*\r
217  * Tasks that test the context switch mechanism by filling the processor \r
218  * registers with known values, then checking that the values contained\r
219  * within the registers is as expected.  The tasks are likely to get swapped\r
220  * in and out between setting the register values and checking the register\r
221  * values. \r
222  */\r
223 static void prvRegTestTask1( void *pvParameters );\r
224 static void prvRegTestTask2( void *pvParameters );\r
225 \r
226 /*-----------------------------------------------------------*/\r
227 \r
228 /* The queue used to send messages to the LCD task. */\r
229 static xQueueHandle xLCDQueue;\r
230 \r
231 /* Flag used by prvRegTestTask1() and prvRegTestTask2() to indicate their status\r
232 (pass/fail). */\r
233 volatile unsigned long ulStatus1 = pdPASS;\r
234 \r
235 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively on \r
236 each iteration of their function.  This is used to detect either task stopping\r
237 their execution.. */\r
238 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
239 \r
240 /*-----------------------------------------------------------*/\r
241 \r
242 /*\r
243  * Create the demo tasks then start the scheduler.\r
244  */\r
245 int main_full( void )\r
246 {\r
247 xTimerHandle xTimer = NULL;\r
248 \r
249         /* Create the LCD task - this returns the queue to use when writing \r
250         messages to the LCD. */\r
251         xLCDQueue = xStartLCDTask();\r
252 \r
253         /* Create all the other standard demo tasks. */\r
254         vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );\r
255     vCreateBlockTimeTasks();\r
256     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
257     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
258     vStartQueuePeekTasks();\r
259         vStartInterruptQueueTasks();\r
260 \r
261         /* Create the tasks defined within this file. */\r
262         xTaskCreate( prvRegTestTask1, ( const signed char * const ) "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
263         xTaskCreate( prvRegTestTask2, ( const signed char * const ) "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
264 \r
265     /* The PIC32MX795 uses an 8 deep fifo where TX interrupts are asserted \r
266         whilst the TX buffer is empty.  This causes an issue with the test driver so \r
267         it is not used in this demo */\r
268         #if !defined(__32MX795F512L__)\r
269                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
270         #endif  \r
271         \r
272         /* Create the software timer that performs the 'check' functionality, as \r
273         described at the top of this file. */\r
274         xTimer = xTimerCreate(  ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
275                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
276                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
277                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
278                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
279                                                 );      \r
280         \r
281         if( xTimer != NULL )\r
282         {\r
283                 xTimerStart( xTimer, mainDONT_BLOCK );\r
284         }\r
285         \r
286         /* A software timer is also used to start the high frequency timer test.\r
287         This is to ensure the test does not start before the kernel.  This time a\r
288         one shot software timer is used. */\r
289         xTimer = xTimerCreate( ( const signed char * ) "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );\r
290         if( xTimer != NULL )\r
291         {\r
292                 xTimerStart( xTimer, mainDONT_BLOCK );\r
293         }\r
294 \r
295         /* Finally start the scheduler. */\r
296         vTaskStartScheduler();\r
297 \r
298         /* If all is well, the scheduler will now be running, and the following line\r
299         will never be reached.  If the following line does execute, then there was\r
300         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
301         to be created.  See the memory management section on the FreeRTOS web site\r
302         for more details. */\r
303         for( ;; );\r
304 }\r
305 /*-----------------------------------------------------------*/\r
306 \r
307 static void prvRegTestTask1( void *pvParameters )\r
308 {\r
309 extern void vRegTest1( volatile unsigned long * );\r
310 \r
311         for( ;; )\r
312         {\r
313                 /* Perform the register test function. */\r
314                 vRegTest1( &ulStatus1 );\r
315 \r
316                 /* Increment the counter so the check task knows we are still \r
317                 running. */\r
318                 ulRegTest1Cycles++;\r
319         }\r
320 }\r
321 /*-----------------------------------------------------------*/\r
322 \r
323 static void prvRegTestTask2( void *pvParameters )\r
324 {\r
325 extern void vRegTest2( volatile unsigned long * );\r
326 \r
327         for( ;; )\r
328         {\r
329                 /* Perform the register test function. */\r
330                 vRegTest2( &ulStatus1 );\r
331 \r
332                 /* Increment the counter so the check task knows we are still\r
333                 running. */\r
334                 ulRegTest2Cycles++;\r
335         }\r
336 }\r
337 /*-----------------------------------------------------------*/\r
338 \r
339 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
340 {\r
341 static long lChangedTimerPeriodAlready = pdFALSE;\r
342 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
343 \r
344 /* Buffer into which the high frequency timer count is written as a string. */\r
345 static char cStringBuffer[ mainMAX_STRING_LENGTH ];\r
346 \r
347 /* The count of the high frequency timer interrupts. */\r
348 extern unsigned long ulHighFrequencyTimerInterrupts;\r
349 static xLCDMessage xMessage = { ( 200 / portTICK_RATE_MS ), cStringBuffer };\r
350 \r
351         /* Has either register check 1 or 2 task discovered an error? */\r
352         if( ulStatus1 != pdPASS )\r
353         {\r
354                 xMessage.pcMessage = "Error: Reg test1";\r
355         }\r
356 \r
357         /* Check that the register test 1 task is still running. */\r
358         if( ulLastRegTest1Value == ulRegTest1Cycles )\r
359         {\r
360                 xMessage.pcMessage = "Error: Reg test2";\r
361         }\r
362         ulLastRegTest1Value = ulRegTest1Cycles;\r
363 \r
364         \r
365         /* Check that the register test 2 task is still running. */\r
366         if( ulLastRegTest2Value == ulRegTest2Cycles )\r
367         {\r
368                 xMessage.pcMessage = "Error: Reg test3";\r
369         }\r
370         ulLastRegTest2Value = ulRegTest2Cycles;\r
371         \r
372 \r
373         /* Have any of the standard demo tasks detected an error in their \r
374         operation? */\r
375         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
376         {\r
377                 xMessage.pcMessage = "Error: Gen Q";\r
378         }\r
379         else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
380         {\r
381                 xMessage.pcMessage = "Error: Q Peek";\r
382         }\r
383         else if( xAreComTestTasksStillRunning() != pdTRUE )\r
384         {\r
385                 xMessage.pcMessage = "Error: COM test";\r
386         }\r
387         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
388         {\r
389                 xMessage.pcMessage = "Error: Blck time";\r
390         }\r
391         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
392         {\r
393                 xMessage.pcMessage = "Error: Sem test";\r
394         }\r
395         else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
396         {\r
397                 xMessage.pcMessage = "Error: Int queue";\r
398         }\r
399 \r
400         if( xMessage.pcMessage != cStringBuffer )\r
401         {\r
402                 /* An error string has been logged.  If the timer period has not yet\r
403                 been changed it should be changed now.  Increasing the frequency of the\r
404                 LED gives visual feedback of the error status (although it is written\r
405                 to the LCD too!). */\r
406                 if( lChangedTimerPeriodAlready == pdFALSE )\r
407                 {\r
408                         lChangedTimerPeriodAlready = pdTRUE;\r
409                         \r
410                         /* This call to xTimerChangePeriod() uses a zero block time.\r
411                         Functions called from inside of a timer callback function must\r
412                         *never* attempt to block as to do so could impact other software\r
413                         timers. */\r
414                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
415                 }               \r
416         }\r
417         else\r
418         {\r
419                 /* Write the ulHighFrequencyTimerInterrupts value to the string \r
420                 buffer.  It will only be displayed if no errors have been detected. */\r
421                 sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );\r
422         }\r
423 \r
424         /* Send the status message to the LCD task for display on the LCD.  This is \r
425         a timer callback function, so the queue send function *must not* block. */\r
426         xQueueSend( xLCDQueue, &xMessage, mainDONT_BLOCK );\r
427         vParTestToggleLED( mainCHECK_LED );     \r
428 }\r
429 /*-----------------------------------------------------------*/\r
430 \r
431 static void prvSetupHighFrequencyTimerTest( xTimerHandle xTimer )\r
432 {\r
433         /* Setup the high frequency, high priority, timer test.  It is setup in this\r
434         software timer callback to ensure it does not start before the kernel does.\r
435         This is a one shot timer - so the setup routine will only be executed once. */\r
436         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );        \r
437 }\r
438 \r