]> git.sur5r.net Git - freertos/blob - Demo/PIC32MX_MPLAB/main.c
Update version numbers to V4.8.0
[freertos] / Demo / PIC32MX_MPLAB / main.c
1 /*\r
2         FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         ***************************************************************************\r
28         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 /*\r
50  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
51  * documentation provides more details of the standard demo application tasks.\r
52  * In addition to the standard demo tasks, the following tasks and tests are\r
53  * defined and/or created within this file:\r
54  *\r
55  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
56  * using a free running timer to demonstrate the use of the\r
57  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
58  * service routine measures the number of processor clocks that occur between\r
59  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
60  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
61  * displayed on the LCD display by the 'LCD' task as described below.  The\r
62  * fast interrupt is configured and handled in the timertest.c source file.\r
63  *\r
64  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
65  * is permitted to access the display directly.  Other tasks wishing to write a\r
66  * message to the LCD send the message on a queue to the LCD task instead of\r
67  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
68  * for messages - waking and displaying the messages as they arrive.\r
69  *\r
70  * "Check" task -  This only executes every three seconds but has the highest\r
71  * priority so is guaranteed to get processor time.  Its main function is to \r
72  * check that all the standard demo tasks are still operational.  Should any \r
73  * unexpected behaviour within a demo task be discovered the check task will \r
74  * write an error to the LCD (via the LCD task).  If all the demo tasks are \r
75  * executing with their expected behaviour then the check task writes the \r
76  * maximum jitter time to the LCD (as described above) - again via the LCD task.\r
77  *\r
78  * "Register test" tasks - These tasks are used in part to test the kernel port.\r
79  * They set each processor register to a known value, then check that the \r
80  * register still contains that value.  Each of the tasks sets the registers\r
81  * to different values, and will get swapping in and out between setting and \r
82  * then subsequently checking the register values.  Discovery of an incorrect\r
83  * value would be indicative of an error in the task switching mechanism.\r
84  */\r
85 \r
86 /* Standard includes. */\r
87 #include <stdio.h>\r
88 \r
89 /* Scheduler includes. */\r
90 #include "FreeRTOS.h"\r
91 #include "task.h"\r
92 #include "queue.h"\r
93 \r
94 /* Demo application includes. */\r
95 #include "partest.h"\r
96 #include "integer.h"\r
97 #include "blocktim.h"\r
98 #include "flash.h"\r
99 #include "semtest.h"\r
100 #include "GenQTest.h"\r
101 #include "QPeek.h"\r
102 #include "lcd.h"\r
103 #include "comtest2.h"\r
104 #include "timertest.h"\r
105 \r
106 #pragma config FPLLMUL = MUL_18, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF\r
107 #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_2\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* The rate at which the LED controlled by the 'check' task will flash when no\r
112 errors have been detected. */\r
113 #define mainNO_ERROR_PERIOD     ( 3000 / portTICK_RATE_MS )\r
114 \r
115 /* The rate at which the LED controlled by the 'check' task will flash when an\r
116 error has been detected. */\r
117 #define mainERROR_PERIOD        ( 500 )\r
118 \r
119 /* The priorities of the various demo application tasks. */\r
120 #define mainCHECK_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 4 )\r
121 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
122 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
123 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
124 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
125 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
126 \r
127 /* The LED controlled by the 'check' task. */\r
128 #define mainCHECK_LED                                           ( 7 )\r
129 \r
130 /* The LED used by the comtest tasks.  mainCOM_TEST_LED + 1 is also used.\r
131 See the comtest.c file for more information. */\r
132 #define mainCOM_TEST_LED                                        ( 4 )\r
133 \r
134 /* Baud rate used by the comtest tasks. */\r
135 #define mainCOM_TEST_BAUD_RATE                          ( 115200 )\r
136 \r
137 /* Misc. */\r
138 #define mainDONT_WAIT                                           ( 0 )\r
139 \r
140 /* Dimension the buffer used to hold the value of the maximum jitter time when\r
141 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 we expect to occur between each "fast\r
148 interrupt 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 portLONG ) ( ( 1.0 / ( double ) ( configCPU_CLOCK_HZ >> 1 ) ) * 1000000000.0 ) )\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /*\r
157  * Setup the processor ready for the demo.\r
158  */\r
159 static void prvSetupHardware( void );\r
160 \r
161 /*\r
162  * Implements the 'check' task functionality as described at the top of this \r
163  * file. \r
164  */\r
165 static void prvCheckTask( void *pvParameters ) __attribute__((noreturn));\r
166 \r
167 /*\r
168  * Tasks that test the context switch mechanism by filling the processor \r
169  * registers with known values, then checking that the values contained\r
170  * within the registers is as expected.  The tasks are likely to get swapped\r
171  * in and out between setting the register values and checking the register\r
172  * values. */\r
173 static void prvTestTask1( void *pvParameters );\r
174 static void prvTestTask2( void *pvParameters );\r
175 \r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /* The queue used to send messages to the LCD task. */\r
179 static xQueueHandle xLCDQueue;\r
180 \r
181 /* Flag used by prvTestTask1() and prvTestTask2() to indicate their status\r
182 (pass/fail). */\r
183 unsigned portLONG ulStatus1 = pdPASS;\r
184 \r
185 /* Variables incremented by prvTestTask1() and prvTestTask2() respectively on \r
186 each iteration of their function.  This is used to detect either task stopping\r
187 their execution.. */\r
188 unsigned portLONG ulRegTest1Cycles = 0, ulRegTest2Cycles = 0;\r
189 \r
190 /*-----------------------------------------------------------*/\r
191 \r
192 \r
193 /*\r
194  * Create the demo tasks then start the scheduler.\r
195  */\r
196 int main( void )\r
197 {\r
198         /* Configure any hardware required for this demo. */\r
199         prvSetupHardware();\r
200 \r
201         /* Create the LCD task - this returns the queue to use when writing \r
202         messages to the LCD. */\r
203         xLCDQueue = xStartLCDTask();\r
204 \r
205         /* Create all the other standard demo tasks. */\r
206         vStartLEDFlashTasks( tskIDLE_PRIORITY );\r
207     vCreateBlockTimeTasks();\r
208     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
209     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
210     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
211     vStartQueuePeekTasks();\r
212         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
213 \r
214         /* Create the tasks defined within this file. */\r
215         xTaskCreate( prvTestTask1, "Tst1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
216         xTaskCreate( prvTestTask2, "Tst2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
217 \r
218         /* prvCheckTask uses sprintf so requires more stack. */\r
219         xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE * 2, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
220 \r
221         /* Setup the high frequency, high priority, timer test. */\r
222         vSetupTimerTest( mainTEST_INTERRUPT_FREQUENCY );\r
223 \r
224         /* Finally start the scheduler. */\r
225         vTaskStartScheduler();\r
226 \r
227         /* Will only reach here if there is insufficient heap available to start\r
228         the scheduler. */\r
229         return 0;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvTestTask1( void *pvParameters )\r
234 {\r
235 extern void vRegTest1( unsigned long * );\r
236 \r
237         for( ;; )\r
238         {\r
239                 /* Perform the register test function. */\r
240                 vRegTest1( &ulStatus1 );\r
241 \r
242                 /* Increment the counter so the check task knows we are still \r
243                 running. */\r
244                 ulRegTest1Cycles++;\r
245         }\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 static void prvTestTask2( void *pvParameters )\r
250 {\r
251 extern void vRegTest2( unsigned long * );\r
252 \r
253         for( ;; )\r
254         {\r
255                 /* Perform the register test function. */\r
256                 vRegTest2( &ulStatus1 );\r
257 \r
258                 /* Increment the counter so the check task knows we are still\r
259                 running. */\r
260                 ulRegTest2Cycles++;\r
261         }\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 static void prvSetupHardware( void )\r
266 {\r
267         /* Set the system and peripheral bus speeds and enable the program cache*/\r
268     SYSTEMConfigPerformance( configCPU_CLOCK_HZ );\r
269 \r
270         /* Setup to use the external interrupt controller. */\r
271     INTEnableSystemMultiVectoredInt();\r
272 \r
273         portDISABLE_INTERRUPTS();\r
274 \r
275         /* Setup the digital IO for the LED's. */\r
276         vParTestInitialise();\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 static void prvCheckTask( void *pvParameters )\r
281 {\r
282 unsigned portLONG ulLastRegTest1Value = 0, ulLastRegTest2Value = 0, ulTicksToWait = mainNO_ERROR_PERIOD;\r
283 portTickType xLastExecutionTime;\r
284 \r
285 /* Buffer into which the maximum jitter time is written as a string. */\r
286 static portCHAR cStringBuffer[ mainMAX_STRING_LENGTH ];\r
287 \r
288 /* The maximum jitter time measured by the fast interrupt test. */\r
289 extern unsigned portLONG ulMaxJitter ;\r
290 xLCDMessage xMessage = { ( 200 / portTICK_RATE_MS ), cStringBuffer };\r
291 \r
292         /* Initialise the variable used to control our iteration rate prior to\r
293         its first use. */\r
294         xLastExecutionTime = xTaskGetTickCount();\r
295 \r
296         for( ;; )\r
297         {\r
298                 /* Wait until it is time to run the tests again. */\r
299                 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );\r
300 \r
301                 /* Has either register check 1 or 2 task discovered an error? */\r
302                 if( ulStatus1 != pdPASS )\r
303                 {\r
304                         ulTicksToWait = mainERROR_PERIOD;\r
305                         xMessage.pcMessage = "Error: Reg test1";\r
306                 }\r
307 \r
308                 /* Check that the register test 1 task is still running. */\r
309                 if( ulLastRegTest1Value == ulRegTest1Cycles )\r
310                 {\r
311                         ulTicksToWait = mainERROR_PERIOD;\r
312                         xMessage.pcMessage = "Error: Reg test2";\r
313                 }\r
314                 ulLastRegTest1Value = ulRegTest1Cycles;\r
315 \r
316                 \r
317                 /* Check that the register test 2 task is still running. */\r
318                 if( ulLastRegTest2Value == ulRegTest2Cycles )\r
319                 {\r
320                         ulTicksToWait = mainERROR_PERIOD;\r
321                         xMessage.pcMessage = "Error: Reg test3";\r
322                 }\r
323                 ulLastRegTest2Value = ulRegTest2Cycles;\r
324                 \r
325 \r
326                 /* Have any of the standard demo tasks detected an error in their \r
327                 operation? */\r
328                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
329                 {\r
330                         ulTicksToWait = mainERROR_PERIOD;\r
331                         xMessage.pcMessage = "Error: Gen Q";\r
332                 }\r
333                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
334                 {\r
335                         ulTicksToWait = mainERROR_PERIOD;\r
336                         xMessage.pcMessage = "Error: Q Peek";\r
337                 }\r
338                 else if( xAreComTestTasksStillRunning() != pdTRUE )\r
339                 {\r
340                         ulTicksToWait = mainERROR_PERIOD;\r
341                         xMessage.pcMessage = "Error: COM test";\r
342                 }\r
343                 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
344                 {\r
345                         ulTicksToWait = mainERROR_PERIOD;\r
346                         xMessage.pcMessage = "Error: Blck time";\r
347                 }\r
348             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
349             {\r
350                 ulTicksToWait = mainERROR_PERIOD;\r
351                         xMessage.pcMessage = "Error: Sem test";\r
352             }\r
353             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
354             {\r
355                 ulTicksToWait = mainERROR_PERIOD;\r
356                         xMessage.pcMessage = "Error: Int math";\r
357             }\r
358 \r
359                 /* Write the max jitter time to the string buffer.  It will only be \r
360                 displayed if no errors have been detected. */\r
361                 sprintf( cStringBuffer, "%dns max jitter", ( int ) ( ( ulMaxJitter - mainEXPECTED_CLOCKS_BETWEEN_INTERRUPTS ) * mainNS_PER_CLOCK ) );\r
362 \r
363                 xQueueSend( xLCDQueue, &xMessage, mainDONT_WAIT );\r
364                 vParTestToggleLED( mainCHECK_LED );\r
365         }\r
366 }\r
367 /*-----------------------------------------------------------*/\r
368 \r
369 void vApplicationGeneralExceptionHandler( unsigned portLONG ulCause, unsigned portLONG ulStatus )\r
370 {\r
371         /* This overrides the definition provided by the kernel.  Other exceptions \r
372         should be handled here. */\r
373         for( ;; );\r
374 }\r