]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/PIC32MX_MPLAB/main_full.c
5d901791794ac73e4da3fa77c4ed13328f9edb24
[freertos] / FreeRTOS / Demo / PIC32MX_MPLAB / main_full.c
1 /*\r
2     FreeRTOS V8.2.1 - Copyright (C) 2015 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
72  * project, and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
74  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
75  * in main.c.  This file implements the comprehensive test and demo version.\r
76  *\r
77  * NOTE 2:  This file only contains the source code that is specific to the\r
78  * full demo.  Generic functions, such FreeRTOS hook functions, and functions\r
79  * required to configure the hardware, are defined in main.c.\r
80  ******************************************************************************\r
81  *\r
82  * main_full() creates all the demo application tasks and software timers, then\r
83  * starts the scheduler.  The WEB documentation provides more details of the\r
84  * standard demo application tasks.  In addition to the standard demo tasks, the\r
85  * following tasks and tests are defined and/or created within this file:\r
86  *\r
87  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
88  * is permitted to access the display directly.  Other tasks wishing to write a\r
89  * message to the LCD send the message on a queue to the LCD task instead of\r
90  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
91  * for messages - waking and displaying the messages as they arrive.\r
92  *\r
93  * "Check" timer - The check software timer period is initially set to three\r
94  * seconds.  The callback function associated with the check software timer\r
95  * checks that all the standard demo tasks, and the register check tasks, are\r
96  * not only still executing, but are executing without reporting any errors.  If\r
97  * the check software timer discovers that a task has either stalled, or\r
98  * reported an error, then it changes its own execution period from the initial\r
99  * three seconds, to just 200ms.  The check software timer callback function\r
100  * also writes a status message to the LCD (via the LCD task).  If all the demo\r
101  * tasks are executing with their expected behaviour then the check task writes\r
102  * a count of the number of times the high frequency interrupt has incremented\r
103  * ulHighFrequencyTimerInterrupts - which is one in every 20,000 interrupts.\r
104  *\r
105  * "Register test" tasks - These tasks are used in part to test the kernel port.\r
106  * They set each processor register to a known value, then check that the\r
107  * register still contains that value.  Each of the tasks sets the registers\r
108  * to different values, and will get swapping in and out between setting and\r
109  * then subsequently checking the register values.  Discovery of an incorrect\r
110  * value would be indicative of an error in the task switching mechanism.\r
111  *\r
112  * By way of demonstration, the demo application defines\r
113  * configMAX_SYSCALL_INTERRUPT_PRIORITY to be 3, configKERNEL_INTERRUPT_PRIORITY\r
114  * to be 1, and all other interrupts as follows:\r
115  *\r
116  *      + The UART is allocated a priority of 2. This means it can interrupt the\r
117  *    RTOS tick, and can also safely use queues.\r
118  *  + Two timers are configured to generate interrupts just to test the nesting\r
119  *    and queue access mechanisms. These timers are allocated priorities 2 and 3\r
120  *    respectively. Even though they both access the same two queues, the\r
121  *    priority 3 interrupt can safely interrupt the priority 2 interrupt. Both\r
122  *    can interrupt the RTOS tick.\r
123  *  + Finally a high frequency timer interrupt is configured to use priority 4 -\r
124  *    therefore kernel activity will never prevent the high frequency timer from\r
125  *    executing immediately that the interrupt is raised (within the limitations\r
126  *    of the hardware itself). It would not be safe to access a queue from this\r
127  *    interrupt as it is above configMAX_SYSCALL_INTERRUPT_PRIORITY.\r
128  *\r
129  * See the online documentation for this demo for more information on interrupt\r
130  * usage.\r
131  */\r
132 \r
133 /* Standard includes. */\r
134 #include <stdio.h>\r
135 \r
136 /* Scheduler includes. */\r
137 #include "FreeRTOS.h"\r
138 #include "task.h"\r
139 #include "queue.h"\r
140 #include "timers.h"\r
141 \r
142 /* Demo application includes. */\r
143 #include "partest.h"\r
144 #include "blocktim.h"\r
145 #include "flash_timer.h"\r
146 #include "semtest.h"\r
147 #include "GenQTest.h"\r
148 #include "QPeek.h"\r
149 #include "lcd.h"\r
150 #include "comtest2.h"\r
151 #include "timertest.h"\r
152 #include "IntQueue.h"\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /* The period after which the check timer will expire, in ms, provided no errors\r
157 have been reported by any of the standard demo tasks.  ms are converted to the\r
158 equivalent in ticks using the portTICK_PERIOD_MS constant. */\r
159 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_PERIOD_MS )\r
160 \r
161 /* The period at which the check timer will expire, in ms, if an error has been\r
162 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
163 in ticks using the portTICK_PERIOD_MS constant. */\r
164 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_PERIOD_MS )\r
165 \r
166 /* The priorities of the various demo application tasks. */\r
167 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
168 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
169 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
170 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
171 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
172 \r
173 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
174 See the comtest.c file for more information. */\r
175 #define mainCOM_TEST_LED                                        ( 4 )\r
176 \r
177 /* Baud rate used by the comtest tasks. */\r
178 #define mainCOM_TEST_BAUD_RATE                          ( 115200 )\r
179 \r
180 /* Misc. */\r
181 #define mainDONT_BLOCK                                          ( 0 )\r
182 \r
183 /* Dimension the buffer used to hold the value of the high frequency timer\r
184 count when it is converted to a string. */\r
185 #define mainMAX_STRING_LENGTH                           ( 20 )\r
186 \r
187 /* The frequency at which the "fast interrupt test" interrupt will occur. */\r
188 #define mainTEST_INTERRUPT_FREQUENCY            ( 20000 )\r
189 \r
190 /* The number of timer clocks expected to occur between each "fast interrupt\r
191 test" interrupt. */\r
192 #define mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ( ( configCPU_CLOCK_HZ >> 1 ) / mainTEST_INTERRUPT_FREQUENCY )\r
193 \r
194 /* The number of nano seconds between each core clock. */\r
195 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )\r
196 \r
197 /* The number of LEDs that should be controlled by the flash software timer\r
198 standard demo and the LED to be toggle by the check task.  The starter kit only\r
199 has three LEDs so when the demo is configured to run on the starter kit there\r
200 is one less flash timer so the check task can use the third LED. */\r
201 #ifdef PIC32_STARTER_KIT\r
202         #define mainNUM_FLASH_TIMER_LEDS                        ( 2 )\r
203         #define mainCHECK_LED                                           ( 2 )\r
204 #else\r
205         #define mainNUM_FLASH_TIMER_LEDS                        ( 3 )\r
206         #define mainCHECK_LED                                           ( 7 )\r
207 #endif\r
208 \r
209 /*-----------------------------------------------------------*/\r
210 \r
211 /*\r
212  * The check timer callback function, as described at the top of this file.\r
213  */\r
214 static void prvCheckTimerCallback( TimerHandle_t xTimer );\r
215 \r
216 /*\r
217  * It is important to ensure the high frequency timer test does not start before\r
218  * the kernel.  It is therefore started from inside a software timer callback\r
219  * function, which will not execute until the timer service/daemon task is\r
220  * executing.  A one-shot timer is used, so the callback function will only\r
221  * execute once (unless it is manually reset/restarted).\r
222  */\r
223 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer );\r
224 \r
225 /*\r
226  * Tasks that test the context switch mechanism by filling the processor\r
227  * registers with known values, then checking that the values contained\r
228  * within the registers is as expected.  The tasks are likely to get swapped\r
229  * in and out between setting the register values and checking the register\r
230  * values.\r
231  */\r
232 static void prvRegTestTask1( void *pvParameters );\r
233 static void prvRegTestTask2( void *pvParameters );\r
234 \r
235 /*-----------------------------------------------------------*/\r
236 \r
237 /* The queue used to send messages to the LCD task. */\r
238 static QueueHandle_t xLCDQueue;\r
239 \r
240 /* Variables incremented by prvRegTestTask1() and prvRegTestTask2() respectively on\r
241 each iteration of their function.  This is used to detect either task stopping\r
242 their execution.. */\r
243 volatile unsigned long ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
244 \r
245 /*-----------------------------------------------------------*/\r
246 \r
247 /*\r
248  * Create the demo tasks then start the scheduler.\r
249  */\r
250 int main_full( void )\r
251 {\r
252 TimerHandle_t xTimer = NULL;\r
253 \r
254         /* Create the LCD task - this returns the queue to use when writing\r
255         messages to the LCD. */\r
256         xLCDQueue = xStartLCDTask();\r
257 \r
258         /* Create all the other standard demo tasks. */\r
259         vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );\r
260     vCreateBlockTimeTasks();\r
261     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
262     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
263     vStartQueuePeekTasks();\r
264         vStartInterruptQueueTasks();\r
265 \r
266         /* Create the tasks defined within this file. */\r
267         xTaskCreate( prvRegTestTask1, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
268         xTaskCreate( prvRegTestTask2, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
269 \r
270     /* The PIC32MX795 uses an 8 deep fifo where TX interrupts are asserted\r
271         whilst the TX buffer is empty.  This causes an issue with the test driver so\r
272         it is not used in this demo */\r
273         #if !defined(__32MX795F512L__)\r
274                 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
275         #endif\r
276 \r
277         /* Create the software timer that performs the 'check' functionality, as\r
278         described at the top of this file. */\r
279         xTimer = xTimerCreate(  "CheckTimer",/* A text name, purely to help debugging. */\r
280                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
281                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
282                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
283                                                         prvCheckTimerCallback                           /* The callback function that inspects the status of all the other tasks. */\r
284                                                 );\r
285 \r
286         if( xTimer != NULL )\r
287         {\r
288                 xTimerStart( xTimer, mainDONT_BLOCK );\r
289         }\r
290 \r
291         /* A software timer is also used to start the high frequency timer test.\r
292         This is to ensure the test does not start before the kernel.  This time a\r
293         one shot software timer is used. */\r
294         xTimer = xTimerCreate( "HighHzTimerSetup", 1, pdFALSE, ( void * ) 0, prvSetupHighFrequencyTimerTest );\r
295         if( xTimer != NULL )\r
296         {\r
297                 xTimerStart( xTimer, mainDONT_BLOCK );\r
298         }\r
299 \r
300         /* Finally start the scheduler. */\r
301         vTaskStartScheduler();\r
302 \r
303         /* If all is well, the scheduler will now be running, and the following line\r
304         will never be reached.  If the following line does execute, then there was\r
305         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
306         to be created.  See the memory management section on the FreeRTOS web site\r
307         for more details. */\r
308         for( ;; );\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 static void prvRegTestTask1( void *pvParameters )\r
313 {\r
314 extern void vRegTest1( volatile unsigned long * );\r
315 \r
316         /* Avoid compiler warnings. */\r
317         ( void ) pvParameters;\r
318 \r
319         /* Pass the address of the RegTest1 loop counter into the test function,\r
320         which is necessarily implemented in assembler. */\r
321         vRegTest1( &ulRegTest1Cycles );\r
322 \r
323         /* vRegTest1 should never exit! */\r
324         vTaskDelete( NULL );\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 static void prvRegTestTask2( void *pvParameters )\r
329 {\r
330 extern void vRegTest2( volatile unsigned long * );\r
331 \r
332         /* Avoid compiler warnings. */\r
333         ( void ) pvParameters;\r
334 \r
335         /* Pass the address of the RegTest2 loop counter into the test function,\r
336         which is necessarily implemented in assembler. */\r
337         vRegTest2( &ulRegTest2Cycles );\r
338 \r
339         /* vRegTest1 should never exit! */\r
340         vTaskDelete( NULL );\r
341 }\r
342 /*-----------------------------------------------------------*/\r
343 \r
344 static void prvCheckTimerCallback( TimerHandle_t xTimer )\r
345 {\r
346 static long lChangedTimerPeriodAlready = pdFALSE;\r
347 static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;\r
348 \r
349 /* Buffer into which the high frequency timer count is written as a string. */\r
350 static char cStringBuffer[ mainMAX_STRING_LENGTH ];\r
351 \r
352 /* The count of the high frequency timer interrupts. */\r
353 extern unsigned long ulHighFrequencyTimerInterrupts;\r
354 static xLCDMessage xMessage = { ( 200 / portTICK_PERIOD_MS ), cStringBuffer };\r
355 \r
356         /* Check that the register test 1 task is still running. */\r
357         if( ulLastRegTest1Value == ulRegTest1Cycles )\r
358         {\r
359                 xMessage.pcMessage = "Error: Reg test2";\r
360         }\r
361         ulLastRegTest1Value = ulRegTest1Cycles;\r
362 \r
363 \r
364         /* Check that the register test 2 task is still running. */\r
365         if( ulLastRegTest2Value == ulRegTest2Cycles )\r
366         {\r
367                 xMessage.pcMessage = "Error: Reg test3";\r
368         }\r
369         ulLastRegTest2Value = ulRegTest2Cycles;\r
370 \r
371 \r
372         /* Have any of the standard demo tasks detected an error in their\r
373         operation? */\r
374         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
375         {\r
376                 xMessage.pcMessage = "Error: Gen Q";\r
377         }\r
378         else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
379         {\r
380                 xMessage.pcMessage = "Error: Q Peek";\r
381         }\r
382         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
383         {\r
384                 xMessage.pcMessage = "Error: Blck time";\r
385         }\r
386         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
387         {\r
388                 xMessage.pcMessage = "Error: Sem test";\r
389         }\r
390         else if( xAreIntQueueTasksStillRunning() != pdTRUE )\r
391         {\r
392                 xMessage.pcMessage = "Error: Int queue";\r
393         }\r
394         #if !defined(__32MX795F512L__)\r
395                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
396                 {\r
397                         xMessage.pcMessage = "Error: COM test";\r
398                 }\r
399         #endif\r
400 \r
401         if( xMessage.pcMessage != cStringBuffer )\r
402         {\r
403                 /* An error string has been logged.  If the timer period has not yet\r
404                 been changed it should be changed now.  Increasing the frequency of the\r
405                 LED gives visual feedback of the error status (although it is written\r
406                 to the LCD too!). */\r
407                 if( lChangedTimerPeriodAlready == pdFALSE )\r
408                 {\r
409                         lChangedTimerPeriodAlready = pdTRUE;\r
410 \r
411                         /* This call to xTimerChangePeriod() uses a zero block time.\r
412                         Functions called from inside of a timer callback function must\r
413                         *never* attempt to block as to do so could impact other software\r
414                         timers. */\r
415                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
416                 }\r
417         }\r
418         else\r
419         {\r
420                 /* Write the ulHighFrequencyTimerInterrupts value to the string\r
421                 buffer.  It will only be displayed if no errors have been detected. */\r
422                 sprintf( cStringBuffer, "Pass %u", ( unsigned int ) ulHighFrequencyTimerInterrupts );\r
423         }\r
424 \r
425         /* Send the status message to the LCD task for display on the LCD.  This is\r
426         a timer callback function, so the queue send function *must not* block. */\r
427         xQueueSend( xLCDQueue, &xMessage, mainDONT_BLOCK );\r
428         vParTestToggleLED( mainCHECK_LED );\r
429 }\r
430 /*-----------------------------------------------------------*/\r
431 \r
432 static void prvSetupHighFrequencyTimerTest( TimerHandle_t xTimer )\r
433 {\r
434         /* Setup the high frequency, high priority, timer test.  It is setup in this\r
435         software timer callback to ensure it does not start before the kernel does.\r
436         This is a one shot timer - so the setup routine will only be executed once. */\r
437         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
438 }\r
439 \r